Beispiel #1
0
def get_dicom_archive_from_xnat(xnat_project, session_label, experiment_label,
                                series):
    """Downloads and extracts a dicom archive from xnat to a local temp folder
    Returns the path to the tempdir (for later cleanup) as well as the
    path to the .dcm files inside the tempdir
    """
    # going to create a local directory and make a copy of the
    # dicom files there
    tempdir = tempfile.mkdtemp(prefix='dm2_xnat_extract_')
    logger.debug('Downloading dicoms for:{}, series:{}.'.format(
        session_label, series))
    try:
        dicom_archive = xnat.get_dicom(xnat_project, session_label,
                                       experiment_label, series)
    except Exception as e:
        logger.error(
            'Failed to download dicom archive for:{}, series:{}'.format(
                session_label, series))
        return None, None

    logger.debug('Unpacking archive')

    try:
        with zipfile.ZipFile(dicom_archive[1], 'r') as myzip:
            myzip.extractall(tempdir)
    except:
        logger.error('An error occurred unpacking dicom archive for:{}'
                     ' skipping'.format(session_label))
        os.remove(dicom_archive[1])
        return None, None

    logger.debug('Deleting archive file')
    os.remove(dicom_archive[1])
    # get the root dir for the extracted files
    archive_files = []
    for root, dirname, filenames in os.walk(tempdir):
        for filename in filenames:
            f = os.path.join(root, filename)
            if is_valid_dicom(f):
                archive_files.append(f)

    try:
        base_dir = os.path.dirname(archive_files[0])
    except IndexError:
        logger.warning(
            'There were no valid dicom files in xnat session:{}, series:{}'.
            format(session_label, series))
        shutil.rmtree(tempdir)
        return None, None
    return (tempdir, base_dir)
Beispiel #2
0
def get_dicom_archive_from_xnat(xnat_project, session_label, experiment_label,
                                series, tempdir):
    """
    Downloads and extracts a dicom archive from XNAT to a local temp folder
    Returns the path to the tempdir (for later cleanup) as well as the
    path to the .dcm files inside the tempdir
    """
    # make a copy of the dicom files in a local directory
    logger.info("Downloading dicoms for: {}, series: {}".format(
        session_label, series))
    try:
        dicom_archive = xnat.get_dicom(xnat_project, session_label,
                                       experiment_label, series)
    except Exception as e:
        logger.error(
            "Failed to download dicom archive for: {}, series: {}".format(
                session_label, series))
        return None

    logger.info("Unpacking archive")

    try:
        with zipfile.ZipFile(dicom_archive, 'r') as myzip:
            myzip.extractall(tempdir)
    except:
        logger.error(
            "An error occurred unpacking dicom archive for: {}. Skipping".
            format(session_label))
        os.remove(dicom_archive)
        return None

    logger.info("Deleting archive file")
    os.remove(dicom_archive)

    # get the root dir for the extracted files
    archive_files = []
    for root, dirname, filenames in os.walk(tempdir):
        for filename in filenames:
            f = os.path.join(root, filename)
            if is_valid_dicom(f):
                archive_files.append(f)

    try:
        base_dir = os.path.dirname(archive_files[0])
    except IndexError:
        logger.warning(
            "There were no valid dicom files in XNAT session: {}, series: {}".
            format(session_label, series))
        return None
    return base_dir