File size: 19,674 Bytes
853e22b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 |
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import random
from enum import Enum
from typing import TYPE_CHECKING
from monai.config import IgniteInfo
from monai.utils import deprecated
from monai.utils.module import min_version, optional_import
__all__ = [
"StrEnum",
"NumpyPadMode",
"GridSampleMode",
"SplineMode",
"InterpolateMode",
"UpsampleMode",
"BlendMode",
"PytorchPadMode",
"NdimageMode",
"GridSamplePadMode",
"Average",
"MetricReduction",
"LossReduction",
"DiceCEReduction",
"Weight",
"ChannelMatching",
"SkipMode",
"Method",
"TraceKeys",
"TraceStatusKeys",
"CommonKeys",
"GanKeys",
"PostFix",
"ForwardMode",
"TransformBackends",
"CompInitMode",
"BoxModeName",
"GridPatchSort",
"FastMRIKeys",
"SpaceKeys",
"MetaKeys",
"ColorOrder",
"EngineStatsKeys",
"DataStatsKeys",
"ImageStatsKeys",
"LabelStatsKeys",
"AlgoEnsembleKeys",
"HoVerNetMode",
"HoVerNetBranch",
"LazyAttr",
"BundleProperty",
"BundlePropertyConfig",
"AlgoKeys",
]
class StrEnum(str, Enum):
"""
Enum subclass that converts its value to a string.
.. code-block:: python
from monai.utils import StrEnum
class Example(StrEnum):
MODE_A = "A"
MODE_B = "B"
assert (list(Example) == ["A", "B"])
assert Example.MODE_A == "A"
assert str(Example.MODE_A) == "A"
assert monai.utils.look_up_option("A", Example) == "A"
"""
def __str__(self):
return self.value
def __repr__(self):
return self.value
if TYPE_CHECKING:
from ignite.engine import EventEnum
else:
EventEnum, _ = optional_import(
"ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "EventEnum", as_type="base"
)
class NumpyPadMode(StrEnum):
"""
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"
class NdimageMode(StrEnum):
"""
The available options determine how the input array is extended beyond its boundaries when interpolating.
See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
"""
REFLECT = "reflect"
GRID_MIRROR = "grid-mirror"
CONSTANT = "constant"
GRID_CONSTANT = "grid-constant"
NEAREST = "nearest"
MIRROR = "mirror"
GRID_WRAP = "grid-wrap"
WRAP = "wrap"
class GridSampleMode(StrEnum):
"""
See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
interpolation mode of `torch.nn.functional.grid_sample`
Note:
(documentation from `torch.nn.functional.grid_sample`)
`mode='bicubic'` supports only 4-D input.
When `mode='bilinear'` and the input is 5-D, the interpolation mode used internally will actually be trilinear.
However, when the input is 4-D, the interpolation mode will legitimately be bilinear.
"""
NEAREST = "nearest"
BILINEAR = "bilinear"
BICUBIC = "bicubic"
class SplineMode(StrEnum):
"""
Order of spline interpolation.
See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
"""
ZERO = 0
ONE = 1
TWO = 2
THREE = 3
FOUR = 4
FIVE = 5
class InterpolateMode(StrEnum):
"""
See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
"""
NEAREST = "nearest"
NEAREST_EXACT = "nearest-exact"
LINEAR = "linear"
BILINEAR = "bilinear"
BICUBIC = "bicubic"
TRILINEAR = "trilinear"
AREA = "area"
class UpsampleMode(StrEnum):
"""
See also: :py:class:`monai.networks.blocks.UpSample`
"""
DECONV = "deconv"
DECONVGROUP = "deconvgroup"
NONTRAINABLE = "nontrainable" # e.g. using torch.nn.Upsample
PIXELSHUFFLE = "pixelshuffle"
class BlendMode(StrEnum):
"""
See also: :py:class:`monai.data.utils.compute_importance_map`
"""
CONSTANT = "constant"
GAUSSIAN = "gaussian"
class PytorchPadMode(StrEnum):
"""
See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
"""
CONSTANT = "constant"
REFLECT = "reflect"
REPLICATE = "replicate"
CIRCULAR = "circular"
class GridSamplePadMode(StrEnum):
"""
See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
"""
ZEROS = "zeros"
BORDER = "border"
REFLECTION = "reflection"
class Average(StrEnum):
"""
See also: :py:class:`monai.metrics.rocauc.compute_roc_auc`
"""
MACRO = "macro"
WEIGHTED = "weighted"
MICRO = "micro"
NONE = "none"
class MetricReduction(StrEnum):
"""
See also: :py:func:`monai.metrics.utils.do_metric_reduction`
"""
NONE = "none"
MEAN = "mean"
SUM = "sum"
MEAN_BATCH = "mean_batch"
SUM_BATCH = "sum_batch"
MEAN_CHANNEL = "mean_channel"
SUM_CHANNEL = "sum_channel"
class LossReduction(StrEnum):
"""
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"
class DiceCEReduction(StrEnum):
"""
See also:
- :py:class:`monai.losses.dice.DiceCELoss`
"""
MEAN = "mean"
SUM = "sum"
class Weight(StrEnum):
"""
See also: :py:class:`monai.losses.dice.GeneralizedDiceLoss`
"""
SQUARE = "square"
SIMPLE = "simple"
UNIFORM = "uniform"
class ChannelMatching(StrEnum):
"""
See also: :py:class:`monai.networks.nets.HighResBlock`
"""
PAD = "pad"
PROJECT = "project"
class SkipMode(StrEnum):
"""
See also: :py:class:`monai.networks.layers.SkipConnection`
"""
CAT = "cat"
ADD = "add"
MUL = "mul"
class Method(StrEnum):
"""
See also: :py:class:`monai.transforms.croppad.array.SpatialPad`
"""
SYMMETRIC = "symmetric"
END = "end"
class ForwardMode(StrEnum):
"""
See also: :py:class:`monai.transforms.engines.evaluator.Evaluator`
"""
TRAIN = "train"
EVAL = "eval"
class TraceKeys(StrEnum):
"""Extra metadata keys used for traceable transforms."""
CLASS_NAME: str = "class"
ID: str = "id"
ORIG_SIZE: str = "orig_size"
EXTRA_INFO: str = "extra_info"
DO_TRANSFORM: str = "do_transforms"
KEY_SUFFIX: str = "_transforms"
NONE: str = "none"
TRACING: str = "tracing"
STATUSES: str = "statuses"
LAZY: str = "lazy"
class TraceStatusKeys(StrEnum):
"""Enumerable status keys for the TraceKeys.STATUS flag"""
PENDING_DURING_APPLY = "pending_during_apply"
class CommonKeys(StrEnum):
"""
A set of common keys for dictionary based supervised training process.
`IMAGE` is the input image data.
`LABEL` is the training or evaluation label of segmentation or classification task.
`PRED` is the prediction data of model output.
`LOSS` is the loss value of current iteration.
`INFO` is some useful information during training or evaluation, like loss value, etc.
"""
IMAGE = "image"
LABEL = "label"
PRED = "pred"
LOSS = "loss"
METADATA = "metadata"
class GanKeys(StrEnum):
"""
A set of common keys for generative adversarial networks.
"""
REALS = "reals"
FAKES = "fakes"
LATENTS = "latents"
GLOSS = "g_loss"
DLOSS = "d_loss"
class PostFix(StrEnum):
"""Post-fixes."""
@staticmethod
def _get_str(prefix: str | None, suffix: str) -> str:
return suffix if prefix is None else f"{prefix}_{suffix}"
@staticmethod
def meta(key: str | None = None) -> str:
return PostFix._get_str(key, "meta_dict")
@staticmethod
def orig_meta(key: str | None = None) -> str:
return PostFix._get_str(key, "orig_meta_dict")
@staticmethod
def transforms(key: str | None = None) -> str:
return PostFix._get_str(key, TraceKeys.KEY_SUFFIX[1:])
class TransformBackends(StrEnum):
"""
Transform backends. Most of `monai.transforms` components first converts the input data into ``torch.Tensor`` or
``monai.data.MetaTensor``. Internally, some transforms are made by converting the data into ``numpy.array`` or
``cupy.array`` and use the underlying transform backend API to achieve the actual output array and
converting back to ``Tensor``/``MetaTensor``. Transforms with more than one backend indicate the that they may
convert the input data types to accommodate the underlying API.
"""
TORCH = "torch"
NUMPY = "numpy"
CUPY = "cupy"
class CompInitMode(StrEnum):
"""
Mode names for instantiating a class or calling a callable.
See also: :py:func:`monai.utils.module.instantiate`
"""
DEFAULT = "default"
CALLABLE = "callable"
DEBUG = "debug"
class JITMetadataKeys(StrEnum):
"""
Keys stored in the metadata file for saved Torchscript models. Some of these are generated by the routines
and others are optionally provided by users.
"""
NAME = "name"
TIMESTAMP = "timestamp"
VERSION = "version"
DESCRIPTION = "description"
class BoxModeName(StrEnum):
"""
Box mode names.
"""
XYXY = "xyxy" # [xmin, ymin, xmax, ymax]
XYZXYZ = "xyzxyz" # [xmin, ymin, zmin, xmax, ymax, zmax]
XXYY = "xxyy" # [xmin, xmax, ymin, ymax]
XXYYZZ = "xxyyzz" # [xmin, xmax, ymin, ymax, zmin, zmax]
XYXYZZ = "xyxyzz" # [xmin, ymin, xmax, ymax, zmin, zmax]
XYWH = "xywh" # [xmin, ymin, xsize, ysize]
XYZWHD = "xyzwhd" # [xmin, ymin, zmin, xsize, ysize, zsize]
CCWH = "ccwh" # [xcenter, ycenter, xsize, ysize]
CCCWHD = "cccwhd" # [xcenter, ycenter, zcenter, xsize, ysize, zsize]
class ProbMapKeys(StrEnum):
"""
The keys to be used for generating the probability maps from patches
"""
LOCATION = "mask_location"
SIZE = "mask_size"
COUNT = "num_patches"
NAME = "name"
class GridPatchSort(StrEnum):
"""
The sorting method for the generated patches in `GridPatch`
"""
RANDOM = "random"
MIN = "min"
MAX = "max"
@staticmethod
def min_fn(x):
return x[0].sum()
@staticmethod
def max_fn(x):
return -x[0].sum()
@staticmethod
def get_sort_fn(sort_fn):
if sort_fn == GridPatchSort.RANDOM:
return random.random
elif sort_fn == GridPatchSort.MIN:
return GridPatchSort.min_fn
elif sort_fn == GridPatchSort.MAX:
return GridPatchSort.max_fn
else:
raise ValueError(
f'sort_fn should be one of the following values, "{sort_fn}" was given:',
[e.value for e in GridPatchSort],
)
class PatchKeys(StrEnum):
"""
The keys to be used for metadata of patches extracted from any kind of image
"""
LOCATION = "location"
SIZE = "size"
COUNT = "count"
class WSIPatchKeys(StrEnum):
"""
The keys to be used for metadata of patches extracted from whole slide images
"""
LOCATION = PatchKeys.LOCATION
SIZE = PatchKeys.SIZE
COUNT = PatchKeys.COUNT
LEVEL = "level"
PATH = "path"
class FastMRIKeys(StrEnum):
"""
The keys to be used for extracting data from the fastMRI dataset
"""
KSPACE = "kspace"
MASK = "mask"
FILENAME = "filename"
RECON = "reconstruction_rss"
ACQUISITION = "acquisition"
MAX = "max"
NORM = "norm"
PID = "patient_id"
class SpaceKeys(StrEnum):
"""
The coordinate system keys, for example, Nifti1 uses Right-Anterior-Superior or "RAS",
DICOM (0020,0032) uses Left-Posterior-Superior or "LPS". This type does not distinguish spatial 1/2/3D.
"""
RAS = "RAS"
LPS = "LPS"
class MetaKeys(StrEnum):
"""
Typical keys for MetaObj.meta
"""
AFFINE = "affine" # MetaTensor.affine
ORIGINAL_AFFINE = "original_affine" # the affine after image loading before any data processing
SPATIAL_SHAPE = "spatial_shape" # optional key for the length in each spatial dimension
SPACE = "space" # possible values of space type are defined in `SpaceKeys`
ORIGINAL_CHANNEL_DIM = "original_channel_dim" # an integer or float("nan")
class ColorOrder(StrEnum):
"""
Enums for color order. Expand as necessary.
"""
RGB = "RGB"
BGR = "BGR"
class EngineStatsKeys(StrEnum):
"""
Default keys for the statistics of trainer and evaluator engines.
"""
RANK = "rank"
CURRENT_ITERATION = "current_iteration"
CURRENT_EPOCH = "current_epoch"
TOTAL_EPOCHS = "total_epochs"
TOTAL_ITERATIONS = "total_iterations"
BEST_VALIDATION_EPOCH = "best_validation_epoch"
BEST_VALIDATION_METRIC = "best_validation_metric"
class DataStatsKeys(StrEnum):
"""
Defaults keys for dataset statistical analysis modules
"""
SUMMARY = "stats_summary"
BY_CASE = "stats_by_cases"
BY_CASE_IMAGE_PATH = "image_filepath"
BY_CASE_LABEL_PATH = "label_filepath"
IMAGE_STATS = "image_stats"
FG_IMAGE_STATS = "image_foreground_stats"
LABEL_STATS = "label_stats"
IMAGE_HISTOGRAM = "image_histogram"
class ImageStatsKeys(StrEnum):
"""
Defaults keys for dataset statistical analysis image modules
"""
SHAPE = "shape"
CHANNELS = "channels"
CROPPED_SHAPE = "cropped_shape"
SPACING = "spacing"
SIZEMM = "sizemm"
INTENSITY = "intensity"
HISTOGRAM = "histogram"
class LabelStatsKeys(StrEnum):
"""
Defaults keys for dataset statistical analysis label modules
"""
LABEL_UID = "labels"
PIXEL_PCT = "foreground_percentage"
IMAGE_INTST = "image_intensity"
LABEL = "label"
LABEL_SHAPE = "shape"
LABEL_NCOMP = "ncomponents"
@deprecated(since="1.2", removed="1.4", msg_suffix="please use `AlgoKeys` instead.")
class AlgoEnsembleKeys(StrEnum):
"""
Default keys for Mixed Ensemble
"""
ID = "identifier"
ALGO = "infer_algo"
SCORE = "best_metric"
class HoVerNetMode(StrEnum):
"""
Modes for HoVerNet model:
`FAST`: a faster implementation (than original)
`ORIGINAL`: the original implementation
"""
FAST = "FAST"
ORIGINAL = "ORIGINAL"
class HoVerNetBranch(StrEnum):
"""
Three branches of HoVerNet model, which results in three outputs:
`HV` is horizontal and vertical gradient map of each nucleus (regression),
`NP` is the pixel prediction of all nuclei (segmentation), and
`NC` is the type of each nucleus (classification).
"""
HV = "horizontal_vertical"
NP = "nucleus_prediction"
NC = "type_prediction"
class LazyAttr(StrEnum):
"""
MetaTensor with pending operations requires some key attributes tracked especially when the primary array
is not up-to-date due to lazy evaluation.
This class specifies the set of key attributes to be tracked for each MetaTensor.
See also: :py:func:`monai.transforms.lazy.utils.resample` for more details.
"""
SHAPE = "lazy_shape" # spatial shape
AFFINE = "lazy_affine"
PADDING_MODE = "lazy_padding_mode"
INTERP_MODE = "lazy_interpolation_mode"
DTYPE = "lazy_dtype"
ALIGN_CORNERS = "lazy_align_corners"
RESAMPLE_MODE = "lazy_resample_mode"
class BundleProperty(StrEnum):
"""
Bundle property fields:
`DESC` is the description of the property.
`REQUIRED` is flag to indicate whether the property is required or optional.
"""
DESC = "description"
REQUIRED = "required"
class BundlePropertyConfig(StrEnum):
"""
additional bundle property fields for config based bundle workflow:
`ID` is the config item ID of the property.
`REF_ID` is the ID of config item which is supposed to refer to this property.
For properties that do not have `REF_ID`, `None` should be set.
this field is only useful to check the optional property ID.
"""
ID = "id"
REF_ID = "refer_id"
class AlgoKeys(StrEnum):
"""
Default keys for templated Auto3DSeg Algo.
`ID` is the identifier of the algorithm. The string has the format of <name>_<idx>_<other>.
`ALGO` is the Auto3DSeg Algo instance.
`IS_TRAINED` is the status that shows if the Algo has been trained.
`SCORE` is the score the Algo has achieved after training.
"""
ID = "identifier"
ALGO = "algo_instance"
IS_TRAINED = "is_trained"
SCORE = "best_metric"
class AdversarialKeys(StrEnum):
"""
Keys used by the AdversarialTrainer.
`REALS` are real images from the batch.
`FAKES` are fake images generated by the generator. Are the same as PRED.
`REAL_LOGITS` are logits of the discriminator for the real images.
`FAKE_LOGIT` are logits of the discriminator for the fake images.
`RECONSTRUCTION_LOSS` is the loss value computed by the reconstruction loss function.
`GENERATOR_LOSS` is the loss value computed by the generator loss function. It is the
discriminator loss for the fake images. That is backpropagated through the generator only.
`DISCRIMINATOR_LOSS` is the loss value computed by the discriminator loss function. It is the
discriminator loss for the real images and the fake images. That is backpropagated through the
discriminator only.
"""
REALS = "reals"
REAL_LOGITS = "real_logits"
FAKES = "fakes"
FAKE_LOGITS = "fake_logits"
RECONSTRUCTION_LOSS = "reconstruction_loss"
GENERATOR_LOSS = "generator_loss"
DISCRIMINATOR_LOSS = "discriminator_loss"
class AdversarialIterationEvents(EventEnum):
"""
Keys used to define events as used in the AdversarialTrainer.
"""
RECONSTRUCTION_LOSS_COMPLETED = "reconstruction_loss_completed"
GENERATOR_FORWARD_COMPLETED = "generator_forward_completed"
GENERATOR_DISCRIMINATOR_FORWARD_COMPLETED = "generator_discriminator_forward_completed"
GENERATOR_LOSS_COMPLETED = "generator_loss_completed"
GENERATOR_BACKWARD_COMPLETED = "generator_backward_completed"
GENERATOR_MODEL_COMPLETED = "generator_model_completed"
DISCRIMINATOR_REALS_FORWARD_COMPLETED = "discriminator_reals_forward_completed"
DISCRIMINATOR_FAKES_FORWARD_COMPLETED = "discriminator_fakes_forward_completed"
DISCRIMINATOR_LOSS_COMPLETED = "discriminator_loss_completed"
DISCRIMINATOR_BACKWARD_COMPLETED = "discriminator_backward_completed"
DISCRIMINATOR_MODEL_COMPLETED = "discriminator_model_completed"
class OrderingType(StrEnum):
RASTER_SCAN = "raster_scan"
S_CURVE = "s_curve"
RANDOM = "random"
class OrderingTransformations(StrEnum):
ROTATE_90 = "rotate_90"
TRANSPOSE = "transpose"
REFLECT = "reflect"
|