Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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]
Note: SpatialY and SpatialX will always be added to the list if not present.
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_y(self) -> int:
return self._size_of_dimension(Dimensions.SpatialY)
def _daread(
img: Path,
offsets: List[np.ndarray],
read_lengths: np.ndarray,
chunk_by_dims: List[str] = [
Dimensions.SpatialZ,
Dimensions.SpatialY,
Dimensions.SpatialX,
],
S: int = 0,
) -> Tuple[da.core.Array, str]:
"""
Read a LIF image file as a delayed dask array where certain dimensions act as
the chunk size.
Parameters
----------
img: Path
The filepath to read.
offsets: List[numpy.ndarray]
A List of numpy ndarrays offsets, see _compute_offsets for more details.
read_lengths: numpy.ndarray
A 1D numpy array of read lengths, the index is the scene index
def size_y(self) -> int:
return self._size_of_dimension(Dimensions.SpatialY)
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)
else:
# If the list is length one that means that all the scenes in the image
# have the same dimensions
# Just select the first dictionary in the list
image_dim_indices = image_dim_indices[0]
# Uppercase dimensions provided to chunk by dims
chunk_by_dims = [d.upper() for d in chunk_by_dims]
# Always add Y and X dims to chunk by dims because that is how CZI files work
if Dimensions.SpatialY not in chunk_by_dims:
log.info(
"Adding the Spatial Y dimension to chunk by dimensions as it was not "
"found."
)
chunk_by_dims.append(Dimensions.SpatialY)
if Dimensions.SpatialX not in chunk_by_dims:
log.info(
"Adding the Spatial X dimension to chunk by dimensions as it was not "
"found."
)
chunk_by_dims.append(Dimensions.SpatialX)
# Setup read dimensions for an example chunk
first_chunk_read_dims = {}
for dim, (dim_begin_index, dim_end_index) in image_dim_indices.items():
# Only add the dimension if the dimension isn't a part of the chunk
if dim not in chunk_by_dims:
# Add to read dims
first_chunk_read_dims[dim] = dim_begin_index
# Read first chunk for information used by dask.array.from_delayed
else:
# If the list is length one that means that all the scenes in the image
# have the same dimensions
# Just select the first dictionary in the list
image_dim_indices = image_dim_indices[0]
# Uppercase dimensions provided to chunk by dims
chunk_by_dims = [d.upper() for d in chunk_by_dims]
# Always add Y and X dims to chunk by dims because that is how LIF files work
if Dimensions.SpatialY not in chunk_by_dims:
log.info(
"Adding the Spatial Y dimension to chunk by dimensions as it was not "
"found."
)
chunk_by_dims.append(Dimensions.SpatialY)
if Dimensions.SpatialX not in chunk_by_dims:
log.info(
"Adding the Spatial X dimension to chunk by dimensions as it was not "
"found."
)
chunk_by_dims.append(Dimensions.SpatialX)
# Setup read dimensions for an example chunk
first_chunk_read_dims = {}
for dim, (dim_begin_index, dim_end_index) in image_dim_indices.items():
# Only add the dimension if the dimension isn't a part of the chunk
if dim not in chunk_by_dims:
# Add to read dims
first_chunk_read_dims[dim] = dim_begin_index
# Read first chunk for information used by dask.array.from_delayed
def size_y(self) -> int:
"""
Returns
-------
size: int
The size of the Spatial Y dimension.
"""
return self.size(Dimensions.SpatialY)[0]
def size_y(self) -> int:
"""
Returns
-------
size: int
The size of the Spatial Y dimension.
"""
return self.size(Dimensions.SpatialY)[0]