|
1 |
| -# # Copyright 2021-2022 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. |
| 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 | 11 |
|
12 |
| -# import numpy as np |
| 12 | +from pathlib import Path |
13 | 13 |
|
14 |
| -# import monai.deploy.core as md |
15 |
| -# from monai.deploy.core import DataPath, ExecutionContext, InputContext, IOType, Operator, OutputContext |
16 |
| -# from monai.deploy.utils.importutil import optional_import |
| 14 | +import numpy as np |
17 | 15 |
|
18 |
| -# SimpleITK, _ = optional_import("SimpleITK") |
| 16 | +import monai.deploy.core as md |
| 17 | +from monai.deploy.core import ConditionType, Fragment, Operator, OperatorSpec |
| 18 | +from monai.deploy.utils.importutil import optional_import |
| 19 | + |
| 20 | +SimpleITK, _ = optional_import("SimpleITK") |
19 | 21 |
|
20 | 22 |
|
21 | 23 | # @md.input("image_path", DataPath, IOType.DISK)
|
22 | 24 | # @md.output("image", np.ndarray, IOType.IN_MEMORY)
|
23 | 25 | # @md.env(pip_packages=["SimpleITK>=2.0.2"])
|
24 |
| -# class NiftiDataLoader(Operator): |
25 |
| -# """ |
26 |
| -# This operator reads a nifti image, extracts the numpy array and forwards it to the next operator |
27 |
| -# """ |
| 26 | +class NiftiDataLoader(Operator): |
| 27 | + """ |
| 28 | + This operator reads a nifti image, extracts the numpy array and forwards it to the next operator |
| 29 | +
|
| 30 | + Named input: |
| 31 | + image_path: Path to the image file, optional, used to override the path set in the object. |
| 32 | +
|
| 33 | + Named output: |
| 34 | + image: A Numpy object in memory. Downstream receiver optional. |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self, fragment: Fragment, *args, input_path: Path, **kwargs) -> None: |
| 38 | + """Creates an instance with the file path to load image from. |
| 39 | +
|
| 40 | + Args: |
| 41 | + fragment (Fragment): An instance of the Application class which is derived from Fragment. |
| 42 | + input_path (Path): The Path to read the image file from, overriden by the named input. |
| 43 | + """ |
| 44 | + self.input_path = input_path # Allow to be None, to be overridden when compute is called. |
| 45 | + self.input_name_path = "image_path" |
| 46 | + self.output_name_image = "image" |
| 47 | + |
| 48 | + # Need to call the base class constructor last |
| 49 | + super().__init__(fragment, *args, **kwargs) |
| 50 | + |
| 51 | + def setup(self, spec: OperatorSpec): |
| 52 | + spec.input(self.input_name_path).condition(ConditionType.NONE) |
| 53 | + spec.output(self.output_name_image).condition(ConditionType.NONE) # Fine for no or not-ready receiver ports. |
| 54 | + |
| 55 | + def compute(self, op_input, op_output, context): |
| 56 | + input_path = None |
| 57 | + try: |
| 58 | + input_path = op_input.receive(self.input_name_path) |
| 59 | + except Exception: |
| 60 | + if self.input_path and not self.input_path.is_file(): |
| 61 | + input_path = self.input_path |
| 62 | + else: |
| 63 | + raise ValueError("No path given to load image from.") |
28 | 64 |
|
29 |
| -# def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext): |
30 |
| -# input_path = op_input.get().path |
31 |
| -# image_np = self.convert_and_save(input_path) |
32 |
| -# op_output.set(image_np) |
| 65 | + image_np = self.convert_and_save(input_path) |
| 66 | + op_output.emit(image_np, self.output_name_image) |
33 | 67 |
|
34 |
| -# def convert_and_save(self, nii_path): |
35 |
| -# """ |
36 |
| -# reads the nifti image and |
37 |
| -# """ |
38 |
| -# image_reader = SimpleITK.ImageFileReader() |
39 |
| -# image_reader.SetFileName(str(nii_path)) |
40 |
| -# image = image_reader.Execute() |
41 |
| -# image_np = np.transpose(SimpleITK.GetArrayFromImage(image), [2, 1, 0]) |
42 |
| -# return image_np |
| 68 | + def convert_and_save(self, nii_path): |
| 69 | + """ |
| 70 | + reads the nifti image and returns a numpy image array |
| 71 | + """ |
| 72 | + image_reader = SimpleITK.ImageFileReader() |
| 73 | + image_reader.SetFileName(str(nii_path)) |
| 74 | + image = image_reader.Execute() |
| 75 | + image_np = np.transpose(SimpleITK.GetArrayFromImage(image), [2, 1, 0]) |
| 76 | + return image_np |
43 | 77 |
|
44 | 78 |
|
45 |
| -# def test(): |
46 |
| -# filepath = "/home/gupta/Documents/mni_icbm152_nlin_sym_09a/mni_icbm152_gm_tal_nlin_sym_09a.nii" |
47 |
| -# nii_operator = NiftiDataLoader() |
48 |
| -# _ = nii_operator.convert_and_save(filepath) |
| 79 | +def test(): |
| 80 | + filepath = "/home/mqin/src/monai-deploy-app-sdk/inputs/lung_seg_ct/nii/volume-covid19-A-0001.nii" # "/home/gupta/Documents/mni_icbm152_nlin_sym_09a/mni_icbm152_gm_tal_nlin_sym_09a.nii" |
| 81 | + fragment = Fragment() |
| 82 | + nii_operator = NiftiDataLoader(fragment, input_path=filepath) |
| 83 | + _ = nii_operator.convert_and_save(filepath) |
49 | 84 |
|
50 | 85 |
|
51 |
| -# def main(): |
52 |
| -# test() |
| 86 | +def main(): |
| 87 | + test() |
53 | 88 |
|
54 | 89 |
|
55 |
| -# if __name__ == "__main__": |
56 |
| -# main() |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments