Skip to content

Commit

Permalink
feat(repersonlization): add customizable fields
Browse files Browse the repository at this point in the history
* feat: add customize repersonalization fields

* update readme

* Added file with accession number

* Add test for custom fields

---------

Co-authored-by: Ashley Rudelsheim <ashley.rudelsheim@itsphere.ca>
Co-authored-by: Ashley Rudelsheim <ashley@e.ashleyr.ca>
  • Loading branch information
3 people committed Jan 31, 2024
1 parent a59ca10 commit d99f434
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 13 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Returns:

## Repersonalisation

It is the process of copying over data regarding the identity of the encapsualted pdf from a template dicom. Currently, the fileds that are repersonalised are-
It is the process of copying over data regarding the identity of the encapsualted pdf from a template dicom. Currently, the fields that are repersonalised by default are-

- PatientName
- PatientID
Expand All @@ -103,3 +103,20 @@ It is the process of copying over data regarding the identity of the encapsualte
- ~~SOPInstanceUID~~

The fields `SeriesInstanceUID` and `SOPInstanceUID` have been removed from the repersonalization by copying as it violates the DICOM standards.

You can set the fields to repersonalize by passing repersonalisation_fields into `Pdf2EncapsDCM()`, or `Pdf2RgbSC()`

Example:

```python
fields = [
"PatientName",
"PatientID",
"PatientSex",
"StudyInstanceUID",
"AccessionNumber"
]
converter = Pdf2RgbSC(repersonalisation_fields=fields)
```

note: this will overwrite the default fields.
17 changes: 10 additions & 7 deletions pdf2dcm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@


class BaseConverter(ABC):
def __init__(self):
self.repersonalisation_fields = [
"PatientName",
"PatientID",
"PatientSex",
"StudyInstanceUID",
]
def __init__(self, repersonalisation_fields=[]):
if len(repersonalisation_fields):
self.repersonalisation_fields = repersonalisation_fields
else:
self.repersonalisation_fields = [
"PatientName",
"PatientID",
"PatientSex",
"StudyInstanceUID",
]

def personalize_dcm(
self, template_dcm_path: Path, pdf_dcm: FileDataset
Expand Down
10 changes: 7 additions & 3 deletions pdf2dcm/pdf2encaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@


class Pdf2EncapsDCM(BaseConverter):
def __init__(self):
"""Class for Encapsulated PDF generation"""
super().__init__()
def __init__(self, repersonalisation_fields=[]):
"""Class for Encapsulated PDF generation
Args:
repersonalisation_fields (List<String>, optional): fields to be copied to the new image
"""
super().__init__(repersonalisation_fields=repersonalisation_fields)

def _get_encapspdf_meta(self) -> FileMetaDataset:
"""Get and set the file meta information for the pdf encaps dicom
Expand Down
6 changes: 4 additions & 2 deletions pdf2dcm/pdf2rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@


class Pdf2RgbSC(BaseConverter):
def __init__(self, dpi=144, merge_pages=False):
def __init__(self, dpi=144, merge_pages=False, repersonalisation_fields=[]):
"""Class for the generation RGB Secondary capture
Args:
dpi (int, optional): dots per inch, set resolution of the image. Defaults to 144.
merge_pages (bool, optional): multiple pgs must be put into 1 dicom. Defaults to False.
repersonalisation_fields (List<String>, optional): fields to be copied to the new image
"""
self.merge_pages_flag = merge_pages
self.dpi = dpi
super().__init__()
super().__init__(repersonalisation_fields=repersonalisation_fields)

def _get_rgbsc_meta(self) -> FileMetaDataset:
"""Get and set the file meta information for the rgb secondary capture dicom
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ def pdfencapsconverter():
@pytest.fixture
def rgbscconverter():
yield Pdf2RgbSC()

@pytest.fixture
def pdfrepersonconverter():
yield Pdf2EncapsDCM(repersonalisation_fields=[
"PatientName",
"PatientID",
"PatientSex",
"StudyInstanceUID",
"AccessionNumber"
])
23 changes: 23 additions & 0 deletions tests/test_03_repersonalisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,26 @@ def test_03_2_name_missing(pdfencapsconverter):
assert dcm_ds.PatientName == ""

os.remove(stored_path)

@pytest.mark.reperson
def test_03_4_additional_fields_personlisation(pdfrepersonconverter):
path_pdf = "tests/test_data/test_file.pdf"
ref_dicom = "tests/test_data/CT_small_accession_number.dcm"

# with personalisation
stored_path = pdfrepersonconverter.run(path_pdf, ref_dicom)[0]

assert os.path.exists(stored_path)
assert pdfrepersonconverter.check_valid_dcm(stored_path)

dcm_ds = pydicom.dcmread(stored_path)
ref_dcm_ds = pydicom.dcmread(ref_dicom)

# check repersonaliation attribute
assert len(dcm_ds.EncapsulatedDocument) == 898332
assert dcm_ds.PatientName == ref_dcm_ds.PatientName
assert dcm_ds.PatientID == ref_dcm_ds.PatientID
assert dcm_ds.PatientSex == ref_dcm_ds.PatientSex
assert dcm_ds.AccessionNumber == ref_dcm_ds.AccessionNumber

os.remove(stored_path)
Binary file added tests/test_data/CT_small_accession_number.dcm
Binary file not shown.

0 comments on commit d99f434

Please sign in to comment.