def share_redcap_record(session, shared_record): logger.debug( f"Sharing redcap record {shared_record.record_id} from participant " f"{shared_record.id} with ID {session}" ) target_session = dashboard.get_session(session) if not target_session: logger.error( f"Can't link redcap record in dashboard. {session} not found" ) return if target_session.redcap_record: logger.debug( f"Session {target_session} already has record " f"{target_session.redcap_record}" ) return source_session = dashboard.get_session(shared_record.id) if not source_session.redcap_record: logger.debug("Redcap record has not been added to original session " "yet, will re-attempt sharing later") return try: source_session.redcap_record.share_record(target_session) except Exception as e: logger.error(f"Failed to link redcap record. Reason: {e}")
def share_redcap_record(session, shared_record): logger.debug("Sharing redcap record {} from participant {} with ID " "{}".format(shared_record.record_id, shared_record.id, session)) target_session = dashboard.get_session(session) if not target_session: logger.error("Can't link redcap record in dashboard. Participant {} " "not found".format(session)) return if target_session.redcap_record: logger.debug("Session {} already has record {}".format( target_session, target_session.redcap_record)) return source_session = dashboard.get_session(shared_record.id) if not source_session.redcap_record: logger.debug("Redcap record has not been added to original session " "yet, will re-attempt sharing later") return try: source_session.redcap_record.share_record(target_session) except Exception as e: logger.error("Failed to link redcap record. Reason: {}".format(e))
def process_experiment(xnat, project, ident): experiment_label = ident.get_xnat_experiment_id() logger.info("Processing experiment: {}".format(experiment_label)) try: xnat_experiment = xnat.get_experiment(project, ident.get_xnat_subject_id(), experiment_label) except Exception as e: logger.error("Unable to retrieve experiment {} from XNAT server. " "{}: {}".format(experiment_label, type(e).__name__, e)) return if not db_ignore: logger.debug("Adding session {} to dashboard".format(experiment_label)) try: db_session = dashboard.get_session(ident, create=True) except dashboard.DashboardException as e: logger.error("Failed adding session {}. Reason: {}".format( experiment_label, e)) else: set_alt_ids(db_session, ident) set_date(db_session, xnat_experiment) if xnat_experiment.resource_files: process_resources(xnat, ident, xnat_experiment) if xnat_experiment.scans: process_scans(xnat, ident, xnat_experiment)
def add_session_redcap(record): record_id = record['record_id'] subject_id = record[cfg.get_key(['REDCAP_SUBJ'])].upper() if not datman.scanid.is_scanid(subject_id): try: subject_id = subject_id + '_01' datman.scanid.is_scanid(subject_id) except: logger.error('Invalid session: {}, skipping'.format(subject_id)) return try: ident = datman.scanid.parse(subject_id) except datman.scanid.ParseException: logger.error('Invalid session: {}, skipping'.format(subject_id)) return session_date = record[cfg.get_key(['REDCAP_DATE'])] try: session = dashboard.get_session(ident, date=session_date, create=True) except datman.exceptions.DashboardException as e: logger.error('Failed adding session {} to dashboard. Reason: {}'.format( ident, e)) try: session.add_redcap(record_id, redcap_project, redcap_url, instrument, date=session_date, comment=record[cfg.get_key(['REDCAP_COMMENTS'])], event_id=cfg.get_key(['REDCAP_EVENTID'])[record['redcap_event_name']], version=redcap_version) except: logger.error('Failed adding REDCap info for session {} to dashboard'.format(ident))
def add_session_redcap(record, subj_val, date_field, redcap_comments, event_key, redcap_project, redcap_url, instrument, redcap_version): record_id = record['record_id'] subject_id = record[subj_val].upper() if not datman.scanid.is_scanid(subject_id): try: subject_id = subject_id + '_01' datman.scanid.is_scanid(subject_id) except: logger.error('Invalid session: {}, skipping'.format(subject_id)) return try: ident = datman.scanid.parse(subject_id) except datman.scanid.ParseException: logger.error('Invalid session: {}, skipping'.format(subject_id)) return session_date = record[date_field] try: session = dashboard.get_session(ident, date=session_date, create=True) except datman.exceptions.DashboardException as e: logger.error('Failed adding session {} to dashboard. Reason: {}'.format( ident, e)) return try: session.add_redcap(record_id, redcap_project, redcap_url, instrument, date=session_date, comment=record[redcap_comments], event_id=event_key[record['redcap_event_name']], version=redcap_version) except: logger.error('Failed adding REDCap info for session {} to dashboard'.format(ident))
def add_session_redcap(record, record_key): record_id = record[record_key] subject_id = record[cfg.get_key('RedcapSubj')].upper() if not datman.scanid.is_scanid(subject_id): subject_id = subject_id + '_01' try: datman.scanid.is_scanid(subject_id) except datman.scanid.ParseException: logger.error('Invalid session: {}, skipping'.format(subject_id)) return try: ident = parse_id(subject_id) except datman.scanid.ParseException: logger.error('Invalid session: {}, skipping'.format(subject_id)) return session_date = record[cfg.get_key('RedcapDate')] try: session = dashboard.get_session(ident, date=session_date, create=True) except datman.exceptions.DashboardException as e: logger.error('Failed adding session {} to dashboard. Reason: ' '{}'.format(ident, e)) return try: record_comment = record[cfg.get_key('RedcapComments')] event_id = cfg.get_key('RedcapEventId')[record['redcap_event_name']] except (datman.config.UndefinedSetting, datman.config.ConfigException): logger.error("Can't add REDCap session info. Verify that " "values 'RedcapComments' and 'RedcapEventId' are " "correctly defined in the config file") return except KeyError: record_comment = None event_id = None try: session.add_redcap(record_id, session_date, project=redcap_project, url=redcap_url, instrument=instrument, comment=record_comment, event_id=event_id, redcap_version=redcap_version) except Exception: logger.error('Failed adding REDCap info for session {} to ' 'dashboard'.format(ident))
def add_to_dashboard(session, task_file): if not dashboard.dash_found: return db_session = dashboard.get_session(session) if not db_session: logger.info("{} not yet in dashboard database. Cannot add task file " "{}".format(session, task_file)) return task = db_session.add_task(task_file) if not task: logger.error("Failed to add task file {} to dashboard " "database".format(task_file)) return
def _fetch_checklist(subject=None, study=None, config=None): """ Support function for read_checklist(). Gets a list of existing / signed off sessions from the dashboard. The checklist.csv file dropped the session number, so only information on the first session is reported to maintain consistency. :( Returns a dictionary formatted like that of '_parse_checklist' or a string comment if the 'subject' argument was given """ if not (subject or study or config): raise MetadataException( "Can't retrieve dashboard checklist " "contents without either 1) a subject or study ID 2) a " "datman.config object") if subject: session = dashboard.get_session(subject) if not session: return if session.signed_off: return str(session.reviewer) return '' if config and not study: study = config.study_name db_study = dashboard.get_project(study) entries = {} for timepoint in db_study.timepoints: if timepoint.is_phantom or not len(timepoint.sessions): continue session = timepoint.sessions.values()[0] if session.signed_off: comment = str(session.reviewer) else: comment = '' str_name = timepoint.name.encode('utf-8') entries[str_name] = comment return entries
def add_session_redcap(record, record_key): record_id = record[record_key] subject_id = record[cfg.get_key('REDCAP_SUBJ')].upper() if not datman.scanid.is_scanid(subject_id): try: subject_id = subject_id + '_01' datman.scanid.is_scanid(subject_id) except: logger.error('Invalid session: {}, skipping'.format(subject_id)) return try: ident = datman.scanid.parse(subject_id) except datman.scanid.ParseException: logger.error('Invalid session: {}, skipping'.format(subject_id)) return session_date = record[cfg.get_key('REDCAP_DATE')] try: session = dashboard.get_session(ident, date=session_date, create=True) except datman.exceptions.DashboardException as e: logger.error( 'Failed adding session {} to dashboard. Reason: {}'.format( ident, e)) return try: session.add_redcap(record_id, redcap_project, redcap_url, instrument, date=session_date, comment=record[cfg.get_key('REDCAP_COMMENTS')], event_id=cfg.get_key('REDCAP_EVENTID')[ record['redcap_event_name']], version=redcap_version) except: logger.error( 'Failed adding REDCap info for session {} to dashboard'.format( ident))
def _fetch_checklist( subject=None, study=None, config=None, bids_id=None, bids_ses=None, use_bids=None, ): """ Support function for read_checklist(). Gets a list of existing / signed off sessions from the dashboard. The checklist.csv file dropped the session number, so only information on the first session is reported to maintain consistency. :( Returns a dictionary formatted like that of '_parse_checklist' or a string comment if the 'subject' argument was given. If 'use_bids' is specified, the checklist will be organized with BIDS IDs instead of datman IDs. Subjects missing a bids ID will be omitted. """ if not (subject or study or config): raise MetadataException("Can't retrieve dashboard checklist " "contents without either 1) a subject or " "study ID 2) a datman.config object") if subject: session = dashboard.get_session(subject) if bids_id: if not (study and bids_ses): raise MetadataException( "Cant retrieve checklist entry for BIDS " f"ID {bids_id} without a study and BIDS session " "number") session = dashboard.get_bids_subject(bids_id, bids_ses, study=study) if subject or bids_id: if not session: return if session.is_qcd(): return str(session.reviewer) return "" if config and not study: study = config.study_name db_study = dashboard.get_project(study) entries = {} for timepoint in db_study.timepoints: if timepoint.is_phantom or not len(timepoint.sessions): continue session = list(timepoint.sessions.values())[0] if session.signed_off: comment = str(session.reviewer) else: comment = "" if use_bids: if not timepoint.bids_name: # If bids is requested ignore subjects without a bids name continue str_name = timepoint.bids_name else: str_name = timepoint.name entries[str_name] = comment return entries
def process_session(session): xnat_project = session[0] session_label = session[1] logger.info("Processing session: {}".format(session_label)) # session_label should be a valid datman scanid try: ident = datman.scanid.parse(session_label) except datman.scanid.ParseException: logger.error("Invalid session: {}. Skipping".format(session_label)) return # check that the session is valid on XNAT try: xnat.get_session(xnat_project, session_label) except Exception as e: logger.error("Error while getting session {} from XNAT. " "Message: {}".format(session_label, e.message)) return # look into XNAT project and get list of experiments try: experiments = xnat.get_experiments(xnat_project, session_label) except Exception as e: logger.warning("Failed getting experiments for: {} in project: {} " "with reason: {}".format(session_label, xnat_project, e)) return # we expect exactly 1 experiment per session if len(experiments) > 1: logger.error("Found more than one experiment for session: {} " "in study: {}. Skipping".format(session_label, xnat_project)) return if not experiments: logger.error("Session: {} in study: {} has no experiments".format( session_label, xnat_project)) return experiment_label = experiments[0]['label'] # experiment_label should be the same as the session_label if not experiment_label == session_label: logger.warning( "Experiment label: {} doesn't match session label: {}".format( experiment_label, session_label)) # retrieve json table from project --> session --> experiment try: experiment = xnat.get_experiment(xnat_project, session_label, experiment_label) except Exception as e: logger.error( "Failed getting experiment for session: {} with reason".format( session_label, e)) return if not experiment: logger.warning( "No experiments found for session: {}".format(session_label)) return if not db_ignore: logger.debug("Adding session {} to dashboard".format(session_label)) try: db_session = dashboard.get_session(ident, create=True) except dashboard.DashboardException as e: logger.error("Failed adding session {}. Reason: {}".format( session_label, e)) else: set_date(db_session, experiment) # experiment['children'] is a list of top level folders in XNAT # project --> session --> experiments for data in experiment['children']: if data['field'] == 'resources/resource': process_resources(xnat_project, session_label, experiment_label, data) elif data['field'] == 'scans/scan': process_scans(ident, xnat_project, session_label, experiment_label, data) else: logger.warning("Unrecognised field type: {} for experiment: {} " "in session: {} from study: {}".format( data['field'], experiment_label, session_label, xnat_project))