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

fix: upgrade to pydicom 3 #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
Referenced versions in headers are tagged on Github, in parentheses are for pypi.

## [vxx](https://github.com/pydicom/deid/tree/master) (master)
- Update to use pydicom 3 [#267](https://github.com/pydicom/deid/pull/267) (1.0.0)
- Refactor INCLUDE_REQUIRES and provide max pydicom version [#267](https://github.com/pydicom/deid/pull/267) (0.3.25)
- Support pydicom.Dataset objects created from BytesIO [#265](https://github.com/pydicom/deid/pull/265) (0.3.24)
- Exception with missing filters for non-string VR [#256](https://github.com/pydicom/deid/issues/256) (0.3.23)
Expand Down
2 changes: 1 addition & 1 deletion deid/dicom/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def apply_filter(dicom, field, filter_name, value):

Parameters
==========
dicom: the pydicom.dataset Dataset (pydicom.read_file)
dicom: the pydicom.dataset Dataset (pydicom.dcmread)
field: the name of the field to apply the filter to,
or the tag number as a string '0xGGGGEEEE'
filer_name: the name of the filter to apply (e.g., contains)
Expand Down
4 changes: 2 additions & 2 deletions deid/dicom/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import os

from pydicom import read_file
from pydicom import dcmread

from deid.dicom.parser import DicomParser
from deid.dicom.utils import save_dicom
Expand Down Expand Up @@ -68,7 +68,7 @@ def remove_private_identifiers(
dicom_files = [dicom_files]

for dicom_file in dicom_files:
dicom = read_file(dicom_file, force=force)
dicom = dcmread(dicom_file, force=force)
dicom.remove_private_tags()
dicom_name = os.path.basename(dicom_file)
bot.debug("Removed private identifiers for %s" % dicom_name)
Expand Down
4 changes: 2 additions & 2 deletions deid/dicom/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from copy import deepcopy
from io import BytesIO

from pydicom import read_file
from pydicom import dcmread
from pydicom.dataelem import DataElement
from pydicom.dataset import Dataset
from pydicom.tag import Tag
Expand Down Expand Up @@ -99,7 +99,7 @@ def load(self, dicom_file, force=True):
# If we must read the file, the path must exist
if not os.path.exists(dicom_file):
bot.exit("%s does not exist." % dicom_file)
self.dicom = read_file(dicom_file, force=force)
self.dicom = dcmread(dicom_file, force=force)

# Set class variables that might be helpful later
df = self.dicom.get("filename")
Expand Down
4 changes: 2 additions & 2 deletions deid/dicom/pixels/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import matplotlib
import numpy
from numpy.typing import NDArray
from pydicom import read_file
from pydicom import dcmread
from pydicom.pixel_data_handlers.util import get_expected_length

from deid.config import DeidRecipe
Expand Down Expand Up @@ -245,7 +245,7 @@ def save_dicom(self, output_folder=None, image_type="cleaned"):
# Having clean also means has dicom image
if hasattr(self, image_type):
dicom_name = self._get_clean_name(output_folder)
dicom = read_file(self.dicom_file, force=True)
dicom = dcmread(self.dicom_file, force=True)
# If going from compressed, change TransferSyntax
if dicom.file_meta.TransferSyntaxUID.is_compressed is True:
dicom.decompress()
Expand Down
4 changes: 2 additions & 2 deletions deid/dicom/pixels/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from typing import List, Optional, Union

from pydicom import FileDataset, read_file
from pydicom import FileDataset, dcmread
from pydicom.sequence import Sequence

from deid.config import DeidRecipe
Expand Down Expand Up @@ -114,7 +114,7 @@ def _has_burned_pixels_single(dicom_file, force: bool, deid):
if isinstance(dicom_file, FileDataset):
dicom = dicom_file
else:
dicom = read_file(dicom_file, force=force)
dicom = dcmread(dicom_file, force=force)

# Return list with lookup as dicom_file
results = []
Expand Down
6 changes: 3 additions & 3 deletions deid/dicom/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def update_tag(dicom, field, value):

Parameters
==========
dicom: the pydicom.dataset Dataset (pydicom.read_file)
dicom: the pydicom.dataset Dataset (pydicom.dcmread)
field: the name of the field to update
value: the value to set, if name is a valid tag

Expand Down Expand Up @@ -157,7 +157,7 @@ def get_private(dicom):

Parameters
==========
dicom: the pydicom.dataset Dataset (pydicom.read_file)
dicom: the pydicom.dataset Dataset (pydicom.dcmread)
"""
datasets = [dicom]
private_tags = []
Expand Down Expand Up @@ -188,7 +188,7 @@ def has_private(dicom):

Parameters
==========
dicom: the pydicom.dataset Dataset (pydicom.read_file)
dicom: the pydicom.dataset Dataset (pydicom.dcmread)

"""
private_tags = len(get_private(dicom))
Expand Down
2 changes: 1 addition & 1 deletion deid/dicom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ def load_dicom(dcm_file):
if isinstance(dcm_file, FileDataset):
return dcm_file
else:
return pydicom.read_file(dcm_file, force=True)
return pydicom.dcmread(dcm_file, force=True)
4 changes: 2 additions & 2 deletions deid/dicom/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__copyright__ = "Copyright 2016-2023, Vanessa Sochat"
__license__ = "MIT"

from pydicom import read_file
from pydicom import dcmread

from deid.logger import bot

Expand All @@ -28,7 +28,7 @@ def validate_dicoms(dcm_files, force=False):
for dcm_file in dcm_files:
try:
with open(dcm_file, "rb") as filey:
read_file(filey, force=force)
dcmread(filey, force=force)
valids.append(dcm_file)
except Exception:
bot.warning("Cannot read input file {0!s}, skipping.".format(dcm_file))
Expand Down
10 changes: 5 additions & 5 deletions deid/tests/Xtest_dicom_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def test_get_identifiers(self):

def test_replace_identifiers(self):
print("Testing deid.dicom replace_identifiers")
from pydicom import read_file
from pydicom import dcmread

from deid.dicom import get_identifiers, replace_identifiers

dicom_files = get_dicom(self.dataset, return_dir=True)
ids = get_identifiers(dicom_files)

# Before blanking, 28 fields don't have blanks
notblanked = read_file(dicom_files[0])
notblanked = dcmread(dicom_files[0])
notblanked_fields = [
x for x in notblanked.dir() if notblanked.get(x) != ""
] # 28
Expand All @@ -64,21 +64,21 @@ def test_replace_identifiers(self):
updated_files = replace_identifiers(dicom_files, ids, output_folder=self.tmpdir)

# After replacing only 9 don't have blanks
blanked = read_file(updated_files[0])
blanked = dcmread(updated_files[0])
blanked_fields = [x for x in blanked.dir() if blanked.get(x) != ""]
self.assertTrue(len(blanked_fields) == 9)


def get_dicom(dataset, return_dir=False):
"""helper function to load a dicom"""
from pydicom import read_file
from pydicom import dcmread

from deid.dicom import get_files

dicom_files = get_files(dataset)
if return_dir:
return list(dicom_files)
return read_file(next(dicom_files))
return dcmread(next(dicom_files))


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions deid/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def get_dicom(dataset):
"""
helper function to load a dicom
"""
from pydicom import read_file
from pydicom import dcmread

from deid.dicom import get_files

dicom_files = get_files(dataset)
return read_file(next(dicom_files))
return dcmread(next(dicom_files))


def get_same_file(dataset):
Expand Down
Loading
Loading