Project-MONAI / MONAI

AI Toolkit for Healthcare Imaging
https://monai.io/
Apache License 2.0
5.79k stars 1.07k forks source link

Enumeration incorporation #484

Closed crtrentz closed 4 years ago

crtrentz commented 4 years ago

Co-authored-by: Benjamin Gorman benjamin-gorman@uiowa.edu

Is your feature request related to a problem? Please describe.

Iteration of [#403] [#407]

Describe the solution you'd like

Docstring, Enumeration, Type Hint

mode [1]
"""
mode: {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, ``"mean"``,
    ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
    One of the listed string values or a user supplied function. Defaults to ``FIXME``.
    See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
"""

class NumpyPadMode(Enum):
    """
    See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
    """

    CONSTANT = "constant"
    EDGE = "edge"
    LINEAR_RAMP = "linear_ramp"
    MAXIMUM = "maximum"
    MEAN = "mean"
    MEDIAN = "median"
    MINIMUM = "minimum"
    REFLECT = "reflect"
    SYMMETRIC = "symmetric"
    WRAP = "wrap"
    EMPTY = "empty"

Union[NumpyPadMode, str]
Optional[Union[NumpyPadMode, str]]
Union[Sequence[object], NumpyPadMode, str]
mode [2]
"""
mode: {``"bilinear"``, ``"nearest"``}
    Interpolation mode to calculate output values. Defaults to ``FIXME``.
    See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
"""

class GridSampleMode(Enum):
    """
    See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
    """

    BILINEAR = "bilinear"
    NEAREST = "nearest"

Union[GridSampleMode, str]
Optional[Union[GridSampleMode, str]]
Union[Sequence[object], GridSampleMode, str]
mode [3]
"""
mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``}
    The interpolation mode. Defaults to ``FIXME``.
    See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
"""

class InterpolateMode(Enum):
    """
    See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
    """

    NEAREST = "nearest"
    LINEAR = "linear"
    BILINEAR = "bilinear"
    BICUBIC = "bicubic"
    TRILINEAR = "trilinear"
    AREA = "area"

Union[InterpolateMode, str]
Optional[Union[InterpolateMode, str]]
Union[Sequence[object], InterpolateMode, str]
mode [4]
"""
mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``}
    If ends with ``"linear"`` will use ``spatial dims`` to determine the correct interpolation.
    This corresponds to linear, bilinear, trilinear for 1D, 2D, and 3D respectively.
    The interpolation mode. Defaults to ``FIXME``.
    See also: https://pytorch.org/docs/stable/nn.html#upsample
"""

class UpsampleMode(Enum):
    """
    See also: https://pytorch.org/docs/stable/nn.html#upsample
    """

    NEAREST = "nearest"
    LINEAR = "linear"
    BILINEAR = "bilinear"
    BICUBIC = "bicubic"
    TRILINEAR = "trilinear"

Union[UpsampleMode, str]
Optional[Union[UpsampleMode, str]]
Union[Sequence[object], UpsampleMode, str]
mode [5]
"""
mode: {``"constant"``, ``"gaussian"``}
    How to blend output of overlapping windows. Defaults to ``FIXME``.

    - ``"constant``": gives equal weight to all predictions.
    - ``"gaussian``": gives less weight to predictions on edges of windows.
"""

class BlendMode(Enum):
    """
    See also: :py:class:`monai.data.utils.compute_importance_map`
    """

    CONSTANT = "constant"
    GAUSSIAN = "gaussian"

Union[BlendMode, str]
Optional[Union[BlendMode, str]]
Union[Sequence[object], BlendMode, str]
padding_mode [6]
# the underlying function call has it named 'mode'
# only called in 'sliding_window_inference' where it conflicts with 'mode [5]'
# naming it 'mode' for this function would be misleading anyway
"""
padding_mode: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}
    Padding mode when ``roi_size`` is larger than inputs. Defaults to ``FIXME``
    See also: https://pytorch.org/docs/stable/nn.functional.html#pad
"""

class PytorchPadMode(Enum):
    """
    See also: https://pytorch.org/docs/stable/nn.functional.html#pad
    """

    CONSTANT = "constant"
    REFLECT = "reflect"
    REPLICATE = "replicate"
    CIRCULAR = "circular"

Union[PytorchPadMode, str]
Optional[Union[PytorchPadMode, str]]
Union[Sequence[object], PytorchPadMode, str]
padding_mode [7]
"""
padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
    Padding mode for outside grid values. Defaults to ``FIXME``.
    See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
"""

class GridSamplePadMode(Enum):
    """
    See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
    """

    ZEROS = "zeros"
    BORDER = "border"
    REFLECTION = "reflection"

Union[GridSamplePadMode, str]
Optional[Union[GridSamplePadMode, str]]
Union[Sequence[object], GridSamplePadMode, str]
average [8]
"""
average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``}
    Type of averaging performed if not binary classification. Defaults to ``FIXME``.

    - ``"macro"``: calculate metrics for each label, and find their unweighted mean.
        This does not take label imbalance into account.
    - ``"weighted"``: calculate metrics for each label, and find their average,
        weighted by support (the number of true instances for each label).
    - ``"micro"``: calculate metrics globally by considering each element of the label
        indicator matrix as a label.
    - ``"none"``: the scores for each class are returned.
"""

class Average(Enum):
    """
    See also: :py:class:`monai.metrics.rocauc.compute_roc_auc`
    """

    MACRO = "macro"
    WEIGHTED = "weighted"
    MICRO = "micro"
    NONE = "none"

Union[Average, str]
Optional[Union[Average, str]]
Union[Sequence[object], Average, str]
reduction [9]
"""
reduction: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, ``"mean_channel"``, ``"sum_channel"``}
    Define the mode to reduce computation result of 1 batch data. Defaults to ``FIXME``.
"""

class MetricReduction(Enum):
    """
    See also: :py:class:`monai.metrics.meandice.DiceMetric`
    """

    NONE = "none"
    MEAN = "mean"
    SUM = "sum"
    MEAN_BATCH = "mean_batch"
    SUM_BATCH = "sum_batch"
    MEAN_CHANNEL = "mean_channel"
    SUM_CHANNEL = "sum_channel"

Union[MetricReduction, str]
Optional[Union[MetricReduction, str]]
Union[Sequence[object], MetricReduction, str]
reduction [10]
"""
reduction: {``"none"``, ``"mean"``, ``"sum"``}
    Specifies the reduction to apply to the output. Defaults to ``FIXME``.

    - ``"none"``: no reduction will be applied.
    - ``"mean"``: the sum of the output will be divided by the number of elements in the output.
    - ``"sum"``: the output will be summed.
"""

class LossReduction(Enum):
    """
    See also:
        - :py:class:`monai.losses.dice.DiceLoss`
        - :py:class:`monai.losses.dice.GeneralizedDiceLoss`
        - :py:class:`monai.losses.focal_loss.FocalLoss`
        - :py:class:`monai.losses.tversky.TverskyLoss`
    """

    NONE = "none"
    MEAN = "mean"
    SUM = "sum"

Union[LossReduction, str]
Optional[Union[LossReduction, str]]
Union[Sequence[object], LossReduction, str]
w_type [11]
"""
w_type: {``"square"``, ``"simple"``, ``"uniform"``}
    Type of function to transform ground truth volume to a weight factor. Defaults to ``FIXME``.
"""

class Weight(Enum):
    """
    See also: :py:class:`monai.losses.dice.GeneralizedDiceLoss`
    """

    SQUARE = "square"
    SIMPLE = "simple"
    UNIFORM = "uniform"

Union[Weight, str]
Optional[Union[Weight, str]]
Union[Sequence[object], Weight, str]
norm_type [12]
"""
norm_type: {``"batch"``, ``"instance"``}
    Feature normalisation with batchnorm or instancenorm. Defaults to ``FIXME``.
"""

# other 'norm_type' are using 'monai.networks.layers.factories.Norm'
class Normalisation(Enum):
    """
    See also:
        - :py:class:`monai.networks.nets.ConvNormActi`
        - :py:class:`monai.networks.nets.HighResBlock`
        - :py:class:`monai.networks.nets.HighResNet`
    """

    BATCH = "batch"
    INSTANCE = "instance"

Union[Normalisation, str]
Optional[Union[Normalisation, str]]
Union[Sequence[object], Normalisation, str]
acti_type [13]
"""
acti_type: {``"relu"``, ``"prelu"``, ``"relu6"``}
    Non-linear activation using ReLU or PReLU. Defaults to ``FIXME``.
"""

# other 'acti_type' are using 'monai.networks.layers.factories.Act'
class Activation(Enum):
    """
    See also:
        - :py:class:`monai.networks.nets.ConvNormActi`
        - :py:class:`monai.networks.nets.HighResBlock`
        - :py:class:`monai.networks.nets.HighResNet`
    """

    RELU = "relu"
    PRELU = "prelu"
    RELU6 = "relu6"

Union[Activation, str]
Optional[Union[Activation, str]]
Union[Sequence[object], Activation, str]
channel_matching [14]
"""
channel_matching: {``"pad"``, ``"project"``}
    Specifies handling residual branch and conv branch channel mismatches. Defaults to ``FIXME``.

    - ``"pad"``: with zero padding.
    - ``"project"``: with a trainable conv with kernel size.
"""

class ChannelMatching(Enum):
    """
    See also: :py:class:`monai.networks.nets.HighResBlock`
    """

    PAD = "pad"
    PROJECT = "project"

Union[ChannelMatching, str]
Optional[Union[ChannelMatching, str]]
Union[Sequence[object], ChannelMatching, str]
method [15]
"""
method: {``"symmetric"``, ``"end"``}
    Pad image symmetric on every side or only pad at the end sides. Defaults to ``FIXME``.
"""

class Method(Enum):
    """
    See also: :py:class:`monai.transforms.croppad.array.SpatialPad`
    """

    SYMMETRIC = "symmetric"
    END = "end"

Union[Method, str]
Optional[Union[Method, str]]
Union[Sequence[object], Method, str]

Describe alternatives/additions you've considered

crtrentz commented 4 years ago

@benjamin-gorman @hjmjohnson

hjmjohnson commented 4 years ago

@crtrentz @benjamin-gorman Very nice. Thank you for identifying the other issues related to this.

The next step would be to write unit tests related to the way these will be used BEFORE modifying any of the main source code.

wyli commented 4 years ago

thanks, the InterpolationOrder class with BI_* seems to suggest spatially 2D? I feel using SPLINE0 - SPLINE5 is more appropriate here. otherwise they look good.

ghost commented 4 years ago

thanks, the InterpolationOrder class with BI_* seems to suggest spatially 2D? I feel using SPLINE0 - SPLINE5 is more appropriate here. otherwise they look good.

We found those on skimage.transform.warp after being redirected from skimage.transform.resize.

wyli commented 4 years ago

thanks, the InterpolationOrder class with BI_* seems to suggest spatially 2D? I feel using SPLINE0 - SPLINE5 is more appropriate here. otherwise they look good.

We found those on skimage.transform.warp after being redirected from skimage.transform.resize.

still not sure if it is correct... for your first link, a few lines after the interpolation order the output shape docstring also suggests 2D:

output_shape tuple (rows, cols), optional Shape of the output image generated. By default the shape of the input image is preserved. Note that, even for multi-band images, only rows and columns need to be specified."

but for the second link, it is ND:

Size of the generated output image (rows, cols[, …][, dim]). If dim is not provided, the number of channels is preserved. In case the number of input channels does not equal the number of output channels a n-dimensional interpolation is applied.

had a quick look at the source code of these, in the end they call scipy.ndimage.map_coordinates