MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / look_up_option

Function look_up_option

monai/utils/module.py:62–142  ·  view source on GitHub ↗

Look up the option in the supported collection and return the matched item. Raise a value error possibly with a guess of the closest match. Args: opt_str: The option string or Enum to look up. supported: The collection of supported options, it can be list, tuple, set, d

(
    opt_str: Hashable,
    supported: Collection | enum.EnumMeta,
    default: Any = "no_default",
    print_all_options: bool = True,
)

Source from the content-addressed store, hash-verified

60
61
62def look_up_option(
63 opt_str: Hashable,
64 supported: Collection | enum.EnumMeta,
65 default: Any = "no_default",
66 print_all_options: bool = True,
67) -> Any:
68 """
69 Look up the option in the supported collection and return the matched item.
70 Raise a value error possibly with a guess of the closest match.
71
72 Args:
73 opt_str: The option string or Enum to look up.
74 supported: The collection of supported options, it can be list, tuple, set, dict, or Enum.
75 default: If it is given, this method will return `default` when `opt_str` is not found,
76 instead of raising a `ValueError`. Otherwise, it defaults to `"no_default"`,
77 so that the method may raise a `ValueError`.
78 print_all_options: whether to print all available options when `opt_str` is not found. Defaults to True
79
80 Examples:
81
82 .. code-block:: python
83
84 from enum import Enum
85 from monai.utils import look_up_option
86 class Color(Enum):
87 RED = "red"
88 BLUE = "blue"
89 look_up_option("red", Color) # <Color.RED: 'red'>
90 look_up_option(Color.RED, Color) # <Color.RED: 'red'>
91 look_up_option("read", Color)
92 # ValueError: By 'read', did you mean 'red'?
93 # 'read' is not a valid option.
94 # Available options are {'blue', 'red'}.
95 look_up_option("red", {"red", "blue"}) # "red"
96
97 Adapted from https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/utilities/util_common.py#L249
98 """
99 if not isinstance(opt_str, Hashable):
100 raise ValueError(f"Unrecognized option type: {type(opt_str)}:{opt_str}.")
101 if isinstance(opt_str, str):
102 opt_str = opt_str.strip()
103 if isinstance(supported, enum.EnumMeta):
104 if isinstance(opt_str, str) and opt_str in {item.value for item in supported}: # type: ignore
105 # such as: "example" in MyEnum
106 return supported(opt_str)
107 if isinstance(opt_str, enum.Enum) and opt_str in supported:
108 # such as: MyEnum.EXAMPLE in MyEnum
109 return opt_str
110 elif isinstance(supported, Mapping) and opt_str in supported:
111 # such as: MyDict[key]
112 return supported[opt_str]
113 elif isinstance(supported, Collection) and opt_str in supported:
114 return opt_str
115
116 if default != "no_default":
117 return default
118
119 # find a close match

Callers 15

look_up_named_moduleFunction · 0.90
_load_state_dictFunction · 0.90
_load_state_dictFunction · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
_load_state_dictFunction · 0.90
_make_layerMethod · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls 2

minFunction · 0.85

Tested by 4

test_look_upMethod · 0.72
test_defaultMethod · 0.72
test_str_enumMethod · 0.72
test_no_foundMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…