Fix square mesh assumption in create_graph to support non-square domains#373
Fix square mesh assumption in create_graph to support non-square domains#373osten-antonio wants to merge 6 commits intomllam:mainfrom
Conversation
| for lev in range(1, mesh_levels + 1): | ||
| n = int(nleaf / (nx**lev)) | ||
| g = mk_2d_graph(xy, n, n) | ||
| n_larger = int(nleaf / (nx**lev)) |
There was a problem hiding this comment.
This rounding scheme is not compatible with the existing [1::3, 1::3] parent-selection logic below. For a rectangular grid like xy.shape == (100, 600), this produces level dims (14, 81) and then (4, 27), but sampling the previous level yields (5, 27) parents, so create_graph(..., n_max_levels=2) crashes at line 399 with ValueError: cannot reshape array of size 270 into shape (108,2). The coarse dims need to be derived from the sampled previous level, or the relabel/compose logic needs to change.
| assert r.shape[1] == d_features | ||
|
|
||
|
|
||
| def test_graph_creation_non_square_aspect_ratio(): |
There was a problem hiding this comment.
This test only covers the n_max_levels=1 case, so it misses the actual breakage introduced by the new sizing logic. Please add at least one rectangular multiscale case (n_max_levels=2) and one square-grid regression check for unchanged g2m connectivity, otherwise both failures above still pass CI.
| dm = np.sqrt( | ||
| np.sum((vm.data("pos")[(0, 1, 0)] - vm.data("pos")[(0, 0, 0)]) ** 2) | ||
| ) | ||
| dx = x_extent / mesh_dims[0][0] # cell width at finest mesh level |
There was a problem hiding this comment.
This changes g2m connectivity for every existing square graph, not just non-square ones. Previously the radius was based on one mesh-node spacing; here it becomes the cell diagonal, which is sqrt(2) larger on square meshes. On an 81x81 square grid, g2m edges jump from 9009 on main to 17757 here. That is a topology regression unless it was explicitly intended and rebase lined everywhere.
kshirajahere
left a comment
There was a problem hiding this comment.
The aspect-ratio fix is not safe to merge as written. It introduces a hard failure for non-square multiscale graphs because the rounded per-level mesh dimensions no longer match the existing 3x downsampling/relabeling assumption in the non-hierarchical path. A concrete repro is create_graph(..., xy.shape=(100, 600), n_max_levels=2), which now fails at create_graph.py:399 with a reshape error.
It also changes g2m topology globally by switching the coverage radius from single-axis neighbor spacing to the mesh-cell diagonal. On a square 81x81 grid, that increases g2m edges from 9009 on main to 17757 in this PR. The added test only exercises the one-level rectangular case, so neither regression is caught.
I would block this until:
- Multi-level rectangular meshes are generated with level sizes that are consistent with the parent mapping logic.
- Square-grid g2m connectivity is preserved, or the topology change is made explicit and covered with updated tests.
|
Hi @kshirajahere , thanks for the review! I have addressed both issue; fixed n_max_levels = 2 crash by using the previous level's coarse dimension, and on square grids I reverted to using the original dm formula. I have also added the tests covering multi-level rectangular mesh, and square grid g2m edge count preservation. Let me know if there is anything else to address :D |
|
Sorry for not looking at this earlier, but I would not put effort into such improvements to create_graph.py, as this functionality already exists and is much more robust in weather-model-graphs. The plan is to drop create_graph.py in its current form once #83 is complete. |
kshirajahere
left a comment
There was a problem hiding this comment.
Thanks for addressing the earlier blockers.
The multiscale reshape crash concern and square-grid g2m topology regression were both handled, and the added tests for those paths are useful.
I am requesting some more changes please address them.
| dy = y_extent / mesh_dims[0][1] # cell height at finest mesh level | ||
|
|
||
| # compare using mesh_dims because dx dy can cause rounding issue | ||
| if mesh_dims[0][0] == mesh_dims[0][1]: |
There was a problem hiding this comment.
This condition should be based on spacing equivalence (for example dx vs dy with tolerance), not only n_x == n_y. Rounded equal dims can happen on non-square domains and cause coverage regressions.
| # assign n_larger to whichever direction has greater physical extent | ||
| if x_extent >= y_extent: | ||
| n_x = n_larger | ||
| n_y = max(1, round(n_larger * y_extent / x_extent)) |
There was a problem hiding this comment.
Please guard degenerate coordinate extents before division. A zero extent on one axis should fail fast with a clear error or use a safe fallback.
|
Also add changelog entry |
|
Hi @kshirajahere thanks for the rereview! I think I will follow @joeloskarsson's advice seeing that there is much more development on weather-model-graph end with its multiple layout types. |
|
Thanks @osten-antonio. Closing per your own follow-up - going with @joeloskarsson's earlier suggestion to drop changes to |
Describe your changes
create_graphpreviously generated square mesh graphs regardless of the data grid's physical aspect ratio, by passing the same valuenfor both x and y dimensions ofmk_2d_graph. This caused two concrete problems, as stated in issue #371 :Changes:
n_xandn_yare now derived independently by scaling the larger dimension proportionally to the physicaldomain extent (
x_extent / y_extent), assigningn_largerto whichever axis has greater physical extent.mesh_dimstracks(n_x, n_y)per level, replacing the square assumption in the merge loop reshape.dmis now computed from the mesh cell diagonal (sqrt(dx**2 + dy**2)) rather than x-direction spacing only. This would also guarantee full grid coverage with zero orphan node, which was a problem when migrating to a non square resolution.No new dependencies.
Issue Link
Closes #371
Type of change
Checklist before requesting a review
pullwith--rebaseoption if possible).Checklist for reviewers
Each PR comes with its own improvements and flaws. The reviewer should check the following:
Author checklist after completed review
reflecting type of change (add section where missing):
Checklist for assignee