示例#1
0
def process_archive(file_name, dicom_dir):
    """Upload data from a zip archive to the xnat server"""

    try:
        scanid = get_scanid(file_name)
    except datman.scanid.ParseException as e:
        logger.error("Failed to find valid identifier for {}. Reason: {}"
                     "".format(file_name, e))
        return

    # Make full path after ID conversion, in case user gave different naming
    # convention than file system uses.
    archive_file = os.path.join(dicom_dir, str(scanid) + ".zip")

    xnat = datman.xnat.get_connection(CFG,
                                      site=scanid.site,
                                      url=SERVER_OVERRIDE,
                                      auth=AUTH,
                                      server_cache=SERVERS)

    xnat_subject = get_xnat_subject(scanid, xnat)
    if not xnat_subject:
        # failed to get xnat info
        return

    exper_id = scanid.get_xnat_experiment_id()
    try:
        xnat_experiment = xnat_subject.experiments[exper_id]
    except KeyError:
        data_exists = False
        resource_exists = False
    else:
        try:
            data_exists, resource_exists = check_files_exist(archive_file,
                                                             xnat_experiment,
                                                             xnat)
        except Exception:
            logger.error("Failed checking xnat for experiment {}".format(
                exper_id))
            return

    if not data_exists:
        logger.info("Uploading dicoms from {}".format(archive_file))
        try:
            upload_dicom_data(archive_file, xnat_subject.project, scanid, xnat)
        except Exception as e:
            logger.error("Failed uploading archive {} to xnat project {} "
                         "for subject {}. Check Prearchive. Reason - {}"
                         .format(archive_file, xnat_subject.project,
                                 xnat_subject.name, e))

    if not resource_exists:
        logger.debug("Uploading resource from: {}".format(archive_file))
        try:
            upload_non_dicom_data(archive_file, xnat_subject.project, scanid,
                                  xnat)
        except Exception as e:
            logger.debug("An exception occurred: {}".format(e))
            pass
示例#2
0
def upload_dicom_data(archive, xnat_project, scanid, xnat):
    # XNAT API for upload fails if the zip contains a mix of dicom and nifti.
    # OPT CU definitely contains a mix and others may later on. Soooo
    # here's an ugly but effective fix! The niftis will get uploaded with
    # upload_non_dicom_data and added to resources - Dawn

    if not contains_niftis(archive):
        xnat.put_dicoms(xnat_project, scanid.get_xnat_subject_id(),
                        scanid.get_xnat_experiment_id(), archive)
        return

    # Need to account for when only niftis are available
    with datman.utils.make_temp_directory() as temp:
        new_archive = strip_niftis(archive, temp)

        if new_archive:
            xnat.put_dicoms(xnat_project, scanid.get_xnat_subject_id(),
                            scanid.get_xnat_experiment_id(), new_archive)
        else:
            logger.info("No dicoms exist within archive {}, skipping dicom "
                        "upload!".format(archive))
示例#3
0
def upload_non_dicom_data(archive, xnat_project, scanid, xnat):
    with zipfile.ZipFile(archive) as zf:
        resource_files = datman.utils.get_resources(zf)
        logger.info("Uploading {} files of non-dicom data...".format(
            len(resource_files)))
        uploaded_files = []
        for item in resource_files:
            # convert to HTTP language
            try:
                contents = zf.read(item)
                # By default files are placed in a MISC subfolder
                # if this is changed it may require changes to
                # check_duplicate_resources()
                xnat.put_resource(xnat_project, scanid.get_xnat_subject_id(),
                                  scanid.get_xnat_experiment_id(), item,
                                  contents, "MISC")
                uploaded_files.append(item)
            except Exception as e:
                logger.error("Failed uploading file {} with error:{}".format(
                    item, str(e)))
        return uploaded_files