Skip to content

Commit

Permalink
Fix xarray dimension renaming
Browse files Browse the repository at this point in the history
xarray used to produce a UserWarning when renaming DataArrays with
coordinates. At the time this warning would print to screen anytime
ij.py.to_xarray() was used with a dimension order sequence. In order to
address this issue I followed the warning's suggestion and used
swap_dims and set_index to by pass this issue. Unfortunately I did not
see [this discussion](pydata/xarray#7950)
indicating the proper remedy of supressing the warning. This warning was
slated for removal as, I presume, the xarray team decided to maintain
its behavior.

As such *my* fix is bad and produces this error when trying to rename
any axis that has a coordinate:

ValueError: replacement dimension 'row' is not a 1D variable along the
old dimension 'ch'

This commit fixes this bug by simply reverting back to our old behavior
of using the rename() method of xarray.DataArray.

Closes #320
  • Loading branch information
elevans committed Dec 19, 2024
1 parent 54cf2f3 commit d68bc65
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions src/imagej/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,7 @@ def _rename_xarray_dims(xarr, new_dims: Sequence[str]):
for i in range(xarr.ndim):
dim_map[curr_dims[i]] = new_dims[i]

# swap dims and set indexed coordinates
coord_map = {v: k for k, v in dim_map.items()}
xarr = xarr.swap_dims(dim_map)

return xarr.set_index(coord_map)
return xarr.rename(dim_map)


def _delete_labeling_files(filepath):
Expand Down

0 comments on commit d68bc65

Please sign in to comment.