Skip to content

Commit a7d5898

Browse files
fix dir case error
Signed-off-by: Yiheng Wang <[email protected]>
1 parent a79f4de commit a7d5898

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

monai/data/image_reader.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,21 +523,25 @@ def read(self, data: Sequence[PathLike] | PathLike, **kwargs):
523523
series_slcs = [slc for slc in glob.glob(os.path.join(name, "*")) if re.match(self.fname_regex, slc)]
524524
else:
525525
series_slcs = [slc for slc in glob.glob(os.path.join(name, "*")) if pydicom.misc.is_dicom(slc)]
526-
self.filenames[i] = series_slcs # type: ignore
527526
slices = []
527+
loaded_slc_names = []
528528
for slc in series_slcs:
529529
try:
530530
slices.append(pydicom.dcmread(fp=slc, **kwargs_))
531+
loaded_slc_names.append(slc)
531532
except pydicom.errors.InvalidDicomError as e:
532533
warnings.warn(f"Failed to read {slc} with exception: \n{e}.", stacklevel=2)
533-
img_.append(slices if len(slices) > 1 else slices[0])
534534
if len(slices) > 1:
535535
self.has_series = True
536+
img_.append(slices)
537+
self.filenames[i] = loaded_slc_names # type: ignore
538+
else:
539+
img_.append(slices[0]) # type: ignore
540+
self.filenames[i] = loaded_slc_names[0] # type: ignore
536541
else:
537542
ds = pydicom.dcmread(fp=name, **kwargs_)
538-
img_.append(ds)
543+
img_.append(ds) # type: ignore
539544
if len(filenames) == 1:
540-
self.filenames = self.filenames[0] # type: ignore
541545
return img_[0]
542546
return img_
543547

@@ -638,7 +642,8 @@ def get_data(self, data) -> tuple[np.ndarray, dict]:
638642
if self.has_series is True:
639643
# a list, all objects within a list belong to one dicom series
640644
if not isinstance(data[0], list):
641-
dicom_data.append(self._combine_dicom_series(data, self.filenames))
645+
# input is a dir, self.filenames is a list of list of filenames
646+
dicom_data.append(self._combine_dicom_series(data, self.filenames[0])) # type: ignore
642647
# a list of list, each inner list represents a dicom series
643648
else:
644649
for i, series in enumerate(data):

0 commit comments

Comments
 (0)