Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{'S': (0, 3), 'T': (0,7), 'X': (0, 475), 'Y': (0, 325), 'Z': (0, 4)}
].
The result for a similarly shaped file but with different number of time
points per scene would yield
[
{'S': (0, 1), 'T': (0,8), 'X': (0, 475), 'Y': (0, 325), 'Z': (0, 4)},
{'S': (1, 2), 'T': (0,6), 'X': (0, 475), 'Y': (0, 325), 'Z': (0, 4)},
{'S': (2, 3), 'T': (0,7), 'X': (0, 475), 'Y': (0, 325), 'Z': (0, 4)}
]
"""
shape_list = [
{
Dimensions.Time: (0, img.nt),
Dimensions.Channel: (0, img.channels),
Dimensions.SpatialZ: (0, img.nz),
Dimensions.SpatialY: (0, img.dims[1]),
Dimensions.SpatialX: (0, img.dims[0]),
}
for idx, img in enumerate(lif.get_iter_image())
]
consistent = all(elem == shape_list[0] for elem in shape_list)
if consistent:
shape_list[0][Dimensions.Scene] = (0, len(shape_list))
shape_list = [shape_list[0]]
else:
for idx, lst in enumerate(shape_list):
lst[Dimensions.Scene] = (idx, idx + 1)
return shape_list
def __init__(
self,
data: types.FileLike,
chunk_by_dims: List[str] = [
Dimensions.SpatialZ,
Dimensions.SpatialY,
Dimensions.SpatialX,
],
S: int = 0,
**kwargs,
):
# Run super init to check filepath provided
super().__init__(data, **kwargs)
# Store parameters needed for _daread
self.chunk_by_dims = chunk_by_dims
self.specific_s_index = S
lif = LifFile(filename=self._file)
# _chunk_offsets is a list of ndarrays
# (only way I could deal with inconsistent scene shape)
self._chunk_offsets, self._chunk_lengths = LifReader._compute_offsets(lif=lif)
def __init__(
self,
data: types.FileLike,
chunk_by_dims: List[str] = [
Dimensions.SpatialZ,
Dimensions.SpatialY,
Dimensions.SpatialX,
],
S: int = 0,
**kwargs,
):
# Run super init to check filepath provided
super().__init__(data, **kwargs)
# Store parameters needed for _daread
self.chunk_by_dims = chunk_by_dims
self.specific_s_index = S
def size_z(self) -> int:
return self._size_of_dimension(Dimensions.SpatialZ)
def _daread_safe(
img: Union[str, Path],
chunk_by_dims: List[str] = [
Dimensions.SpatialZ,
Dimensions.SpatialY,
Dimensions.SpatialX,
],
S: int = 0,
) -> Tuple[da.core.Array, str]:
"""
Safely read a CZI image file as a delayed dask array where certain dimensions
act as the chunk size.
Parameters
----------
img: Union[str, Path]
The filepath to read.
chunk_by_dims: List[str]
The dimensions to use as the for mapping the chunks / blocks.
Default: [Dimensions.SpatialZ, Dimensions.SpatialY, Dimensions.SpatialX]
# If S is in read_dims then use the specified value and the specified dims for
# that scene
if Dimensions.Scene in read_dims:
s_range = range(
read_dims[Dimensions.Scene], read_dims[Dimensions.Scene] + 1
)
s_dict = data_shape[s_range[0]]
else:
s_range = range(*data_shape[0][Dimensions.Scene])
s_dict = data_shape[0]
# Map the dims over to ranges and if the dim is in read_dims make the range
# over the single dim
integrated_dims = {Dimensions.Scene: s_range}
for dim in [Dimensions.Time, Dimensions.Channel, Dimensions.SpatialZ]:
if dim in read_dims:
integrated_dims[dim] = range(read_dims[dim], read_dims[dim] + 1)
else:
integrated_dims[dim] = range(*s_dict[dim])
return integrated_dims
title = f"napari: {self.reader._file.name}"
# Handle RGB entirely differently
if rgb:
# Swap channel to last dimension
new_dims = f"{dims.replace(Dimensions.Channel, '')}{Dimensions.Channel}"
data = transforms.transpose_to_dims(
data=data, given_dims=dims, return_dims=new_dims
)
# Run napari
with napari.gui_qt():
napari.view_image(
data,
is_pyramid=False,
ndisplay=3 if Dimensions.SpatialZ in dims else 2,
title=title,
axis_labels=dims.replace(Dimensions.Channel, ""),
rgb=rgb,
**kwargs,
)
# Handle all other images besides RGB not requested
else:
# Channel axis
c_axis = (
dims.index(Dimensions.Channel)
if Dimensions.Channel in dims
else None
)
# Set visible based on number of channels
def size_z(self) -> int:
"""
Returns
-------
size: int
The size of the Spatial Z dimension.
"""
return self.size(Dimensions.SpatialZ)[0]
else:
visible = True
# Drop channel from dims string
dims = (
dims.replace(Dimensions.Channel, "")
if Dimensions.Channel in dims
else dims
)
# Run napari
with napari.gui_qt():
napari.view_image(
data,
is_pyramid=False,
ndisplay=3 if Dimensions.SpatialZ in dims else 2,
channel_axis=c_axis,
axis_labels=dims,
title=title,
visible=visible,
**kwargs,
)
except ModuleNotFoundError:
raise ModuleNotFoundError(
f"'napari' has not been installed. To use this function install napari "
f"with either: pip install napari' or "
def size_z(self) -> int:
"""
Returns
-------
size: int
The size of the Spatial Z dimension.
"""
return self.size(Dimensions.SpatialZ)[0]