| 32 | // the more specific type (it does NOT inherit from Image). |
| 33 | |
| 34 | void InitIOUtil(py::module_& m) |
| 35 | { |
| 36 | py::class_<IOUtil>(m, "IOUtil", |
| 37 | R"(File I/O for MITK objects. |
| 38 | |
| 39 | ``IOUtil`` is a namespace-style class with static methods for loading and |
| 40 | saving the data types currently bound in Python (:py:class:`Image` and |
| 41 | :py:class:`MultiLabelSegmentation`). The wheel bundles auto-load modules |
| 42 | for the common image formats (NRRD, NIfTI, MetaImage, DICOM, and others); |
| 43 | no further setup is required. |
| 44 | |
| 45 | The :py:class:`Image` constructor and the shortcut methods |
| 46 | ``Image.load`` / ``Image.save`` ultimately call into this class. |
| 47 | )") |
| 48 | .def_static("load", |
| 49 | [](const std::filesystem::path& path, |
| 50 | std::optional<std::vector<std::string>> reader_preferences, |
| 51 | std::optional<std::vector<std::string>> reader_blacklist) { |
| 52 | const auto path_str = path.string(); |
| 53 | std::vector<BaseData::Pointer> results; |
| 54 | |
| 55 | if (reader_preferences.has_value() || reader_blacklist.has_value()) |
| 56 | { |
| 57 | PreferenceListReaderOptionsFunctor functor( |
| 58 | reader_preferences.value_or(std::vector<std::string>{}), |
| 59 | reader_blacklist.value_or(std::vector<std::string>{})); |
| 60 | results = IOUtil::Load(path_str, &functor); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | results = IOUtil::Load(path_str); |
| 65 | } |
| 66 | |
| 67 | py::list out; |
| 68 | |
| 69 | for (auto& base : results) |
| 70 | { |
| 71 | if (auto* seg = dynamic_cast<MultiLabelSegmentation*>(base.GetPointer())) |
| 72 | out.append(MultiLabelSegmentation::Pointer(seg)); |
| 73 | else if (auto* img = dynamic_cast<Image*>(base.GetPointer())) |
| 74 | out.append(Image::Pointer(img)); |
| 75 | // other BaseData subtypes not yet bound are silently skipped |
| 76 | } |
| 77 | |
| 78 | return out; |
| 79 | }, |
| 80 | py::arg("path"), |
| 81 | py::arg("reader_preferences") = py::none(), |
| 82 | py::arg("reader_blacklist") = py::none(), |
| 83 | R"(Load a file and return a list of bound objects. |
| 84 | |
| 85 | Each element of the returned list is either an :py:class:`Image` or a |
| 86 | :py:class:`MultiLabelSegmentation`, depending on what the MITK I/O system |
| 87 | chose to produce. ``BaseData`` subclasses that are not yet exposed to |
| 88 | Python are silently skipped. |
| 89 | |
| 90 | Reader selection can be steered with ``reader_preferences`` (preferred |
| 91 | reader description strings) and ``reader_blacklist`` (readers to skip). |
no test coverage detected