Repository navigation
Vectorize Coordinate Conversion Functions - #748
Conversation
|
I'm almost done with vectorizing all the functions. The only issue I'm running into is one of the point inside polygon tests is now failing ( I stepped through the function, and was able to determine that the issue somewhere in this portion of the return (
_check_intersection(ref_edge_north, north_edges)
+ _check_intersection(ref_edge_south, south_edges)
) % 2 != 0With the changes I've made here, the sum of the intersections is 2, however prior to the change the number of intersections is 1. Do you know what might be causing this? Could it be a precision issue? Every other test is passing except for this one. |
Thank you very much for your work! I feel like the problem might be a combination of precision and the coordinate conversion style for this test case. I tried to change our error tolerance to 10-3 but it's still failing, so I don't think that's the case (at least not the only factor) So the test case face tests a very extreme case: The face goes across the equator while still encompassing the North Pole. The face may be referred to as the "bigger face" on the sphere, while it should refer to the "smaller face". I can look into this tomorrow and see what I can find out! But if you feel this test case is unrealistically complicate for our users, you can feel free to remove it! |
Thanks for the response! If you'd like to look into it, that would be greatly appreciated. I'll be requesting reviews soon, so if we don't get to fixing this test, we can comment it out and put together an issue for this edge case. |
There was a problem hiding this comment.
uxarray/uxarray/grid/coordinates.py
Line 55 in edb1b79
Something I found here:
the line lat = np.where(np.abs(z) > 1.0 - ERROR_TOLERANCE, np.sign(z) * np.pi / 2, lat) will turn lat from a scalar to an size1 np.ndarray
For example, before this line, lat = 0.64052231, after the line lat = array(0.64052231), is this something we're expecting?
There was a problem hiding this comment.
lat = 0.64052231 and lat = array(0.64052231) are both treated the same by numpy.
There was a problem hiding this comment.
The reason I discovered it because the return is [lon, array(lat)], for example when I call the _node_xyz_to_lonlat_rad on an array of vertices, I will sometimes get [[3.14, array(-1.57)],[0, array(1.57)],[1, array(0.29)]] as the return. The dimensions don't look uniform for the longitude and latitude.
Although it's treated the same by numpy, it's still a good idea to make the dimension consistent and following the API description
There was a problem hiding this comment.
thank you for your explanation. I understood it will be treated the same by numpy. My concern is
-
Most of my functions would use
pythong.listfor the coordinates conversion according to the previous implementation. And I noticed some of them are still using the samepythong.listright now. I just worry they might cause some potential problems. -
When we do the array conversion between
python.listandnumpy.array()or between other data types for some nested array, I am worrying that this inconsistent will cause problems. Especially in this function call, sometimes it will actually return[lon, lat]with lat as a scalar.
I just encountered so many minor issues between the conversion of python.list and numpy.array when I was calling the cartesian coordinates conversion function and being very cautious about it now.
There was a problem hiding this comment.
I've actually mostly left the original implementations the same, so there isn't any issues with using the lists.
For example, some of the functions would have a gca_pt (or something similar) that would be a list of cartesiain coordinates: [x, y, z]. We can still use these new functions with minimal changes.
gca_pt = [0, 1, 0]
# unpack a list for parameters
lon, lat = _xyz_to_lonlat_rad(*gca_pt)
# equivalent to
lon, lat = _xyz_to_lonlat_rad(gca_pt[0], gca_pt[1], gca_pt[2])Even if one of these returns a np.array(0) or something similar, it hasn't broken anything and is still being treated as a scalar.
|
Hi @philipc2 , I think I have figured out why it will break, it's related to the coordinates conversion style in my current So when in your branch, one of the south pole was presented as [3.14,-0.5 * np.pi ], and it cannot be handled by my function. if np.absolute(dz) < (1.0 - ERROR_TOLERANCE):
d_lon_rad = math.atan2(dy, dx)
d_lat_rad = np.arcsin(dz)
if d_lon_rad < 0.0:
d_lon_rad += 2.0 * np.pi
elif dz > 0.0:
d_lon_rad = 0.0
d_lat_rad = 0.5 * np.pi
else:
d_lon_rad = 0.0
d_lat_rad = -0.5 * np.piAfter I add an extra if scalar:
if np.abs(z) > 1.0 - ERROR_TOLERANCE:
lat = np.sign(z) * np.pi / 2
lon = 0.0I feel like this will be a good test case for the new coordinates conversion function. |
Thank you for the explanation! It's working for me locally now too. |
|
For combability, the coordinate conversion functions here are all written in pure Numpy, no longer depending on Numba in certain cases. The main reason for this is that many Numpy functions (such as Once Rachel joins us this summer, we can do some further Numba exploration as was described in #202 |
|
Performance improvements sound great; I am excited to review it as soon as I can (aiming at today) |
erogluorhan
left a comment
There was a problem hiding this comment.
This is looking great; just one comment below
Good work. After some profiling and understanding the number of calls to certain functions - I believe in introducing some numba decorator again, that were removed from this PR - specifically the one's in area calculation; it might speedup things. Getting things compatible and decorating all function->function->function with Numba can be a pain. It is known that Numba struggles with functions that use dynamic data structures. |
|
There is some numba in grid/ geometry, neighbors and connectivity - all mostly non-nested functions. A note for #202 |
Thanks for that! Can you reference that in the issue directly too? |
Yeah, I'm hoping we can explore this more this summer, since there is still room for improvement with the performance. |






Closes #346
Overview
_lonlat_rad_to_xyz,_xyz_to_lonlat_rad, and_xyz_to_lonlat_degUsage Example