Skip to content

Commit 48262e3

Browse files
committed
add javadoc to class decodenumpy
1 parent 7c2a684 commit 48262e3

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/main/java/io/bioimage/modelrunner/numpy/DecodeNumpy.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,9 @@
7575
import net.imglib2.util.Util;
7676

7777
/**
78-
* TODO
79-
* TODO
80-
* TODO ADD imglib2 to numpy file
8178
*
8279
*
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
8481
* @author Carlos Garcia Lopez de Haro
8582
*
8683
*/
@@ -119,10 +116,25 @@ public class DecodeNumpy {
119116
DATA_TYPES_MAP.put("float64", 8);
120117
}
121118

119+
/**
120+
* Key used to refer to the values of the array
121+
*/
122122
public final static String DATA_KEY = "data";
123+
/**
124+
* Key referred to the shape of the array (array dimensions)
125+
*/
123126
public final static String SHAPE_KEY = "shape";
127+
/**
128+
* Key referred to the shape of the array data type
129+
*/
124130
public final static String DTYPE_KEY = "dtype";
131+
/**
132+
* Key referred to the order when flattening the array, c-order or fortran-order
133+
*/
125134
public final static String IS_FORTRAN_ORDER_KEY = "is_fortran_order";
135+
/**
136+
* Key referred to the byte order in the array
137+
*/
126138
public final static String BYTE_ORDER_KEY = "byte_order";
127139

128140
/**
@@ -133,6 +145,8 @@ public class DecodeNumpy {
133145

134146
/**
135147
* Main method to test the ImgLib2 creation
148+
* @param T
149+
* possible ImgLib2 data types of the provided {@link RandomAccessibleInterval}
136150
* @param args
137151
* no args are needed
138152
* @throws FileNotFoundException if the numpy array file is not found
@@ -260,6 +274,7 @@ public static HashMap<String, Object> decodeNumpyFromByteArrayStreamToRawMap(Inp
260274

261275
/**
262276
* MEthod to decode the bytes corresponding to a numpy array stored in the numpy file
277+
* and convert them into a {@link RandomAccessibleInterval}
263278
* @param <T>
264279
* possible data types that the ImgLib2 image can have
265280
* @param is
@@ -388,8 +403,10 @@ else if (npDtype.equals("c8"))
388403
/**
389404
* Get a String representing a datatype explicitly from the String that numpy uses to
390405
* 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
393410
* @return a String defining the datatype in a explicit manner
394411
* @throws IllegalArgumentException if the String provided is not a numpy datatype
395412
*/
@@ -546,6 +563,18 @@ private static Img<ByteType> buildBoolean(ByteBuffer buf, ByteOrder byteOrder, l
546563
return outputImg;
547564
}
548565

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+
*/
549578
public static < T extends RealType< T > & NativeType< T > > byte[]
550579
createNumpyStyleByteArray(RandomAccessibleInterval<T> rai) {
551580
String strHeader = "{'descr': '<";
@@ -632,6 +661,18 @@ private static Img<ByteType> buildBoolean(ByteBuffer buf, ByteOrder byteOrder, l
632661
return total;
633662
}
634663

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+
*/
635676
public static < T extends RealType< T > & NativeType< T > >
636677
void writeRaiToNpyFile(String filePath, RandomAccessibleInterval<T> rai) throws FileNotFoundException, IOException {
637678
byte[] total = createNumpyStyleByteArray(rai);

0 commit comments

Comments
 (0)