publicstaticvoidpathInfo(Path p){try{System.out.println(String.format("%s is %d bytes", p, Files.size(p)));}catch(IOException e ){}}publicstaticvoidprintBlocks(String path)throwsIOException{try(Stream<Path> stream = Files.walk(Paths.get(path))){ stream.filter(Files::isRegularFile).filter( p -> p.getFileName().toString().matches("[0-9]")).forEach( x ->{pathInfo(x);});}}
In [4]:
Code
// N5Factory can make N5Readers and N5Writersvar factory =newN5Factory();// trying to open a reader for a container that does not yet exist will throw an error // var n5Reader = factory.openReader("my-container.n5");// creating a writer creates a container at the given location// if it does not already existvar n5Writer = factory.openWriter("my-container.n5");// now we can make a readervar n5Reader = factory.openReader("my-container.n5");// test if the container existsn5Reader.exists("");// true// "" and "/" both refer to the root of the containern5Reader.exists("/");// true
In [5]:
Code
factory.openWriter("my-container.h5").getClass();// HDF5 Format N5Writerfactory.openWriter("my-container.n5").getClass();// N5 Format N5Writerfactory.openWriter("my-container.zarr").getClass();// Zarr Format N5Writer
publicstatic RandomAccessibleInterval<FloatType>demoImage(long... size){final RandomAccessibleInterval<FloatType> img = ArrayImgs.floats(size);float f =0f;Cursor<FloatType> c = Views.flatIterable(img).cursor();while(c.hasNext()) c.next().set(f++);return img;}
In [10]:
Code
// the parametersvar img =demoImage(64,64);// the image to write- size 64 x 64var groupPath ="data";var blockSize =newint[]{32,32};var compression =newGzipCompression();// save the imageN5Utils.save(img, n5Writer, groupPath, blockSize, compression);
In [11]:
Code
var exec =Executors.newFixedThreadPool(4);// with 4 parallel threadsN5Utils.save(img, n5Writer, groupPath, blockSize, compression, exec);
In [12]:
Code
printBlocks("my-container.n5/data");
my-container.n5/data/1/1 is 1762 bytes
my-container.n5/data/1/0 is 2012 bytes
my-container.n5/data/0/1 is 1763 bytes
my-container.n5/data/0/0 is 2020 bytes
In [13]:
Code
// remove the old datan5Writer.remove(groupPath);// rewrite with a different block sizevar blockSize =newint[]{64,8};N5Utils.save(img, n5Writer, groupPath, blockSize, compression);// how many blocks are there?printBlocks("my-container.n5/data");
my-container.n5/data/0/1 is 837 bytes
my-container.n5/data/0/7 is 847 bytes
my-container.n5/data/0/3 is 839 bytes
my-container.n5/data/0/6 is 844 bytes
my-container.n5/data/0/0 is 968 bytes
my-container.n5/data/0/4 is 846 bytes
my-container.n5/data/0/2 is 840 bytes
my-container.n5/data/0/5 is 847 bytes
In [14]:
Code
// rewrite without compressionvar groupPath ="dataNoCompression";var blockSize =newint[]{32,32};var compression =newRawCompression();N5Utils.save(img, n5Writer, groupPath, blockSize, compression);// what size are the blocks?
In [15]:
Code
printBlocks("my-container.n5/dataNoCompression");
my-container.n5/dataNoCompression/1/1 is 4108 bytes
my-container.n5/dataNoCompression/1/0 is 4108 bytes
my-container.n5/dataNoCompression/0/1 is 4108 bytes
my-container.n5/dataNoCompression/0/0 is 4108 bytes
In [16]:
Code
var loadedImg = N5Utils.open(n5Writer, groupPath);Util.getTypeFromInterval(loadedImg).getClass();// FloatTypeArrays.toString(loadedImg.dimensionsAsLongArray());// [64, 64]
In [17]:
Code
// overwrite our previous datavar img = ArrayImgs.unsignedBytes(2,2);N5Utils.save(img, n5Writer, groupPath, blockSize, compression);// load the new data, the old data are no longer accessiblevar loadedImg = N5Utils.open(n5Writer, groupPath);Arrays.toString(loadedImg.dimensionsAsLongArray());// [2, 2]
In [18]:
Code
// create a group inside the container (think: "folder")var groupName ="put-data-in-me";n5Writer.createGroup(groupName);// attributes have names and values// make an attribute called "date" with a String valuevar attributeName ="date";n5Writer.setAttribute(groupName, attributeName,"2024-Jan-01");// Ask the N5 API to make a double array from the data attribute// it will try and fail, so an exception will be throwntry{var nothing = n5Writer.getAttribute(groupName, attributeName,double[].class);}catch( N5Exception e ){System.out.println("Error: could not get attribute as double[]");}// get the value of the "date" attribute as a StringString date = n5Writer.getAttribute(groupName, attributeName,String.class);date
Error: could not get attribute as double[]
2024-Jan-01
In [19]:
Code
n5Writer.setAttribute(groupName,"a",42);var num = n5Writer.getAttribute(groupName,"a",double.class);// 42.0var str = n5Writer.getAttribute(groupName,"a",String.class);// "42"
var metadata =newFunWithMetadata("Dorothy",2,newdouble[]{2.72,3.14});n5Writer.setAttribute(groupName,"metadata", metadata);// get attribute as an instance of FunWithMetdatan5Writer.getAttribute(groupName,"metadata", FunWithMetadata.class);
FunWithMetadata{Dorothy(2): [2.72, 3.14]}
In [22]:
Code
// get attribute as an instance of JsonElementn5Writer.getAttribute(groupName,"/", JsonElement.class);
// set attributesn5Writer.setAttribute(groupName,"sender","Alice");n5Writer.setAttribute(groupName,"receiver","Bob");// notice that they're setn5Writer.getAttribute(groupName,"sender",String.class);// Alicen5Writer.getAttribute(groupName,"receiver",String.class);// Bob// remove "sender"n5Writer.removeAttribute(groupName,"sender");// remove "receiver" and store result in a variablevar receiver = n5Writer.removeAttribute(groupName,"receiver",String.class);// Bobn5Writer.getAttribute(groupName,"sender",String.class);// nulln5Writer.getAttribute(groupName,"receiver",String.class);// null
var arrayMetadata = n5Writer.getDatasetAttributes("data");arrayMetadata.getDimensions();arrayMetadata.getBlockSize();arrayMetadata.getDataType();arrayMetadata.getCompression();