From d7ba85385978ba652dc144e92eee38f32d50d2ed Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 5 Mar 2022 20:37:47 +0100 Subject: [PATCH] use None for no selection, empty list is 'nothing' --- python/damask/_grid.py | 4 ++-- python/damask/seeds.py | 2 +- python/damask/util.py | 2 +- python/tests/test_Grid.py | 11 ++++++++++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/python/damask/_grid.py b/python/damask/_grid.py index 0eb376e11..61b64e018 100644 --- a/python/damask/_grid.py +++ b/python/damask/_grid.py @@ -958,7 +958,7 @@ class Grid: """ def mostFrequent(arr: np.ndarray, selection: List, invert: bool): me = arr[arr.size//2] - if len(selection) == 0 or np.isin(me,selection,invert=invert): + if selection is None or np.isin(me,selection,invert=invert): unique, inverse = np.unique(arr, return_inverse=True) return unique[np.argmax(np.bincount(inverse))] else: @@ -1179,7 +1179,7 @@ class Grid: """ def tainted_neighborhood(stencil: np.ndarray, selection): me = stencil[stencil.shape[0]//2] - return np.any(stencil != me if len(selection) == 0 else + return np.any(stencil != me if selection is None else np.in1d(stencil,np.array(list(set(selection) - {me})))) offset_ = np.nanmax(self.material)+1 if offset is None else offset diff --git a/python/damask/seeds.py b/python/damask/seeds.py index d5e1ec7ed..8a61bb040 100644 --- a/python/damask/seeds.py +++ b/python/damask/seeds.py @@ -135,7 +135,7 @@ def from_grid(grid, """ material = grid.material.reshape((-1,1),order='F') selection_ = _util.tbd(selection) - mask = _np.full(grid.cells.prod(),True,dtype=bool) if len(selection_) == 0 else \ + mask = _np.full(grid.cells.prod(),True,dtype=bool) if selection_ is None else \ _np.isin(material,selection_,invert=invert_selection).flatten() coords = _grid_filters.coordinates0_point(grid.cells,grid.size).reshape(-1,3,order='F') diff --git a/python/damask/util.py b/python/damask/util.py index f19b6a54a..1588906a2 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -754,7 +754,7 @@ def tail_repack(extended: Union[str, Sequence[str]], def tbd(arg) -> List: if arg is None: - return [] + return None elif isinstance(arg,(np.ndarray,Collection)): return list(arg) else: diff --git a/python/tests/test_Grid.py b/python/tests/test_Grid.py index 6be2bc18a..a66b47719 100644 --- a/python/tests/test_Grid.py +++ b/python/tests/test_Grid.py @@ -182,6 +182,11 @@ class TestGrid: assert random.clean(selection=selection,invert_selection=invert) == \ random.clean(selection=selection_inverse,invert_selection=not invert) + def test_clean_selection_empty(self,random): + assert random.clean(selection=[],invert_selection=False) == random and \ + random.clean(selection=[],invert_selection=True) == random.clean() + + @pytest.mark.parametrize('cells',[ (10,11,10), [10,13,10], @@ -319,7 +324,7 @@ class TestGrid: assert grid_equal(G_1,G_2) - @pytest.mark.parametrize('selection',[1,[]]) + @pytest.mark.parametrize('selection',[1,None]) def test_vicinity_offset(self,selection): offset = np.random.randint(2,4) vicinity = np.random.randint(2,4) @@ -346,6 +351,10 @@ class TestGrid: assert random.vicinity_offset(selection=selection,invert_selection=invert) == \ random.vicinity_offset(selection=selection_inverse,invert_selection=not invert) + def test_vicinity_offset_selection_empty(self,random): + assert random.vicinity_offset(selection=[],invert_selection=False) == random and \ + random.vicinity_offset(selection=[],invert_selection=True) == random.vicinity_offset() + @pytest.mark.parametrize('periodic',[True,False]) def test_vicinity_offset_invariant(self,default,periodic):