Skip to content

Commit

Permalink
Merge pull request #23 from Australian-Imaging-Service/staging-timest…
Browse files Browse the repository at this point in the history
…amps

Add "run UID" to differentiate between staged directories that are created by different runs
  • Loading branch information
tclose authored Oct 22, 2024
2 parents e0d753a + 376d774 commit 8f4877d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion xnat_ingest/cli/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def do_stage() -> None:
else:
raise

if loop:
if loop is not None:
while True:
start_time = datetime.datetime.now()
do_stage()
Expand Down
2 changes: 1 addition & 1 deletion xnat_ingest/cli/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def do_upload() -> None:
else:
assert False

if loop:
if loop is not None:
while True:
start_time = datetime.datetime.now()
do_upload()
Expand Down
27 changes: 25 additions & 2 deletions xnat_ingest/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import logging
from functools import cached_property
import random
import hashlib
from datetime import datetime
import string
from itertools import chain
from collections import defaultdict, Counter
Expand Down Expand Up @@ -44,6 +46,7 @@ class ImagingSession:
converter=scans_converter,
validator=attrs.validators.instance_of(dict),
)
run_uid: ty.Optional[str] = attrs.field(default=None)

def __attrs_post_init__(self) -> None:
for scan in self.scans.values():
Expand Down Expand Up @@ -296,6 +299,16 @@ def from_paths(
else:
fspaths = [Path(p) for p in glob(files_path, recursive=True)]

# Create a UID out of the paths that session was created from and the
# timestamp
crypto = hashlib.sha256()
for fspath in fspaths:
crypto.update(str(fspath.absolute()).encode())
run_uid: str = crypto.hexdigest()[:6] + datetime.strftime(
datetime.now(),
"%Y%m%d%H%M%S",
)

from_paths_kwargs = {}
if datatypes is DicomSeries:
from_paths_kwargs["specific_tags"] = [
Expand Down Expand Up @@ -387,6 +400,7 @@ def get_id(field_type: str, field_name: str) -> str:
project_id=project_id,
subject_id=subject_id,
visit_id=visit_id,
run_uid=run_uid,
)
sessions[session_uid] = session
else:
Expand Down Expand Up @@ -594,11 +608,17 @@ def load(
ImagingSession
the loaded session
"""
project_id, subject_id, visit_id = session_dir.name.split("-")
parts = session_dir.name.split("-")
if len(parts) == 4:
project_id, subject_id, visit_id, run_uid = parts
else:
project_id, subject_id, visit_id = parts
run_uid = None
session = cls(
project_id=project_id,
subject_id=subject_id,
visit_id=visit_id,
run_uid=run_uid,
)
for scan_dir in session_dir.iterdir():
if scan_dir.is_dir():
Expand Down Expand Up @@ -661,7 +681,10 @@ def save(
project_id = self.project_id
else:
project_id = "INVALID_UNRECOGNISED_" + self.project_id
session_dir = dest_dir / "-".join((project_id, self.subject_id, self.visit_id))
session_dirname = "-".join((project_id, self.subject_id, self.visit_id))
if self.run_uid:
session_dirname += f"-{self.run_uid}"
session_dir = dest_dir / session_dirname
session_dir.mkdir(parents=True, exist_ok=True)
for scan in tqdm(self.scans.values(), f"Staging sessions to {session_dir}"):
saved_scan = scan.save(session_dir, copy_mode=copy_mode)
Expand Down
2 changes: 1 addition & 1 deletion xnat_ingest/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def create_data_tree(
id: str,
leaves: ty.List[ty.Tuple[str, ...]],
hierarchy: ty.List[str],
axes: type,
axes: ty.Type[Axes],
**kwargs: ty.Any,
) -> DataTree:
raise NotImplementedError
Expand Down

0 comments on commit 8f4877d

Please sign in to comment.