Skip to content

Commit

Permalink
Merge pull request #124 from mehta-lab/tests/labeled_positions
Browse files Browse the repository at this point in the history
Adds test for reading data with unordered position labeles
  • Loading branch information
henrypinkard committed Aug 4, 2023
2 parents bf48030 + eb80810 commit 41d212a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/ndtiff/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version_info = (2, 2, 0)
version_info = (2, 2, 1)
__version__ = ".".join(map(str, version_info))
2 changes: 1 addition & 1 deletion python/ndtiff/nd_tiff_current.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def close(self):

def _read_one_image(self, block_id, axes_to_stack=None, axes_to_slice=None, stitched=False, rgb=False):
# a function that reads in one chunk of data
axes = {key: block_id[i] for i, key in enumerate(axes_to_stack.keys())}
axes = {key: axes_to_stack[key][block_id[i]] for i, key in enumerate(axes_to_stack.keys())}
if stitched:
# Combine all rows and cols into one stitched image
# get spatial layout of position indices
Expand Down
72 changes: 72 additions & 0 deletions python/ndtiff/test/data_loading_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,77 @@ def test_v3_2_no_magellan_explore_no_channels(test_data_path):
dataset = Dataset(data_path)
assert(np.sum(np.array(dataset.as_array(stitched=True)[0])) > 0)

def test_labeled_positions(test_data_path):
"""
This dataset was acquired with the following events:
events = [
{'axes': {'position': 'Pos2'}, 'x': 0, 'y': 0},
{'axes': {'position': 'Pos0'}, 'x': 0, 'y': 1},
{'axes': {'position': 'Pos1'}, 'x': 0, 'y': 2}
]
Further, images at position ('Pos2', 'Pos0', 'Pos1') have (0, 0) pixels
equal to (0, 1, 2).
Here we test that the axis and image order of the dataset is as expected.
"""

data_path = os.path.join(test_data_path, 'v3', 'labeled_positions_1')
position_names = ('Pos2', 'Pos0', 'Pos1')

dataset = Dataset(data_path)
data = np.asarray(dataset.as_array())

# ndtiff sorts the dataset axes. Check that the position axis is correctly
# sorted
assert dataset.axes['position'] == set(('Pos0', 'Pos1', 'Pos2'))

# Check that the image metadata includes the position name
for position_name in position_names:
metadata = dataset.read_metadata(position=position_name)
assert metadata['PositionName'] == position_name

# Check that data is in the ('Pos0', 'Pos1', 'Pos2') order
pixel_00 = (1, 2, 0)
for idx, image in enumerate(data):
assert image[0, 0] == pixel_00[idx]

def test_unordered_z_axis(test_data_path):
"""
This dataset was acquired with the following events:
events1 = [{'axes': {'z': z_idx}, 'z': z_idx} for z_idx in range(10)]
events2 = [{'axes': {'z': z_idx}, 'z': z_idx} for z_idx in range(-10, 0)]
and the following image process hook function:
def fun(image, metadata):
if not hasattr(fun, "idx"):
fun.idx = 0
image[0, 0] = np.uint16(fun.idx)
fun.idx += 1
return image, metadata
Here we test that the axis and image order of the dataset is as expected.
"""

data_path = os.path.join(test_data_path, 'v3', 'unordered_z_1')
acquisition_z_axis = list(range(10)) + list(range(-10, 0))
sorting_index = np.argsort(acquisition_z_axis)
sorted_z_axis = np.sort(acquisition_z_axis)

dataset = Dataset(data_path)
data = np.asarray(dataset.as_array())

# ndtiff sorts the dataset axes. Check that the position axis is correctly
# sorted
assert dataset.axes['z'] == set(sorted_z_axis)

# Check that data is in the sorted(acquisition_z_axis) order
pixel_00 = sorting_index
for idx, image in enumerate(data):
assert image[0, 0] == pixel_00[idx]
Binary file added test_data/v3/labeled_positions_1/NDTiff.index
Binary file not shown.
Binary file not shown.
Binary file added test_data/v3/unordered_z_1/NDTiff.index
Binary file not shown.
Binary file not shown.

0 comments on commit 41d212a

Please sign in to comment.