Skip to content

Commit

Permalink
updated code to match metadata access in fileformats
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Mar 1, 2024
1 parent 7351e26 commit dad811e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion xnat_ingest/cli/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
help=("The keyword or tag of the DICOM field to extract the XNAT subject ID from "),
)
@click.option(
"--session-field",
"--visit-field",
type=DicomField,
default="AccessionNumber",
envvar="XNAT_INGEST_SESSION",
Expand Down
12 changes: 6 additions & 6 deletions xnat_ingest/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def make_session_id(cls, project_id, subject_id, visit_id):

@cached_property
def modalities(self) -> ty.Set[str]:
modalities = self["Modality"]
modalities = self.metadata["Modality"]
if not isinstance(modalities, str):
modalities = set(tuple(m) if not isinstance(m, str) else m for m in modalities)
return modalities
Expand Down Expand Up @@ -180,10 +180,10 @@ def metadata(self):
common_keys = [
k for k in set(chain(*all_keys)) if all(k in keys for keys in all_keys)
]
collated = {k: all_dicoms[0][k] for k in common_keys}
collated = {k: all_dicoms[0].metadata[k] for k in common_keys}
for i, series in enumerate(all_dicoms[1:], start=1):
for key in common_keys:
val = series[key]
val = series.metadata[key]
if val != collated[key]:
# Check whether the value is the same as the values in the previous
# images in the series
Expand Down Expand Up @@ -266,7 +266,7 @@ def from_dicoms(
):
# Restrict the metadata fields that are loaded (others are ignored),
# for performance reasons
dicom_sessions[series["StudyInstanceUID"]].append(series)
dicom_sessions[series.metadata["StudyInstanceUID"]].append(series)

# Construct sessions from sorted series
logger.info("Searching for associated files ")
Expand Down Expand Up @@ -295,12 +295,12 @@ def get_id(field):

scans = []
for dicom_series in session_dicom_series:
series_description = dicom_series["SeriesDescription"]
series_description = dicom_series.metadata["SeriesDescription"]
if isinstance(series_description, list):
series_description = series_description[0]
scans.append(
ImagingScan(
id=str(dicom_series["SeriesNumber"]),
id=str(dicom_series.metadata["SeriesNumber"]),
type=str(series_description),
resources={"DICOM": dicom_series},
)
Expand Down
16 changes: 8 additions & 8 deletions xnat_ingest/tests/test_dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def dicom_series(scope="module") -> ImagingSession:
return DicomSeries(get_pet_image().iterdir())


def test_mrtrix_dicom_metadata(dicom_series):
def test_mrtrix_dicom_metadata(dicom_series: DicomSeries):
keys = [
"AccessionNumber",
"PatientID",
Expand All @@ -27,10 +27,10 @@ def test_mrtrix_dicom_metadata(dicom_series):
dicom_series.select_metadata(keys)

assert sorted(dicom_series.metadata) == sorted(keys + ['SpecificCharacterSet'])
assert dicom_series["PatientName"] == "GivenName^FamilyName"
assert dicom_series["AccessionNumber"] == "987654321"
assert dicom_series["PatientID"] == 'Session Label'
assert dicom_series["StudyID"] == "PROJECT_ID"
assert not isinstance(dicom_series["StudyInstanceUID"], list)
assert isinstance(dicom_series["SOPInstanceUID"], list)
assert len(dicom_series["SOPInstanceUID"]) == len(list(dicom_series.contents))
assert dicom_series.metadata["PatientName"] == "GivenName^FamilyName"
assert dicom_series.metadata["AccessionNumber"] == "987654321"
assert dicom_series.metadata["PatientID"] == 'Session Label'
assert dicom_series.metadata["StudyID"] == "PROJECT_ID"
assert not isinstance(dicom_series.metadata["StudyInstanceUID"], list)
assert isinstance(dicom_series.metadata["SOPInstanceUID"], list)
assert len(dicom_series.metadata["SOPInstanceUID"]) == len(list(dicom_series.contents))
4 changes: 2 additions & 2 deletions xnat_ingest/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def imaging_session() -> ImagingSession:
]
scans = [
ImagingScan(
id=str(d["SeriesNumber"]),
type=str(d["SeriesDescription"]),
id=str(d.metadata["SeriesNumber"]),
type=str(d.metadata["SeriesDescription"]),
resources={"DICOM": d}) for d in dicoms]
return ImagingSession(
project_id="PROJECTID",
Expand Down

0 comments on commit dad811e

Please sign in to comment.