From 624d2549e8a4c184ce734e86822c7231ec83948a Mon Sep 17 00:00:00 2001 From: Paul Kent Date: Fri, 8 Nov 2024 19:41:30 -0500 Subject: [PATCH 1/2] prototype nexus numpy2 support --- nexus/lib/pwscf_input.py | 6 +++++- nexus/lib/pyscf_input.py | 2 ++ nexus/lib/rmg_input.py | 6 +++--- nexus/lib/testing.py | 8 ++++---- nexus/tests/unit/test_qmcpack_input.py | 2 +- nexus/tests/unit/test_quantum_package_input.py | 2 +- nexus/tests/unit/test_vasp_input.py | 2 +- 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/nexus/lib/pwscf_input.py b/nexus/lib/pwscf_input.py index 5ee4fbb7e5..f106e9a846 100644 --- a/nexus/lib/pwscf_input.py +++ b/nexus/lib/pwscf_input.py @@ -48,7 +48,7 @@ import inspect from copy import deepcopy from superstring import string2val -from numpy import fromstring,empty,array,float64,ones,pi,dot,ceil,ndarray, where, append, unique +from numpy import fromstring,empty,array,float64,ones,pi,dot,ceil,ndarray, where, append, unique, set_printoptions, version from numpy.linalg import inv from unit_converter import convert from generic import obj @@ -395,6 +395,8 @@ def read(self,lines): def write(self,parent): + if (int('.'.join(version.version.split('.', 1)[:-1]))>1): + set_printoptions(legacy="1.25") atom_index = obj() if 'atomic_species' in parent and 'atoms' in parent.atomic_species: for i,a in enumerate(parent.atomic_species.atoms): @@ -1931,6 +1933,8 @@ def standardize_types(self): def generate_pwscf_input(selector,**kwargs): + if (int('.'.join(version.version.split('.', 1)[:-1]))>1): + set_printoptions(legacy="1.25") if 'system' in kwargs: system = kwargs['system'] if isinstance(system,PhysicalSystem): diff --git a/nexus/lib/pyscf_input.py b/nexus/lib/pyscf_input.py index 30ca0778a3..36ed015ba0 100644 --- a/nexus/lib/pyscf_input.py +++ b/nexus/lib/pyscf_input.py @@ -498,5 +498,7 @@ def write_text(self,filepath=None): def generate_pyscf_input(*args,**kwargs): + if (int('.'.join(np.version.version.split('.', 1)[:-1]))>1): + np.set_printoptions(legacy="1.25") return PyscfInput(*args,**kwargs) #end def generate_pyscf_input diff --git a/nexus/lib/rmg_input.py b/nexus/lib/rmg_input.py index 480bef36fc..44f8ce4965 100644 --- a/nexus/lib/rmg_input.py +++ b/nexus/lib/rmg_input.py @@ -2414,10 +2414,10 @@ def write_double_array(v): rmg_value_types = obj({ - 'string' : (str , np.string_), + 'string' : (str , np.bytes_), 'boolean' : (bool , np.bool_ ), 'integer' : (int , np.int_ ), - 'double' : (float, np.float_ ), + 'double' : (float, np.float64 ), 'integer array' : (tuple,list,np.ndarray), 'double array' : (tuple,list,np.ndarray), }) @@ -3142,7 +3142,7 @@ def assign(self,**values): unrecognized = [] for k,v in values.items(): if k in input_spec.keywords: - if isinstance(v,(str,np.string_)): + if isinstance(v,(str,np.bytes_)): self[k] = input_spec.keywords[k].read(v) else: self[k] = input_spec.keywords[k].assign(v) diff --git a/nexus/lib/testing.py b/nexus/lib/testing.py index 569f16caa0..9925edc0ee 100644 --- a/nexus/lib/testing.py +++ b/nexus/lib/testing.py @@ -23,10 +23,10 @@ def value_diff(v1,v2,atol=def_atol,rtol=def_rtol,int_as_float=False): v2_bool = isinstance(v2,(bool,np.bool_)) v1_int = isinstance(v1,(int,np.int_)) and not v1_bool v2_int = isinstance(v2,(int,np.int_)) and not v2_bool - v1_float = isinstance(v1,(float,np.float_)) - v2_float = isinstance(v2,(float,np.float_)) - v1_str = isinstance(v1,(str,np.string_)) - v2_str = isinstance(v2,(str,np.string_)) + v1_float = isinstance(v1,(float,np.float64)) + v2_float = isinstance(v2,(float,np.float64)) + v1_str = isinstance(v1,(str,np.bytes_)) + v2_str = isinstance(v2,(str,np.bytes_)) if id(v1)==id(v2): None elif int_as_float and (v1_int or v1_float) and (v2_int or v2_float): diff --git a/nexus/tests/unit/test_qmcpack_input.py b/nexus/tests/unit/test_qmcpack_input.py index 5dd15de4ae..4646c3d0af 100644 --- a/nexus/tests/unit/test_qmcpack_input.py +++ b/nexus/tests/unit/test_qmcpack_input.py @@ -30,7 +30,7 @@ def format_value(v): s = s[:-2] #end if s += '])' - elif isinstance(v,(str,np.string_)): + elif isinstance(v,(str,np.bytes_)): s = "'"+v+"'" else: s = str(v) diff --git a/nexus/tests/unit/test_quantum_package_input.py b/nexus/tests/unit/test_quantum_package_input.py index d49f19f786..47ec827c84 100644 --- a/nexus/tests/unit/test_quantum_package_input.py +++ b/nexus/tests/unit/test_quantum_package_input.py @@ -22,7 +22,7 @@ def format_value(v): s = s[:-2] #end if s += '])' - elif isinstance(v,(str,np.string_)): + elif isinstance(v,(str,np.bytes_)): s = "'"+v+"'" else: s = str(v) diff --git a/nexus/tests/unit/test_vasp_input.py b/nexus/tests/unit/test_vasp_input.py index 6186ae4816..f499e83068 100644 --- a/nexus/tests/unit/test_vasp_input.py +++ b/nexus/tests/unit/test_vasp_input.py @@ -31,7 +31,7 @@ def format_value(v): s = s[:-2] #end if s += '])' - elif isinstance(v,(str,np.string_)): + elif isinstance(v,(str,np.bytes_)): s = "'"+v+"'" else: s = str(v) From 2fb5ddeac9ac520030ce9d7b37ec4605eb214e20 Mon Sep 17 00:00:00 2001 From: Paul Kent Date: Mon, 25 Nov 2024 14:27:08 -0500 Subject: [PATCH 2/2] add+use function --- nexus/lib/pwscf_input.py | 6 +----- nexus/lib/pyscf_input.py | 2 -- nexus/lib/rmg_input.py | 6 +++--- nexus/lib/testing.py | 19 +++++++++++++++---- nexus/tests/unit/test_qmcpack_input.py | 2 +- .../tests/unit/test_quantum_package_input.py | 2 +- nexus/tests/unit/test_vasp_input.py | 2 +- 7 files changed, 22 insertions(+), 17 deletions(-) diff --git a/nexus/lib/pwscf_input.py b/nexus/lib/pwscf_input.py index f106e9a846..5ee4fbb7e5 100644 --- a/nexus/lib/pwscf_input.py +++ b/nexus/lib/pwscf_input.py @@ -48,7 +48,7 @@ import inspect from copy import deepcopy from superstring import string2val -from numpy import fromstring,empty,array,float64,ones,pi,dot,ceil,ndarray, where, append, unique, set_printoptions, version +from numpy import fromstring,empty,array,float64,ones,pi,dot,ceil,ndarray, where, append, unique from numpy.linalg import inv from unit_converter import convert from generic import obj @@ -395,8 +395,6 @@ def read(self,lines): def write(self,parent): - if (int('.'.join(version.version.split('.', 1)[:-1]))>1): - set_printoptions(legacy="1.25") atom_index = obj() if 'atomic_species' in parent and 'atoms' in parent.atomic_species: for i,a in enumerate(parent.atomic_species.atoms): @@ -1933,8 +1931,6 @@ def standardize_types(self): def generate_pwscf_input(selector,**kwargs): - if (int('.'.join(version.version.split('.', 1)[:-1]))>1): - set_printoptions(legacy="1.25") if 'system' in kwargs: system = kwargs['system'] if isinstance(system,PhysicalSystem): diff --git a/nexus/lib/pyscf_input.py b/nexus/lib/pyscf_input.py index 36ed015ba0..30ca0778a3 100644 --- a/nexus/lib/pyscf_input.py +++ b/nexus/lib/pyscf_input.py @@ -498,7 +498,5 @@ def write_text(self,filepath=None): def generate_pyscf_input(*args,**kwargs): - if (int('.'.join(np.version.version.split('.', 1)[:-1]))>1): - np.set_printoptions(legacy="1.25") return PyscfInput(*args,**kwargs) #end def generate_pyscf_input diff --git a/nexus/lib/rmg_input.py b/nexus/lib/rmg_input.py index 44f8ce4965..480bef36fc 100644 --- a/nexus/lib/rmg_input.py +++ b/nexus/lib/rmg_input.py @@ -2414,10 +2414,10 @@ def write_double_array(v): rmg_value_types = obj({ - 'string' : (str , np.bytes_), + 'string' : (str , np.string_), 'boolean' : (bool , np.bool_ ), 'integer' : (int , np.int_ ), - 'double' : (float, np.float64 ), + 'double' : (float, np.float_ ), 'integer array' : (tuple,list,np.ndarray), 'double array' : (tuple,list,np.ndarray), }) @@ -3142,7 +3142,7 @@ def assign(self,**values): unrecognized = [] for k,v in values.items(): if k in input_spec.keywords: - if isinstance(v,(str,np.bytes_)): + if isinstance(v,(str,np.string_)): self[k] = input_spec.keywords[k].read(v) else: self[k] = input_spec.keywords[k].assign(v) diff --git a/nexus/lib/testing.py b/nexus/lib/testing.py index 9925edc0ee..8fa6f3558f 100644 --- a/nexus/lib/testing.py +++ b/nexus/lib/testing.py @@ -1,6 +1,16 @@ +# Workaround numpy2 changes +def portable_numpy(np): + if np.lib.NumpyVersion(np.__version__) >= '2.0.0b1': + np.set_printoptions(legacy="1.25") + np.string_ = np.bytes_ + np.float_ = np.float64 + #end if +#end def portable_numpy + try: import numpy as np + portable_numpy(np) numpy_available = True except: numpy_available = False @@ -10,6 +20,7 @@ def_atol = 0.0 def_rtol = 1e-6 + # determine if two floats differ def float_diff(v1,v2,atol=def_atol,rtol=def_rtol): return np.abs(v1-v2)>atol+rtol*np.abs(v2) @@ -23,10 +34,10 @@ def value_diff(v1,v2,atol=def_atol,rtol=def_rtol,int_as_float=False): v2_bool = isinstance(v2,(bool,np.bool_)) v1_int = isinstance(v1,(int,np.int_)) and not v1_bool v2_int = isinstance(v2,(int,np.int_)) and not v2_bool - v1_float = isinstance(v1,(float,np.float64)) - v2_float = isinstance(v2,(float,np.float64)) - v1_str = isinstance(v1,(str,np.bytes_)) - v2_str = isinstance(v2,(str,np.bytes_)) + v1_float = isinstance(v1,(float,np.float_)) + v2_float = isinstance(v2,(float,np.float_)) + v1_str = isinstance(v1,(str,np.string_)) + v2_str = isinstance(v2,(str,np.string_)) if id(v1)==id(v2): None elif int_as_float and (v1_int or v1_float) and (v2_int or v2_float): diff --git a/nexus/tests/unit/test_qmcpack_input.py b/nexus/tests/unit/test_qmcpack_input.py index 4646c3d0af..5dd15de4ae 100644 --- a/nexus/tests/unit/test_qmcpack_input.py +++ b/nexus/tests/unit/test_qmcpack_input.py @@ -30,7 +30,7 @@ def format_value(v): s = s[:-2] #end if s += '])' - elif isinstance(v,(str,np.bytes_)): + elif isinstance(v,(str,np.string_)): s = "'"+v+"'" else: s = str(v) diff --git a/nexus/tests/unit/test_quantum_package_input.py b/nexus/tests/unit/test_quantum_package_input.py index 47ec827c84..d49f19f786 100644 --- a/nexus/tests/unit/test_quantum_package_input.py +++ b/nexus/tests/unit/test_quantum_package_input.py @@ -22,7 +22,7 @@ def format_value(v): s = s[:-2] #end if s += '])' - elif isinstance(v,(str,np.bytes_)): + elif isinstance(v,(str,np.string_)): s = "'"+v+"'" else: s = str(v) diff --git a/nexus/tests/unit/test_vasp_input.py b/nexus/tests/unit/test_vasp_input.py index f499e83068..6186ae4816 100644 --- a/nexus/tests/unit/test_vasp_input.py +++ b/nexus/tests/unit/test_vasp_input.py @@ -31,7 +31,7 @@ def format_value(v): s = s[:-2] #end if s += '])' - elif isinstance(v,(str,np.bytes_)): + elif isinstance(v,(str,np.string_)): s = "'"+v+"'" else: s = str(v)