Skip to content

Instantly share code, notes, and snippets.

@igorpisarev
Created May 6, 2019 16:02
Show Gist options
  • Save igorpisarev/591212a26228e4ac7cb482b0134217a1 to your computer and use it in GitHub Desktop.
Save igorpisarev/591212a26228e4ac7cb482b0134217a1 to your computer and use it in GitHub Desktop.
CellRandomAccess unexpected behavior
import java.io.IOException;
import java.util.Arrays;
import org.janelia.saalfeldlab.n5.GzipCompression;
import org.janelia.saalfeldlab.n5.N5FSWriter;
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.imglib2.N5Utils;
import net.imglib2.Cursor;
import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.basictypeaccess.array.ShortArray;
import net.imglib2.img.cell.CellGrid;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.integer.ShortType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;
public class N5CellAccessTest {
static private final String testDirPath = System.getProperty("user.home") + "/tmp/n5-imglib2-test";
static private final String dataset = "test-cell-access";
static private final long[] dimensions = {8, 8, 8};
static private final int[] blockSize = {2, 2, 2};
public static <T extends NativeType<T> & IntegerType<T>> void main(final String[] args) throws IOException {
// fill image data
final short[] data = new short[(int) Intervals.numElements(dimensions)];
for (int i = 0; i < data.length; ++i)
data[i] = (short) (i + 1);
final ArrayImg<ShortType, ShortArray> arrayImg = ArrayImgs.shorts(data, dimensions);
// save the image as n5
final N5Writer n5 = new N5FSWriter(testDirPath);
N5Utils.save(arrayImg, n5, dataset, blockSize, new GzipCompression());
// create an interval for cell at grid position [2,2,2]
final CellGrid cellGrid = new CellGrid(dimensions, blockSize);
final long[] cellMin = new long[3], cellMax = new long[3];
final int[] cellDims = new int[3];
cellGrid.getCellDimensions(new long[] {2, 2, 2}, cellMin, cellDims);
Arrays.setAll(cellMax, d -> cellMin[d] + cellDims[d] - 1);
final Interval cellInterval = new FinalInterval(cellMin, cellMax);
// open the image and iterate inside the cell [2,2,2]
final RandomAccessibleInterval<T> img = N5Utils.open(n5, dataset);
final RandomAccessibleInterval< T > crop = Views.interval(img, cellInterval);
final Cursor<T> cursor = Views.flatIterable(crop).cursor();
while (cursor.hasNext())
System.out.println(cursor.next().getInteger());
// The above loop is expected to only load cell [2,2,2].
// However, if I add code to log accessed cells in N5CellLoader, it shows that these cells have been accessed:
// [0,0,0], [2,2,2], [1,2,2], [2,3,2]
n5.remove();
}
}
@igorpisarev
Copy link
Author

To see positions of accessed cells you would need to modify N5CellLoader::load() and trace the requested grid position there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment