Skip to content

Commit

Permalink
added test coverage and debugged imaging session save
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Feb 12, 2024
1 parent 22b584b commit a694d06
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
16 changes: 13 additions & 3 deletions xnat_ingest/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import subprocess as sp
from functools import cached_property
import shutil
from copy import deepcopy
import yaml
from tqdm import tqdm
import attrs
Expand Down Expand Up @@ -295,7 +296,7 @@ def get_id(field):
return sessions

@classmethod
def load(cls, save_dir: Path) -> "ImagingSession":
def load(cls, save_dir: Path, ignore_manifest: bool = False) -> "ImagingSession":
"""Override IDs extracted from DICOM metadata with manually specified IDs loaded
from a YAML
Expand All @@ -310,7 +311,7 @@ def load(cls, save_dir: Path) -> "ImagingSession":
the loaded session
"""
yaml_file = save_dir / cls.SAVE_FILENAME
if yaml_file.exists():
if yaml_file.exists() and not ignore_manifest:
# Load session from YAML file metadata
try:
with open(yaml_file) as f:
Expand Down Expand Up @@ -342,6 +343,8 @@ def load(cls, save_dir: Path) -> "ImagingSession":
# Load session based on directory structure
scans = []
for scan_dir in save_dir.iterdir():
if not scan_dir.is_dir():
continue
scan_id, scan_type = scan_dir.name.split("-")
scan_resources = {}
for resource_dir in scan_dir.iterdir():
Expand Down Expand Up @@ -373,9 +376,15 @@ def save(self, save_dir: Path) -> "ImagingSession":
save_dir: Path
the path to save the session metadata into (NB: the data is typically also
stored in the directory structure of the session, but this is not necessary)
Returns
-------
saved : ImagingSession
a copy of the session with updated paths
"""
dct = attrs.asdict(self, recurse=False)
dct["scans"] = {}
saved = deepcopy(self)
for scan in self.scans.values():
resources_dict = {}
for resource_name, fileset in scan.resources.items():
Expand All @@ -386,7 +395,7 @@ def save(self, save_dir: Path) -> "ImagingSession":
fileset = fileset.copy(
resource_dir, mode=fileset.CopyMode.hardlink_or_copy
)
self.scans[scan.id].resources[resource_name] = fileset
saved.scans[scan.id].resources[resource_name] = fileset
resources_dict[resource_name] = {
"datatype": to_mime(fileset, official=False),
"fspaths": [
Expand All @@ -405,6 +414,7 @@ def save(self, save_dir: Path) -> "ImagingSession":
dct,
f,
)
return saved

def stage(
self,
Expand Down
25 changes: 21 additions & 4 deletions xnat_ingest/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,25 @@ def test_session_select_resources(


def test_session_save_roundtrip(tmp_path: Path, imaging_session: ImagingSession):
imaging_session.save(tmp_path)
reloaded = ImagingSession.load(tmp_path)

assert reloaded is not imaging_session
assert reloaded == imaging_session
save_dir = tmp_path / imaging_session.project_id / imaging_session.subject_id / imaging_session.session_id
save_dir.mkdir(parents=True)

saved = imaging_session.save(save_dir)
reloaded = ImagingSession.load(save_dir)

assert reloaded is not saved
assert reloaded == saved

reloaded.save(save_dir)
rereloaded = ImagingSession.load(save_dir)

assert rereloaded == saved

loaded_no_manifest = ImagingSession.load(save_dir, ignore_manifest=True)

for scan in loaded_no_manifest.scans.values():
for key, resource in list(scan.resources.items()):
if key == "DICOM":
scan.resources[key] = DicomSeries(resource)
assert loaded_no_manifest == saved

0 comments on commit a694d06

Please sign in to comment.