Skip to content

Commit

Permalink
Adding more attributes to the resources
Browse files Browse the repository at this point in the history
  • Loading branch information
gacou54 committed Aug 30, 2023
1 parent 8ec0e3f commit 29cb4c7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
44 changes: 35 additions & 9 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 @@ -96,15 +96,41 @@ def creation_date(self) -> datetime:
return util.make_datetime_from_dicom_date(date_string, time_string)

@property
def series_id(self) -> str:
"""Get the parent series identifier
Returns
-------
str
The parent series identifier.
"""
def series_identifier(self) -> str:
"""Get the parent series identifier"""
return self.get_main_information()['ParentSeries']

@property
def acquisition_number(self) -> int:
try:
return int(self.get_main_information()['MainDicomTags']['AcquisitionNumber'])
except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no AcquisitionNumber tag.')

@property
def image_orientation_patient(self) -> List[float]:
try:
orientation = self.get_main_information()['MainDicomTags']['ImageOrientationPatient']
return [float(i) for i in orientation.split('\\')]

except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no ImageOrientationPatient tag.')

@property
def image_position_patient(self) -> List[float]:
try:
position = self.get_main_information()['MainDicomTags']['ImagePositionPatient']
return [float(i) for i in position.split('\\')]

except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no ImagePositionPatient tag.')

@property
def instance_number(self) -> int:
try:
return int(self.get_main_information()['MainDicomTags']['InstanceNumber'])
except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no InstanceNumber tag.')

@property
def first_level_tags(self) -> Any:
Expand Down
19 changes: 6 additions & 13 deletions pyorthanc/resources/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,39 +95,32 @@ def modality(self) -> str:
return self.get_main_information()['MainDicomTags']['Modality']

@property
def series_number(self) -> str:
"""Get series number
Returns
-------
str
Series number.
"""
return self.get_main_information()['MainDicomTags']['SeriesNumber']
def series_number(self) -> int:
return int(self.get_main_information()['MainDicomTags']['SeriesNumber'])

@property
def performed_procedure_step_description(self):
def performed_procedure_step_description(self) -> str:
try:
return self.get_main_information()['MainDicomTags']['PerformedProcedureStepDescription']
except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no PerformedProcedureStepDescription tag.')

@property
def protocol_name(self):
def protocol_name(self) -> str:
try:
return self.get_main_information()['MainDicomTags']['ProtocolName']
except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no ProtocolName tag.')

@property
def station_name(self):
def station_name(self) -> str:
try:
return self.get_main_information()['MainDicomTags']['StationName']
except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no StationName tag.')

@property
def is_stable(self):
def is_stable(self) -> bool:
return self.get_main_information()['IsStable']

@property
Expand Down
10 changes: 5 additions & 5 deletions pyorthanc/resources/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,32 +125,32 @@ def series(self) -> List[Series]:
return [Series(i, self.client, self.lock) for i in series_ids]

@property
def accession_number(self):
def accession_number(self) -> str:
return self.get_main_information()['MainDicomTags']['AccessionNumber']

@property
def description(self):
def description(self) -> str:
try:
return self.get_main_information()['MainDicomTags']['StudyDescription']
except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no StudyDescription tag.')

@property
def institution_name(self):
def institution_name(self) -> str:
try:
return self.get_main_information()['MainDicomTags']['InstitutionName']
except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no InstitutionName tag.')

@property
def requested_procedure_description(self):
def requested_procedure_description(self) -> str:
try:
return self.get_main_information()['MainDicomTags']['RequestedProcedureDescription']
except KeyError:
raise errors.OptionalTagDoesNotExistError(f'{self} has no RequestedProcedureDescription tag.')

@property
def is_stable(self):
def is_stable(self) -> bool:
return self.get_main_information()['IsStable']

@property
Expand Down
10 changes: 9 additions & 1 deletion tests/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import pydicom
import pytest

from pyorthanc import errors
from .conftest import LABEL_INSTANCE
from .data import an_instance
from .data import a_series, an_instance

EXPECTED_DATE = datetime(
year=2010,
Expand All @@ -23,11 +24,18 @@ def test_attributes(instance):
assert type(instance.file_size) == int
assert instance.creation_date == EXPECTED_DATE
assert instance.labels == [LABEL_INSTANCE]
assert instance.series_identifier == a_series.IDENTIFIER
assert instance.image_orientation_patient == [1, 0, 0, 0, 1, 0]
assert instance.image_position_patient == [-223.9880065918, -158.08148193359, -117.78499603271]
assert instance.instance_number == int(an_instance.INFORMATION['MainDicomTags']['InstanceNumber'])

assert '0008,0012' in instance.tags.keys()
assert 'Value' in instance.tags['0008,0012'].keys()
assert str(instance) == f'Instance({an_instance.IDENTIFIER})'

with pytest.raises(errors.OptionalTagDoesNotExistError):
instance.acquisition_number


def test_get_tag_content(instance):
tag, expected_content = 'ManufacturerModelName', 'Pinnacle3'
Expand Down
4 changes: 2 additions & 2 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def test_attributes(series):
assert isinstance(series.last_update, datetime)
assert series.instances != []
assert str(series) == f'Series({a_series.IDENTIFIER})'
assert series.study_identifier
assert series.station_name == a_series.INFORMATION['MainDicomTags']['StationName']

with pytest.raises(errors.OptionalTagDoesNotExistError):
series.performed_procedure_step_description
with pytest.raises(errors.OptionalTagDoesNotExistError):
series.protocol_name
with pytest.raises(errors.OptionalTagDoesNotExistError):
series.station_name


def test_zip(series):
Expand Down

0 comments on commit 29cb4c7

Please sign in to comment.