Skip to content

Commit 4bd82b2

Browse files
committedFeb 27, 2025·
Merge branch '471-typehints-for-pandas-and-scipy' into 'development'
Resolve "Typehints for pandas and scipy" Closes #471 See merge request damask/DAMASK!1030
2 parents 7b26696 + 695e113 commit 4bd82b2

9 files changed

+24
-24
lines changed
 

‎.gitlab-ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ create_testroot:
7171

7272
.python_default:
7373
extends: .python_base
74-
variables: {PYTHON_IMAGE: 2025.01.05}
74+
variables: {PYTHON_IMAGE: 2025.02.25}
7575

7676
.python_min:
7777
extends: .python_base

‎PRIVATE

‎python/damask/_colormap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def at(self,
301301
return interp.interp1d(np.linspace(0,1,self.N),
302302
self.colors,
303303
axis=0,
304-
assume_sorted=True)(fraction)
304+
assume_sorted=True)(np.asarray(fraction))
305305

306306

307307
def shade(self,

‎python/damask/_geomgrid.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,7 @@ def from_Voronoi_tessellation(cells: IntSequence,
564564
565565
"""
566566
coords = grid_filters.coordinates0_point(cells,size).reshape(-1,3)
567-
tree = spatial.KDTree(seeds,boxsize=size) if periodic else \
568-
spatial.KDTree(seeds)
567+
tree = spatial.KDTree(seeds,boxsize=np.asarray(size) if periodic else None)
569568
material_ = tree.query(coords, workers = int(os.environ.get('OMP_NUM_THREADS',4)))[1]
570569

571570
return GeomGrid(material = (material_ if material is None else np.array(material)[material_]).reshape(cells),

‎python/damask/_table.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def __getitem__(self,
9797
1 3 4
9898
2 6 7
9999
3 9 10
100+
>>> tbl[3]
101+
colA colB colC
102+
3 9 10 11
100103
>>> tbl[::2,['colB','colA']]
101104
colB colA
102105
0 1 0
@@ -109,8 +112,9 @@ def __getitem__(self,
109112
"""
110113
item_ = (item,slice(None,None,None)) if isinstance(item,(slice,np.ndarray)) else \
111114
(np.array(item),slice(None,None,None)) if isinstance(item,list) and np.array(item).dtype == np.bool_ else \
115+
(slice(item,item),slice(None,None,None)) if isinstance(item,int) else \
112116
(np.array(item[0]),item[1]) if isinstance(item[0],list) else \
113-
item if isinstance(item[0],(slice,np.ndarray)) else \
117+
(item[0], item[1]) if isinstance(item[0],(slice,np.ndarray)) else \
114118
(slice(None,None,None),item)
115119
sliced = self.data.loc[item_]
116120
cols = np.array(sliced.columns if isinstance(sliced,pd.core.frame.DataFrame) else [item_[1]])
@@ -423,7 +427,7 @@ def add_comment(label: str, shape: Tuple[int, ...],info: str) -> List[str]:
423427
idx = np.ravel_multi_index(tuple(map(int,m.group(2).split(","))),
424428
self.shapes[key])
425429
iloc = dup.data.columns.get_loc(key).tolist().index(True) + idx # type: ignore
426-
dup.data.iloc[:,iloc] = data
430+
dup.data.iloc[:,iloc] = data # type: ignore
427431
else:
428432
dup.data[label] = data.reshape(dup.data[label].shape)
429433

‎python/damask/grid_filters.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
"""
1414

15-
from typing import Tuple as _Tuple
15+
from typing import Tuple as _Tuple, Union as _Union
1616

1717
from scipy import spatial as _spatial
1818
import numpy as _np
@@ -665,7 +665,7 @@ def unravel(d_raveled: _np.ndarray,
665665

666666
def regrid(size: _FloatSequence,
667667
F: _np.ndarray,
668-
cells: _IntSequence) -> _np.ndarray:
668+
cells: _IntSequence) -> _Union[_np.intp, _np.ndarray]:
669669
"""
670670
Map a deformed grid A back to a rectilinear grid B.
671671

‎python/damask/seeds.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def from_random(size: _FloatSequence,
3737
Seed coordinates in 3D space.
3838
3939
"""
40-
size_ = _np.array(size,float)
40+
size_ = _np.asarray(size,float)
4141
rng = _np.random.default_rng(rng_seed)
4242
if cells is None:
4343
coords = rng.random((N_seeds,3)) * size_
4444
else:
4545
grid_coords = _grid_filters.coordinates0_point(cells,size).reshape(-1,3,order='F')
46-
coords = grid_coords[rng.choice(_np.prod(cells),N_seeds, replace=False)] \
46+
coords = grid_coords[rng.choice(_np.prod(cells),N_seeds,replace=False)] \
4747
+ _np.broadcast_to(size_/_np.array(cells,_np.int64),(N_seeds,3))*(rng.random((N_seeds,3))*.5-.25) # wobble w/o leaving grid
4848

4949
return coords
@@ -81,24 +81,23 @@ def from_Poisson_disc(size: _FloatSequence,
8181
Seed coordinates in 3D space.
8282
8383
"""
84+
size_ = _np.asarray(size,float)
8485
rng = _np.random.default_rng(rng_seed)
8586
coords = _np.empty((N_seeds,3))
86-
coords[0] = rng.random(3) * _np.array(size,float)
8787

88-
s = 1
88+
s = 0
8989
i = 0
90-
progress = _util.ProgressBar(N_seeds+1,'',50)
90+
progress = _util.ProgressBar(N_seeds,'',50)
9191
while s < N_seeds:
9292
i += 1
93-
candidates = rng.random((N_candidates,3))*_np.broadcast_to(size,(N_candidates,3))
94-
tree = _spatial.cKDTree(coords[:s],boxsize=size) if periodic else \
95-
_spatial.cKDTree(coords[:s])
96-
distances = tree.query(candidates)[0]
93+
candidates = rng.random((N_candidates,3))*_np.broadcast_to(size_,(N_candidates,3))
94+
tree = _spatial.cKDTree(coords[:s],boxsize=size_ if periodic else None)
95+
distances = _np.asarray(tree.query(candidates)[0])
9796
if distances.max() > distance: # require minimum separation
98-
i = 0
9997
coords[s] = candidates[distances.argmax()] # maximum separation to existing point cloud
100-
s += 1
10198
progress.update(s)
99+
s += 1
100+
i = 0
102101

103102
if i >= 100:
104103
raise ValueError('seeding not possible')

‎python/mypy.ini

-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
warn_redundant_casts = True
33
plugins = numpy.typing.mypy_plugin
44

5-
[mypy-scipy.*]
6-
ignore_missing_imports = True
75
[mypy-h5py.*]
86
ignore_missing_imports = True
97
[mypy-vtkmodules.*]
@@ -12,7 +10,5 @@ ignore_missing_imports = True
1210
ignore_missing_imports = True
1311
[mypy-matplotlib.*]
1412
ignore_missing_imports = True
15-
[mypy-pandas.*]
16-
ignore_missing_imports = True
1713
[mypy-wx.*]
1814
ignore_missing_imports = True

‎python/tests/test_Table.py

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def test_allclose(self,default):
8484
@pytest.mark.parametrize('N',[1,3,4])
8585
def test_slice(self,np_rng,default,N):
8686
mask = np_rng.choice([True,False],len(default))
87+
assert len(default[N]) == 1
88+
assert len(default[1:N]) == N
8789
assert len(default[:N]) == 1+N
8890
assert len(default[:N,['F','s']]) == 1+N
8991
assert len(default[mask,['F','s']]) == np.count_nonzero(mask)

0 commit comments

Comments
 (0)
Please sign in to comment.