Skip to content

Commit 9aad2bd

Browse files
committed
Merge branch 'numpy-2.0-compatible' into 'development'
compatible with NumPy 2.0 See merge request damask/DAMASK!953
2 parents e4a5445 + 6d7ef46 commit 9aad2bd

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

python/damask/_geomgrid.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ def material(self,
132132
material: np.ndarray):
133133
if len(material.shape) != 3:
134134
raise ValueError(f'invalid material shape {material.shape}')
135-
if material.dtype not in np.sctypes['float'] and material.dtype not in np.sctypes['int']:
135+
if material.dtype not in [np.float32,np.float64, np.int32,np.int64]:
136136
raise TypeError(f'invalid material data type "{material.dtype}"')
137137

138138
self._material = np.copy(material)
139139

140-
if self.material.dtype in np.sctypes['float'] and \
140+
if self.material.dtype in [np.float32,np.float64] and \
141141
np.all(self.material == self.material.astype(np.int64).astype(float)):
142142
self._material = self.material.astype(np.int64)
143143

@@ -153,7 +153,7 @@ def size(self,
153153
if len(size) != 3 or any(np.array(size) < 0):
154154
raise ValueError(f'invalid size {size}')
155155

156-
self._size = np.array(size)
156+
self._size = np.array(size,np.float64)
157157

158158
@property
159159
def origin(self) -> np.ndarray:
@@ -166,7 +166,7 @@ def origin(self,
166166
if len(origin) != 3:
167167
raise ValueError(f'invalid origin {origin}')
168168

169-
self._origin = np.array(origin)
169+
self._origin = np.array(origin,np.float64)
170170

171171
@property
172172
def initial_conditions(self) -> Dict[str,np.ndarray]:
@@ -471,7 +471,7 @@ def load_DREAM3D(fname: Union[str, Path],
471471
O = Rotation.from_Euler_angles(f['/'.join([b,c,Euler_angles])]).as_quaternion().reshape(-1,4) # noqa
472472
unique,unique_inverse = np.unique(np.hstack([O,phase]),return_inverse=True,axis=0)
473473
ma = np.arange(cells.prod()) if len(unique) == cells.prod() else \
474-
np.arange(unique.size)[np.argsort(pd.unique(unique_inverse))][unique_inverse]
474+
np.arange(unique.size)[np.argsort(pd.unique(unique_inverse.squeeze()))][unique_inverse]
475475
else:
476476
ma = f['/'.join([b,c,feature_IDs])][()].flatten()
477477

@@ -512,7 +512,7 @@ def from_table(table: Table,
512512
unique,unique_inverse = np.unique(np.hstack([table.get(l) for l in labels_]),return_inverse=True,axis=0)
513513

514514
ma = np.arange(cells.prod()) if len(unique) == cells.prod() else \
515-
np.arange(unique.size)[np.argsort(pd.unique(unique_inverse))][unique_inverse]
515+
np.arange(unique.size)[np.argsort(pd.unique(unique_inverse.squeeze()))][unique_inverse]
516516

517517
return GeomGrid(material = ma.reshape(cells,order='F'),
518518
size = size,
@@ -812,7 +812,7 @@ def save_ASCII(self,
812812
'homogenization 1',
813813
]
814814

815-
format_string = '%g' if self.material.dtype in np.sctypes['float'] else \
815+
format_string = '%g' if self.material.dtype in [np.float32,np.float64] else \
816816
'%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.material)))))
817817
np.savetxt(fname,
818818
self.material.reshape([self.cells[0],np.prod(self.cells[1:])],order='F').T,
@@ -1347,14 +1347,14 @@ def add_primitive(self,
13471347
13481348
"""
13491349
# radius and center
1350-
r = np.array(dimension)/2.0*self.size/self.cells if np.array(dimension).dtype in np.sctypes['int'] else \
1350+
r = np.array(dimension)/2.0*self.size/self.cells if np.issubdtype(np.array(dimension).dtype,np.integer) else \
13511351
np.array(dimension)/2.0
1352-
c = (np.array(center) + .5)*self.size/self.cells if np.array(center).dtype in np.sctypes['int'] else \
1352+
c = (np.array(center) + .5)*self.size/self.cells if np.issubdtype(np.array(center).dtype, np.integer) else \
13531353
(np.array(center) - self.origin)
13541354

13551355
coords = grid_filters.coordinates0_point(self.cells,self.size,
13561356
-(0.5*(self.size + (self.size/self.cells
1357-
if np.array(center).dtype in np.sctypes['int'] else
1357+
if np.issubdtype(np.array(center).dtype,np.integer) else
13581358
0)) if periodic else c))
13591359
coords_rot = R.broadcast_to(tuple(self.cells))@coords
13601360

python/damask/_result.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
from . import util
2626
from ._typehints import FloatSequence, IntSequence, DADF5Dataset
2727

28+
2829
h5py3 = h5py.__version__[0] == '3'
2930

3031
chunk_size = 1024**2//8 # for compression in HDF5
32+
3133
prefix_inc = 'increment_'
3234

35+
3336
def _read(dataset: h5py._hl.dataset.Dataset) -> np.ndarray:
3437
"""Read a dataset and its metadata into a numpy.ndarray."""
3538
metadata = {k:(v.decode() if not h5py3 and type(v) is bytes else v) for k,v in dataset.attrs.items()}
@@ -59,7 +62,7 @@ def _empty_like(dataset: np.ma.core.MaskedArray,
5962
fill_int: int) -> np.ma.core.MaskedArray:
6063
"""Create empty numpy.ma.MaskedArray."""
6164
return ma.array(np.empty((N_materialpoints,)+dataset.shape[1:],dataset.dtype),
62-
fill_value = fill_float if dataset.dtype in np.sctypes['float'] else fill_int,
65+
fill_value = fill_float if np.issubdtype(dataset.dtype,np.floating) else fill_int,
6366
mask = True)
6467

6568

@@ -1783,9 +1786,10 @@ def export_XDMF(self,
17831786
attribute_type_map = defaultdict(lambda:'Matrix', ( ((),'Scalar'), ((3,),'Vector'), ((3,3),'Tensor')) )
17841787

17851788
def number_type_map(dtype):
1786-
if dtype in np.sctypes['int']: return 'Int'
1787-
if dtype in np.sctypes['uint']: return 'UInt'
1788-
if dtype in np.sctypes['float']: return 'Float'
1789+
if np.issubdtype(dtype,np.signedinteger): return 'Int'
1790+
if np.issubdtype(dtype,np.unsignedinteger): return 'UInt'
1791+
if np.issubdtype(dtype,np.floating): return 'Float'
1792+
raise TypeError(f'invalid type "{dtype}"')
17891793

17901794

17911795
xdmf = ET.Element('Xdmf')

python/damask/util.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from . import version as _version
2424
from ._typehints import FloatSequence as _FloatSequence, NumpyRngSeed as _NumpyRngSeed, FileHandle as _FileHandle
2525

26+
2627
# https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py
2728
# https://stackoverflow.com/questions/287871
2829
_colors = {
@@ -323,8 +324,9 @@ def lcm(a,b):
323324

324325
max_denominator = int(10**(N_significant-1))
325326

326-
if (v_ := _np.asarray(v)).dtype in _np.sctypes['float']:
327-
v_ = _np.round(_np.asarray(v,'float64')/_np.max(_np.abs(v)),N_significant)
327+
v_ = _np.asarray(v)
328+
if _np.issubdtype(v_.dtype,_np.inexact):
329+
v_ = _np.round(_np.asarray(v,_np.float64)/_np.max(_np.abs(v)),N_significant)
328330
m = (v_ * _reduce(lcm, map(lambda x: int(get_square_denominator(x,max_denominator)),v_))**0.5).astype(_np.int64)
329331
m = m//_reduce(_np.gcd,m)
330332

python/tests/test_GeomGrid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def test_invalid_material_type(self):
8080
GeomGrid(np.zeros((3,3,3),dtype='complex'),np.ones(3))
8181

8282
def test_cast_to_int(self):
83-
g = GeomGrid(np.zeros((3,3,3)),np.ones(3))
84-
assert g.material.dtype in np.sctypes['int']
83+
g = GeomGrid(np.zeros((3,3,3),dtype=np.float64),np.ones(3))
84+
assert g.material.dtype in [np.int32,np.int64]
8585

8686
def test_invalid_size(self,default):
8787
with pytest.raises(ValueError):

python/tests/test_grid_filters.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ def test_div(self,field_def,div_def):
394394
def test_ravel_index(self):
395395
cells = np.random.randint(8,32,(3))
396396

397-
indices = np.block(np.meshgrid(np.arange(cells[0]),
398-
np.arange(cells[1]),
399-
np.arange(cells[2]),indexing='ij')).reshape(tuple(cells)+(3,),order='F')
397+
indices = np.block(list(np.meshgrid(np.arange(cells[0]),
398+
np.arange(cells[1]),
399+
np.arange(cells[2]),indexing='ij'))).reshape(tuple(cells)+(3,),order='F')
400400
x,y,z = map(np.random.randint,cells)
401401
assert grid_filters.ravel_index(indices)[x,y,z] == np.arange(0,np.prod(cells)).reshape(cells,order='F')[x,y,z]
402402

src/grid/VTI.f90

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ subroutine VTI_readCellsSizeOrigin(cells,geomSize,origin, &
186186

187187
end do outer
188188

189-
if (any(geomSize<=0)) call IO_error(error_ID = 844, ext_msg='size')
190-
if (any(cells<1)) call IO_error(error_ID = 844, ext_msg='cells')
189+
if (any(geomSize<=0)) call IO_error(error_ID = 844, ext_msg='size')
190+
if (any(cells<1)) call IO_error(error_ID = 844, ext_msg='cells')
191191

192192
end subroutine VTI_readCellsSizeOrigin
193193

0 commit comments

Comments
 (0)