From 722312421d565cb3d2286bfcaee6e82687cddf2e Mon Sep 17 00:00:00 2001 From: Dawn Smith Date: Thu, 16 Jan 2025 20:54:32 -0500 Subject: [PATCH] [FIX] Ensure ID modifications are applied to shared sessions --- datman/exporters.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/datman/exporters.py b/datman/exporters.py index 69c7d37e..db326d42 100644 --- a/datman/exporters.py +++ b/datman/exporters.py @@ -22,7 +22,8 @@ import datman.config import datman.dashboard import datman.scan -from datman.exceptions import UndefinedSetting, DashboardException +from datman.exceptions import (UndefinedSetting, DashboardException, + ConfigException) from datman.scanid import (parse_bids_filename, ParseException, make_filename, KCNIIdentifier) from datman.utils import (run, make_temp_directory, get_extension, @@ -857,18 +858,19 @@ def make_scan(self, file_stem): f"with error: {exc}") return if self.experiment.is_shared(): - self._make_linked(scan) + source_session = self._get_source_session() + self._make_linked(scan, source_session) self._add_bids_scan_name(scan, file_stem) self._add_side_car(scan, file_stem) self._update_conversion_errors(scan, file_stem) - def _make_linked(self, scan): + def _make_linked(self, scan, source_session): try: - source_session = datman.dashboard.get_session(self.experiment.name) + source_session = datman.dashboard.get_session(source_session) except datman.dashboard.DashboardException as exc: logger.error( f"Failed to link shared scan {scan} to source " - f"{self.experiment.name}. Reason - {exc}" + f"{source_session}. Reason - {exc}" ) return matches = [ @@ -878,7 +880,7 @@ def _make_linked(self, scan): ] if not matches or len(matches) > 1: logger.error( - f"Failed to link shared scan {scan} to {self.experiment.name}." + f"Failed to link shared scan {scan} to {source_session}." " Reason - Unable to find source scan database record." ) return @@ -886,6 +888,20 @@ def _make_linked(self, scan): scan.source_id = matches[0].id scan.save() + def _get_source_session(self): + """Get the ID of the source experiment for a shared XNATExperiment.""" + try: + config = datman.config.config(study=self.experiment.source_name) + except ConfigException: + return self.experiment.source_name + + try: + id_map = config.get_key('IdMap') + except UndefinedSetting: + return self.experiment.source_name + + return str(datman.scanid.parse(self.experiment.source_name, id_map)) + def _add_bids_scan_name(self, scan, dm_stem): """Add a bids format file name to a series in the QC database.