3838import org .junit .Rule ;
3939import org .junit .Test ;
4040import org .junit .rules .TemporaryFolder ;
41+ import com .glencoesoftware .omero .zarr .ZarrPixelBuffer .Axes ;
4142
4243import com .bc .zarr .ArrayParams ;
4344import com .bc .zarr .DimensionSeparator ;
@@ -186,14 +187,16 @@ static Path fake(Map<String, String> options,
186187 }
187188
188189 public Path writeTestZarr (
189- int sizeT ,
190- int sizeC ,
191- int sizeZ ,
192- int sizeY ,
193- int sizeX ,
194- String pixelType ,
195- int resolutions ) throws IOException {
196- Path input = fake (
190+ int sizeT ,
191+ int sizeC ,
192+ int sizeZ ,
193+ int sizeY ,
194+ int sizeX ,
195+ String pixelType ,
196+ int resolutions ,
197+ String order ) throws IOException {
198+
199+ Path input = fake (
197200 "sizeT" , Integer .toString (sizeT ),
198201 "sizeC" , Integer .toString (sizeC ),
199202 "sizeZ" , Integer .toString (sizeZ ),
@@ -202,99 +205,41 @@ public Path writeTestZarr(
202205 "pixelType" , pixelType ,
203206 "resolutions" , Integer .toString (resolutions ));
204207 Path output = tmpDir .getRoot ().toPath ().resolve ("output.zarr" );
205- assertBioFormats2Raw (input , output );
206- List <Object > msArray = new ArrayList <>();
207- Map <String , Object > msData = new HashMap <>();
208- Map <String , Object > msMetadata = new HashMap <>();
209- msMetadata .put ("method" , "loci.common.image.SimpleImageScaler" );
210- msMetadata .put ("version" , "Bio-Formats 6.5.1" );
211- msData .put ("metadata" , msMetadata );
212- msData .put ("datasets" , getDatasets (resolutions ));
213- msData .put ("version" , "0.1" );
214- msArray .add (msData );
215- ZarrGroup z = ZarrGroup .open (output .resolve ("0" ));
216- Map <String ,Object > attrs = new HashMap <String , Object >();
217- attrs .put ("multiscales" , msArray );
218- z .writeAttributes (attrs );
219- return output ;
220- }
221-
222- public Path writeTestZarrWithAxes (int sizeT ,
223- int sizeC ,
224- int sizeZ ,
225- int sizeY ,
226- int sizeX ,
227- String order ) throws IOException , InvalidRangeException {
228- Path output = tmpDir .getRoot ().toPath ().resolve ("output.zarr" );
229- Path series_path = output .resolve ("0" );
230- Path img_path = series_path .resolve ("0" );
231- int [] shape = new int [5 ];
232- shape [order .indexOf ("C" )] = sizeC ;
233- shape [order .indexOf ("T" )] = sizeT ;
234- shape [order .indexOf ("Z" )] = sizeZ ;
235- shape [order .indexOf ("Y" )] = sizeY ;
236- shape [order .indexOf ("X" )] = sizeX ;
237- ZarrArray array = ZarrArray .create (img_path .toString (), new ArrayParams ()
238- .shape (shape )
239- .dataType (com .bc .zarr .DataType .i4 )
240- .dimensionSeparator (DimensionSeparator .SLASH )
241- );
242-
243- for (int i = 0 ; i < sizeC ; i ++) {
244- for (int j = 0 ; j < sizeT ; j ++) {
245- for (int k = 0 ; k < sizeZ ; k ++) {
246- int [] plane = getRandomPlane (sizeX , sizeY );
247- int [] sh = new int [5 ];
248- int [] off = new int [5 ];
249- sh [order .indexOf ("C" )] = 1 ;
250- sh [order .indexOf ("T" )] = 1 ;
251- sh [order .indexOf ("Z" )] = 1 ;
252- sh [order .indexOf ("Y" )] = sizeY ;
253- sh [order .indexOf ("X" )] = sizeX ;
254- off [order .indexOf ("C" )] = i ;
255- off [order .indexOf ("T" )] = j ;
256- off [order .indexOf ("Z" )] = k ;
257- off [order .indexOf ("Y" )] = 0 ;
258- off [order .indexOf ("X" )] = 0 ;
259- array .write (plane , sh , off );
260- }
261- }
208+ if (order != null ) {
209+ assertBioFormats2Raw (input , output , "--dimension-order" , order );
210+ } else {
211+ assertBioFormats2Raw (input , output );
262212 }
263-
264- String rootZattrs = "{\n " + //
265- " \" bioformats2raw.layout\" : 3\n " + //
266- "}" ;
267- String zgroup = "{\n " + //
268- " \" zarr_format\" : 2\n " + //
269- "}" ;
270- Files .write (output .resolve (".zattrs" ), rootZattrs .getBytes ());
271- Files .write (output .resolve (".zgroup" ), zgroup .getBytes ());
272- Files .write (series_path .resolve (".zgroup" ), zgroup .getBytes ());
273-
213+
274214 List <Object > msArray = new ArrayList <>();
275215 Map <String , Object > msData = new HashMap <>();
276216 Map <String , Object > msMetadata = new HashMap <>();
277217 msMetadata .put ("method" , "loci.common.image.SimpleImageScaler" );
278218 msMetadata .put ("version" , "Bio-Formats 6.5.1" );
279219 msData .put ("metadata" , msMetadata );
280- msData .put ("axes" , getAxes (order ));
281- msData .put ("datasets" , getDatasets (1 ));
220+ if (order != null ) {
221+ msData .put ("axes" , getAxes (new StringBuffer (order ).reverse ().toString ()));
222+ }
223+ msData .put ("datasets" , getDatasets (resolutions ));
282224 msData .put ("version" , "0.1" );
283225 msArray .add (msData );
284226 ZarrGroup z = ZarrGroup .open (output .resolve ("0" ));
285227 Map <String ,Object > attrs = new HashMap <String , Object >();
286228 attrs .put ("multiscales" , msArray );
287229 z .writeAttributes (attrs );
288-
289230 return output ;
290- }
291-
292- private int [] getRandomPlane (int x , int y ) {
293- int [] res = new int [x *y ];
294- for (int i = 0 ; i < res .length ; i ++) {
295- res [i ] = (int ) (Math .random () * 255 );
231+
296232 }
297- return res ;
233+ public Path writeTestZarr (
234+ int sizeT ,
235+ int sizeC ,
236+ int sizeZ ,
237+ int sizeY ,
238+ int sizeX ,
239+ String pixelType ,
240+ int resolutions ) throws IOException {
241+ return writeTestZarr (sizeT , sizeC , sizeZ , sizeY , sizeX , pixelType ,
242+ resolutions , null );
298243 }
299244
300245 List <Map <String , String >> getDatasets (int resolutions ) {
@@ -907,7 +852,7 @@ public void testDownsampledZ() throws IOException {
907852 }
908853
909854 @ Test
910- public void testAxes ()
855+ public void testReadDataNonDefaultAxes ()
911856 throws IOException , InvalidRangeException {
912857 // Pretty much the same as testGetTimepointStackPlaneRowCol()
913858 // but testing a different axes order.
@@ -916,22 +861,24 @@ public void testAxes()
916861 int sizeZ = 4 ;
917862 int sizeY = 1024 ;
918863 int sizeX = 2048 ;
864+ int resolutions = 1 ;
919865 String order = DimensionOrder .VALUE_XYCTZ ;
920866 Pixels pixels = new Pixels (
921867 null , new PixelsType (PixelsType .VALUE_INT32 ),
922868 sizeX , sizeY , sizeZ , sizeC , sizeT , "" , new DimensionOrder (order ));
923- Path output = writeTestZarrWithAxes (sizeT , sizeC , sizeZ , sizeY , sizeX , order );
869+ Path output = writeTestZarr (
870+ sizeT , sizeC , sizeZ , sizeY , sizeX , "int32" , resolutions , order );
871+ String reverse_order = new StringBuilder (order ).reverse ().toString ();
924872 try (ZarrPixelBuffer zpbuf =
925873 createPixelBuffer (pixels , output .resolve ("0" ), sizeX , sizeY )) {
926- System .out .println ("pixel type: " + zpbuf .getPixelsType ());
927874 for (int t = 0 ; t < sizeT ; t ++) {
928875 // Assert timepoint
929876 byte [] timepoint = zpbuf .getTimepoint (t ).getData ().array ();
930877 for (int c = 0 ; c < sizeC ; c ++) {
931878 // Assert stack
932879 byte [] stack = zpbuf .getStack (c , t ).getData ().array ();
933880 byte [] stackFromTimepoint =
934- getStack (timepoint , c , sizeC , sizeZ , sizeX , sizeY , order );
881+ getStack (timepoint , c , sizeC , sizeZ , sizeX , sizeY , reverse_order );
935882 Assert .assertArrayEquals (stack , stackFromTimepoint );
936883 for (int z = 0 ; z < sizeZ ; z ++) {
937884 // Assert plane
@@ -958,4 +905,69 @@ null, new PixelsType(PixelsType.VALUE_INT32),
958905 }
959906 }
960907 }
908+
909+ @ Test
910+ public void testNonDefaultAxes ()
911+ throws IOException , InvalidRangeException {
912+ int sizeT = 1 ;
913+ int sizeC = 2 ;
914+ int sizeZ = 16 ;
915+ int sizeY = 256 ;
916+ int sizeX = 256 ;
917+ int resolutions = 1 ;
918+ String order = DimensionOrder .VALUE_XYCTZ ;
919+ Pixels pixels = new Pixels (
920+ null , null , sizeX , sizeY , sizeZ , sizeC , sizeT , "" , new DimensionOrder (order ));
921+ Path output = writeTestZarr (
922+ sizeT , sizeC , sizeZ , sizeY , sizeX , "uint8" , resolutions , order );
923+ try (ZarrPixelBuffer zpbuf =
924+ createPixelBuffer (pixels , output .resolve ("0" ), sizeX , sizeY )) {
925+ Map <Axes , Integer > axes = zpbuf .getAxes ();
926+ Assert .assertEquals (0 , axes .get (Axes .Z ).intValue ());
927+ Assert .assertEquals (1 , axes .get (Axes .T ).intValue ());
928+ Assert .assertEquals (2 , axes .get (Axes .C ).intValue ());
929+ Assert .assertEquals (3 , axes .get (Axes .Y ).intValue ());
930+ Assert .assertEquals (4 , axes .get (Axes .X ).intValue ());
931+ Assert .assertEquals (sizeT , zpbuf .getSizeT ());
932+ Assert .assertEquals (sizeC , zpbuf .getSizeC ());
933+ Assert .assertEquals (sizeZ , zpbuf .getSizeZ ());
934+ Assert .assertEquals (sizeY , zpbuf .getSizeY ());
935+ Assert .assertEquals (sizeX , zpbuf .getSizeX ());
936+ }
937+ }
938+
939+ @ Test
940+ public void testDefaultAxes ()
941+ throws IOException , InvalidRangeException {
942+ // Check that if access are not in the file it defaults to TCZYX order when no axes found
943+
944+ int sizeT = 1 ;
945+ int sizeC = 2 ;
946+ int sizeZ = 16 ;
947+ int sizeY = 256 ;
948+ int sizeX = 256 ;
949+ int resolutions = 1 ;
950+
951+ Pixels pixels = new Pixels (
952+ null , null , sizeX , sizeY , sizeZ , sizeC , sizeT , "" , null );
953+ Path output = writeTestZarr (
954+ sizeT , sizeC , sizeZ , sizeY , sizeX , "uint8" , resolutions );
955+
956+ try (ZarrPixelBuffer zpbuf =
957+ createPixelBuffer (pixels , output .resolve ("0" ), sizeX , sizeY )) {
958+ Map <Axes , Integer > axes = zpbuf .getAxes ();
959+ Assert .assertEquals (0 , axes .get (Axes .T ).intValue ());
960+ Assert .assertEquals (1 , axes .get (Axes .C ).intValue ());
961+ Assert .assertEquals (2 , axes .get (Axes .Z ).intValue ());
962+ Assert .assertEquals (3 , axes .get (Axes .Y ).intValue ());
963+ Assert .assertEquals (4 , axes .get (Axes .X ).intValue ());
964+ Assert .assertEquals (sizeT , zpbuf .getSizeT ());
965+ Assert .assertEquals (sizeC , zpbuf .getSizeC ());
966+ Assert .assertEquals (sizeZ , zpbuf .getSizeZ ());
967+ Assert .assertEquals (sizeY , zpbuf .getSizeY ());
968+ Assert .assertEquals (sizeX , zpbuf .getSizeX ());
969+ }
970+ }
971+
972+
961973}
0 commit comments