Ejemplo n.º 1
0
def get_external_bids(scan_info, local_file):
    """Download an external T1 or T2 image.

    Query flywheel to find the correct acquisition and get its BIDS
    info. scan_info came from context.get_input('*_anatomy').
    """
    modality = scan_info['object']['modality']
    logger.info("Adding additional %s folder...", modality)
    external_acq = fw.get(scan_info['hierarchy']['id'])
    external_niftis = [f for f in external_acq.files if
                       f.name == scan_info['location']['name']]
    if not len(external_niftis) == 1:
        raise Exception("Unable to find location for extra %s" % modality)
    nifti = external_niftis[0]
    nifti_bids_path = bids_root / nifti.info['BIDS']['Path']
    json_bids_path = str(nifti_bids_path).replace(
        "nii.gz", ".json").replace(".nii", ".json")
    # Warn if overwriting: Should never happen on purpose
    if nifti_bids_path.exists():
        logger.warning("Overwriting current T1w image...")
    # Copy to / overwrite its place in BIDS
    local_file.replace(nifti_bids_path)

    # Download the sidecar
    export.download_sidecar(nifti.info, json_bids_path)
    assert PosixPath(json_bids_path).exists()
    assert nifti_bids_path.exists()
        path = "/flywheel/v0/output/bids_dataset/" + path
        fname = nifti.info['BIDS']['Filename']
        sidecar = nifti.info

        if not os.path.exists(path):
            os.makedirs(path)

        if os.path.isfile("/".join([path, fname])):
            logger.info("Overwriting current T1w image...")
            os.remove("/".join([path, fname]))
        t1_acq.download_file(nifti.name, "/".join([path, fname]))

        sidecar_name = fname.replace('.nii.gz', '.json')
        if os.path.isfile(path + sidecar_name):
            os.remove(path + sidecar_name)
        export.download_sidecar(sidecar, "/".join([path, sidecar_name]))

    if t2_acq:
        logger.info("Adding additional T2w folder...")

        nifti = [f for f in t2_acq.files if '.nii' in f.name].pop()
        path = nifti.info['BIDS']['Path']
        path = "/flywheel/v0/output/bids_dataset/" + path
        fname = nifti.info['BIDS']['Filename']
        sidecar = nifti.info

        if not os.path.exists(path):
            os.makedirs(path)

        if os.path.isfile("/".join([path, fname])):
            logger.info("Overwriting current T2w image...")