You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.sel() unexpectedly forgets to slice the uxgrid too, for UxDataArray and UxDataset #1641
When calling .sel() on a UxDataArray or UxDataset, the underlying uxgrid does not get sliced at all.
An example workflow that might want to utilize .sel() instead of .isel() is if you are slicing multiple times in a row, or doing subsetting, and want to utilize faces' original labels. In that case, it would be reasonable to do something like:
arr=ux.tutorial.open_dataset("outCSne30-vortex")['psi']
arr1=arr.assign_coords(n_face=np.arange(arr.n_face.size)) # make sure to track the faces' original labelsarr2=arr1.isel(n_face=range(0, 100, 5))
arr3=arr2+7# or some other math or analysis operations# then realize "oh I want to check what the results look like on what were originally faces 20, 30, and 40"arr4=arr3.sel(n_face=[20,30,40])
# and "I want to remind myself where the data is located so I want to plot this"arr4.plot()
This currently works up until the last line, when there is a crash because the grid wasn't also sliced to just faces 20, 30, and 40.
What did you expect to happen?
I expected .sel() to work similarly to .isel(); the underlying grid should be sliced appropriately. Or, at the very least, I expected to receive a warning message or a crash to suggest that .sel() is not yet fully supported by uxarray, rather than a confusing error later on when the data no longer aligns with the grid (for example, the code above makes ValueError: Length of values (3) does not match length of index (20)).
Oh! I didn't realize it originally, but this is almost certainly related to #1444! @dylannelson@erogluorhan. This would easily explain how the Grid's time and the data's time ended up being different times, because the example there used sel!
@erogluorhan@Sevans711 Agreed, and the framing I'd use is that neither case is a UXarray invention: xarray already treats sel on a dim with no coordinate as positional, so case A is just xarray semantics and case B is xarray's label lookup. So implement both by resolving labels to positions and forwarding to isel, keeping Grid.isel as the single place that rebuilds topology — uxgrid._ds.sel(n_face=7) would slice only the variables carrying n_face without renumbering face_node_connectivity or dropping orphaned nodes. The rule to agree on: sel is xarray's sel plus the grid comes along, with no other divergence.
Agreed, and the framing I'd use is that neither case is a UXarray invention: xarray already treats sel on a dim with no coordinate as positional, so case A is just xarray semantics and case B is xarray's label lookup. So implement both by resolving labels to positions and forwarding to isel, keeping Grid.isel as the single place that rebuilds topology — uxgrid._ds.sel(n_face=7) would slice only the variables carrying n_face without renumbering face_node_connectivity or dropping orphaned nodes. The rule to agree on: sel is xarray's sel plus the grid comes along, with no other divergence.
I think the cases here are referring to thoughts I sent via DM but didn't put in this issue yet, so I am posting them here for future reference:
My thoughts are to avoid any complicated logical paths for now, and only implement the following for grid dims (n_face, n_edge, n_node):
(A) If the dim doesn't have separately-assigned coordinates, then sel() works just like isel(), because the implied coordinates are just the indices anyway.
(B) If the dim has separately-assigned coordinates, then sel() just uses xarray's sel() implementation, but for now also assert that the data is also on that dim. E.g., for now, if uxarr is face-centered, then uxarr.sel(n_face=7) just uses uxarr.data.sel(n_face=7) and uxarr.uxgrid._ds.sel(n_face=7). But, uxarr.sel(n_node=5) would fail with NotImplementedError.
Now, responding to your message: great point about still needing to forward to Grid.isel() even in case B. Resolving labels to positions then forwarding to Grid.isel() will likely be a cleaner implementation than using Grid._ds.data.sel().
Activity
Sevans711 commentedon Aug 5, 2026
Oh! I didn't realize it originally, but this is almost certainly related to #1444! @dylannelson @erogluorhan. This would easily explain how the Grid's
timeand the data'stimeended up being different times, because the example there usedsel!rajeeja commentedon Aug 19, 2026
@erogluorhan @Sevans711 Agreed, and the framing I'd use is that neither case is a UXarray invention: xarray already treats
selon a dim with no coordinate as positional, so case A is just xarray semantics and case B is xarray's label lookup. So implement both by resolving labels to positions and forwarding toisel, keepingGrid.iselas the single place that rebuilds topology —uxgrid._ds.sel(n_face=7)would slice only the variables carryingn_facewithout renumberingface_node_connectivityor dropping orphaned nodes. The rule to agree on:selis xarray'sselplus the grid comes along, with no other divergence.Sevans711 commentedon Aug 19, 2026
I think the cases here are referring to thoughts I sent via DM but didn't put in this issue yet, so I am posting them here for future reference:
Now, responding to your message: great point about still needing to forward to
Grid.isel()even in case B. Resolving labels to positions then forwarding toGrid.isel()will likely be a cleaner implementation than usingGrid._ds.data.sel().