|
| 1 | +# Copyright 2021-2023 MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | + |
| 13 | +import logging |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import numpy as np |
| 17 | + |
| 18 | +from monai.deploy.core import ConditionType, Fragment, Operator, OperatorSpec |
| 19 | +from monai.deploy.utils.importutil import optional_import |
| 20 | + |
| 21 | +SimpleITK, _ = optional_import("SimpleITK") |
| 22 | + |
| 23 | + |
| 24 | +# @md.env(pip_packages=["SimpleITK>=2.0.2"]) |
| 25 | +class NiftiDataWriter(Operator): |
| 26 | + |
| 27 | + def __init__(self, fragment: Fragment, *args, output_file: Path, **kwargs) -> None: |
| 28 | + """Creates an instance with the file path to load image from. |
| 29 | +
|
| 30 | + Args: |
| 31 | + fragment (Fragment): An instance of the Application class which is derived from Fragment. |
| 32 | + input_path (Path): The file Path to read from, overridden by valid named input on compute. |
| 33 | + """ |
| 34 | + self._logger = logging.getLogger("{}.{}".format(__name__, type(self).__name__)) |
| 35 | + |
| 36 | + self.output_file = output_file |
| 37 | + self.input_name_seg = "seg_image" |
| 38 | + self.input_name_output_file = "output_file" |
| 39 | + |
| 40 | + # Need to call the base class constructor last |
| 41 | + super().__init__(fragment, *args, **kwargs) |
| 42 | + |
| 43 | + def setup(self, spec: OperatorSpec): |
| 44 | + spec.input(self.input_name_seg) |
| 45 | + spec.input(self.input_name_output_file).condition(ConditionType.NONE) # Optional input not requiring sender. |
| 46 | + |
| 47 | + def compute(self, op_input, op_output, context): |
| 48 | + """Performs computation with the provided context.""" |
| 49 | + |
| 50 | + |
| 51 | + seg_image = op_input.receive(self.input_name_seg) |
| 52 | + |
| 53 | + |
| 54 | + # If the optional named input, output_folder, has content, use it instead of the one set on the object. |
| 55 | + # Since this input is optional, must check if data present and if Path or str. |
| 56 | + output_file = None |
| 57 | + try: |
| 58 | + output_file = op_input.receive(self.input_name_output_file) |
| 59 | + except Exception: |
| 60 | + pass |
| 61 | + |
| 62 | + if not output_file or not isinstance(output_file, (Path, str)): |
| 63 | + output_file = self.output_file |
| 64 | + |
| 65 | + self.convert_and_save(seg_image, output_file) |
| 66 | + |
| 67 | + def convert_and_save(self, seg_image, nii_path): |
| 68 | + """ |
| 69 | + reads the nifti image and returns a numpy image array |
| 70 | + """ |
| 71 | + image_writer = SimpleITK.ImageFileWriter() |
| 72 | + |
| 73 | + image = SimpleITK.GetImageFromArray(seg_image._data) |
| 74 | + image.SetSpacing(seg_image.metadata()["pixdim"]) |
| 75 | + |
| 76 | + if len(seg_image.metadata()["direction"]) == 16: |
| 77 | + direction = [] |
| 78 | + direction.extend(seg_image.metadata()["direction"][0:3]) |
| 79 | + direction.extend(seg_image.metadata()["direction"][4:7]) |
| 80 | + direction.extend(seg_image.metadata()["direction"][8:11]) |
| 81 | + image.SetDirection(direction) |
| 82 | + else: |
| 83 | + image.SetDirection(seg_image.metadata()["direction"]) |
| 84 | + |
| 85 | + image.SetOrigin(seg_image.metadata()["origin"]) |
| 86 | + |
| 87 | + image_writer.SetFileName(nii_path) |
| 88 | + image_writer.Execute(image) |
| 89 | + |
| 90 | + |
| 91 | +def test(): |
| 92 | + ... |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + test() |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments