Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "run UID" to differentiate between staged directories that are created by different runs #23

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading