|
14 | 14 | import numpy as np
|
15 | 15 | import torch
|
16 | 16 |
|
| 17 | +# Commonly used concepts |
| 18 | +# This module provides naming and type specifications for commonly used concepts |
| 19 | +# within the MONAI package. The intent is to explicitly identify information |
| 20 | +# that should be used consistently throughout the entire MONAI package. |
| 21 | +# |
| 22 | +# A type would be named as type_definitions.KeysCollection |
| 23 | +# which includes a meaningful name for the consent in the name itself. The |
| 24 | +# definitions in this file map context meaningful names to the underlying |
| 25 | +# object properties that define the expected API. |
| 26 | +# |
| 27 | +# A conceptual type is represented by a new type name but is also one which |
| 28 | +# can be different depending on an environment (i.e. differences for python 3.6 vs 3.9 |
| 29 | +# may be implemented). Consistent use of the concept and recorded documentation of |
| 30 | +# the rationale and convention behind it lowers the learning curve for new |
| 31 | +# developers. For readability, short names are preferred. |
17 | 32 | __all__ = ["KeysCollection", "IndexSelection", "DtypeLike", "NdarrayTensor", "NdarrayOrTensor", "TensorOrList"]
|
18 | 33 |
|
19 |
| -"""Commonly used concepts |
20 |
| -This module provides naming and type specifications for commonly used concepts |
21 |
| -within the MONAI package. The intent is to explicitly identify information |
22 |
| -that should be used consistently throughout the entire MONAI package. |
23 |
| -
|
24 |
| -A type would be named as type_definitions.KeysCollection |
25 |
| -which includes a meaningful name for the consent in the name itself. The |
26 |
| -definitions in this file map context meaningful names to the underlying |
27 |
| -object properties that define the expected API. |
28 |
| -
|
29 |
| -A conceptual type is represented by a new type name but is also one which |
30 |
| -can be different depending on an environment (i.e. differences for python 3.6 vs 3.9 |
31 |
| -may be implemented). Consistent use of the concept and recorded documentation of |
32 |
| -the rationale and convention behind it lowers the learning curve for new |
33 |
| -developers. For readability, short names are preferred. |
34 |
| -""" |
35 | 34 |
|
| 35 | +#: KeysCollection |
| 36 | +# |
| 37 | +# The KeyCollection type is used to for defining variables |
| 38 | +# that store a subset of keys to select items from a dictionary. |
| 39 | +# The container of keys must contain hashable elements. |
| 40 | +# NOTE: `Hashable` is not a collection, but is provided as a |
| 41 | +# convenience to end-users. All supplied values will be |
| 42 | +# internally converted to a tuple of `Hashable`'s before |
| 43 | +# use |
36 | 44 | KeysCollection = Union[Collection[Hashable], Hashable]
|
37 |
| -"""KeysCollection |
38 |
| -
|
39 |
| -The KeyCollection type is used to for defining variables |
40 |
| -that store a subset of keys to select items from a dictionary. |
41 |
| -The container of keys must contain hashable elements. |
42 |
| -NOTE: `Hashable` is not a collection, but is provided as a |
43 |
| - convenience to end-users. All supplied values will be |
44 |
| - internally converted to a tuple of `Hashable`'s before |
45 |
| - use |
46 |
| -""" |
47 |
| - |
48 | 45 |
|
| 46 | +#: IndexSelection |
| 47 | +# |
| 48 | +# The IndexSelection type is used to for defining variables |
| 49 | +# that store a subset of indices to select items from a List or Array like objects. |
| 50 | +# The indices must be integers, and if a container of indices is specified, the |
| 51 | +# container must be iterable. |
49 | 52 | IndexSelection = Union[Iterable[int], int]
|
50 |
| -"""IndexSelection |
51 |
| -
|
52 |
| -The IndexSelection type is used to for defining variables |
53 |
| -that store a subset of indices to select items from a List or Array like objects. |
54 |
| -The indices must be integers, and if a container of indices is specified, the |
55 |
| -container must be iterable. |
56 |
| -""" |
57 |
| - |
58 | 53 |
|
| 54 | +#: Type of datatypes: Adapted from https://github.com/numpy/numpy/blob/master/numpy/typing/_dtype_like.py |
59 | 55 | DtypeLike = Union[np.dtype, type, None]
|
60 |
| -"""Type of datatypes |
61 |
| -
|
62 |
| -Adapted from https://github.com/numpy/numpy/blob/master/numpy/typing/_dtype_like.py |
63 |
| -""" |
64 | 56 |
|
| 57 | +#: NdarrayTensor |
| 58 | +# |
| 59 | +# Generic type which can represent either a numpy.ndarray or a torch.Tensor |
| 60 | +# Unlike Union can create a dependence between parameter(s) / return(s) |
65 | 61 | NdarrayTensor = TypeVar("NdarrayTensor", np.ndarray, torch.Tensor)
|
66 |
| -"""NdarrayTensor |
67 | 62 |
|
68 |
| -Generic type which can represent either a numpy.ndarray or a torch.Tensor |
69 |
| -Unlike Union can create a dependence between parameter(s) / return(s) |
70 |
| -""" |
71 | 63 |
|
| 64 | +#: NdarrayOrTensor: Union of numpy.ndarray and torch.Tensor to be used for typing |
72 | 65 | NdarrayOrTensor = Union[np.ndarray, torch.Tensor]
|
73 |
| -"""NdarrayOrTensor |
74 |
| -
|
75 |
| -Union of numpy.ndarray and torch.Tensor to be used for typing |
76 |
| -""" |
77 | 66 |
|
| 67 | +#: TensorOrList: The TensorOrList type is used for defining `batch-first Tensor` or `list of channel-first Tensor`. |
78 | 68 | TensorOrList = Union[torch.Tensor, Sequence[torch.Tensor]]
|
79 |
| -"""TensorOrList |
80 |
| -
|
81 |
| -The TensorOrList type is used for defining `batch-first Tensor` or `list of channel-first Tensor`. |
82 |
| -""" |
0 commit comments