Skip to content

Commit

Permalink
Add regression test for lsm modality
Browse files Browse the repository at this point in the history
  • Loading branch information
balbasty authored and calvinchai committed Nov 7, 2024
1 parent fc4b48a commit 1b34faa
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/data/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os

Check failure on line 1 in tests/data/generate.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D100)

tests/data/generate.py:1:1: D100 Missing docstring in public module
import tempfile

import test_lsm
import zarr

from linc_convert.modalities.lsm import mosaic

if __name__ == "__main__":
with tempfile.TemporaryDirectory() as tmp_dir:
test_lsm._write_test_data(tmp_dir)
output_zarr = os.path.join(tmp_dir, "output.zarr")
mosaic.convert(tmp_dir, output_zarr)
zarr.copy_all(zarr.open(output_zarr), zarr.open("data/lsm.zarr.zip", "w"))
Binary file added tests/data/lsm.zarr.zip
Binary file not shown.
75 changes: 75 additions & 0 deletions tests/test_lsm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os

Check failure on line 1 in tests/test_lsm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D100)

tests/test_lsm.py:1:1: D100 Missing docstring in public module
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase

import numpy as np
import tifffile
import zarr

from linc_convert.modalities.lsm import mosaic


def _compare_zarr_archives(path1, path2):

Check failure on line 13 in tests/test_lsm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ANN202)

tests/test_lsm.py:13:5: ANN202 Missing return type annotation for private function `_compare_zarr_archives`

Check failure on line 13 in tests/test_lsm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ANN001)

tests/test_lsm.py:13:28: ANN001 Missing type annotation for function argument `path1`

Check failure on line 13 in tests/test_lsm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ANN001)

tests/test_lsm.py:13:35: ANN001 Missing type annotation for function argument `path2`
"""
Compare two Zarr archives to check if they contain the same data.
Parameters
----------
- path1 (str): Path to the first Zarr archive.
- path2 (str): Path to the second Zarr archive.
Returns
-------
- bool: True if both archives contain the same data, False otherwise.
"""
# Open both Zarr groups
zarr1 = zarr.open(path1, mode="r")
zarr2 = zarr.open(path2, mode="r")

# Compare keys (dataset structure)
if zarr1.keys() != zarr2.keys():
return False
if zarr1.attrs != zarr2.attrs:
return False

# Compare each array in both archives
for key in zarr1.keys():
array1 = zarr1[key][:]
array2 = zarr2[key][:]

# Check for equality of the arrays
if not np.array_equal(array1, array2):
print(f"Mismatch found in dataset: {key}")
return False
if zarr1[key].attrs != zarr2[key].attrs:
return False

# If all checks pass
print("The Zarr archives are identical.")
return True


def _write_test_data(directory):

Check failure on line 53 in tests/test_lsm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ANN202)

tests/test_lsm.py:53:5: ANN202 Missing return type annotation for private function `_write_test_data`

Check failure on line 53 in tests/test_lsm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ANN001)

tests/test_lsm.py:53:22: ANN001 Missing type annotation for function argument `directory`
root_path = Path(directory)
for z in range(1, 2, 1):
for y in range(1, 3, 1):
folder = root_path / f"test_z{z}_y{y}"
folder.mkdir(parents=True, exist_ok=True)
for plane in range(1, 4, 1):
for c in range(1, 3, 1):
image = np.zeros((1024, 768)) + z * y * plane * c
tifffile.imwrite(
folder / f"test_z{z}_y{y}_plane{plane}_c{c}.tiff", image
)


class Test(TestCase):

Check failure on line 67 in tests/test_lsm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D101)

tests/test_lsm.py:67:7: D101 Missing docstring in public class
def test_convert(self):

Check failure on line 68 in tests/test_lsm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ANN201)

tests/test_lsm.py:68:9: ANN201 Missing return type annotation for public function `test_convert`
with TemporaryDirectory() as tmp_dir:
_write_test_data(tmp_dir)
output_zarr = os.path.join(tmp_dir, "output.zarr")
mosaic.convert(tmp_dir, output_zarr)
self.assertTrue(
_compare_zarr_archives(output_zarr, "tests/data/lsm.zarr.zip")
)

0 comments on commit 1b34faa

Please sign in to comment.