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(
img: Path,
czi: CziFile,
chunk_by_dims: List[str] = [
Dimensions.SpatialZ,
Dimensions.SpatialY,
Dimensions.SpatialX,
],
S: int = 0,
) -> Tuple[da.core.Array, str]:
"""
Read a CZI image file as a delayed dask array where certain dimensions act as
the chunk size.
Parameters
----------
img: Path
The filepath to read.
czi: CziFile
The loaded CziFile object created from reading the filepath.
chunk_by_dims: List[str]
The dimensions to use as the for mapping the chunks / blocks.
Default: [Dimensions.SpatialZ, Dimensions.SpatialY, Dimensions.SpatialX]
def size_x(self) -> int:
"""
Returns
-------
size: int
The size of the Spatial X dimension.
"""
return self.size(Dimensions.SpatialX)[0]
def size_x(self) -> int:
return self._size_of_dimension(Dimensions.SpatialX)
# Read the image data as a bytearray
byte_array = image.read(read_lengths[s_index])
# Convert the bytearray to a the type pixel_type
typed_array = np.frombuffer(
byte_array, dtype=pixel_type
).reshape(x_size, y_size)
# LIF stores YX planes so transpose them to get YX
typed_array = typed_array.transpose()
# Append the YX plane to the image stack.
img_stack.append(typed_array)
shape = [len(selected_ranges[dim[0]]) for dim in ranged_dims]
shape.append(y_size)
shape.append(x_size)
ranged_dims.append((Dimensions.SpatialY, y_size))
ranged_dims.append((Dimensions.SpatialX, x_size))
return (
np.array(img_stack).reshape(*shape),
ranged_dims,
) # in some subset of STCZYX order
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
# 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
sample, sample_dims = LifReader._get_array_from_offset(
im_path=img,
offsets=offsets,
read_lengths=read_lengths,
meta=lif.xml_root,
read_dims=first_chunk_read_dims,
# 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
sample, sample_dims = czi.read_image(**first_chunk_read_dims)
# Get the shape for the chunk and operating shape for the dask array
# We also collect the chunk and non chunk dimension ordering so that we can
# swap the dimensions after we
# block the dask array together.
def size_x(self) -> int:
return self._size_of_dimension(Dimensions.SpatialX)
def size_x(self) -> int:
"""
Returns
-------
size: int
The size of the Spatial X dimension.
"""
return self.size(Dimensions.SpatialX)[0]