Skip to content

Commit

Permalink
Modify instance
Browse files Browse the repository at this point in the history
  • Loading branch information
gacou54 committed Sep 6, 2023
1 parent 0db2da8 commit 150184c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
59 changes: 56 additions & 3 deletions pyorthanc/_resources/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pydicom

from .resource import Resource
from .. import util
from .. import errors, util


class Instance(Resource):
Expand Down Expand Up @@ -183,7 +183,7 @@ def anonymize(self, remove: List = None, replace: Dict = None, keep: List = None
private_creator: str = None, force: bool = False, dicom_version: str = None) -> bytes:
"""Anonymize Instance
If no error has been raise, then it creates a new anonymous series.
If no error has been raise, then it creates a new anonymous instance.
Documentation: https://book.orthanc-server.com/users/anonymization.html
Parameters
Expand All @@ -195,7 +195,7 @@ def anonymize(self, remove: List = None, replace: Dict = None, keep: List = None
keep
List of tag to keep unchanged
force
Some tags can't be changed without forcing it (e.g. PatientID) for security reason
Some tags can't be changed without forcing it (e.g. SOPInstanceUID) for security reason
keep_private_tags
If True, keep the private tags from the DICOM instances.
keep_source
Expand Down Expand Up @@ -231,6 +231,59 @@ def anonymize(self, remove: List = None, replace: Dict = None, keep: List = None

return self.client.post_instances_id_anonymize(self.id_, data)

def modify(self, remove: List = None, replace: Dict = None, keep: List = None,
remove_private_tags: bool = False, keep_source: bool = True,
private_creator: str = None, force: bool = False) -> bytes:
"""Modify Instance
If no error has been raise, then it creates a new modified instance.
Documentation: https://book.orthanc-server.com/users/anonymization.html
Parameters
----------
remove
List of tag to remove
replace
Dictionary of {tag: new_content}
keep
Keep the original value of the specified tags, to be chosen among the StudyInstanceUID,
SeriesInstanceUID and SOPInstanceUID tags. Avoid this feature as much as possible,
as this breaks the DICOM model of the real world.
force
Some tags can't be changed without forcing it (e.g. SOPInstanceUID) for security reason
remove_private_tags
If True, remove the private tags from the DICOM instances.
keep_source
If False, instructs Orthanc to the remove original resources.
By default, the original resources are kept in Orthanc.
private_creator
The private creator to be used for private tags in replace.
Returns
-------
bytes
Raw bytes of the modified instance.
"""
remove = [] if remove is None else remove
replace = {} if replace is None else replace
keep = [] if keep is None else keep

if 'SOPInstanceUID' in replace and not force:
raise errors.ModificationError('If SOPInstanceUID is replaced, `force` must be `True`')

data = {
'Remove': remove,
'Replace': replace,
'Keep': keep,
'Force': force,
'RemovePrivateTags': remove_private_tags,
'KeepSource': keep_source,
}
if private_creator is not None:
data['PrivateCreator'] = private_creator

return self.client.post_instances_id_modify(self.id_, data)

def get_pydicom(self) -> pydicom.FileDataset:
"""Retrieve a pydicom.FileDataset object corresponding to the instance."""
return util.get_pydicom(self.client, self.id_)
6 changes: 6 additions & 0 deletions tests/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def test_anonymize(instance):
assert type(anonymized_instance) == bytes


def test_modify(instance):
modified_instance = instance.modify(replace={'NumberOfFrames': '10'})

assert type(modified_instance) == bytes


def test_pydicom(instance):
result = instance.get_pydicom()

Expand Down

0 comments on commit 150184c

Please sign in to comment.