I'm trying to assess an interpolation scheme, so I'm looking to compare the input points (red) to the resultant grid (blue). I've built two K-D trees looking to find the nearest blue dot to each red dot (with some radius). My limited understanding suggests that the query_ball_tree would provide the results I'm looking for once my trees are built.
gridTree = spatial.cKDTree(data=np.c_[np.array(xx.flatten()), np.array(yy.flatten())])
obsTree = spatial.cKDTree(data=np.c_[np.array(bathy['x']), np.array(bathy['y'])])
dist, idx = obsTree.query_ball_tree(gridTree, 2) # find points in grid that match obs
which produces the below error.
Traceback (most recent call last):
File "/home/repos/pyObjectiveMapping/operation_FRF_duneLidarDEM_bathy_interp.py", line 211, in <module>
dist, idx = obsTree.query_ball_tree(gridTree, 2)
ValueError: too many values to unpack (expected 2)
if I try to remove the "extra" argument:
dist, idx = obsTree.query_ball_tree(gridTree)
I get the below error (as expected, because it needs both)
Traceback (most recent call last):
File "/home/spike/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-022d5ab7a46f>", line 4, in <module>
dist, idx = obsTree.query_ball_tree(gridTree)
File "ckdtree.pyx", line 1001, in scipy.spatial.ckdtree.cKDTree.query_ball_tree
TypeError: query_ball_tree() takes at least 2 positional arguments (1 given)
so the error is not representing the problem effectively. I've checked and removed all masked data before entering it into the trees. I've tried removing the reds that were outside of the blue domain, that didn't help (didn't think it should matter, but wanted to check anyway). I've tried different radii values and had similar results.
Related
I want to load a graph from XML, i.e. .osm file, using Osmnx Python library.
The .osm file contains roads not connected each other, for example only highway=primary and highway=primary_link of a country's region.
I use the parameter retain_all to avoid discarding all the roads, since
retain_all: if True, return the entire graph even if it is not connected. otherwise, retain only the largest weakly connected component.
I use this instruction:
graph = ox.graph_from_xml('temp.osm', retain_all=True)
But I get the following error
AttributeError: 'float' object has no attribute 'deg2rad'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\code.py", line 37, in <module>
graph = ox.graph_from_xml('temp.osm', retain_all=True)
File "D:\Python\lib\site-packages\osmnx\graph.py", line 518, in graph_from_xml
G = _create_graph(response_jsons, bidirectional=bidirectional, retain_all=retain_all)
File "D:\Python\lib\site-packages\osmnx\graph.py", line 587, in _create_graph
G = distance.add_edge_lengths(G)
File "D:\Python\lib\site-packages\osmnx\distance.py", line 154, in add_edge_lengths
dists = great_circle_vec(c[:, 0], c[:, 1], c[:, 2], c[:, 3]).round(precision)
File "D:\Python\lib\site-packages\osmnx\distance.py", line 60, in great_circle_vec
y1 = np.deg2rad(lat1)
TypeError: loop of ufunc does not support argument 0 of type float which has no callable deg2rad method
If I remove retain_all parameter, of course, the error does not occurr but the graph will contain only one primary road.
How can I keep all the roads even if not connected in the map?
I forgot to post my solution. I solved using another Python library, called Pyrosm:
osm = OSM('temp.pbf')
nodes, edges = osm.get_network(nodes=True, network_type='driving')
graph = osm.to_graph(nodes, edges, graph_type='networkx', retain_all=True)
The official documentation describes that layers can be used to set the occlusion of rendered elements
canvas.layer(name, above=None, below=None)
This method creates or gets a layer with name name.
A layer is a canvas itself and can be used to combine drawing operations for ordering purposes, i.e., what is above and below each
other. The layer name name is a dotted string, where dots are used to
form a hierarchy of layer groups. When inserting a layer, it is put on
top of its layer group except when another layer instance of this
group is specified by means of the parameters above or below.
Well I tried the following:
c = canvas.canvas().layer("top")
t = canvas.canvas().layer("bot", below="top")
t = canvas.canvas().layer("bot", below=c)
t = canvas.canvas().layer("bot", below=0)
They all return with some error. For example the string version:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pyx/canvas.py", line 296, in layer
group, layer = name.split(".", 1)
ValueError: not enough values to unpack (expected 2, got 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "EdgeRefining/refine_edges.py", line 174, in <module>
t = canvas.canvas().layer("bot", below="top")
File "/usr/lib/python3/dist-packages/pyx/canvas.py", line 312, in layer
self.items.insert(self.items.index(self.layers[below]), self.layers[name])
KeyError: 'top'
Has anyone used this functionality?
Layers are canvas instances within a canvas. Here is an example:
from pyx import *
c = canvas.canvas()
l1 = c.layer('l1')
l2 = c.layer('l2')
l1.fill(path.circle(0, 0, 2), [color.rgb.red])
l2.fill(path.circle(3, 0, 2), [color.rgb.green])
c.writePDFfile()
Now you can add below='l1' when creating the layer l2 and the red circle will be placed above the green circle. The problem in your code was, that you created new canvas instances all the time, however, multiple layers are to be used within the same canvas.
for i=1:n
centersX(:,i)=linspace(min(xData)+dX/2,max(xData)-dX/2,nbins)';
centersY(:,i)=linspace(min(yData)+dY/2,max(phase)-dY/2,nbins)';
centers = {centersX(:,i),centersY(:,i)};
H(:,:,i) = hist3([xData yData],centers);
end
In each iteration, I construct centersX and centersY with linspace function. I then store them in a 2x1 cell array called centers. H is a nbins X nbins X n struct. In each iteration I fill a nbins X nbins slice of H with the data from hist3.
I'm looking for the Python equivalent. I'm having trouble with passing the arguments for numpy.histogram2d:
H[:,:,i] = numpy.histogram2d(xData,yData,centers)
I get the following error:
Traceback (most recent call last):
line 714, in histogramdd
N, D = sample.shape
AttributeError: 'list' object has no attribute 'shape'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
line 36, in <module>
H[:,:,i] = numpy.histogram2d(xData, yData, centers)
line 714, in histogram2d
hist, edges = histogramdd([x, y], bins, range, normed, weights)
line 718, in histogramdd
N, D = sample.shape
ValueError: too many values to unpack (expected 2)
Since Python doesn't have cell arrays, I changed centers to be an array of arrays where centers[0] = centersX and centers[1] = centersY. What do I need to change such that that assuming the data are the same between matlab and python that the outputs will match?
EDIT:
I have also tried H[:,:,i] = numpy.histogram2d(xData,yData, bins=(centersX,centersY)) to cutout the combining step into centers but no luck.
Have you tried combing them with square brackets?
Maybe you can also use matplotlib.pyplot.hist2d.
H[:,:,i], *_ = numpy.histogram2d(xData,yData,bins=[centers[0], centers[1]])
H[:,:,i], *_ = matplotlib.pyplot.hist2d(xData,yData,bins=[centers[0], centers[1]])
In both, the values in centers are the bin edges, not the centers. You have to adjust the calculation. I think it is enough to remove the dX/2:
centersX(:,i)=linspace(min(xData),max(xData),nbins)';
centersY(:,i)=linspace(min(yData),max(phase),nbins)';
So I'm trying to generate a random directed graph such that each vertex has 3 in-nodes and 1 outnode. But graph tool seems to be getting stuck in the deg_sampler() function.
from graph_tool.all import *
def deg_sampler():
return 1,2
g = random_graph(1000,deg_sampler,verbose=True)
I get this error after running the code
adding vertices: 1000 of 1000 (100%)
fixing average degrees. Total degree difference: 1000^CTraceback (most recent call last):
File "code.py", line 6, in <module>
g = random_graph(1000,deg_sampler,verbose=True)
File "/usr/lib/python2.7/dist-packages/graph_tool/generation/__init__.py", line 384, in random_graph
_get_rng(), verbose, True)
File "/usr/lib/python2.7/dist-packages/graph_tool/generation/__init__.py", line 379, in <lambda>
sampler = lambda i: deg_sampler()
KeyboardInterrupt
The degree sampler function should return the in- and out-degrees of the nodes. In your implementation, each node has an in-degree of 1 and out-degree of 2. It is, of course, impossible to construct a graph with this degree sequence, since the average in- and out-degrees must identical. This is why the algorithm gets stuck in the "fixing average degrees" phase.
I'm getting a ZeroDivisionError from the following code:
#stacking the array into a complex array allows np.unique to choose
#truely unique points. We also keep a handle on the unique indices
#to allow us to index `self` in the same order.
unique_points,index = np.unique(xdata[mask]+1j*ydata[mask],
return_index=True)
#Now we break it into the data structure we need.
points = np.column_stack((unique_points.real,unique_points.imag))
xx1,xx2 = self.meta['rcm_xx1'],self.meta['rcm_xx2']
yy1 = self.meta['rcm_yy2']
gx = np.arange(xx1,xx2+dx,dx)
gy = np.arange(-yy1,yy1+dy,dy)
GX,GY = np.meshgrid(gx,gy)
xi = np.column_stack((GX.ravel(),GY.ravel()))
gdata = griddata(points,self[mask][index],xi,method='linear',
fill_value=np.nan)
Here, xdata,ydata and self are all 2D numpy.ndarrays (or subclasses thereof) with the same shape and dtype=np.float32. mask is a 2d ndarray with the same shape and dtype=bool. Here's a link for those wanting to peruse the scipy.interpolate.griddata documentation.
Originally, xdata and ydata are derived from a non-uniform cylindrical grid that has a 4 point stencil -- I thought that the error might be coming from the fact that the same point was defined multiple times, so I made the set of input points unique as suggested in this question. Unfortunately, that hasn't seemed to help. The full traceback is:
Traceback (most recent call last):
File "/xxxxxxx/rcm.py", line 428, in <module>
x[...,1].to_pz0()
File "/xxxxxxx/rcm.py", line 285, in to_pz0
fill_value=fill_value)
File "/usr/local/lib/python2.7/site-packages/scipy/interpolate/ndgriddata.py", line 183, in griddata
ip = LinearNDInterpolator(points, values, fill_value=fill_value)
File "interpnd.pyx", line 192, in scipy.interpolate.interpnd.LinearNDInterpolator.__init__ (scipy/interpolate/interpnd.c:2935)
File "qhull.pyx", line 996, in scipy.spatial.qhull.Delaunay.__init__ (scipy/spatial/qhull.c:6607)
File "qhull.pyx", line 183, in scipy.spatial.qhull._construct_delaunay (scipy/spatial/qhull.c:1919)
ZeroDivisionError: float division
For what it's worth, the code "works" (No exception) if I use the "nearest" method.