示例#1
0
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)
示例#2
0
def xnat_to_bids(xnat, project, ident, dcm2bids_opt):
    bids_sub = ident.get_bids_name()
    bids_ses = ident.timepoint
    experiment_label = ident.get_xnat_experiment_id()

    try:
        xnat_experiment = xnat.get_experiment(project,
                                              ident.get_xnat_subject_id(),
                                              experiment_label)
    except datman.exceptions.XnatException as e:
        logger.error("Unable to retrieve experiment {} from XNAT server. "
                     "{}: {}".format(experiment_label,
                                     type(e).__name__, e))
        return

    with datman.utils.make_temp_directory(prefix='xnat_to_bids_') as tempdir:
        for scan in xnat_experiment.scans:
            scan_temp = get_dicom_archive_from_xnat(xnat, scan, tempdir)
            if not scan_temp:
                logger.error("Failed getting series {} for experiment {} "
                             "from XNAT".format(scan.series, scan.experiment))
                return

        sub_dcm_dir = os.path.join(tempdir, experiment_label, "scans")
        try:
            dcm2bids_app = Dcm2bids(sub_dcm_dir,
                                    bids_sub,
                                    dcm2bids_opt.dcm2bids_config,
                                    output_dir=dcm2bids_opt.bids_out,
                                    session=bids_ses,
                                    clobber=dcm2bids_opt.clobber,
                                    forceDcm2niix=dcm2bids_opt.force_dcm2niix,
                                    log_level="INFO")
            dcm2bids_app.run()
        except Exception as e:
            logger.error("Dcm2Bids failed to run for experiment {}. {}:"
                         " {}".format(experiment_label,
                                      type(e).__name__, e))
            return
示例#3
0
def process_session(session):
    xnat_project = session[0]
    session_label = session[1]

    logger.info('Processing session:{}'.format(session[1]))

    # check the session is valid on xnat
    try:
        xnat.get_session(xnat_project, session_label)
    except Exception as e:
        return

    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

    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']
    # sesssion_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

    # experiment_label should be the same as the session_label
    if not experiment_label == session_label:
        logger.warning(
            'Experiment label:{} doesnt match session_label:{}'.format(
                experiment_label, session_label))

    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 dashboard:
        logger.debug('Adding session:{} to db'.format(session_label))
        try:
            db_session_name = ident.get_full_subjectid_with_timepoint()
            db_session = dashboard.get_add_session(
                db_session_name,
                date=experiment['data_fields']['date'],
                create=True)
            if ident.session and int(ident.session) > 1:
                db_session.is_repeated = True
                db_session.repeat_count = int(ident.session)

        except datman.dashboard.DashboardException as e:
            logger.error(
                'Failed adding session:{} to dashboard'.format(session_label))

    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(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))
示例#4
0
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))