Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions src/stack_to_chunk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _create_zarr_group(self, array_spec: ArraySpec) -> None:

@property
def _full_res_array(self) -> zarr.Array:
if "0" not in self._group:
if 0 not in self.levels:
msg = (
"Full resolution dataset not present. "
"Run `create_initial_datset()` first."
Expand All @@ -229,7 +229,7 @@ def levels(self) -> list[int]:
Level 0 corresponds to full resolution data, and level ``i`` to
data downsampled by a factor of ``2**i``.
"""
return sorted(int(k) for k in self._group)
return sorted(int(k) for k in self._group.array_keys())

@property
def chunk_size_z(self) -> int:
Expand Down Expand Up @@ -271,12 +271,6 @@ def add_full_res_data(
z-index at which this stack of input data starts. Can be useful to write
multiple slabs in parallel using a compute cluster where the job wants
to be split into many small individual Python processes.

Notes
-----
Make sure create_initial_dataset has been run first to set up the
zarr dataset.

"""
assert data.ndim == 3, "Input array is not 3-dimensional"
if start_z_idx % self.chunk_size_z != 0:
Expand Down Expand Up @@ -365,18 +359,17 @@ def add_downsample_level(
msg = "level must be an integer >= 1"
raise ValueError(msg)

level_str = str(int(level))
if level_str in self._group:
msg = f"Level {level_str} already found in zarr group"
if level in self.levels:
msg = f"Level {level} already found in zarr group"
raise RuntimeError(msg)

if (level_minus_one := str(int(level) - 1)) not in self._group:
msg = f"Level below (level={level_minus_one}) not present in group."
if level - 1 not in self.levels:
msg = f"Level below (level={level - 1}) not present in group."
raise RuntimeError(
msg,
)

source_arr: zarr.Array = self._group[level_minus_one]
source_arr: zarr.Array = self._group[str(level - 1)]
source_chunk_shape = source_arr.chunks

new_shape = tuple(math.ceil(i / 2) for i in source_arr.shape)
Expand All @@ -389,7 +382,7 @@ def add_downsample_level(
)

sink_arr = self._group.create_array(
name=level_str,
name=str(level),
shape=new_shape,
shards=new_shard_shape,
chunks=source_arr.chunks,
Expand All @@ -413,15 +406,15 @@ def add_downsample_level(
]
] = [
(
self._path / str(level_minus_one),
self._path / level_str,
self._path / str(level - 1),
self._path / str(level),
idxs,
downsample_func,
)
for idxs in block_indices
]

logger.info(f"Starting downsampling from level {level_minus_one} > {level}...")
logger.info(f"Starting downsampling from level {level - 1} > {level}...")
blosc_use_threads = blosc.use_threads
blosc.use_threads = 0

Expand All @@ -431,7 +424,7 @@ def add_downsample_level(

self._add_level_metadata(level)
blosc.use_threads = blosc_use_threads
logger.info(f"Finished downsampling from level {level_minus_one} > {level}")
logger.info(f"Finished downsampling from level {level - 1} > {level}")

def _add_level_metadata(self, level: int = 0) -> None:
"""
Expand Down