Repository navigation
Consistent coordinates for pre-compute and 1d profile generation - #50
Conversation
Summary of ChangesHello @sungeunbae, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a crash related to coordinate inconsistencies during 1D profile generation by pre-calculating mesh coordinates before checking for basin membership. The changes look solid and effectively fix the reported bug. Additionally, the defensive change to use a tolerance for floating-point comparisons in the geometry module improves the robustness of the code.
I've added a couple of suggestions to improve code clarity and remove minor redundancies in the profile generation script. Overall, this is a good set of changes.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
lispandfound
left a comment
There was a problem hiding this comment.
Just minor improvement suggestions. Provided they past tests I'm happy for you to merge after considering the changes without requiring another review.
| if np.abs(lat - lats[0]) <= 1e-07: | ||
| if np.abs(lat - lats[0]) <= tol: | ||
| corner_lat_ind = 0 | ||
| elif np.abs(lat - lats[-1]) <= 1e-07: | ||
| elif np.abs(lat - lats[-1]) <= tol: | ||
| corner_lat_ind = nlat - 1 | ||
| else: | ||
| raise ValueError( | ||
| f"Point lies outside of surface bounds. Lat {lat} outside [{lats[0]} {lats[-1]}]" | ||
| ) | ||
|
|
||
| if np.abs(lon - lons[0]) <= 1e-07: | ||
| if np.abs(lon - lons[0]) <= tol: | ||
| corner_lon_ind = 0 | ||
| elif np.abs(lon - lons[-1]) <= 1e-07: | ||
| elif np.abs(lon - lons[-1]) <= tol: | ||
| corner_lon_ind = nlon - 1 |
There was a problem hiding this comment.
Similarly here you should use np.isclose
| @@ -239,17 +243,20 @@ def point_on_vertex( | |||
| Latitude of the point to check. | |||
| lon : float | |||
| Longitude of the point to check. | |||
| tol : float | |||
| Tolerance for floating point equality test, default: 1e-07 | |||
|
|
|||
|
|
|||
| Returns | |||
| ------- | |||
| bool | |||
| True if the point matches a boundary vertex within tolerance (1e-07), otherwise False. | |||
| True if the point matches a boundary vertex within tolerance, otherwise False. | |||
| """ | |||
| # Vectorized comparison using NumPy with tolerance | |||
| lat_matches = ( | |||
| np.abs(boundary_lats - lat) <= 1e-07 | |||
| np.abs(boundary_lats - lat) <= tol | |||
| ) # np.isclose is not supported by njit | |||
| lon_matches = np.abs(boundary_lons - lon) <= 1e-07 | |||
| lon_matches = np.abs(boundary_lons - lon) <= tol | |||
|
|
|||
| matches = np.logical_and(lat_matches, lon_matches) | |||
There was a problem hiding this comment.
This is just np.isclose. both np.isclose and np.allclose have been supported by numba since four years ago, also here.
lat_close = np.isclose(lat, boundary_lats, rtol=0.0, atol=tol)
lon_close = np.isclose(lon, boundary_lons, rtol=0.0, atol=tol)
return np.any(lat_close & lon_close)>>> @numba.njit
... def test(lat, lon, boundary_lats, boundary_lons, tol=1e-7):
... lat_close = np.isclose(lat, boundary_lats, rtol=0.0, atol=tol)
... lon_close = np.isclose(lon, boundary_lons, rtol=0.0, atol=tol)
... return np.any(lat_close & lon_close)
...
>>> test(-43.0, 172.64, np.array([-43.0, -44.0]), np.array([172.0, 172.65]))
False
>>> test(-43.0, 172.64, np.array([-43.0, -44.0]), np.array([172.0, 172.65]))
False
>>> test(-43.0, 172.64, np.array([-43.0, -44.0]), np.array([172.0, 172.65]))
False
>>> test(-43.0, 172.64, np.array([-43.0, -44.0]), np.array([172.0, 172.64]))
False
>>> test(-43.0, 172.64, np.array([-43.0, -43.0]), np.array([172.0, 172.64]))
True
There was a problem hiding this comment.
@lispandfound Thanks for the update regarding np.isclose() support in Numba—I wasn't aware it was supported now.However, after reviewing the documentation, I prefer to stick with np.abs(a - b) <= tol for this specific case.
np.isclose uses a relative tolerance by default (rtol=1e-05)
numpy.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
and internally checks
absolute(a - b) <= (atol + rtol * absolute(b))
For geographic coordinates, we need a fixed spatial tolerance (e.g., 1e-7 degrees) regardless of the coordinate magnitude (e.g., relative error at 170° is much larger than at 1°). To achieve this with np.isclose(), I would have to explicitly disable the relative tolerance
np.isclose(a, b, rtol=0, atol=tol)
This is more verbose, and less readable than the direct comparision.. Also inside @njit, direct subtraction is the most efficient primitive operation avoiding the extra overheads of np.isclose() (eg. internal handing of NaN, infinites etc).
https://numpy.org/doc/stable/reference/generated/numpy.isclose.html
There was a problem hiding this comment.
Based on my observation, I even feel like to replace the 2 uses of "np.isclose()" with np.abs() <=tol
Line 1032-1040
lat = np.where(
np.isclose(zg, 0),
0,
90 - np.arctan(np.sqrt(xg**2 + yg**2) / zg) / RPERD - np.where(zg < 0, 180, 0),
)
lon = np.where(
np.isclose(xg, 0), 0, np.arctan(yg / xg) / RPERD - np.where(xg < 0, 180, 0)
)
Perhaps this is not critical as it is comparing with 0, and np.isclose(x,0) is abs(x-0) <= atol + rtol*0, effectively `abs(x)<=atol (which is 1e-8)
But for the consistency, I feel like to go ahead. Thought? @lispandfound
This PR fixes a ValueError crash ("Basin point lies outside of the extent of the basin surface") occurring during 1D profile generation for points situated extremely close to basin boundaries.
From inspection, the location that caused this was (172.72932, -43.0)
The root cause of the crash was a discrepancy between the coordinates used for basin membership checks and those used for velocity interpolation:
Added a tolerance (tol=1e-07) to floating-point comparisons in the geometry logic (specifically find_basin_adjacent_points and related Numba functions).