-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle Zarr data that is downsampled in Z #12
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
|
@@ -75,6 +76,12 @@ public class ZarrPixelBuffer implements PixelBuffer { | |
/** Zarr array corresponding to the current resolution level */ | ||
private ZarrArray array; | ||
|
||
/** | ||
* Mapping of Z plane indexes in full resolution to | ||
* Z plane indexes in current resolution. | ||
*/ | ||
private Map<Integer, Integer> zIndexMap; | ||
|
||
/** { resolutionLevel, z, c, t, x, y, w, h } vs. tile byte array cache */ | ||
private final AsyncLoadingCache<List<Integer>, byte[]> tileCache; | ||
|
||
|
@@ -191,49 +198,64 @@ private void read(byte[] buffer, int[] shape, int[] offset) | |
// Check planar read size (sizeX and sizeY only) | ||
checkReadSize(Arrays.copyOfRange(shape, 3, 5)); | ||
|
||
// if reading from a resolution downsampled in Z, | ||
// adjust the shape/offset for the Z coordinate only | ||
// this ensures that the correct Zs are read from the correct offsets | ||
// since the requested shape/offset may not match the underlying array | ||
int planes = 1; | ||
int originalZIndex = offset[2]; | ||
if (getSizeZ() != getTrueSizeZ()) { | ||
offset[2] = zIndexMap.get(originalZIndex); | ||
planes = shape[2]; | ||
shape[2] = 1; | ||
} | ||
|
||
try { | ||
ByteBuffer asByteBuffer = ByteBuffer.wrap(buffer); | ||
DataType dataType = array.getDataType(); | ||
switch (dataType) { | ||
case u1: | ||
case i1: | ||
array.read(buffer, shape, offset); | ||
break; | ||
case u2: | ||
case i2: | ||
{ | ||
short[] data = (short[]) array.read(shape, offset); | ||
asByteBuffer.asShortBuffer().put(data); | ||
break; | ||
} | ||
case u4: | ||
case i4: | ||
{ | ||
int[] data = (int[]) array.read(shape, offset); | ||
asByteBuffer.asIntBuffer().put(data); | ||
break; | ||
} | ||
case i8: | ||
{ | ||
long[] data = (long[]) array.read(shape, offset); | ||
asByteBuffer.asLongBuffer().put(data); | ||
break; | ||
} | ||
case f4: | ||
{ | ||
float[] data = (float[]) array.read(shape, offset); | ||
asByteBuffer.asFloatBuffer().put(data); | ||
break; | ||
} | ||
case f8: | ||
{ | ||
double[] data = (double[]) array.read(shape, offset); | ||
asByteBuffer.asDoubleBuffer().put(data); | ||
break; | ||
} | ||
default: | ||
throw new IllegalArgumentException( | ||
"Data type " + dataType + " not supported"); | ||
for (int z=0; z<planes; z++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the loop over |
||
offset[2] = zIndexMap.get(originalZIndex + z); | ||
switch (dataType) { | ||
case u1: | ||
case i1: | ||
array.read(buffer, shape, offset); | ||
break; | ||
case u2: | ||
case i2: | ||
{ | ||
short[] data = (short[]) array.read(shape, offset); | ||
asByteBuffer.asShortBuffer().put(data); | ||
break; | ||
} | ||
case u4: | ||
case i4: | ||
{ | ||
int[] data = (int[]) array.read(shape, offset); | ||
asByteBuffer.asIntBuffer().put(data); | ||
break; | ||
} | ||
case i8: | ||
{ | ||
long[] data = (long[]) array.read(shape, offset); | ||
asByteBuffer.asLongBuffer().put(data); | ||
break; | ||
} | ||
case f4: | ||
{ | ||
float[] data = (float[]) array.read(shape, offset); | ||
asByteBuffer.asFloatBuffer().put(data); | ||
break; | ||
} | ||
case f8: | ||
{ | ||
double[] data = (double[]) array.read(shape, offset); | ||
asByteBuffer.asDoubleBuffer().put(data); | ||
break; | ||
} | ||
default: | ||
throw new IllegalArgumentException( | ||
"Data type " + dataType + " not supported"); | ||
} | ||
} | ||
} catch (InvalidRangeException e) { | ||
log.error("Error reading Zarr data", e); | ||
|
@@ -745,6 +767,14 @@ public int getSizeY() { | |
|
||
@Override | ||
public int getSizeZ() { | ||
// this is expected to be the Z size of the full resolution array | ||
return zIndexMap.size(); | ||
} | ||
|
||
/** | ||
* @return Z size of the current underlying Zarr array | ||
*/ | ||
private int getTrueSizeZ() { | ||
return array.getShape()[2]; | ||
} | ||
|
||
|
@@ -783,9 +813,27 @@ public void setResolutionLevel(int resolutionLevel) { | |
throw new IllegalArgumentException( | ||
"This Zarr file has no pixel data"); | ||
} | ||
if (zIndexMap == null) { | ||
zIndexMap = new HashMap<Integer, Integer>(); | ||
} | ||
else { | ||
zIndexMap.clear(); | ||
} | ||
try { | ||
array = zarrArrayCache.get( | ||
root.resolve(Integer.toString(this.resolutionLevel))).get(); | ||
|
||
ZarrArray fullResolutionArray = zarrArrayCache.get( | ||
root.resolve("0")).get(); | ||
|
||
// map each Z index in the full resolution array | ||
// to a Z index in the subresolution array | ||
// if no Z downsampling, this is just an identity map | ||
int fullResZ = fullResolutionArray.getShape()[2]; | ||
int arrayZ = array.getShape()[2]; | ||
for (int z=0; z<fullResZ; z++) { | ||
zIndexMap.put(z, Math.round(z * arrayZ / fullResZ)); | ||
} | ||
} catch (Exception e) { | ||
// FIXME: Throw the right exception | ||
throw new RuntimeException(e); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went back and forth on whether this should become a
Map<Integer, Map<Integer, Integer>>
and store all the mappings for all the resolutions once rather than invalidating them and recomputing every timesetResolutionLevel
is called.At least in the context of the image-region endpoints invoked by a viewer like PathViewer, my understanding is that each resolution call will initialize its own
ZarrPixelBuffer
in a separate thread. So the value of such computation would be limited.