9
9
# See the License for the specific language governing permissions and
10
10
# limitations under the License.
11
11
12
+
13
+ import logging
12
14
from pathlib import Path
13
15
14
16
import numpy as np
15
17
16
- import monai .deploy .core as md
17
18
from monai .deploy .core import ConditionType , Fragment , Operator , OperatorSpec
18
19
from monai .deploy .utils .importutil import optional_import
19
20
@@ -28,19 +29,20 @@ class NiftiDataLoader(Operator):
28
29
This operator reads a nifti image, extracts the numpy array and forwards it to the next operator
29
30
30
31
Named input:
31
- image_path: Path to the image file, optional, used to override the path set in the object.
32
+ image_path: Path to the image file, optional. Use it to override the input path set on the object.
32
33
33
34
Named output:
34
- image: A Numpy object in memory . Downstream receiver optional.
35
+ image: A Numpy array object . Downstream receiver optional.
35
36
"""
36
37
37
38
def __init__ (self , fragment : Fragment , * args , input_path : Path , ** kwargs ) -> None :
38
39
"""Creates an instance with the file path to load image from.
39
40
40
41
Args:
41
42
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
+ input_path (Path): The file Path to read from, overridden by valid named input on compute .
43
44
"""
45
+ self ._logger = logging .getLogger ("{}.{}" .format (__name__ , type (self ).__name__ ))
44
46
self .input_path = input_path # Allow to be None, to be overridden when compute is called.
45
47
self .input_name_path = "image_path"
46
48
self .output_name_image = "image"
@@ -53,14 +55,17 @@ def setup(self, spec: OperatorSpec):
53
55
spec .output (self .output_name_image ).condition (ConditionType .NONE ) # Fine for no or not-ready receiver ports.
54
56
55
57
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 ():
58
+ """Performs computation with the provided context."""
59
+
60
+ # The named input port is optional, so must check for and validate the data
61
+ input_path = op_input .receive (self .input_name_path )
62
+ if not input_path or not Path (input_path ).is_file :
63
+ self ._logger .info (f"No or invalid file path from the optional input port: { input_path } " )
64
+ # Try to fall back to use the object attribute if it is valid
65
+ if self .input_path and self .input_path .is_file ():
61
66
input_path = self .input_path
62
67
else :
63
- raise ValueError ("No path given to load image from. " )
68
+ raise ValueError (f "No valid file path from input port or obj attribute: { self . input_path } " )
64
69
65
70
image_np = self .convert_and_save (input_path )
66
71
op_output .emit (image_np , self .output_name_image )
@@ -77,7 +82,8 @@ def convert_and_save(self, nii_path):
77
82
78
83
79
84
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"
85
+ # Make sure the file path is correct.
86
+ filepath = Path (__file__ ).parent .resolve () / "../../../inputs/lung_seg_ct/nii/volume-covid19-A-0001.nii"
81
87
fragment = Fragment ()
82
88
nii_operator = NiftiDataLoader (fragment , input_path = filepath )
83
89
_ = nii_operator .convert_and_save (filepath )
0 commit comments