Skip to content

Commit

Permalink
swap axes instead of transposing
Browse files Browse the repository at this point in the history
  • Loading branch information
camisowers committed Sep 5, 2023
1 parent 76f83d5 commit e1292c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/alpineer/load_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,22 @@ def fov_to_ome(
# Reorder the DataArray as OME-TIFFs require [Channel, Y, X]
fov_xr: xr.DataArray = load_imgs_from_tree(
data_dir=data_dir, img_sub_folder=img_sub_folder, fovs=fovs, channels=channels
).transpose("fovs", "channels", "cols", "rows")
)

# convert to numpy for axes swapping without image rotation
fov_np = fov_xr.as_numpy()
fov_np = np.swapaxes(fov_np, 2, 3)
fov_np = np.swapaxes(fov_np, 1, 2)

fov_xr_reordered = xr.DataArray(
fov_np,
coords=[fov_xr.fovs, fov_xr.channels, fov_xr.rows, fov_xr.cols],
dims=["fovs", "channels", "rows", "cols"],
)

_compression: dict = {"algorithm": "zlib", "args": {"level": 6}}

for fov in fov_xr:
for fov in fov_xr_reordered:
fov_name: str = fov.fovs.values
ome_file_path: pathlib.Path = pathlib.Path(ome_save_dir) / f"{fov_name}.ome.tiff"

Expand Down Expand Up @@ -613,6 +624,6 @@ def ome_to_fov(ome: Union[str, pathlib.Path], data_dir: Union[str, pathlib.Path]

image_utils.save_image(
fname=save_dir / f"{channel}.tiff",
data=ome_tiff_page.asarray().transpose(),
data=ome_tiff_page.asarray(),
compression_level=6,
)
13 changes: 11 additions & 2 deletions tests/load_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,16 @@ def create_img_data(tmp_path_factory) -> Iterator[Tuple[pathlib.Path, xr.DataArr

# Create OME-TIFFs
_compression: dict = {"algorithm": "zlib", "args": {"level": 6}}
data_xr_transposed = data_xr.transpose("fovs", "channels", "cols", "rows")
data_np = data_xr.as_numpy()
data_np = np.swapaxes(data_np, 2, 3)
data_np = np.swapaxes(data_np, 1, 2)

data_xr_transposed = xr.DataArray(
data_np,
coords=[data_xr.fovs, data_xr.channels, data_xr.rows, data_xr.cols],
dims=["fovs", "channels", "rows", "cols"],
)

for fov in data_xr_transposed:
fov_name: str = fov.fovs.values
ome_file_path: pathlib.Path = pathlib.Path(test_dir) / f"{fov_name}.ome.tiff"
Expand Down Expand Up @@ -726,7 +735,7 @@ def test_fov_to_ome(self, fovs, channels) -> None:

# Assert that the channel values in the OME-TIFF are correct.
for page, chan in zip(ome_tiff.pages, channels):
actual_data = page.asarray().transpose()
actual_data = page.asarray()
desired_data = self.data_xr.sel(fovs=fov_name, channels=chan).values
np.testing.assert_equal(actual_data, desired_data)

Expand Down

0 comments on commit e1292c6

Please sign in to comment.