def vegtype_texture(strata_path):
    """ Creates a greyscale image from a given strata GeoTiff """

    with rasterio.open(strata_path, 'r') as src:

        strata_data = src.read(1).astype('int32')
        shape = strata_data.shape
        strata_flat = strata_data.ravel()

        keys = np.unique(strata_flat)
        mapping = dict()

        # 0 should map to 0
        # -9999 should map to 0
        # otherwise, map key to value directly
        for key in keys:
            value = key
            if key == -9999:
                value = 0
            mapping[
                key] = value  # * 30 # debug, to check that things are actually there

        image_shape = (shape[1], shape[0])  # images are column major
        texture = Image.new('L', image_shape)

        image_data = npi.remap(strata_flat, list(mapping.keys()),
                               list(mapping.values()))
        texture.putdata(image_data)
        return texture
示例#2
0
def remap_3D(input, from_list, to_list):
    input = np.round(input)  # always annotation so should never be non-integer
    input = input.astype(
        int)  # always annotation so should never be non-integer

    input_shape = input.shape
    input = input.reshape(-1)
    input = npi.remap(input, from_list, to_list)
    input = input.reshape(input_shape)

    return input
def castle_creek_heightmap(path):
    """
    Serve the Castle Creek dem
    """
    with Dataset(path, 'r') as src:

        width = src.variables['x'][:].size
        height = src.variables['y'][:].size
        elev = src.variables['ned30m42116_snap_clip'][:].astype('int16')
        elev_flat = elev.ravel()
        unique_codes = [x for x in range(np.min(elev), np.max(elev)+1)]
        # Pack the elevation into the color channels.
        # Allows for a max of a 24 unsigned integer, and 1 8bit integer for depth below 0

        r_map = dict()
        g_map = dict()
        b_map = dict()
        a_map = dict()
        for value in list(unique_codes):
            if value >= 0:
                r_map[value] = (value & 0xFF)
                g_map[value] = (value & 0xFF00) >> 8
                b_map[value] = (value & 0xFF0000) >> 16
                a_map[value] = 255
            elif value < -255:
                r_map[value] = g_map[value] = b_map[value] = a_map[value] = 0
            else:
                r_map[value] = g_map[value] = b_map[value] = 0
                a_map[value] = 255 + value  # value is negative at this point, so add it

        image_shape = (width, height)  # images are column major
        texture = Image.new('RGBA', image_shape)
        r_array = npi.remap(elev_flat, list(r_map.keys()), list(r_map.values()))
        g_array = npi.remap(elev_flat, list(g_map.keys()), list(g_map.values()))
        b_array = npi.remap(elev_flat, list(b_map.keys()), list(b_map.values()))
        a_array = npi.remap(elev_flat, list(a_map.keys()), list(a_map.values()))
        image_data = [(r_array[i], g_array[i], b_array[i], a_array[i]) for i in range(width * height)]
        texture.putdata(image_data)
        return texture
def create_stateclass_texture(sc_tif, colormap):

    r_map = dict()
    g_map = dict()
    b_map = dict()

    fill = -9999
    zero = 0
    r_map[fill] = g_map[fill] = b_map[fill] = r_map[zero] = g_map[
        zero] = b_map[zero] = 0

    for row in colormap:
        idx = int(row['ID'])
        r_map[idx] = int(row['r'])
        g_map[idx] = int(row['g'])
        b_map[idx] = int(row['b'])

    # open the stateclass geotiff and get as a linear array
    with rasterio.open(sc_tif, 'r') as src:
        # cast the values to integers since they are uniquely identifiable
        sc_data = src.read(1).astype('int32')
        shape = sc_data.shape
        sc_flat = sc_data.ravel()
        # copy and remap the numpy arrays to the rgb values we want
        sc_copy = np.copy(sc_flat)
        r_array = npi.remap(sc_copy, list(r_map.keys()), list(r_map.values()))
        g_array = npi.remap(sc_copy, list(g_map.keys()), list(g_map.values()))
        b_array = npi.remap(sc_copy, list(b_map.keys()), list(b_map.values()))

        # get construct the image data based on the mapped values
        image_data = [(r_array[i], g_array[i], b_array[i])
                      for i in range(shape[0] * shape[1])]

        # build the png image
        image_shape = (shape[1], shape[0])  # images are column major
        texture = Image.new('RGB', image_shape)
        texture.putdata(image_data)
    return texture
            ) * (np.array(output_table_pername_peratlas['fractionDifference'])
                 > 0)
            annotation_aFD_path = annotation_aFD_path + '_aFD_volIncrease' + '.nii.gz'
        elif i == 2:
            map_to = np.abs(
                np.array(output_table_pername_peratlas['fractionDifference'])
            ) * (np.array(output_table_pername_peratlas['fractionDifference'])
                 < 0)
            annotation_aFD_path = annotation_aFD_path + '_aFD_volDecrease' + '.nii.gz'

        annotation_remapped = np.round(
            annotation)  # always annotation so should never be non-integer
        # input = input.astype(int) # always annotation so should never be non-integer
        annotation_remapped_shape = annotation_remapped.shape
        annotation_remapped = annotation_remapped.reshape(-1)
        annotation_remapped = npi.remap(annotation_remapped, map_from, map_to)
        annotation_remapped = annotation_remapped.reshape(
            annotation_remapped_shape)
        # annotation_remapped = remap_3D(annotation, map_from_filtered.astype(int), map_to_filtered)

        output_image = nib.Nifti1Image(annotation_remapped,
                                       annotation_image.affine)
        nib.save(output_image, annotation_aFD_path)

## Plotting
# cmap = matplotlib.cm.get_cmap('lines')
# VOIs = ['substantia nigra pars reticula',
#         'substantia nigra pars compacta',
#         'nucleus accumbens',
#         'mask',
#         'Right_VIIb',
示例#6
0
# reference_structure_output_path = os.path.join(reference_path, 'structure_graph_remapped_lowdetail_mc.csv')
analysis_table_path = os.path.join(analysis_path, 'all_volumes.csv')
# VOIs = ['Lobule II', 'Lobules IV-V', 'Substantia nigra, compact part', 'Substantia nigra, reticular part']
# VOIs = ['Substantia nigra, compact part', 'Substantia nigra, reticular part']



# Load
structure = pd.read_csv(reference_structure_path)
volume_table = pd.read_csv(analysis_table_path)

# Convert reference structure table
id_custom_to_id_mc = [762, 500, 821, 300, 977, 350]
id_mc_to_id_custom = [500, 762, 300, 821, 350, 977]
structure['id_mc'] = npi.remap(structure['id_custom'],
                               id_custom_to_id_mc,
                               id_mc_to_id_custom)
structure.to_csv(reference_structure_mc_path)

# Convert reference and data annotation files
for Path in mouse_path_list + [annotation_path] + mouse_lobular_path_list:
    print(Path)

    annotation_image = nib.load(Path)
    annotation = annotation_image.get_fdata()

    annotation = remap_3D(annotation, id_custom_to_id_mc, id_mc_to_id_custom)

    print(Path)
    output_path = Path.split('.')[0]+'_mc.nii.gz'
    print(output_path)
示例#7
0
    elif i == 11:
        map_to = np.array(mouse_table_pername['pVal_inv'] *
                          (mouse_table_pername['pVal'] < 0.05) *
                          (mean_diff < 0))
        annotation_pVal_path = annotation_pVal_path + '_pVal_inv_sig_volDecrease' + '.nii.gz'

    map_to_filt = np.logical_not(np.isnan(map_to))
    map_to_filtered = map_to[map_to_filt]
    map_from_filtered = map_from[map_to_filt]

    annotation_remapped = np.round(
        annotation)  # always annotation so should never be non-integer
    # input = input.astype(int) # always annotation so should never be non-integer
    annotation_remapped_shape = annotation_remapped.shape
    annotation_remapped = annotation_remapped.reshape(-1)
    annotation_remapped = npi.remap(annotation_remapped, map_from_filtered,
                                    map_to_filtered)
    annotation_remapped = annotation_remapped.reshape(
        annotation_remapped_shape)
    # annotation_remapped = remap_3D(annotation, map_from_filtered.astype(int), map_to_filtered)

    output_image = nib.Nifti1Image(annotation_remapped,
                                   annotation_image.affine)
    nib.save(output_image, annotation_pVal_path)

## Plotting
# cmap = matplotlib.cm.get_cmap('lines')
VOIs = [
    'Substantia nigra, compact part', 'Substantia nigra, reticular part',
    'Lobule II', 'cerebal peduncle', 'root', 'Folium-tuber vermis (VII)',
    'Lobules IV-V', 'Pyramus (VIII)', 'Nucleus accumbens',
    'cerebellum lobules I-III', 'cerebellum lobules VI-VII',
示例#8
0
# Else create new structure graph
else:
    # Save structure graph as json
    allen_average_template_json_path=os.path.join(allen_dir, 'structure_graph.json')
    with open(allen_average_template_json_path, 'w') as output_json:
        json.dump(allen_structure_graph_dict, output_json)
    # Also save structure graph as csv
    allen_structure_graph_df = pd.DataFrame(allen_structure_graph_dict)
    allen_structure_graph_df.to_csv(allen_average_template_csv_path)

    allen_structure_graph_remapped_df = allen_structure_graph_df.copy()
    allen_structure_graph_remapped_df['id_custom'] = np.random.permutation(len(allen_structure_graph_df))+1
    structure_id_path_custom = list()
    for iRow in range(len(allen_structure_graph_df)):
        structure_id_path_custom.append(list(npi.remap(allen_structure_graph_remapped_df.loc[iRow, 'structure_id_path'],
                                                  list(allen_structure_graph_remapped_df['id']),
                                                  list(allen_structure_graph_remapped_df['id_custom']))))
    allen_structure_graph_remapped_df['structure_id_path_custom'] = structure_id_path_custom
    allen_structure_graph_remapped_df.to_csv(allen_average_template_csv_remapped_path)



# Alter annotation by remapping integers
annot_shape = annot.shape
annot = annot.reshape(-1)
annot = npi.remap(annot, list(allen_structure_graph_remapped_df['id']), list(allen_structure_graph_remapped_df['id_custom']))
annot = annot.reshape(annot_shape)
img_annotation = nib.Nifti1Image(annot, np.eye(4))
img_annotation.set_qform(qform, code=1)
img_annotation.set_sform(np.eye(4), code=0)
nib.save(img_annotation, allen_annotation_remapped_path)
annotation_path = os.path.join(reference_path, 'suit', 'atlasesSUIT', 'Lobules-SUIT.nii')
input_path_list = glob.glob(os.path.join(data_path, '*', '*_lobular.nii.gz'))
# mouse_lobular_path_list = glob.glob(os.path.join(data_path, '*', '*invsynned*cerebellum*lobular.nii.gz'))
reference_structure_path = os.path.join(reference_path, 'suit', 'atlasesSUIT', 'Lobules-SUIT.csv')
reference_structure_mc_path = os.path.join(reference_path, 'suit', 'atlasesSUIT', 'Lobules-SUIT_mc.csv')



# Load
structure = pd.read_csv(reference_structure_path)

# Convert reference structure table
id_custom_to_id_mc = [25, 1, 23, 12, 19, 5, 17, 2, 11, 22, 10, 20, 5, 13, 3, 30, 31, 7, 18]
id_mc_to_id_custom = [1, 25, 12, 23, 5, 19, 2, 17, 22, 11, 20, 10, 13, 5, 30, 3, 7, 31]
structure['VolumeInteger_mc'] = npi.remap(structure['VolumeInteger'],
                               id_custom_to_id_mc,
                               id_mc_to_id_custom)
structure.to_csv(reference_structure_mc_path)

# Convert reference and data annotation files
for Path in input_path_list + [annotation_path]:
    annotation_image = nib.load(Path)
    annotation = annotation_image.get_fdata()

    annotation = remap_3D(annotation, id_custom_to_id_mc, id_mc_to_id_custom)

    print(Path)
    output_path = Path.split('.')[0]+'_mc.nii.gz'
    print(output_path)
    save_image(annotation, annotation_image, output_path)
示例#10
0
]
annotation_path_list = [
    os.path.join(reference_path, 'suit', 'atlasesSUIT', 'Lobules-SUIT.nii'),
    os.path.join(reference_path, 'subcortical',
                 'prob_atlas_bilateral_thrarg_0.4.nii.gz'),
    os.path.join(reference_path, 'CerebrA',
                 'mni_icbm152_CerebrA_tal_nlin_sym_09c_reoriented.nii.gz')
]
annotation_name_list = ['suit', 'subcortical', 'CerebrA']
input_path_list = glob.glob(
    os.path.join(data_path, '*', 'MDS', '*orsuit*MDS*.nii.gz'))

for Path in input_path_list:
    output_path = Path.split('.')[0] + '_RFMDS.nii.gz'
    print(output_path)

    input_image = nib.load(Path)
    input = input_image.get_fdata()

    input = np.round(input).astype(int)  # ensure integer annotation input

    input_shape = input.shape
    input = input.reshape(-1)
    input = npi.remap(input,
                      list(structure_table_list[0]['VolumeInteger_MDS']),
                      list(structure_table_list[0]['VolumeInteger']))
    input = input.reshape(input_shape)
    output_image = nib.Nifti1Image(input, input_image.affine,
                                   input_image.header)
    nib.save(output_image, output_path)
示例#11
0
                    iRow, 'structure_id_path_custom'].strip('][').split(', ')))
        structure_graph_path_len = len(structure_graph_path)

        # structure is already low detail (level 3 or lower), remap integer to itself (no change in annotation)
        if structure_graph_path_len < 3:
            structure_id_low_detail.append(structure_graph_path[-1])
        # structure has parent with lower detail, remap integer to lower detail annotation volume
        else:
            if structure_graph_path[2] == 90:
                structure_id_low_detail.append(structure_graph_path[-1])
            else:
                structure_id_low_detail.append(structure_graph_path[2])

    structure_graph['id_low_detail'] = structure_id_low_detail
    structure_graph.to_csv(allen_structure_table_path_lowdetail)

    # Alter invwarped by remapping integers
    invwarped_shape = invwarped.shape
    invwarped = invwarped.reshape(-1)
    invwarped = npi.remap(invwarped, list(structure_graph['id_custom']),
                          list(structure_graph['id_low_detail']))
    invwarped = invwarped.reshape(invwarped_shape)
    img_invwarped = nib.Nifti1Image(invwarped, np.eye(4))
    img_invwarped.set_qform(invwarped_image.get_qform(), code=1)
    img_invwarped.set_sform(np.eye(4), code=0)
    invwarped_lowdetail_path = invwarped_list[iInv]
    invwarped_lowdetail_path = os.path.splitext(
        os.path.splitext(invwarped_lowdetail_path)[0])[0]
    invwarped_lowdetail_path = invwarped_lowdetail_path + '_lowdetail.nii.gz'
    nib.save(img_invwarped, invwarped_lowdetail_path)
示例#12
0
structure_MDS = structure_MDS.astype({'id_MDS': int})
structure_MDS.to_csv(os.path.join(reference_path, 'structure_graph_MDS.csv'))

mouse_path_list = glob.glob(os.path.join(data_path, '*', '*invsynned*cerebellum.nii.gz')) + \
    [os.path.join(reference_path, 'annotation_50_reoriented.nii.gz')]
for Mouse in mouse_path_list:
    print(Mouse)

    output_path = Mouse.split('.')[0] + '_MDS.nii.gz'

    input_image = nib.load(Mouse)
    input = input_image.get_fdata()

    input_shape = input.shape
    input = input.reshape(-1)
    input = npi.remap(input, list(structure_MDS['id_custom']),
                      list(structure_MDS['id_MDS']))
    input = input.reshape(input_shape)
    output_image = nib.Nifti1Image(input, input_image.affine,
                                   input_image.header)
    nib.save(output_image, output_path)

# for Mouse in np.unique(structure_centers_table['mouse']):
# structure_centers_mouse_table = structure_centers_table[structure_centers_table['mouse']==Mouse]

# # for each VOI, isolate it and create new nifti files
# mouse_VOI = mouse.copy()
# mouse_VOI[np.logical_not(mouse_VOI_logical)] = 0
# mouse_VOI_image = nib.Nifti1Image(mouse_VOI, mouse_image.affine, mouse_image.header)
# nib.save(mouse_VOI_image, mouse_VOI_path)
# # view in mricrogl outside of python
示例#13
0
CoM_structure_table_all = pd.concat(CoM_structure_table_list, sort=True)
CoM_structure_table_all.to_csv(os.path.join(analysis_path, 'reference_CoM.csv'))



# Apply MDS on human output (use CoM_structure_table_list)
input_path_list_list = [glob.glob(os.path.join(data_path, '*', '*annotation_orsuit_thrarg_adjusted.nii.gz')) + [annotation_path_list[0]],
                        glob.glob(os.path.join(data_path, '*', '*annotation_subcortical_thrarg.nii.gz')) + [annotation_path_list[1]],
                        glob.glob(os.path.join(data_path, '*', '*annotation_CerebrA_thrarg_adjusted.nii.gz')) + [annotation_path_list[2]]]
for iInput, input_path_list in enumerate(input_path_list_list):
    print(iInput)
    CoM_structure_table = CoM_structure_table_list[iInput]

    for InputPath in input_path_list:
        output_path = InputPath.split('.')[0]+'_MDS.nii.gz'
        print(output_path)

        input_image = nib.load(InputPath)
        input = input_image.get_fdata()

        input = np.round(input).astype(int) # ensure integer annotation input

        input_shape = input.shape
        input = input.reshape(-1)
        print(list(CoM_structure_table['VolumeInteger']))
        print(list(CoM_structure_table['VolumeInteger_MDS']))
        input = npi.remap(input, list(CoM_structure_table['VolumeInteger']), list(CoM_structure_table['VolumeInteger_MDS']))
        input = input.reshape(input_shape)
        output_image = nib.Nifti1Image(input, input_image.affine, input_image.header)
        nib.save(output_image, output_path)