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)
| 90 | |
| 91 | |
| 92 | def 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 | |
| 125 | class ImageWriter: |
searching dependent graphs…