Skip to content

Commit

Permalink
Merge pull request #122 from amoodie/from_array
Browse files Browse the repository at this point in the history
Implement array initiation for masks
  • Loading branch information
Andrew Moodie authored Oct 12, 2022
2 parents c2d59d6 + 19c2d7f commit 552ff6f
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.7, 3.8, 3.9]
python-version: ['3.8', '3.9', '3.10']
env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python-version }}
Expand Down
118 changes: 88 additions & 30 deletions deltametrics/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,28 +278,10 @@ class ThresholdValueMask(BaseMask, abc.ABC):
This mask implements a binarization of a raster based on a threshold
values.
"""
@staticmethod
def from_array(_arr):
"""Create an `ElevationMask` from an array.
.. note::
Instantiation with `from_array` will attempt to any data type
(`dtype`) to boolean. This may have unexpected results. Convert
your array to a boolean before using `from_array` to ensure the
mask is created correctly.
Parameters
----------
_arr : :obj:`ndarray`
The array with values to set as the mask. Can be any `dtype` but
will be coerced to `boolean`.
"""
# set directly
raise NotImplementedError

def __init__(self, *args, threshold, cube_key=None, **kwargs):

# super().__init__('threshold', *args, **kwargs)

self._threshold = threshold

# temporary storage of args as needed for processing
Expand Down Expand Up @@ -356,17 +338,45 @@ class ElevationMask(ThresholdValueMask):
plt.show()
"""

@staticmethod
def from_array(_arr):
"""Create an `ElevationMask` from an array.
.. note::
Instantiation with `from_array` will attempt to any data type
(`dtype`) to boolean. This may have unexpected results. Convert
your array to a boolean before using `from_array` to ensure the
mask is created correctly.
Parameters
----------
_arr : :obj:`ndarray`
The array with values to set as the mask. Can be any `dtype` but
will be coerced to `boolean`.
"""
# set directly
_EM = ElevationMask(allow_empty=True, elevation_threshold=None)
_EM._set_shape_mask(_arr)
_EM._input_elevation_threshold = None
_EM._elevation_offset = None
_EM._input_flag = None
_EM._mask[:] = _arr.astype(bool) # set the array as mask
return _EM

def __init__(self, *args, elevation_threshold, elevation_offset=0,
cube_key='eta', **kwargs):
"""Initialize the ElevationMask.
.. note:: Needs docstring!
"""

self._input_elevation_threshold = elevation_threshold
self._elevation_offset = elevation_offset
_threshold = elevation_threshold + elevation_offset
if elevation_threshold is None:
_threshold = None
else:
_threshold = elevation_threshold + elevation_offset

BaseMask.__init__(self, 'elevation', *args, **kwargs)
ThresholdValueMask.__init__(self, *args, threshold=_threshold,
Expand Down Expand Up @@ -426,6 +436,30 @@ class FlowMask(ThresholdValueMask):
plt.show()
"""

@staticmethod
def from_array(_arr):
"""Create an `ElevationMask` from an array.
.. note::
Instantiation with `from_array` will attempt to any data type
(`dtype`) to boolean. This may have unexpected results. Convert
your array to a boolean before using `from_array` to ensure the
mask is created correctly.
Parameters
----------
_arr : :obj:`ndarray`
The array with values to set as the mask. Can be any `dtype` but
will be coerced to `boolean`.
"""
# set directly
_FM = FlowMask(allow_empty=True, flow_threshold=None)
_FM._set_shape_mask(_arr)
_FM._input_flag = None
_FM._mask[:] = _arr.astype(bool) # set the array as mask
return _FM

def __init__(self, *args, flow_threshold, cube_key='velocity', **kwargs):
"""Initialize the FlowMask.
Expand Down Expand Up @@ -590,7 +624,7 @@ def from_array(_arr):
_CM = ChannelMask(allow_empty=True)
_CM._set_shape_mask(_arr)
_CM._input_flag = None
_CM._mask = _arr.astype(bool) # set the array as mask
_CM._mask[:] = _arr.astype(bool) # set the array as mask
return _CM

def __init__(self, *args, is_mask=None, **kwargs):
Expand Down Expand Up @@ -813,7 +847,11 @@ def from_array(_arr):
will be coerced to `boolean`.
"""
# set directly
raise NotImplementedError
_WM = WetMask(allow_empty=True)
_WM._set_shape_mask(_arr)
_WM._input_flag = None
_WM._mask[:] = _arr.astype(bool) # set the array as mask
return _WM

def __init__(self, *args, **kwargs):
"""Initialize the WetMask.
Expand Down Expand Up @@ -1037,7 +1075,11 @@ def from_array(_arr):
will be coerced to `boolean`.
"""
# set directly
raise NotImplementedError
_LM = LandMask(allow_empty=True)
_LM._set_shape_mask(_arr)
_LM._input_flag = None
_LM._mask[:] = _arr.astype(bool) # set the array as mask
return _LM

def __init__(self, *args, contour_threshold=75,
method='OAM', **kwargs):
Expand Down Expand Up @@ -1252,7 +1294,7 @@ def from_array(_arr):
_SM._set_shape_mask(_arr)
_SM._contour_threshold = None
_SM._input_flag = None
_SM._mask = _arr.astype(bool) # set the array as mask
_SM._mask[:] = _arr.astype(bool) # set the array as mask
return _SM

def __init__(self, *args, contour_threshold=75, method='OAM', **kwargs):
Expand Down Expand Up @@ -1619,7 +1661,11 @@ def from_array(_arr):
The array with values to set as the mask. Can be any `dtype` but
will be coerced to `boolean`.
"""
raise NotImplementedError
_EM = EdgeMask(allow_empty=True)
_EM._set_shape_mask(_arr)
_EM._input_flag = None
_EM._mask[:] = _arr.astype(bool) # set the array as mask
return _EM

def __init__(self, *args, **kwargs):
"""Initialize the EdgeMask.
Expand Down Expand Up @@ -1856,7 +1902,11 @@ def from_array(_arr):
will be coerced to `boolean`.
"""
# set directly
raise NotImplementedError
_CM = CenterlineMask(allow_empty=True)
_CM._set_shape_mask(_arr)
_CM._input_flag = None
_CM._mask[:] = _arr.astype(bool) # set the array as mask
return _CM

def __init__(self, *args, method='skeletonize', **kwargs):
"""Initialize the CenterlineMask.
Expand Down Expand Up @@ -2075,7 +2125,11 @@ def from_array(_arr):
will be coerced to `boolean`.
"""
# set directly
raise NotImplementedError
_GM = GeometricMask(allow_empty=True)
_GM._set_shape_mask(_arr)
_GM._input_flag = None
_GM._mask[:] = _arr.astype(bool) # set the array as mask
return _GM

def __init__(self, *args, origin=None, **kwargs):
"""Initialize the GeometricMask.
Expand Down Expand Up @@ -2413,7 +2467,11 @@ def from_array(_arr):
will be coerced to `boolean`.
"""
# set directly
raise NotImplementedError
_DM = DepositMask(allow_empty=True)
_DM._set_shape_mask(_arr)
_DM._input_flag = None
_DM._mask[:] = _arr.astype(bool) # set the array as mask
return _DM

def __init__(self, *args, background_value=0,
elevation_tolerance=0.1, **kwargs):
Expand Down
Loading

0 comments on commit 552ff6f

Please sign in to comment.