1+ import warnings
2+ from packaging .version import parse as parse_version
13import json
24import numpy as np
35
46
7+ # Both np.float_ and np.complex_ were deprecated in Numpy 2.0.0
8+ if parse_version (np .__version__ ) < parse_version ("2.0.0" ):
9+ TYPES = {
10+ "float" : (np .float_ , np .float16 , np .float32 , np .float64 ),
11+ "complex" : (np .complex_ , np .complex64 , np .complex128 ),
12+ }
13+ warnings .warn (
14+ f"You are using an old version of Numpy ({ np .__version__ } ), "
15+ "support for 'np.float_' and 'np.complex_' were deprecated "
16+ "in Numpy 2.0.0. If you use these datatypes in your code "
17+ "please consider updating them." ,
18+ category = DeprecationWarning ,
19+ )
20+ else :
21+ TYPES = {
22+ "float" : (np .float16 , np .float32 , np .float64 ),
23+ "complex" : (np .complex64 , np .complex128 ),
24+ }
25+
26+
527class NumpyEncoder (json .JSONEncoder ):
628 """Custom encoder for numpy data types"""
729
@@ -22,13 +44,12 @@ def default(self, obj):
2244 np .uint64 ,
2345 ),
2446 ):
25-
2647 return int (obj )
2748
28- elif isinstance (obj , ( np . float16 , np . float32 , np . float64 ) ):
49+ elif isinstance (obj , TYPES [ "float" ] ):
2950 return float (obj )
3051
31- elif isinstance (obj , ( np . complex_ , np . complex64 , np . complex128 ) ):
52+ elif isinstance (obj , TYPES [ "complex" ] ):
3253 return {"real" : obj .real , "imag" : obj .imag }
3354
3455 elif isinstance (obj , (np .ndarray ,)):
0 commit comments