Skip to content

Commit

Permalink
Make image reader stateless to fix cubing (#460)
Browse files Browse the repository at this point in the history
* test cubing with start_z and padding

* make image_reader stateless to fix cubing

* fix lint

* update changelog

* make methods static

* unify the static method calls
  • Loading branch information
rschwanhold authored Oct 21, 2021
1 parent 993c6e3 commit 26da565
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 209 deletions.
1 change: 1 addition & 0 deletions wkcuber/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ For upgrade instructions, please check the respective *Breaking Changes* section
### Added
### Changed
### Fixed
- Fixed two bugs in `cubing` (regarding `start_z` and `pad`). As a result, the ImageConverters do no longer cache metadata. [#460](https://github.com/scalableminds/webknossos-libs/pull/460)

## [v0.8.18](https://github.com/scalableminds/webknossos-cuber/releases/tag/v0.8.18) - 2021-10-18
[Commits](https://github.com/scalableminds/webknossos-libs/compare/v0.8.16...v0.8.18)
Expand Down
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions wkcuber/tests/scripts/tiff_cubing_with_padding.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set -xe
mkdir -p testoutput/tiff_pad
python -m wkcuber.cubing \
--jobs 2 \
--batch_size 8 \
--layer_name color \
--scale 1,1,1 \
--start_z 4 \
--pad \
testdata/tiff_with_different_dimensions testoutput/tiff_pad
[ -d testoutput/tiff_pad/color ]
[ -d testoutput/tiff_pad/color/1 ]
[ -e testoutput/tiff_pad/datasource-properties.json ]
28 changes: 15 additions & 13 deletions wkcuber/wkcuber/cubing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
setup_logging,
add_scale_flag,
)
from .image_readers import image_reader, refresh_global_image_reader
from .image_readers import image_reader

BLOCK_LEN = 32

Expand Down Expand Up @@ -181,7 +181,6 @@ def cubing_job(
InterpolationModes,
List[str],
int,
Tuple[int, int],
bool,
Optional[int],
Optional[int],
Expand All @@ -193,7 +192,6 @@ def cubing_job(
interpolation_mode,
source_file_batches,
batch_size,
image_size,
pad,
channel_index,
sample_index,
Expand Down Expand Up @@ -229,15 +227,15 @@ def cubing_job(

if not pad:
assert (
image.shape[0:2] == image_size
image.shape[0:2] == target_view.size[0:2]
), "Section z={} has the wrong dimensions: {} (expected {}). Consider using --pad.".format(
z, image.shape, image_size
z, image.shape, target_view.size[0:2]
)
slices.append(image)

if pad:
x_max = max(_slice.shape[0] for _slice in slices)
y_max = max(_slice.shape[1] for _slice in slices)
x_max = target_view.size[0]
y_max = target_view.size[1]

slices = [
np.pad(
Expand Down Expand Up @@ -313,11 +311,16 @@ def cubing(
executor_args: Namespace,
) -> Layer:
source_files = find_source_filenames(source_path)
# we need to refresh the image readers because they are no longer stateless for performance reasons
refresh_global_image_reader()

# All images are assumed to have equal dimensions
num_x, num_y = image_reader.read_dimensions(source_files[0])
all_num_x, all_num_y = zip(
*[
image_reader.read_dimensions(source_files[i])
for i in range(len(source_files))
]
)
num_x = max(all_num_x)
num_y = max(all_num_y)
# All images are assumed to have equal channels and samples
num_channels = image_reader.read_channel_count(source_files[0])
num_samples = image_reader.read_sample_count(source_files[0])
num_output_channels = num_channels * num_samples
Expand Down Expand Up @@ -415,13 +418,12 @@ def cubing(
(
target_mag_view.get_view(
(0, 0, z + start_z),
(num_x, num_y, start_z + max_z - z),
(num_x, num_y, max_z - z),
),
target_mag,
interpolation_mode,
source_files_array,
batch_size,
(num_x, num_y),
pad,
channel_index,
sample_index,
Expand Down
Loading

0 comments on commit 26da565

Please sign in to comment.