Skip to content

Commit 63c861d

Browse files
committed
Modified comments per PR review
Signed-off-by: mmelqin <mingmelvinq@nvidia.com>
1 parent 5dd6a78 commit 63c861d

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

monai/deploy/operators/monai_seg_inference_operator.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,13 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
227227
out_ndarray = np.squeeze(out_ndarray, 0)
228228
# NOTE: The domain Image object simply contains a Arraylike obj as image as of now.
229229
# When the original DICOM series is converted by the Series to Volume operator,
230-
# using pydicom pixel_array, the 2D ndarray of each slice is transposed, and the
231-
# depth/axial direction dim made the last. So once post-transforms have completed,
232-
# the resultant ndarray for each slice needs to be transposed back, and the depth
233-
# dim moved back to first, otherwise the seg ndarray would be flipped compared to
234-
# the DICOM pixel array, causing the DICOM Seg Writer generate flipped seg images.
235-
out_ndarray = out_ndarray.T.astype(np.uint8) # np.swapaxes(out_ndarray, 2, 0).astype(np.uint8)
230+
# using pydicom pixel_array, the 2D ndarray of each slice has index order HW, and
231+
# when all slices are stacked with depth as first axis, DHW. In the pre-transforms,
232+
# the image gets transposed to WHD and used as such in the inference pipeline.
233+
# So once post-transforms have completed, and the channel is squeezed out,
234+
# the resultant ndarray for the prediction image needs to be transposed back, so the
235+
# array index order is back to DHW, the same order as the in-memory input Image obj.
236+
out_ndarray = out_ndarray.T.astype(np.uint8)
236237
print(f"Output Seg image numpy array shaped: {out_ndarray.shape}")
237238
print(f"Output Seg image pixel max value: {np.amax(out_ndarray)}")
238239
out_image = Image(out_ndarray, input_img_metadata)
@@ -280,8 +281,12 @@ class InMemImageReader(ImageReader):
280281
class simply converts a in-memory SDK Image object to the expected formats from ImageReader.
281282
282283
The loaded data array will be in C order, for example, a 3D image NumPy array index order
283-
will be `CDWH`. The actual data array loaded is to be the same as that from the
284-
MONAI ITKReader, which can also load DICOM series.
284+
will be `WHDC`. The actual data array loaded is to be the same as that from the
285+
MONAI ITKReader, which can also load DICOM series. Furthermore, all Readers need to return the
286+
array data the same way as the NibabelReader, i.e. a numpy array of index order WHDC with channel
287+
being the last dim if present. More details are in the get_data() function.
288+
289+
285290
"""
286291

287292
def __init__(self, input_image: Image, channel_dim: Optional[int] = None, **kwargs):
@@ -304,13 +309,17 @@ def get_data(self, input_image):
304309
It constructs `affine`, `original_affine`, and `spatial_shape` and stores them in meta dict.
305310
A single image is loaded with a single set of metadata as of now.
306311
307-
The App SDK Image asnumpy function is expected to return a NumPy array of index order `DHW`.
312+
The App SDK Image asnumpy() function is expected to return a numpy array of index order `DHW`.
308313
This is because in the DICOM serie to volume operator pydicom Dataset pixel_array is used to
309-
to get per instance pixel NumPy array, with index order of `HW`. When all instances are stacked,
310-
along the first axis, the Image NumPy array's index order is `DHW`. ITK array_view_from_image
311-
and SimpleITK GetArrayViewFromImage also returns a Numpy array with index order of `DHW`.
312-
This NumPy array is then transposed to be consistent with the NumPy array from NibabelReader,
313-
which loads NIfTI image and gets NumPy array using the image's get_fdata() function.
314+
to get per instance pixel numpy array, with index order of `HW`. When all instances are stacked,
315+
along the first axis, the Image numpy array's index order is `DHW`. ITK array_view_from_image
316+
and SimpleITK GetArrayViewFromImage also returns a numpy array with the index order of `DHW`.
317+
The channel would be the last dim/index if present. In the ITKReader get_data(), this numpy array
318+
is then transposed, and the channel axis moved to be last dim post transpose; this is to be
319+
consistent with the numpy returned from NibabelReader get_data().
320+
321+
The NibabelReader loads NIfTI image and uses the get_fdata() function of the loaded image to get
322+
the numpy array, which has the index order in WHD with the channel being the last dim if present.
314323
315324
Args:
316325
input_image (Image): an App SDK Image object.

0 commit comments

Comments
 (0)