Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for 2D CLI #128

Merged
merged 16 commits into from
Feb 14, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tmp*/
*.n5
*.h5
.idea/
test/test-folder/
4 changes: 2 additions & 2 deletions mobie/image_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def add_bdv_image(xml_path, root, dataset_name,
def add_image(input_path, input_key,
root, dataset_name, image_name,
resolution, scale_factors, chunks,
file_format="bdv.n5", menu_name=None,
file_format="ome.zarr", menu_name=None,
tmp_folder=None, target="local",
max_jobs=multiprocessing.cpu_count(),
view=None, transformation=None,
Expand Down Expand Up @@ -234,7 +234,7 @@ def add_image(input_path, input_key,

if move_only:
if int_to_uint:
raise ValueError("Conversio of integer to unsigned integer is not possible with move_only")
raise ValueError("Conversion of integer to unsigned integer is not possible with move_only")
shutil.move(input_path, data_path)
if "bdv." in file_format:
shutil.move(os.path.splitext(input_path)[0]+".xml", image_metadata_path)
Expand Down
64 changes: 55 additions & 9 deletions test/test_image_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def make_hdf5_data(self, path, key, shape, func=None):
f.create_dataset(key, data=data)

def init_h5_dataset(
self, dataset_name, raw_name, shape, file_format="bdv.n5", func=None, int_to_uint=False
self, dataset_name, raw_name, shape, file_format="ome.zarr", func=None, int_to_uint=False
):

data_path = os.path.join(self.test_folder, "data.h5")
Expand Down Expand Up @@ -169,11 +169,11 @@ def test_bdv_hdf5(self):
shape = (64, 64, 64)
self.init_h5_dataset(dataset_name, raw_name, shape, file_format="bdv.hdf5")

def test_ome_zarr(self):
def test_n5(self):
dataset_name = "test"
raw_name = "test-raw"
shape = (64, 64, 64)
self.init_h5_dataset(dataset_name, raw_name, shape, file_format="ome.zarr")
self.init_h5_dataset(dataset_name, raw_name, shape, file_format="bdv.n5")

#
# tests with existing dataset
Expand Down Expand Up @@ -233,6 +233,44 @@ def test_cli(self):
dataset_folder = os.path.join(self.root, self.dataset_name)
self.check_data(dataset_folder, im_name)

# 2D
@unittest.skipIf(platform == "win32", "CLI does not work on windows")
def test_cli_2D(self):

shape = (1, 512, 512)

im_folder = os.path.join(self.test_folder, "im-stack")
self.make_tif_data(im_folder, shape)

dataset_name = "test"
im_name = "test-cli-2D"

resolution = json.dumps([1., 1.])
scales = json.dumps([[2, 2], [2, 2]])
chunks = json.dumps([64, 64])

tmp_folder = os.path.join(self.test_folder, "cli-im2D")

in_path = os.path.join(im_folder, "z_000.tif")

cmd = ["mobie.add_image",
"--input_path", in_path,
"--input_key", "",
"--root", self.root,
"--dataset_name", self.dataset_name,
"--name", im_name,
"--resolution", resolution,
"--scale_factors", scales,
"--chunks", chunks,
"--tmp_folder", tmp_folder]
subprocess.run(cmd)

exp_data = imageio.imread(in_path)


dataset_folder = os.path.join(self.root, dataset_name)
self.check_data(dataset_folder, im_name, exp_data=exp_data)

#
# test with numpy data
#
Expand Down Expand Up @@ -272,7 +310,7 @@ def _test_with_trafo(self, file_format, transformation):
chunks=(64, 64, 64), tmp_folder=self.tmp_folder,
target="local", max_jobs=self.max_jobs,
transformation=transformation, file_format=file_format)
self.check_data(os.path.join(self.root, self.dataset_name), im_name)
self.check_data(os.path.join(self.root, self.dataset_name), im_name, file_format=file_format)

# TODO implement the test once ome.zarr v0.5 is released
def test_with_trafo_ome_zarr(self):
Expand Down Expand Up @@ -318,7 +356,7 @@ def test_skip_metadata(self):
# data validation
#

def check_dataset(self, dataset_folder, exp_shape, raw_name, file_format="bdv.n5"):
def check_dataset(self, dataset_folder, exp_shape, raw_name, file_format="ome.zarr"):
# validate the full project
mobie.validation.validate_project(
self.root, assert_true=self.assertTrue, assert_in=self.assertIn, assert_equal=self.assertEqual
Expand All @@ -342,8 +380,9 @@ def check_dataset(self, dataset_folder, exp_shape, raw_name, file_format="bdv.n5
self.assertEqual(shape, exp_shape)
self.assertFalse(np.allclose(data, 0.))

def check_data(self, dataset_folder, name):
exp_data = self.data
def check_data(self, dataset_folder, name, exp_data=None, file_format="ome.zarr"):
if exp_data is None:
exp_data = self.data

# check the image metadata
metadata = mobie.metadata.read_dataset_metadata(dataset_folder)
Expand All @@ -352,9 +391,16 @@ def check_data(self, dataset_folder, name):
mobie.validation.validate_source_metadata(name, sources[name], dataset_folder)

# check the image data
im_path = os.path.join(dataset_folder, "images", "bdv-n5", f"{name}.n5")

if file_format == "bdv.n5":
im_path = os.path.join(dataset_folder, "images", "bdv-n5", f"{name}.n5")
key = get_key(False, 0, 0, 0)
else:
im_path = os.path.join(dataset_folder, "images", "ome-zarr", f"{name}.ome.zarr")
key = "s0"

self.assertTrue(os.path.exists(im_path))
key = get_key(False, 0, 0, 0)

with open_file(im_path, "r") as f:
data = f[key][:]
self.assertTrue(np.array_equal(data, exp_data))
Expand Down
3 changes: 2 additions & 1 deletion test/test_spots.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def setUp(self):
chunks = (8, 32, 32)
mobie.add_image(data_path, data_key, self.root, self.dataset_name, self.image_source_name,
resolution=self.resolution, scale_factors=scale_factors, chunks=chunks,
unit="nanometer", tmp_folder=os.path.join(self.test_folder, "tmp_image"))
unit="nanometer", tmp_folder=os.path.join(self.test_folder, "tmp_image"),
file_format="bdv.n5")

gene_names = ["aaa", "bbb", "ccc", "xyz", "123", "456"]
table = {
Expand Down
2 changes: 1 addition & 1 deletion test/test_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def init_dataset(self):
scales = [[2, 2, 2]]
mobie.add_image(data_path, data_key, self.root, self.dataset_name, raw_name,
resolution=(1, 1, 1), chunks=(64, 64, 64), scale_factors=scales,
tmp_folder=tmp_folder)
tmp_folder=tmp_folder, file_format="bdv.n5")

def generate_trace(self, trace_id):
path = os.path.join(self.trace_folder, f"trace_{trace_id}.swc")
Expand Down
2 changes: 1 addition & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def init_dataset(self):
scales = [[2, 2, 2]]
add_image(data_path, data_key, self.root, self.dataset_name, raw_name,
resolution=(1, 1, 1), chunks=(32,)*3, scale_factors=scales,
tmp_folder=tmp_folder)
tmp_folder=tmp_folder, file_format="bdv.n5")

def setUp(self):
os.makedirs(self.test_folder, exist_ok=True)
Expand Down