Skip to content

Commit

Permalink
only move scans on save if they are not in the save directory
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Feb 9, 2024
1 parent 435a0ff commit 0178674
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
8 changes: 1 addition & 7 deletions xnat_ingest/cli/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def stage(
# Deidentify files and save them to the staging directory
staged_session = session.stage(
session_staging_dir, associated_files=associated_files,
delete_original=delete,
remove_original=delete,
deidentify=deidentify,
)
staged_session.save(session_staging_dir)
Expand All @@ -206,9 +206,3 @@ def stage(
continue
else:
raise
else:
if delete:
session.delete()
logger.info("Staged and deleted %s session", session.name)
else:
logger.info("Staged %s session to %s", session.name, str(session_staging_dir))
34 changes: 12 additions & 22 deletions xnat_ingest/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,32 +372,23 @@ 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
the saved session with the updated file-system paths
"""
dct = attrs.asdict(self, recurse=False)
dct["scans"] = {}
scans_dir = save_dir / "scans"
if scans_dir.exists():
shutil.rmtree(scans_dir)
scans_dir.mkdir()
saved = deepcopy(self)
for scan in self.scans.values():
resources_dict = {}
for resource_name, fileset in scan.resources.items():
resource_dir = scans_dir / f"{scan.id}-{scan.type}" / resource_name
resource_dir.mkdir(parents=True)
fileset_copy = fileset.copy(
resource_dir, mode=fileset.CopyMode.hardlink_or_copy
)
resources_dict[resource_name] = {
"datatype": to_mime(fileset, official=False),
"fspaths": [str(p) for p in fileset_copy.fspaths],
}
saved.scans[scan.id].resources[resource_name] = fileset_copy
resource_dir = save_dir / f"{scan.id}-{scan.type}" / resource_name
if fileset.parent != resource_dir:
resource_dir.mkdir(parents=True, exist_ok=True)
fileset_copy = fileset.copy(
resource_dir, mode=fileset.CopyMode.hardlink_or_copy
)
resources_dict[resource_name] = {
"datatype": to_mime(fileset, official=False),
"fspaths": [str(p) for p in fileset_copy.fspaths],
}
self.scans[scan.id].resources[resource_name] = fileset_copy
dct["scans"][scan.id] = {
"type": scan.type,
"resources": resources_dict,
Expand All @@ -408,7 +399,6 @@ def save(self, save_dir: Path) -> "ImagingSession":
dct,
f,
)
return saved

def stage(
self,
Expand Down Expand Up @@ -457,7 +447,7 @@ def stage(
for scan in tqdm(self.scans.values(), f"Staging DICOM sessions to {dest_dir}"):
staged_resources = {}
for resource_name, fileset in scan.resources.items():
scan_dir = dest_dir / scan.id / resource_name
scan_dir = dest_dir / f"{scan.id}-{scan.type}" / resource_name
scan_dir.mkdir(parents=True, exist_ok=True)
if isinstance(fileset, DicomSeries):
staged_dicom_paths = []
Expand Down

0 comments on commit 0178674

Please sign in to comment.