Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter only ".dcm" files when reading DICOM series #6627

Closed
function2-llx opened this issue Jun 19, 2023 · 4 comments · Fixed by #7181
Closed

Filter only ".dcm" files when reading DICOM series #6627

function2-llx opened this issue Jun 19, 2023 · 4 comments · Fixed by #7181

Comments

@function2-llx
Copy link
Contributor

Is your feature request related to a problem? Please describe.
When I try to load a DICOM folder, the loading will fail if there is some irrelevant file in the folder (or I have to set force=True). E.g., when I load the DICOM series from the LIDC-IDRI database, there's a .xml annotation file in the folder, the loader will try to load it as well and cause failure.

Describe the solution you'd like
Change this line

series_slcs = glob.glob(os.path.join(name, "*"))

to something like

series_slcs = Path(name).glob("*.dcm")

I'm not sure if there are other suffixes to be considered.

@mingxin-zheng
Copy link
Contributor

mingxin-zheng commented Jun 19, 2023

Hi @function2-llx , thanks for the suggestion, but I have seen some scanners exporting DICOM without the ".dcm" suffix, e.g. "IM_0001" on some ultrasound systems. It may be better to keep it optional.

@function2-llx
Copy link
Contributor Author

@mingxin-zheng Yes, maybe we can let the user decide whether to filter and what suffix to filter.

@mingxin-zheng
Copy link
Contributor

mingxin-zheng commented Jun 21, 2023

I tagged this as a feature request and see if this is a generalized usage. Besides using the suffix to filter files, there are also many other possible name filters users want to customize. I am not sure whether a filter on suffix or something more general is needed.

For now, as I see the read method take list of files, you may try to glob the .dcm files as a list and pass it to the reader in your script.

def read(self, data: Sequence[PathLike] | PathLike, **kwargs):

import monai
import glob
files = glob.glob("dcm/*.DCM")
imgs = monai.data.PydicomReader().read(p)

If we go for a filter later, maybe we can allow a regex pattern here for file inclusion/exclusion in the pydicom reader.

series_slcs = [slc for slc in series_slcs if "LICENSE" not in slc]

@function2-llx
Copy link
Contributor Author

function2-llx commented Jun 21, 2023

Thanks, I feel like using a regex pattern is a good idea.

On the other hand, reading a list of files may not work as expected because they will be stacked along the channel dimension (even permutation after that is not feasible since the affine compatibility check will fail).

Read image data from specified file or files, it can read a list of images
and stack them together as multi-channel data in `get_data()`.

if affine_key in from_dict and not np.allclose(from_dict[affine_key], to_dict[affine_key]):
raise RuntimeError(
"affine matrix of all images should be the same for channel-wise concatenation. "
f"Got {from_dict[affine_key]} and {to_dict[affine_key]}."
)

wyli added a commit to wyli/MONAI that referenced this issue Nov 1, 2023
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
wyli added a commit to wyli/MONAI that referenced this issue Nov 1, 2023
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
wyli added a commit to wyli/MONAI that referenced this issue Nov 2, 2023
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
@wyli wyli closed this as completed in #7181 Nov 2, 2023
wyli added a commit that referenced this issue Nov 2, 2023
Fixes #6627

### Description
adding a `fname_regex` option to the pydicom reader

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [x] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Wenqi Li <wenqil@nvidia.com>
marksgraham pushed a commit to marksgraham/MONAI that referenced this issue Jan 30, 2024
Fixes Project-MONAI#6627

### Description
adding a `fname_regex` option to the pydicom reader

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [x] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Wenqi Li <wenqil@nvidia.com>
Signed-off-by: Mark Graham <markgraham539@gmail.com>
Yu0610 pushed a commit to Yu0610/MONAI that referenced this issue Apr 11, 2024
Fixes Project-MONAI#6627

### Description
adding a `fname_regex` option to the pydicom reader

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [x] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Wenqi Li <wenqil@nvidia.com>
Signed-off-by: Yu0610 <612410030@alum.ccu.edu.tw>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants