Esempio n. 1
0
def process_resources(xnat, ident, xnat_experiment):
    """Export any non-dicom resources from the XNAT archive"""
    logger.info("Extracting {} resources from {}".format(
        len(xnat_experiment.resource_files), xnat_experiment.name))

    base_path = os.path.join(cfg.get_path('resources'), str(ident))

    if not os.path.isdir(base_path):
        logger.info("Creating resources dir {}".format(base_path))
        try:
            os.makedirs(base_path)
        except OSError:
            logger.error("Failed creating resources dir {}".format(base_path))
            return

    for label in xnat_experiment.resource_IDs:
        if label == 'No Label':
            target_path = os.path.join(base_path, 'MISC')
        else:
            target_path = os.path.join(base_path, label)

        try:
            target_path = datman.utils.define_folder(target_path)
        except OSError:
            logger.error("Failed creating target folder: {}"
                         .format(target_path))
            continue

        xnat_resource_id = xnat_experiment.resource_IDs[label]

        try:
            resources = xnat.get_resource_list(xnat_experiment.project,
                                               xnat_experiment.subject,
                                               xnat_experiment.name,
                                               xnat_resource_id)
        except Exception as e:
            logger.error("Failed getting resource {} for experiment {}. "
                         "Reason - {}".format(xnat_resource_id,
                                              xnat_experiment.name,
                                              e))
            continue

        if not resources:
            continue

        for resource in resources:
            resource_path = os.path.join(target_path, resource['URI'])
            if os.path.isfile(resource_path):
                logger.debug("Resource {} from experiment {} already exists"
                             .format(resource['name'], xnat_experiment.name))

            else:
                logger.info("Downloading {} from experiment {}"
                            .format(resource['name'], xnat_experiment.name))
                download_resource(xnat,
                                  xnat_experiment,
                                  xnat_resource_id,
                                  resource['URI'],
                                  resource_path)
Esempio n. 2
0
def process_resources(xnat_project, session_label, experiment_label, data):
    """Export any non-dicom resources from the xnat archive"""
    global cfg
    logger.info('Extracting {} resources from {}'.format(
        len(data), session_label))
    base_path = os.path.join(cfg.get_path('resources'), session_label)
    if not os.path.isdir(base_path):
        logger.info('Creating dir:{}'.format(base_path))
        try:
            os.makedirs(base_path)
        except OSError:
            logger.error('Failed creating resources dir:{}.'.format(base_path))
            return

    for item in data['items']:
        try:
            data_type = item['data_fields']['label']
        except KeyError:
            data_type = 'misc'

        target_path = os.path.join(base_path, data_type)

        try:
            target_path = datman.utils.define_folder(target_path)
        except OSError:
            logger.error(
                'Failed creating target folder:{}'.format(target_path))
            continue

        xnat_resource_id = item['data_fields']['xnat_abstractresource_id']

        try:
            resources = xnat.get_resource_list(xnat_project, session_label,
                                               experiment_label,
                                               xnat_resource_id)
            if not resources:
                continue
        except Exception as e:
            logger.error('Failed getting resource:{} '
                         'for session:{} in project:{}'.format(
                             xnat_resource_id, session_label, e))
            continue

        for resource in resources:
            resource_path = os.path.join(target_path, resource['URI'])
            if os.path.isfile(resource_path):
                logger.debug('Resource:{} found for session:{}'.format(
                    resource['name'], session_label))
            else:
                logger.info('Resource:{} not found for session:{}'.format(
                    resource['name'], session_label))
                _ = get_resource(xnat_project, session_label, experiment_label,
                                 xnat_resource_id, resource['URI'],
                                 resource_path)

            check_duplicates(resource, base_path, target_path)