Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
tathey1 committed Jan 10, 2024
1 parent e2ebf47 commit f7704a7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
61 changes: 60 additions & 1 deletion brainlit/utils/tests/test_write.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import pytest
import zarr
import numpy as np
from brainlit.utils.write import zarr_to_omezarr, czi_to_zarr, write_trace_layer
from brainlit.utils.write import (
zarr_to_omezarr,
zarr_to_omezarr_single,
czi_to_zarr,
write_trace_layer,
)
import os
import shutil
import zipfile
Expand Down Expand Up @@ -87,6 +92,27 @@ def test_writeome_baddim(init_3dzarr, init_4dzarr):
shutil.rmtree(out_path)


def test_writeome_single_baddim(init_3dzarr, init_4dzarr):
# error for 4d zarrs
zarr_path, data_dir = init_4dzarr
out_path = data_dir / "fg_ome.zarr"
with pytest.raises(ValueError, match=r"Conversion only supported for 3D arrays"):
zarr_to_omezarr_single(zarr_path=zarr_path, out_path=out_path, res=[1, 1, 1])

# error if ome already exists
zarr_path, data_dir = init_3dzarr
out_path = data_dir / "fg_ome.zarr"
zarr_to_omezarr_single(zarr_path=zarr_path, out_path=out_path, res=[1, 1, 1])

with pytest.raises(
ValueError,
match=f"{out_path} already exists, please delete the existing file or change the name of the ome-zarr to be created.",
):
zarr_to_omezarr_single(zarr_path=zarr_path, out_path=out_path, res=[1, 1, 1])

shutil.rmtree(out_path)


def test_writezarr_badpar(init_4dczi):
czi_path, data_dir = init_4dczi
with pytest.raises(ValueError, match="parallel must be positive integer, not 1"):
Expand Down Expand Up @@ -159,6 +185,39 @@ def test_writeome(init_3dzarr):
)


def test_writeome_single(init_3dzarr):
res = [1, 1, 2] # in nm
dimension_map = {"x": 0, "y": 1, "z": 2}
zarr_path, data_dir = init_3dzarr
out_path = data_dir / "fg_ome_single.zarr"

assert not os.path.exists(out_path)
zarr_to_omezarr_single(zarr_path=zarr_path, out_path=out_path, res=res)
assert os.path.exists(out_path)

# check units are micrometers
ome_zarr = zarr.open(out_path)
metadata = ome_zarr.attrs["multiscales"][0]

dimension_names = []
for dimension in metadata["axes"]:
assert dimension["unit"] == "micrometer"
assert dimension["type"] == "space"
dimension_names.append(dimension["name"])

# check resolutions are multiples of 2 scaled in xy
for resolution in metadata["datasets"]:
lvl = int(resolution["path"])
true_res = np.multiply(res, [2**lvl, 2**lvl, 1]) / 1000 # in microns
true_res = [
true_res[dimension_map[dimension_name]]
for dimension_name in dimension_names
]
np.testing.assert_almost_equal(
true_res, resolution["coordinateTransformations"][0]["scale"], decimal=3
)


def test_write_trace_layer(init_omezarr):
data_dir, res = init_omezarr

Expand Down
6 changes: 5 additions & 1 deletion brainlit/utils/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ def zarr_to_omezarr_single(zarr_path: str, out_path: str, res: list, parallel: i
"coordinateTransformations": [
{
"type": "scale",
"scale": [res[2], res[0] * 2**lvl, res[1] * 2**lvl],
"scale": [
res[2] / 1000,
res[0] * 2**lvl / 1000,
res[1] * 2**lvl / 1000,
],
}
],
}
Expand Down

0 comments on commit f7704a7

Please sign in to comment.