Skip to content

Commit

Permalink
Add edge case to test and fix ValueErrors for mixed type comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
annehaley committed Oct 10, 2024
1 parent c85ebf0 commit 1009eb1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 16 additions & 4 deletions sources/zarr/large_image_source_zarr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,22 @@ def getMetadata(self):
all_frame_values = self.frameValues[..., i]
split = np.split(all_frame_values, all_frame_values.shape[i], axis=i)
values = [a.flat[0] for a in split]
uniform = all(len(np.unique(a)) == 1 for a in split)
uniform = all(len(np.unique(
# convert all values to strings for mixed type comparison with np.unique
(a[np.not_equal(a, None)]).astype(str),
)) == 1 for a in split)
try:
min_val = min(values)
max_val = max(values)
except TypeError:
min_val = None
max_val = None
result['Value' + axis.upper()] = dict(
values=values,
uniform=uniform,
units=self.frameUnits.get(axis) if self.frameUnits is not None else None,
min=min(values),
max=max(values),
min=min_val,
max=max_val,
datatype=np.array(values).dtype.name,
)
for idx in range(self._framecount):
Expand Down Expand Up @@ -810,7 +819,10 @@ def _getAxisInternalMetadata(self, axis_name):
all_frame_values.shape[axis_index],
axis=axis_index,
)
uniform = all(len(np.unique(a)) == 1 for a in split)
uniform = all(len(np.unique(
# convert all values to strings for mixed type comparison with np.unique
(a[np.not_equal(a, None)]).astype(str),
)) == 1 for a in split)
if uniform:
values = [a.flat[0] for a in split]
else:
Expand Down
9 changes: 9 additions & 0 deletions test/test_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,12 @@ def testFrameValuesEdgeCases(tmp_path):
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=1, z=2, z_value=6.4, c_value='CD4')
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=1, z=1, z_value=3.1, c_value='CD4')
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=0, z=1, z_value=1.1)

ts = large_image.new()
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=0, z=0, z_value=1, c_value='DAPI')
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=0, z=1, z_value=3.2, c_value='DAPI')
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=0, z=2, z_value=6.3, c_value='DAPI')
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=1, z=2, z_value=6.4, c_value='CD4')
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=1, z=1, z_value=3.1, c_value='CD4')
ts.addTile(np.zeros((100, 100, 3)), x=0, y=0, c=0, z=1, z_value=1.1)
assert len(ts.metadata.get('frames', [])) == 6

0 comments on commit 1009eb1

Please sign in to comment.