75
75
import net .imglib2 .util .Util ;
76
76
77
77
/**
78
- * TODO
79
- * TODO
80
- * TODO ADD imglib2 to numpy file
81
78
*
82
79
*
83
- * Class to convert numpy arrays stored in npy files into ImgLib2 images
80
+ * Class to convert numpy arrays stored in npy files into ImgLib2 images and viceversa
84
81
* @author Carlos Garcia Lopez de Haro
85
82
*
86
83
*/
@@ -119,10 +116,25 @@ public class DecodeNumpy {
119
116
DATA_TYPES_MAP .put ("float64" , 8 );
120
117
}
121
118
119
+ /**
120
+ * Key used to refer to the values of the array
121
+ */
122
122
public final static String DATA_KEY = "data" ;
123
+ /**
124
+ * Key referred to the shape of the array (array dimensions)
125
+ */
123
126
public final static String SHAPE_KEY = "shape" ;
127
+ /**
128
+ * Key referred to the shape of the array data type
129
+ */
124
130
public final static String DTYPE_KEY = "dtype" ;
131
+ /**
132
+ * Key referred to the order when flattening the array, c-order or fortran-order
133
+ */
125
134
public final static String IS_FORTRAN_ORDER_KEY = "is_fortran_order" ;
135
+ /**
136
+ * Key referred to the byte order in the array
137
+ */
126
138
public final static String BYTE_ORDER_KEY = "byte_order" ;
127
139
128
140
/**
@@ -133,6 +145,8 @@ public class DecodeNumpy {
133
145
134
146
/**
135
147
* Main method to test the ImgLib2 creation
148
+ * @param T
149
+ * possible ImgLib2 data types of the provided {@link RandomAccessibleInterval}
136
150
* @param args
137
151
* no args are needed
138
152
* @throws FileNotFoundException if the numpy array file is not found
@@ -260,6 +274,7 @@ public static HashMap<String, Object> decodeNumpyFromByteArrayStreamToRawMap(Inp
260
274
261
275
/**
262
276
* MEthod to decode the bytes corresponding to a numpy array stored in the numpy file
277
+ * and convert them into a {@link RandomAccessibleInterval}
263
278
* @param <T>
264
279
* possible data types that the ImgLib2 image can have
265
280
* @param is
@@ -388,8 +403,10 @@ else if (npDtype.equals("c8"))
388
403
/**
389
404
* Get a String representing a datatype explicitly from the String that numpy uses to
390
405
* name datatypes
391
- * @param npDtype
392
- * datatype defined per Numpy
406
+ * @param <T>
407
+ * possible ImgLib2 data types
408
+ * @param type
409
+ * ImgLib2 possible data type
393
410
* @return a String defining the datatype in a explicit manner
394
411
* @throws IllegalArgumentException if the String provided is not a numpy datatype
395
412
*/
@@ -546,6 +563,18 @@ private static Img<ByteType> buildBoolean(ByteBuffer buf, ByteOrder byteOrder, l
546
563
return outputImg ;
547
564
}
548
565
566
+ /**
567
+ * Method to convert a {@link RandomAccessibleInterval} into the byte array that is used by Numpy
568
+ * to create .npy files.
569
+ * The byte array created contains the flattened data of the {@link RandomAccessibleInterval} plus
570
+ * information of the shape, data type, fortran order and byte order
571
+ * @param <T>
572
+ * possible ImgLib2 data types of the provided {@link RandomAccessibleInterval}
573
+ * @param rai
574
+ * the {@link RandomAccessibleInterval} of interest (an n-dimensional array) that is going to be
575
+ * converted into the byte array
576
+ * @return a byte array containing all the info to recreate it. An array in the format of Numpy .npy
577
+ */
549
578
public static < T extends RealType < T > & NativeType < T > > byte []
550
579
createNumpyStyleByteArray (RandomAccessibleInterval <T > rai ) {
551
580
String strHeader = "{'descr': '<" ;
@@ -632,6 +661,18 @@ private static Img<ByteType> buildBoolean(ByteBuffer buf, ByteOrder byteOrder, l
632
661
return total ;
633
662
}
634
663
664
+ /**
665
+ * Method that saves a {@link RandomAccessibleInterval} nd array to a .npy Numpy radable file
666
+ * @param <T>
667
+ * possible ImgLib2 data types of the provided {@link RandomAccessibleInterval}
668
+ * @param filePath
669
+ * path where the file will be saved
670
+ * @param rai
671
+ * the {@link RandomAccessibleInterval} of interest (an n-dimensional array) that is going to be
672
+ * converted into the byte array
673
+ * @throws FileNotFoundException if the file path provided is invalid
674
+ * @throws IOException if there is any error saving the file
675
+ */
635
676
public static < T extends RealType < T > & NativeType < T > >
636
677
void writeRaiToNpyFile (String filePath , RandomAccessibleInterval <T > rai ) throws FileNotFoundException , IOException {
637
678
byte [] total = createNumpyStyleByteArray (rai );
0 commit comments