I just run into a weird problem. The way I am writing my code, first I write functions on the fly in the Ipython Qt console interactively, and tweak them as necessary. Once I am satisfied with the results, I move them into a py file, to use the functions at a later time.
So, I wrote a function which is supposed to plot a histogram on screen. If I first run my py script, then call the function with the required arguments, I get an error message, which is at the very bottom of this post. If I copy and paste the function code to the qt console, and hit enter, then the function works fine after that point.
Why would this function work fine after I copy paste it to the qt console, but does not work if I directly call it after running my script with the %run script.py magic?
Thanks for any ideas!
Here is my function:
def PlotFreqHist(w_hist_list, head, span, m):
'''
Generates a frequency distribution plot
'''
if head == 30.:
tr_cond = 'Normal'
else:
tr_cond = 'Congested'
histweights = np.zeros_like(w_hist_list[0]) + 1. / w_hist_list[0].size * 100
bmap = brewer2mpl.get_map('RdYlBu', 'diverging', 10)
colors = bmap.mpl_colors
mpl.rcParams['axes.color_cycle'] = colors
fig = plt.figure(figsize=(12,8))
ax = axes()
plt.xlim(0.0,1.2)
plt.ylim(0.0,6)
plt.xlabel('Uniform Load [kips/ft]')
plt.ylabel('Frequency [%]')
plt.title('Frequency Distribution\n Number of Simulations = %i Span Length = %.0fft Traffic Condition = %s' %(m, span, tr_cond))
ax.xaxis.set_major_locator(MultipleLocator(0.1))
ax.xaxis.set_minor_locator(MultipleLocator(0.02))
ax.yaxis.set_major_locator(MultipleLocator(1.0))
ax.yaxis.set_minor_locator(MultipleLocator(0.2))
plt.grid(b=True, which='major', linewidth=1.0)
plt.grid(b=True, which='minor')
for i in xrange(len(w_hist_list)):
hist(w_hist_list[i], bins = 200, range = (0,2), normed = 0, cumulative = 0, histtype = charttype, linewidth=2.0, alpha =0.5, weights = histweights)
plt.show()
And here is the error message:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\ultratb.py", line 779, in structured_traceback
records = _fixed_getinnerframes(etb, context, tb_offset)
File "C:\Python27\lib\site-packages\IPython\core\ultratb.py", line 245, in _fixed_getinnerframes
records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
File "C:\Python27\lib\inspect.py", line 1043, in getinnerframes
framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
File "C:\Python27\lib\inspect.py", line 1007, in getframeinfo
lines, lnum = findsource(frame)
File "C:\Python27\lib\inspect.py", line 580, in findsource
if pat.match(lines[lnum]): break
IndexError: list index out of range
ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.
Unfortunately, your original traceback can not be constructed.
Related
I have a custom geoprocessing task written for me in python that takes 2 inputs - input_rows.csv and event_rows.csv - and creates a file gdb with a feature class of a join of these 2 tables.
input_rows has the x,y coordinates for the feature class.
event_rows.csv contains the attributes.
When I run it locally in ArcCatalog (10.7.1), it runs fine. If I publish it to our ArcGIS 10.81 Linux server and run that in ArcCatalog, it also runs fine. If I try to execute it thru a call in my C# application, I get a generic error on TableToTable_conversion:
<Msg time='2022-10-25T13:01:43,305' type='SEVERE' code='20010' target='export/Download_v3.GPServer' methodName='GPServerSync.CheckMessages' machine='DCP-G-XPLLAGS01.GEOPLAN.UFL.EDU' process='116658' thread='505' user='null' requestId='55b85e40-1ea9-4934-ade7-124fe4dc86c9'>Error executing tool. Download Job ID: j46c9f46dd97642e4890778604255a1fb :
Traceback (most recent call last):
File "<string>", line 248, in execute
File "<string>", line 190, in zip_GIS
File "z:\home\esri\arcgis\server\arcpy\arcpy\conversion.py", line 2272, in TableToTable
raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (TableToTable).
Here is the code to do the conversion:
x_coords = "S4_X"
y_coords = "S4_Y"
out_event_lyr = "event_layer"
v_scratchFolder_ = "%scratchFolder%"
# create Folder
arcpy.CreateFolder_management(v_scratchFolder_, "Geodatabase")
# create File GDB
arcpy.CreateFileGDB_management(x, gdb_name)
wkt = "PROJCS['NAD_1983_HARN_Florida_GDL_Albers',GEOGCS['GCS_North_American_1983_HARN',DATUM['D_North_American_1983_HARN',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',400000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-84.0],PARAMETER['Standard_Parallel_1',24.0],PARAMETER['Standard_Parallel_2',31.5],PARAMETER['Latitude_Of_Origin',24.0],UNIT['Meter',1.0],AUTHORITY['EPSG',3087]]"
sr = arcpy.SpatialReference()
sr.loadFromString(wkt)
out_event_lyr = arcpy.MakeXYEventLayer_management(input_rows, x_coords, y_coords, out_event_lyr, sr)
crash_fc = arcpy.CopyFeatures_management(out_event_lyr, os.path.join("%scratchFolder%\Geodatabase", gdb_name, out_event_fc))
event_tab = arcpy.TableToTable_conversion(event_rows, os.path.join("%scratchFolder%\Geodatabase", gdb_name), "EventTab")
crash_fc_lyr = arcpy.MakeFeatureLayer_management(crash_fc, "fc_lyr")
out_event_lyr_join = arcpy.AddJoin_management(crash_fc_lyr, "REPORT_NUMBER", event_tab, "REPORT_NUMBER")
arcpy.CopyFeatures_management(out_event_lyr_join, os.path.join("%scratchFolder%\Geodatabase", gdb_name, out_event_fc1))
We changed the script to do a CopyFeatures instead of TableToTable to see if that would work but I get a different error:
ExecuteError: ERROR 000210: Cannot create output Z:\opt\esri\arcgis\server\usr\directories\arcgisjobs\export\download_v3_1_gpserver\j46744155824f44e1ad5f7c1a1a6afc71\scratch\Geodatabase\s4_mapped_crashes_gdb.gdb\att
Failed to execute (CopyFeatures).
Here is the CopyFeature version:
x_coords = "S4_X"
y_coords = "S4_Y"
out_event_lyr = "event_layer"
v_scratchFolder_ = "%scratchFolder%"
Geodatabase = v_scratchFolder_
s4_crash_gdb_gdb = Geodatabase
# create Folder
x = arcpy.CreateFolder_management(v_scratchFolder_, "Geodatabase")
arcpy.AddMessage(x)
# create File GDB
arcpy.CreateFileGDB_management(x, gdb_name)
wkt = "PROJCS['NAD_1983_HARN_Florida_GDL_Albers',GEOGCS['GCS_North_American_1983_HARN',DATUM['D_North_American_1983_HARN',SPHEROID['G RS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',400000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-84.0],PARAMETER['Standard_Parallel_1',24.0],PARAMETER['Standard_Parallel_2',31.5],PARAMETER['Latitude_Of_Origin',24.0],UNIT['Meter',1.0],AUTHORITY['EPSG',3087]]"
sr = arcpy.SpatialReference()
sr.loadFromString(wkt)
# create GIS file in geodatabase
out_event_lyr = arcpy.MakeXYEventLayer_management(input_rows, x_coords, y_coords, out_event_lyr, sr)
crash_fc = arcpy.CopyFeatures_management(out_event_lyr, os.path.join("%scratchFolder%\Geodatabase", gdb_name, out_event_fc))
crash_fc_lyr = arcpy.MakeFeatureLayer_management(crash_fc, "fc_lyr")
out_event_lyr_join = arcpy.AddJoin_management(crash_fc_lyr, "REPORT_NUMBER", event_rows, "REPORT_NUMBER")
arcpy.CopyFeatures_management(out_event_lyr_join, os.path.join("%scratchFolder%\Geodatabase", gdb_name, out_event_fc1))
My ArcGIS and Python skills are pretty rudimentary so I'm struggling trying to figure out what the issue is. Doesn't seem like an unreasonable task I'm trying to accomplish but it has me stumped. What am I missing?
I want to display xticks only at the locations of data points (so the xticks == wavelengths). How can I do this?
Here is my code:
from PyQt6.QtWidgets import QApplication
import pyqtgraph as pg
app = QApplication([])
wavelengths = [610, 680, 730, 760, 810, 860]
data = [239.23, 233.81, 187.27, 176.41, 172.35, 173.78]
pw = pg.plot(wavelengths, data, symbol="o")
pw.getPlotItem().getAxis('bottom').setTicks([wavelengths])
app.exec()
I tried using AxisItem.setTicks(), but it results in an error:
for val, strn in level:
TypeError: cannot unpack non-iterable int object
Traceback (most recent call last):
File "/home/jure/.local/share/virtualenvs/serial-plotting-gui-dWOiQ7Th/lib/python3.10/site-packages/pyqtgraph/graphicsItems/AxisItem.py", line 608, in paint
specs = self.generateDrawSpecs(painter)
File "/home/jure/.local/share/virtualenvs/serial-plotting-gui-dWOiQ7Th/lib/python3.10/site-packages/pyqtgraph/graphicsItems/AxisItem.py", line 936, in generateDrawSpecs
P.S. I also tried this SO answer, but it doesn't work for me
It's always bit tricky with ticks in pyqtgraph. It works with tick spacing between two ticks on the axis. On top of it, it defines levels of MajorTick, MinorTicks and so on. With setTicks You have to specify list of levels (we have only MajorTicks) and each level with list of (tick value, "tick string").
So in order for Your example to work, You have to use such a list as a parameter for setTicks method.
Here is Your modified code:
from PyQt6.QtWidgets import QApplication
import pyqtgraph as pg
app = QApplication([])
wavelengths = [610, 680, 730, 760, 810, 860]
data = [239.23, 233.81, 187.27, 176.41, 172.35, 173.78]
pw = pg.plot(wavelengths, data, symbol="o")
pw.getPlotItem().getAxis('bottom').setTicks([[(wavelength, str(wavelength)) for wavelength in wavelengths]])
app.exec()
So I've got a variety of pandas dataframes and I'd like to reuse a bokeh app to view them, rather than hard coding the data into the app. I've adapted the crossfilter example to show the problem with my approach.
This should be called as
dframe = some Pandas dataframe
invokeh(bk_crossfilter, dframe)
and defined as in the code block below.
It seems that bokeh doesn't like the use of functools.partial in the definition of the app below and gives me a 500 server error in the browser. Earlier today the print statement in the bokeh app was showing the dataframe, but that now seems to get mangled into bokeh.Document during the function entry. The console output is now:
Preparing a bokeh application.
Opening Bokeh application on http://localhost:5006/
CROSSFILTER: <bokeh.document.document.Document object at 0x0000029BAB2783C8>
ERROR:tornado.application:Uncaught exception GET / (::1)
HTTPServerRequest(protocol='http', host='localhost:5006', method='GET', uri='/', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\site-packages\tornado\web.py", line 1703, in _execute
result = await result
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\site-packages\bokeh\server\views\doc_handler.py", line 52, in get
session = await self.get_session()
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\site-packages\bokeh\server\views\session_handler.py", line 120, in get_session
session = await self.application_context.create_session_if_needed(session_id, self.request, token)
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\site-packages\bokeh\server\contexts.py", line 218, in create_session_if_needed
self._application.initialize_document(doc)
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\site-packages\bokeh\application\application.py", line 171, in initialize_document
h.modify_document(doc)
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\site-packages\bokeh\application\handlers\function.py", line 132, in modify_document
self._func(doc)
File "c:\users\jdorsey\code\utils\dorsey\visualisation.py", line 36, in bk_crossfilter
columns = sorted(df.columns)
AttributeError: 'Document' object has no attribute 'columns'
ERROR:tornado.access:500 GET / (::1) 7.98ms
WARNING:tornado.access:404 GET /favicon.ico (::1) 0.99ms
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\users\jdorsey\code\utils\dorsey\visualisation.py", line 27, in invokeh
io_loop.start()
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\site-packages\tornado\platform\asyncio.py", line 149, in start
self.asyncio_loop.run_forever()
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\asyncio\base_events.py", line 422, in run_forever
self._run_once()
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\asyncio\base_events.py", line 1396, in _run_once
event_list = self._selector.select(timeout)
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\selectors.py", line 323, in select
r, w, _ = self._select(self._readers, self._writers, [], timeout)
File "C:\Users\jdorsey\code\conda\environment\REDACTED\lib\selectors.py", line 314, in _select
r, w, x = select.select(r, w, w, timeout)
Adding a print statement to the invokeh function confirms dataset is a valid pandas frame there.
[1379 rows x 82 columns]
And the code:
from tornado.ioloop import IOLoop
from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from bokeh.server.server import Server
from bokeh.layouts import column, row
from bokeh.models import Select
from bokeh.palettes import Spectral5
from bokeh.plotting import curdoc, figure
from functools import partial
def invokeh(visual,dataset):
"""
Launch a bokeh server and connect to it.
ARGUMENTS:
visual: A bokeh visualisation
dataset: A dataset to populate the visualisation
"""
print("Preparing a bokeh application.")
io_loop = IOLoop.current()
bokeh_app = Application(FunctionHandler(partial(visual,dataset)))
server = Server({"/": bokeh_app})#, io_loop=io_loop)
server.start()
print("Opening Bokeh application on http://localhost:5006/")
io_loop.add_callback(server.show, "/")
io_loop.start()
def bk_crossfilter(doc,df):
print('CROSSFILTER: ', df)
SIZES = list(range(6, 22, 3))
COLORS = Spectral5
N_SIZES = len(SIZES)
N_COLORS = len(COLORS)
columns = sorted(df.columns)
discrete = [x for x in columns if df[x].dtype == object]
continuous = [x for x in columns if x not in discrete]
def create_figure():
xs = df[x.value].values
ys = df[y.value].values
x_title = x.value.title()
y_title = y.value.title()
kw = dict()
if x.value in discrete:
kw['x_range'] = sorted(set(xs))
if y.value in discrete:
kw['y_range'] = sorted(set(ys))
kw['title'] = "%s vs %s" % (x_title, y_title)
p = figure(plot_height=600, plot_width=800, tools='pan,box_zoom,hover,reset', **kw)
p.xaxis.axis_label = x_title
p.yaxis.axis_label = y_title
if x.value in discrete:
p.xaxis.major_label_orientation = pd.np.pi / 4
sz = 9
if size.value != 'None':
if len(set(df[size.value])) > N_SIZES:
groups = pd.qcut(df[size.value].values, N_SIZES, duplicates='drop')
else:
groups = pd.Categorical(df[size.value])
sz = [SIZES[xx] for xx in groups.codes]
c = "#31AADE"
if color.value != 'None':
if len(set(df[color.value])) > N_COLORS:
groups = pd.qcut(df[color.value].values, N_COLORS, duplicates='drop')
else:
groups = pd.Categorical(df[color.value])
c = [COLORS[xx] for xx in groups.codes]
p.circle(x=xs, y=ys, color=c, size=sz, line_color="white", alpha=0.6, hover_color='white', hover_alpha=0.5)
return p
def update(attr, old, new):
layout.children[1] = create_figure()
print("COLUMNS: ", columns[0], columns[1])
print("DISCRETE: ", discrete, " CONTINUOUS: ", continuous)
x = Select(title='X-Axis', value=columns[0], options=columns)
x.on_change('value', update)
y = Select(title='Y-Axis', value=columns[1], options=columns)
y.on_change('value', update)
size = Select(title='Size', value='None', options=['None'] + continuous)
size.on_change('value', update)
color = Select(title='Color', value='None', options=['None'] + continuous)
color.on_change('value', update)
controls = column(x, y, color, size, width=200)
layout = row(controls, create_figure())
curdoc().add_root(layout)
curdoc().title = "Crossfilter"
Thanks in advance for any suggestions.
By changing :
bokeh_app = Application(FunctionHandler(partial(visual,dataset)))
to:
bokeh_app = Application(FunctionHandler(lambda dataset: visual))
I now only get this in the console:
Opening Bokeh application on http://localhost:5006/
WARNING:tornado.access:404 GET /favicon.ico (::1) 1.00ms
And no error in the browser. But the browser renders a blank page containing the following source (there's more but it doesn't tell me much):
root.Bokeh.embed.embed_items(docs_json, render_items);
}
if (root.Bokeh !== undefined) {
embed_document(root);
} else {
var attempts = 0;
var timer = setInterval(function(root) {
if (root.Bokeh !== undefined) {
clearInterval(timer);
embed_document(root);
} else {
attempts++;
if (attempts > 100) {
clearInterval(timer);
console.log("Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing");
}
}
}, 10, root)
}
The console log doesn't actually display the error message mentioned in the page source...
Pretty sure you need to be explicit with partial about which arguments you want bound to what:
partial(visual, df=dataset)
As it is I am nearly certain you are binding dataset to the doc argument, so when Bokeh calls your app function the Document gets passing in as df instead (the only free argument left).
I have been trying to add two lists of floats to a scatterplot using the Matplotlib. I have simply created a Basemap with mostly default settings. However, whenever I attempt to run the script I get the error below.
#!/usr/bin/python
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
def main():
wmap = Basemap()
wmap.drawmapboundary(fill_color='aqua')
wmap.fillcontinents(color='coral',lake_color='aqua')
wmap.drawcoastlines()
locationData = open("locationData", "r")
lats = []
lons = []
ASN = []
company = []
fileLine = locationData.readlines()
for i in fileLine:
parsedLine = i.split(",")
lats.append(float(parsedLine[0]))
lons.append(float(parsedLine[1]))
ASN.append(parsedLine[2])
company.append(parsedLine[3])
locationData.close()
x,y = wmap(lats,lons)
wmap.scatter(x,y)
plt.show()
if(__name__ == '__main__'):
main()
I receive the error:
Traceback (most recent call last):
File "mapGen.py", line 51, in <module>
main()
File "mapGen.py", line 45, in main
wmap.scatter(lats,lons)
File "/usr/lib/python3.7/site-packages /mpl_toolkits/basemap/__init__.py", line 563, in with_transform
return plotfunc(self,x,y,*args,**kwargs)
File "/usr/lib/python3.7/site-packages/mpl_toolkits/basemap/__init__.py", line 3253, in scatter
self._restore_hold(ax)
File "/usr/lib/python3.7/site-packages/mpl_toolkits/basemap/__init__.py", line 3229, in _restore_hold
ax._hold = self._tmp_hold
AttributeError: can't set attribute
I have tried changing around the attributes to wmap.scatter() and the variables, but there error still persists and I haven't been able to find documentation on this error. Any ideas?
This specific exception is raised when the attribute you're trying to change is a property that doesn't have a setter. It seems the basemap change is needed, adding a setter would solve the issue.
But you can change wmap.scatter(x,y) to plt.scatter(x, y) to solve it.
I'm generating a table that is long enough to go onto second page. What I would like to achieve is to SPAN 4 rows on the second page. I do it using the follwing code in the TableStyle.
('SPAN', (0,38), (0,41))
Span for two rows works though. ('SPAN', (0,38), (0,39))
Here's a code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from reportlab.platypus import SimpleDocTemplate, Table, LongTable, TableStyle, BaseDocTemplate, Frame, Paragraph, NextPageTemplate, PageTemplate
from reportlab.lib.pagesizes import letter, inch
from reportlab.lib import colors
def testPdf():
doc = BaseDocTemplate("testpdf.pdf",pagesize=letter,
rightMargin=72,leftMargin=72,
topMargin=72,bottomMargin=18, showBoundary=True)
width, height = letter
print width
print height
elements = []
datas = []
for x in range(1,50):
datas.append(
[x,x+1]
)
t=LongTable(datas)
tTableStyle=[
('SPAN', (0,38), (0,41)),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
]
t.setStyle(TableStyle(tTableStyle))
elements.append(t)
frameT = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='normal')
doc.addPageTemplates([PageTemplate(id='OneCol',frames=frameT)])
doc.build(elements)
if __name__ == '__main__':
testPdf()
I ran your sample code and saw the same problem. For anyone coming along who might be interested, the error that results is:
Traceback (most recent call last):
File "./test.py", line 39, in <module>
testPdf()
File "./test.py", line 36, in testPdf
doc.build(elements)
File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/doctemplate.py", line 880, in build
self.handle_flowable(flowables)
File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/doctemplate.py", line 763, in handle_flowable
if frame.add(f, canv, trySplit=self.allowSplitting):
File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/frames.py", line 159, in _add
w, h = flowable.wrap(aW, h)
File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/tables.py", line 1113, in wrap
self._calc(availWidth, availHeight)
File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/tables.py", line 587, in _calc
self._calc_height(availHeight,availWidth,W=W)
File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/tables.py", line 553, in _calc_height
spanFixDim(H0,H,spanCons,lim=hmax)
File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/tables.py", line 205, in spanFixDim
t = sum([V[x]+M.get(x,0) for x in xrange(x0,x1)])
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Sort answer: this is a bug in ReportLab and you'll need to report it to the developers.
Long answer: I've seen problems with splitting before. There were (and apparently still are) situations that confuse the algorithm when splitting is plainly possible but the algorithm ends up throwing an error because it can't figure out which page to put it on. It can happen at weird times when you wouldn't expect it, and I know of no especially good work around other than to manually split things yourself (or to wrap content in KeepTogether flowables, but that won't work in your situation).