Skip to content

Commit

Permalink
Merge pull request #1505 from girder/fix-zarr-crop
Browse files Browse the repository at this point in the history
Fix cropping with the zarr sink
  • Loading branch information
manthey authored Apr 11, 2024
2 parents 101a1dd + 4f0262c commit 908be17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Bug Fixes
- Guard a race condition in vips new_from_file ([#1500](../../pull/1500))
- Fix cropping with the zarr sink ([#1505](../../pull/1505))

## 1.28.0

Expand Down
2 changes: 1 addition & 1 deletion sources/zarr/large_image_source_zarr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ def write(
source = self

if self.crop:
top, left, height, width = self.crop
left, top, width, height = self.crop
source = new()
source._zarr.attrs.update(self._zarr.attrs)
for frame in self.getMetadata().get('frames', [{'Index': 0}]):
Expand Down
27 changes: 26 additions & 1 deletion test/test_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def testCropAndDownsample(tmp_path):
assert current_arrays.get('0') is not None
assert current_arrays.get('0').shape == (num_frames, 1900, 1950, num_bands)

sink.crop = (100, 50, 1800, 1825)
sink.crop = (50, 100, 1825, 1800)
sink.write(output_file)
written = large_image_source_zarr.open(output_file)
written_arrays = dict(written._zarr.arrays())
Expand All @@ -308,3 +308,28 @@ def testCropAndDownsample(tmp_path):
assert written_arrays.get('1').shape == (num_frames, 900, 913, num_bands)
assert written_arrays.get('2') is not None
assert written_arrays.get('2').shape == (num_frames, 450, 456, num_bands)


def testCropToTiff(tmp_path):
output_file = tmp_path / 'cropped.tiff'
sink = large_image_source_zarr.new()

# add tiles with some overlap to multiple frames
num_frames = 1
num_bands = 3
for z in range(num_frames):
sink.addTile(np.random.random((1000, 1000, num_bands)), 0, 0, z=z)
sink.addTile(np.random.random((1000, 1000, num_bands)), 950, 0, z=z)
sink.addTile(np.random.random((1000, 1000, num_bands)), 0, 900, z=z)
sink.addTile(np.random.random((1000, 1000, num_bands)), 950, 900, z=z)

current_arrays = dict(sink._zarr.arrays())
assert len(current_arrays) == 1
assert current_arrays.get('0') is not None
assert current_arrays.get('0').shape == (num_frames, 1900, 1950, num_bands)

sink.crop = (100, 50, 1800, 1825)
sink.write(output_file)
source = large_image.open(output_file)
assert source.sizeX == 1800
assert source.sizeY == 1825

0 comments on commit 908be17

Please sign in to comment.