@@ -13,6 +13,7 @@ numeric data types are supported"
13
13
ReadNumPyArray ::invalidFile = "`1` is not a valid NPY file"
14
14
ReadNumPyArray ::invalidVersion = "NPY version `1`.`2` not supported"
15
15
ReadNumPyArray ::invalidDType = "Invalid dtype `1`"
16
+ ReadNumPyArray ::fortranOrder = "Fortran order is not supported"
16
17
17
18
Begin ["Private`" ]
18
19
@@ -38,41 +39,45 @@ ReadNumPyArray[file_] := Module[{fs, formatVersion, headerLength, retVal},
38
39
fortranOrder = headerDic [["fortran_order" ]];
39
40
shape = headerDic [["shape" ]];
40
41
41
- (* Endianness *)
42
- byteOrder =
43
- Switch [StringTake [dtype , 1 ], "<" , - 1 , ">" , 1 , _ , $ByteOrdering ];
42
+ (* Fortran order is not supported *)
43
+ If [! fortranOrder ,
44
44
45
- (* Data type *)
46
- pyType = StringTake [dtype , {2 , - 1 }];
47
- mathematicaDataType = Switch [pyType ,
48
- "b1" , "Integer8" ,
49
- "B1" , "UnsignedInteger8" ,
50
- "i1" , "Integer8" ,
51
- "u1" , "UnsignedInteger8" ,
52
- "i2" , "Integer16" ,
53
- "u2" , "UnsignedInteger16" ,
54
- "i4" , "Integer32" ,
55
- "u4" , "UnsignedInteger32" ,
56
- "i8" , "Integer64" ,
57
- "u8" , "UnsignedInteger64" ,
58
- "i16" , "Integer128" ,
59
- "u16" , "UnsignedInteger128" ,
60
- "f4" , "Real32" ,
61
- "f8" , "Real64" ,
62
- (* "f16", "Real128", *)
63
- "c8" , "Complex64" ,
64
- "c16" , "Complex128" ,
65
- (* "c32", "Complex256", *)
66
- _ , Message [ReadNumPyArray ::invalidDType , pyType ]; "Unknown"
67
- ];
45
+ (* Endianness *)
46
+ byteOrder =
47
+ Switch [StringTake [dtype , 1 ], "<" , - 1 , ">" , 1 , _ , $ByteOrdering ];
68
48
69
- (* Read data *)
70
- If [mathematicaDataType == "Unknown" , $Failed ,
71
- data =
72
- BinaryReadList [fs , mathematicaDataType ,
73
- ByteOrdering -> byteOrder ];
74
- ArrayReshape [data , shape ]
75
- ]
49
+ (* Data type *)
50
+ pyType = StringTake [dtype , {2 , - 1 }];
51
+ mathematicaDataType = Switch [pyType ,
52
+ "b1" , "Integer8" ,
53
+ "B1" , "UnsignedInteger8" ,
54
+ "i1" , "Integer8" ,
55
+ "u1" , "UnsignedInteger8" ,
56
+ "i2" , "Integer16" ,
57
+ "u2" , "UnsignedInteger16" ,
58
+ "i4" , "Integer32" ,
59
+ "u4" , "UnsignedInteger32" ,
60
+ "i8" , "Integer64" ,
61
+ "u8" , "UnsignedInteger64" ,
62
+ "i16" , "Integer128" ,
63
+ "u16" , "UnsignedInteger128" ,
64
+ "f4" , "Real32" ,
65
+ "f8" , "Real64" ,
66
+ (* "f16", "Real128", *)
67
+ "c8" , "Complex64" ,
68
+ "c16" , "Complex128" ,
69
+ (* "c32", "Complex256", *)
70
+ _ , Message [ReadNumPyArray ::invalidDType , pyType ]; "Unknown"
71
+ ];
72
+
73
+ (* Read data *)
74
+ If [mathematicaDataType == "Unknown" , $Failed ,
75
+ data =
76
+ BinaryReadList [fs , mathematicaDataType ,
77
+ ByteOrdering -> byteOrder ];
78
+ ArrayReshape [data , shape ]
79
+ ]
80
+ , Message [ReadNumPyArray ::fortranOrder ]; $Failed ]
76
81
, Message [ReadNumPyArray ::invalidVersion , formatVersion [[1 ]],
77
82
formatVersion [[2 ]]]; $Failed ]
78
83
, Message [ReadNumPyArray ::invalidFile , file ]; $Failed ];
0 commit comments