Esempio n. 1
0
def retrieve_nifti(peaks,retrieve,location_key):
    '''retrieve_nifti
    Download the image to a temporary folder if the user needs to
    retrieve it. Otherwise, return file
    Parameters
    ==========
    peaks: dict
        dictionary (key, is ttl_file, and value, is dictionary of {filename:fullpath}
        for all brainmaps extracted from the ttl files
    retrieve: boolean
        if True, will download brainmaps to temporary folder first. If false, encodes
        path in utf-8 for rendering in javascript
    location_key: str
        key to look up file name in peaks dictionary
    '''
    # Note: retrieve = True has not been tested!
    updated_peaks = dict()
    for nidm,entries in peaks.items():
        if retrieve:
            for e in range(len(entries)):
                if location_key in entries[e]:
                    brainmap = entries[e][location_key]
                    image_ext = get_extension(brainmap)
                    temp_path = get_random_name()
                    temp_dir = tempfile.mkdtemp()
                    temp_image_path = "%s/%s.%s" %(temp_dir,temp_path,image_ext)
                    if download_file(brainmap,temp_image_path):
                        entries[e][location_key] = temp_image_path
        updated_peaks[nidm] = entries
    return updated_peaks
Esempio n. 2
0
def retrieve_nifti(peaks,retrieve,location_key):
    '''retrieve_nifti
    Download the image to a temporary folder if the user needs to
    retrieve it. Otherwise, return file
    Parameters
    ==========
    peaks: dict
        dictionary (key, is ttl_file, and value, is dictionary of {filename:fullpath}
        for all brainmaps extracted from the ttl files
    retrieve: boolean
        if True, will download brainmaps to temporary folder first. If false, encodes
        path in utf-8 for rendering in javascript
    location_key: str
        key to look up file name in peaks dictionary
    '''
    # Note: retrieve = True has not been tested!
    updated_peaks = dict()
    for nidm,entries in peaks.iteritems():
        if retrieve:
            for e in range(len(entries)):
                if location_key in entries[e]:
                    brainmap = entries[e][location_key]
                    image_ext = get_extension(brainmap)
                    temp_path = get_random_name()
                    temp_dir = tempfile.mkdtemp()
                    temp_image_path = "%s/%s.%s" %(temp_dir,temp_path,image_ext)
                    if download_file(brainmap,temp_image_path):
                        entries[e][location_key] = temp_image_path
        updated_peaks[nidm] = entries
    return updated_peaks
Esempio n. 3
0
def generate_temp(peaks, location_key):
    '''generate_temp
    generate a lookup of temporary files
    Parameters
    ==========
    peaks: dict
       data structure from get_coordinates_and_peaks
    location_key: str
       key in peaks data structure for file paths
    Returns
    =======
    peaks: dict
        (key is ttl file, equivalent to peaks, but old location_key path is replaced
        with path to temporary directory
    copy_list: dict
        keys are current paths, values are temporary file names corresponding to
        fullpath in new_nifti_files[ttl_file] dictionary. This is used to copy
        images into the temporary directory with the correct names. 
    '''
    copy_list = dict()
    for nidm, entries in peaks.items():
        nidm_directory = os.path.dirname(nidm)
        for e in range(len(entries)):
            if location_key in entries[e]:
                brainmap = entries[e][location_key]
                brainmap_base = os.path.basename(brainmap)
                if "%s/%s" % (nidm_directory, brainmap_base) not in copy_list:
                    image_ext = get_extension(brainmap)
                    temp_path = get_random_name()
                    temp_image_path = "%s.%s" % (temp_path, image_ext)
                    copy_list["%s/%s" % (nidm_directory,
                                         brainmap_base)] = temp_image_path

    updated_peaks = dict()

    for nidm, entries in peaks.items():
        nidm_directory = os.path.dirname(nidm)
        to_keep = []
        for e in range(len(entries)):
            if location_key in entries[e]:
                brainmap = entries[e][location_key]
                brainmap_base = os.path.basename(brainmap)
                entries[e][location_key] = copy_list["%s/%s" % (nidm_directory,
                                                                brainmap_base)]
                if "x" in entries[e]:
                    if entries[e]["x"] != "nan":
                        to_keep.append(e)

        # Remove coordinates that are nan from the data frame
        entries = [entries[e] for e in range(len(entries)) if e in to_keep]
        updated_peaks[nidm] = entries

    return updated_peaks, copy_list
Esempio n. 4
0
def generate_temp(peaks,location_key):
    '''generate_temp
    generate a lookup of temporary files
    Parameters
    ==========
    peaks: dict
       data structure from get_coordinates_and_peaks
    location_key: str
       key in peaks data structure for file paths
    Returns
    =======
    peaks: dict
        (key is ttl file, equivalent to peaks, but old location_key path is replaced
        with path to temporary directory
    copy_list: dict
        keys are current paths, values are temporary file names corresponding to
        fullpath in new_nifti_files[ttl_file] dictionary. This is used to copy
        images into the temporary directory with the correct names. 
    '''
    copy_list = dict()
    for nidm,entries in peaks.iteritems():
        nidm_directory = os.path.dirname(nidm)
        for e in range(len(entries)):
            if location_key in entries[e]:
                brainmap = entries[e][location_key]
                brainmap_base = os.path.basename(brainmap)
                if "%s/%s" %(nidm_directory,brainmap_base) not in copy_list:
                    image_ext = get_extension(brainmap)
                    temp_path = get_random_name()
                    temp_image_path = "%s.%s" %(temp_path,image_ext)
                    copy_list["%s/%s" %(nidm_directory,brainmap_base)] = temp_image_path

    updated_peaks = dict()

    for nidm,entries in peaks.iteritems():
        nidm_directory = os.path.dirname(nidm)
        to_keep = []
        for e in range(len(entries)):
            if location_key in entries[e]:
                brainmap = entries[e][location_key]
                brainmap_base = os.path.basename(brainmap)
                entries[e][location_key] = copy_list["%s/%s" %(nidm_directory,brainmap_base)]
                if "x" in entries[e]:
                    if entries[e]["x"] != "nan":
                        to_keep.append(e)

        # Remove coordinates that are nan from the data frame
        entries = [entries[e] for e in range(len(entries)) if e in to_keep]
        updated_peaks[nidm] = entries

    return updated_peaks,copy_list