feat: add geometry utilities for global domains (spherical coords, area weights, KNN graph)#473
feat: add geometry utilities for global domains (spherical coords, area weights, KNN graph)#473ayushhbasu wants to merge 3 commits into
Conversation
This function builds a KNN graph on a sphere using Cartesian coordinates, allowing for a specified number of neighbors and a backend for distance computation.
Added methods to compute area weights and Cartesian coordinates.
|
thanks @ayushhbasu I am adding @joeloskarsson here for an initial review. I see some potential overlap with our plan to move to weather-model-graphs for graph creation and coords handling. But he knows the roadmap for that better than I do. Maybe the changes here on the model side will still be required. |
|
That makes sense, especially if graph creation is moving towards weather-model-graphs. Given that, do you think it would be useful to keep the model side a bit more decoupled from how graphs are constructed? For example, having a small interface/adapter so the model can work with graphs regardless of whether they come from this geometry layer or an external source. This could help keep things flexible as the graph creation workflow evolves. Happy to help explore this direction if it aligns with the roadmap. |
|
@princekumarlahon It sounds like you are talking about #384 and yes I believe that does make sense. |
|
Thank you @sadamov — I appreciate you bringing in @joeloskarsson for roadmap clarity. I understand the concern about overlap with
The geometry and datastore changes are independent of where graph construction ultimately lives, and are required for:
These are foundational pieces for global forecasting and will be reused across the project. Given this, I'm happy to scope this PR down to geometry utilities and datastore methods, and remove For context, I've submitted a GSoC proposal (Mar 24) aligned with the Global Weather Forecasting direction, where this PR serves as the initial foundational step. I'll fully align the implementation here with the project's roadmap. Would you recommend removing Looking forward to your guidance, @joeloskarsson. |
|
Hi @ayushhbasu , I’ve been working on a similar direction for global forecasting support and came across this PR by @sadamov this aligns very closely with what I had in mind. I particularly like the separation between geometry utilities and datastore-level accessors. I was also exploring a similar approach for spherical coordinate handling and modular graph construction. Regarding the discussion on graph generation potentially moving to weather-model-graphs, I agree that keeping the geometry layer independent while deferring graph construction might be a cleaner design. I’d be happy to contribute here instead of duplicating pr i had already raised similar pr(#561 (comment)) for example:
Let me know where contributions would be most useful. Thanks! |
|
Hi @ayushhbasu, great work on these geometry utilities! I've been testing the spherical coordinate conversion logic alongside my work on weather-model-graphs with @yuvraajnarula. Regarding the discussion with @joeloskarsson about moving graph construction to WMG: I've already implemented a 3D Plotly visualizer in WMG that handles these Cartesian (x,y,z) coordinates perfectly. I'd be happy to help verify that the graphs generated via your geometry.py utilities are fully compatible with the hierarchical visualization needs we have. Looking forward to seeing how we can bridge this with WMG! |
|
Thanks @arzoo0511 and @Prince637-boo - really appreciate the alignment and the thoughtful inputs here. @arzoo0511 — great to hear this overlaps with your work. I’d definitely welcome collaboration. The most useful areas to contribute (especially after #444 is merged) would be:
On scoping: I’m planning to keep this PR focused on geometry + datastore utilities, and defer graph construction to @Prince637-boo — very helpful context. Great to know WMG already supports Cartesian More broadly, I see:
For context, this PR corresponds to the foundational geometry + datastore layer from my GSoC proposal, so I’m intentionally keeping it minimal and aligned with that direction. I’ll wait for guidance from @joeloskarsson on whether to remove Looking forward to collaborating |
|
Thanks @ayushhbasu for the clear direction. I completely agree with the decoupling: keeping core geometry in Neural-LAM and graph construction in WMG makes total sense. |
|
Thanks @Prince637-boo — great to see this. The Graph Validator seems like a perfect fit for validating outputs from lat_lon_to_cartesian(), especially for geometric consistency and connectivity. Once direction is confirmed, we can validate that these coordinates pass the WMG checks and align on conventions (e.g., normalization, ordering) across both sides. This should help cleanly bridge neural-lam with WMG. Looking forward to collaborating |
|
Hi @ayushhbasu , @Prince637-boo , This direction makes sense to me , keeping geometry utilities in neural-lam and delegating graph construction + validation to WMG feels like the right separation. I can focus on the following on the neural-lam side once #444 is merge @joeloskarsson : @Prince637-boo — the Graph Validator in WMG sounds very useful. Once things are wired up, I can help run geometry outputs through it and flag any inconsistencies early. Let me know if this works |
|
Thanks @arzoo0511! That sounds like a perfect plan. I've already implemented the Graph Validator in WMG (see PR #132: mllam/weather-model-graphs#132). It’s specifically designed to handle the geometric consistency (L2 norm) and connectivity checks we need. I’d be happy to collaborate on the interface documentation to ensure that the outputs from lat_lon_to_cartesian() align perfectly with these WMG requirements. Let’s sync once the core geometry utilities are ready! |
|
Closing this in favour of #650, which consolidates the global forecasting / spherical domain track for v0.8.0. The |
Description of changes
This PR introduces foundational geometry utilities to support global forecasting capabilities. It adds spherical coordinate conversion, latitude-based area weights, and a modular KNN graph builder for global domains.
Changes:
neural_lam/geometry.py:lat_lon_to_cartesian(): Converts (lon, lat) to (x, y, z) on a unit sphereget_area_weights(): Computes cos(latitude) weights normalized by the meanneural_lam/graph_builder.py:build_spherical_knn_graph(): Backend-agnostic KNN graph construction on a sphere using Cartesian coordinatesBaseDatastoreinneural_lam/datastores/base.py:get_area_weights(): Returns a DataArray of latitude-based weightsget_cartesian_coords(): Returns an array of (x, y, z) coordinatesMotivation:
As part of global forecasting support (#63), reusable geometry utilities are required for area-weighted metrics and spherical graph construction. This PR establishes a modular geometry layer that future work can build upon.
Dependencies:
This PR is designed to be standalone but will benefit from the
GlobalDummyDatastoreintroduced in #444 for testing. Tests for global-specific functionality will be added after #444 is merged.Issue Link
Part of #63 (global forecasting support)
Type of change
Checklist before requesting a review
pull --rebaseif possible)