use None for no selection, empty list is 'nothing'

This commit is contained in:
Martin Diehl 2022-03-05 20:37:47 +01:00
parent bafc45d259
commit d7ba853859
4 changed files with 14 additions and 5 deletions

View File

@ -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

View File

@ -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')

View File

@ -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:

View File

@ -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):