I have a random distribution of points in 2D space, and a 2D grid. I want to log in which cell in the grid each point is. For example, grid[5,6] will return [2, 53, 70, 153], which are the indices of the points located inside the cell [5,6].
It is crucial that the data will be saved in the grid by indexing the circles, and not the other way round, since later I'm going to use this grid structure to compare close points to each other, and the grid will allow me to see which points are close.
I'm working in python, and the points are stored as a 2D numpy array.
What is the best way to implement the grid data structure? Notice that the number of circles in each cell is non constant and unknown.
Thanks a lot!
P.S. As a non-native speaker, I think the title for my question is cumbersome and unclear, but I couldn't find a better way to summarize my question. If anyone has a better way to express that, please feel free to fix my title. Thanks!
Related
for my research i am using pygalmesh to create a mesh from an array. The problem i am trying to solve is a vascular network. In it, theres multiple vessel that are not meshed while the surrounding tissue is meshed. I am able to create the array but i am having trouble setting the correct variable. In the readme documentation theres a lot of variable for which i can't find any infos.
For example :
max_edge_size_at_feature_edges=0.025,
min_facet_angle=25,
max_radius_surface_delaunay_ball=0.1,
max_facet_distance=0.001
Is there a file where they explain all those variable, like what do they actually change in the mesh and all the ones that can be put in?
My current goal with meshing would be to reduce the number of 2d element around my vessels to reduce my array dimension in the later computation.
Thx
PS : If there are other meshing alternative that you know of that can mimic pygalmesh meshing from an array and are easy to use, let me know!
Hello,
in my 2d software i have two inputs available:
an array of XY points
[(x,y),(1,1),(2,2),(2,3),(-1,3),...]
and another matrix representing the closed 2D bezier curve handles
[((x,y),(x,y),(x,y)),
((-1,-1),(1,1),(1,2)),
((1,1),(2,2),(2,3)),
...]
How can i check if a point is inside or outside the given curve using python ? using preferably numpy maybe
I don't know how the theory of Bezier curves, so if your second list of points is a kind of compressed way to represent a Bezier curve, first try to sample some points of the curve with the precision you want.
So you have n points of your curve, and then you can apply a simple PIP algorithm : https://en.wikipedia.org/wiki/Point_in_polygon
I can explain in details later if you want to know how to do it programmatically.
I cant write code right here, because I need the entire program to understand properly, however I may provide two approaches how to do that.
The hardest way is to approximate each Bézier curve by a polyline. And then, according to the wiki you can use two techniques:
Ray casting algorithm: the shorthand of the algorithm: You put a ray, which starting from a point and goes through the entire polygon to an another point. Some lines lies inside a polygon, some outside. And then you check to which line belongs a specific point Looks like this:
Winding number algorithm: A little bit about winding numbers. So if a winding number is non-zero, the point lies inside the polygon
The huge drawback of this approach is that the accuracy depends on how close you approximated a curve to a polyline.
The second way is to use a bitmap. For example, you set your points to the white then render the area under the curve to the black and see if your points remain white. This method is more accurate and the fastest one, because you can use the GPU for the render.
And some links related to the first a approach:
https://pomax.github.io/bezierinfo/#intersections
http://web.mit.edu/hyperbook/Patrikalakis-Maekawa-Cho/node80.html
I have a large number of galaxies. I need to sort these galaxies into a spheres of radius N, calculate the average numbers of galaxies in each sphere, and plot a graph of this against radius N.
The galaxies are stored in a .fits file as radial coordinates (right ascension, declination and redshift). I'm using pyFITS and astropy to convert the galaxies coordinates into cartesian coordinates with Earth at (0,0,0) and then store the coordinates in a numpy array with the structure: ((x,y,z),(x1,y1,z1),etc.)
In order to seperate the galaxies into spheres of radius N, I am randomly selecting a galaxy from the array, then iterating through the array calcuating the distance between the randomly selected galaxy and the current galaxy. If the distance is less than or equal to the radius, it is added to the sphere. This is repeated as many times as the number of bubbles that need to be calculated.
My current method for this is really slow. I'm unfamiliar with numpy (I've been figuring things out as I'm going along), and I can't really see a better method than just iterating through all the galaxies.
Is there a way to do this any faster (something to do with numpy arrays - I'm converting them to a normal python list right now)? This is what I'm doing right now (https://github.com/humz2k/EngineeringProjectBethe/blob/humza/bubbles.py).
First, it's generally better to post samples of your code in your question where your issue is (such as the part where you select the radii you want to keep), rather than links to your entire script :)
Second, numpy arrays are great for scientific programming! They allow you to easily store data and perform matrix operations on that data without have to loop through the native Python lists. If you know MATLAB, they basically allow you to do most of the same things MATLAB's arrays do. Some more information can be found here and here. pandas dataframes are also good to use.
On to your code. At the end of your read_data function, you can combine some of those coordinates statements and probably don't need to add the tolist() because it's a numpy.array (which is faster and uses less memory, see the links above).
In your get_bubbles function, I don't think you need to make copies of the data. The copies will also take up memory. The biggest issue I see here is using the variable i twice in your loops. That's bad because i is replaced in the second loop. For example,
for i in [1, 2, 3, 4]:
for i in np.array([5, 6, 7, 8]):
print(i)
print 5, 6, 7, 8 four times. It's also bad because we can't tell which i does what you want (having no comments doesn't help either ;) ). Replace the i variable in the second loop with another variable, like j.
Here are two options to make lists faster: list comprehensions and initializing numpy.arrays. You can read about list comprehensions here. An example of initializing numpy.arrays is
new_data = np.zeros(len(data))
for i in range(len(data)):
new_data[i] = data[i]
Finally, you could create a separate array for the radii and look into using numpy.where to select the indexes of the radii that match your criteria.
That was kind of a lot, hope it helps.
Please allow me to start the question with a simplest task:If I have four points which are vertices of a rectangle, stored in a 4x2 matrix, how can I turn this into a rectangular window? (Please do not use any special command specific to drawing rectangles as the rectangle is raised just to represent a general class of regular geometrical object)
To make things more complicated, suppose I have a nx2 matrix, how can I connect all of the n points so that it becomes a polygon? Note the object is not necessarily convex. I think the main difficulty is that, how can R know which point should be connected with which?
The reason I am asking is that I was doing some image processing on a fish, and I managed to get the body line of the fish by finding the contour with opencv in python, and output it as a nx2 csv file. When I read the csv file into R and tried to use the SpatialPolygnos in the sp package to turn this into a polygon, some very unexpected behavior happened; there seems to be a break somewhere in the middle that the polygon got cut in half, i.e. the boundary of the polygon was not connected. Is there anyway I can fix this problem?
Thank you.
Edit: Someone kindly pointed out that this is possibly a duplicate of another question: drawing polygons in R. However the solution to that question relies on the shape being drawn is convex and hence it makes sense to order by angels; However here the shape is not necessarily convex and it will not work.
Do you want it to be a spatstat study region (of class owin) since you have the spatstat tag on there? In that case you can just use owin(poly=x) where x is your nx2 matrix (after loading the spatstat library of course). The rows in this matrix should contain the vertices of the polygon in the order that you want them connected (that's how R knows which point to connect with which). See help(owin) for more details.
I have a big list of tuples (a, b), where both a and b are 9-dimensional vectors from the same space. This essentially encodes states of a system and some transitions. I would like to visualize the field described by these tuples, as arrows pointing from a->b, either in 2D or 3D. One of my problems however is that this is not a well-behaved vector field (not continuous) but I have reasons to believe that it can probably be laid out nicely, even in 2D.
Does anyone know of a toolbox (for matlab/python) or program that can do this? This would presumably first involve some kind of dimensionality reduction on a and b and then plot little arrows from one point to another.
Thank you for your help!
I'm not 100% sure if this answers your question or not, but you may want to look at Recurrence Plots. If this is what you're after, then you wont need any additional Matlab toolboxes.
Okay, turns out MATLAB can do this but it's not very pretty.
It basically boils down to doing PCA, and then using the quiver function to do the plotting:
My matrix X here contains starting points of my high dimensional nodes in odd rows, and ending points in even rows. Then:
[COEFF, SCORE]= princomp(zscore(X));
x=SCORE(1:2:end,1);
y=SCORE(1:2:end,2);
z=SCORE(1:2:end,3);
u=SCORE(2:2:end,1);
v=SCORE(2:2:end,2);
w=SCORE(2:2:end,3);
quiver3(x,y,z,u-x,v-y,w-z,0);
The downside is that I can't find a good way to color the edges, so I get a huge mess if I just do it trivially. Ah well, good enough for now!
Here's a Matlab toolbox of dimension reduction algorithms. I haven't worked with it, but I have worked with dimension reduction, and it seems like a manifold charting/local coordinates algorithm would be able to extract a low-dimensional representation.
TU Delft Dim. Red. Toolbox