-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Point In Face #1056
base: main
Are you sure you want to change the base?
Point In Face #1056
Conversation
@ philipc2 do you think this should be an internal function or exposed to the user? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include an ASV benchmark. I'd suggest doing a parameterized benchmark for the 120 and 480 km MPAS grids.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the optimized functions from #1072 and try to write the function entirely in Numba. This may require us to pass in both the cartesian and spherical versions of point & polygon. Let me know if you have any questions!
@philipc2 do you know why this is failing? It doesn't have any issues locally when I run the tests, but for some reason, it fails on the CI, saying that it can't resize the array. Could you checkout the branch and try running the test cases? Also, another odd problem is that half of the time the tests do fail locally, saying no faces were found in the subset, but the other half of the time faces are found. I am pretty confused by that as well. So any insight into that would be appreciated if you have any ideas. |
Can you point us to the specific testcase that's been failing?
Are you using the same setting? Did you clear the cache everytime you run it? |
The test cases that are failing are just the ones on the CI, which are listed under it if you click there, they are the two test cases I added for the point containing polygon grid function. Everything should be the same across environments, that I know of. That’s why I wanted to see if it passed or failed on someone else’s machine, to hopefully point me in some direction of what the error could be. |
Can you share your environment information? (Python Version & Installed packages) |
The tests are failing on my machine as well. |
I didn't see any |
uxarray/grid/grid.py
Outdated
face_edge_cartesian = _get_cartesian_face_edge_nodes( | ||
subset.face_node_connectivity.values, | ||
subset.n_face, | ||
subset.n_max_face_edges, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be the issue here.
If you pass in subset.n_max_face_nodes
it works correctly (both values are equivalent).
When the connectivity construction for self.face_edge_connectivity
, it leads to issues. I am looking deeper into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, okay. So something with constructing the face_edge causes issues. Thanks for the fix for now, let me know if you figure out the issue. From what I saw, it seems to have something to do with the inverse indices, it uses the wrong ones, the ones gotten from the subset, instead of the ones stored inside grid._ds["edge_node_connectivity"].attrs
. Maybe it is getting overwritten somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inverse indices in the context of edge_node_connectivity
are different than the inverse_indices
from the subset. I don't believe that they are overwritten, but something is definitely off.
Ah, I see what you mean, no, my local is failing at a different point. But on the same test cases. I will try and reset my environment to make sure it's up to date. |
|
||
assert len(grid.get_faces_containing_point(point_xyz=point_xyz)) == 2 | ||
|
||
# For a node three faces should be found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not always true, a node can have 1, 2, 3 or higher number of faces.
For 1, consider a node on the boundary, not on any other face.
For 2, consider a node on the boundary that is the actual node on the face, on both faces.
For 4/higher, consider a node that is the intersection of 4 faces or higher.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is true, in general. However this is a MPAS grid. Which is always going to have each node connected to 3 faces. So I think it’s an OK assumption here that it should result in 3 faces being found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is true, in general. However this is a MPAS grid. Which is always going to have each node connected to 3 faces. So I think it’s an OK assumption here that it should result in 3 faces being found.
Ok, you can keep it, but if the grid has holes and the node is along the boundary. I think, it'd be good to test these edge cases.
ASV BenchmarkingBenchmark Comparison ResultsBenchmarks that have improved:
Benchmarks that have stayed the same:
Benchmarks that have got worse:
|
uxarray/grid/grid.py
Outdated
# Get the face's edges for the whole subset | ||
face_edge_cartesian = _get_cartesian_face_edge_nodes( | ||
subset.face_node_connectivity.values, | ||
subset.n_face, | ||
subset.n_max_face_nodes, | ||
subset.node_x.values, | ||
subset.node_y.values, | ||
subset.node_z.values, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very expensive to re-compute each time we call this function. Consider adding an internal property or storing this in Grid._ds
like we do with our other methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do that, but it is a new subset each time the operation is run. So won't it be recomputed anyway? It should only be a face or a few at max for each subset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more efficient approach would be to store it in the original Grid
, where it can be computed once, and then once we subset, it should return a Grid
with the face_edge_nodes_xyx
appropriately indexed.
Though, if it is only a few faces at most, the perfromance may be negligible, but we need to consider the cases where we may be running millions of point in polygon queries for higher-resolution grids.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, okay yeah that would work. I didn't realize it would keep it through the subset operation.
return index | ||
|
||
|
||
def get_max_face_radius(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be _populate_max_face_radius
similar to our other population methods.
benchmarks/mpas_ocean.py
Outdated
class PointInPolygon(GridBenchmark): | ||
def time_whole_grid(self, resolution): | ||
point_xyz = np.array([self.uxgrid.face_x[0].values, self.uxgrid.face_y[0].values, self.uxgrid.face_z[0].values]) | ||
|
||
self.uxgrid.get_faces_containing_point(point_xyz=point_xyz) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the GH Actions report, it appears that a single Point in Face query is taking approximately 27.0±0.1ms
Ideally, we want to get this number significantly down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, consider including a single sample point query in the setup, since there is also some overhead with the KD tree construction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the performance I get for a 30km grid after doing the things mentioned above. pretty good for 30km grids
Only issue, is that it doesn't find any faces.
Here is my sample code I used.
import uxarray as ux
import numpy as np
import cProfile
profiler = cProfile.Profile()
grid_path = "/Users/philipc/PycharmProjects/uxarray/unstructured-grid-viz-cookbook/meshfiles/x1.655362.grid.nc"
data_path = "/Users/philipc/PycharmProjects/uxarray/unstructured-grid-viz-cookbook/meshfiles/x1.655362.data.nc"
uxds = ux.open_dataset(grid_path, data_path)
uxds.uxgrid.normalize_cartesian_coordinates()
_ = uxds.uxgrid.face_edge_connectivity
point = np.array([0.0, 0.0, 1.0])
res = uxds.uxgrid.get_faces_containing_point(point)
profiler.enable()
res = uxds.uxgrid.get_faces_containing_point(point)
profiler.disable()
profiler.dump_stats('pface.prof')
print(res)
Can do snakeviz pface.prof
to view the profiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this for me! I am trying to implement these changes now. In terms of it not finding any faces, does the mesh have holes in it? There may not be a face at the pole possibly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The grid is a global MPAS atmosphere grid with no holes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have been on an older version of the code. Just ran it again after pulling and it works. A single face is detected. Going to run a few more tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it working locally for you? It does for me, but the CI is failing. Not sure why.
Closes #905
Overview
Expected Usage
PR Checklist
General
Testing
Documentation
_
) and have been added todocs/internal_api/index.rst
docs/user_api/index.rst