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

Function resolve_writer

monai/data/image_writer.py:92–122  ·  view source on GitHub ↗

Resolves to a tuple of available ``ImageWriter`` in ``SUPPORTED_WRITERS`` according to the filename extension key ``ext_name``. Args: ext_name: the filename extension of the image. As an indexing key it will be converted to a lower case string. error_if_not_

(ext_name, error_if_not_found=True)

Source from the content-addressed store, hash-verified

90
91
92def resolve_writer(ext_name, error_if_not_found=True) -> Sequence:
93 """
94 Resolves to a tuple of available ``ImageWriter`` in ``SUPPORTED_WRITERS``
95 according to the filename extension key ``ext_name``.
96
97 Args:
98 ext_name: the filename extension of the image.
99 As an indexing key it will be converted to a lower case string.
100 error_if_not_found: whether to raise an error if no suitable image writer is found.
101 if True , raise an ``OptionalImportError``, otherwise return an empty tuple. Default is ``True``.
102 """
103 if not SUPPORTED_WRITERS:
104 init()
105 fmt = f"{ext_name}".lower()
106 if fmt.startswith("."):
107 fmt = fmt[1:]
108 avail_writers = []
109 default_writers = SUPPORTED_WRITERS.get(EXT_WILDCARD, ())
110 for _writer in look_up_option(fmt, SUPPORTED_WRITERS, default=default_writers):
111 try:
112 _writer() # this triggers `monai.utils.module.require_pkg` to check the system availability
113 avail_writers.append(_writer)
114 except OptionalImportError:
115 continue
116 except Exception: # other writer init errors indicating it exists
117 avail_writers.append(_writer)
118 if not avail_writers and error_if_not_found:
119 raise OptionalImportError(f"No ImageWriter backend found for {fmt}.")
120 writer_tuple = ensure_tuple(avail_writers)
121 SUPPORTED_WRITERS[fmt] = writer_tuple
122 return writer_tuple
123
124
125class ImageWriter:

Callers 2

test_0_defaultMethod · 0.90
test_1_newMethod · 0.90

Calls 6

look_up_optionFunction · 0.90
OptionalImportErrorClass · 0.90
ensure_tupleFunction · 0.90
initFunction · 0.85
getMethod · 0.80
appendMethod · 0.45

Tested by 2

test_0_defaultMethod · 0.72
test_1_newMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…