def on_draw(self):
        """ Redraws the figure
        """

        self.axes.clear()

        if type(self.ReferenceVasMap) != type(None):
            width = self.ReferenceVasMap.shape[1]
            height = self.ReferenceVasMap.shape[0]
        elif type(self.MatchingVasMapAfterChange) != type(None):
            width = self.MatchingVasMapAfterChange.shape[1]
            height = self.MatchingVasMapAfterChange.shape[0]
        elif type(self.MatchingVasMap) != type(None):
            width = self.MatchingVasMap.shape[1]
            height = self.MatchingVasMap.shape[0]
        else:
            width = 1344
            height = 1024

        if (type(self.ReferenceVasMap) !=
                type(None)) and (self.radiobutton_reference.isChecked()
                                 or self.radiobutton_both.isChecked()):
            greenChannel = ia.resize_image(self.ReferenceVasMap,
                                           (height, width))
            greenChannel = (
                np.power(ia.array_nor(greenChannel), self.reference_contrast) *
                255).astype(np.uint8)
        else:
            greenChannel = np.zeros((height, width)).astype(np.uint8)

        if (self.radiobutton_matching.isChecked()
                or self.radiobutton_both.isChecked()):
            if type(self.MatchingVasMapAfterChange) != type(None):
                redChannel = ia.resize_image(self.MatchingVasMapAfterChange,
                                             (height, width))
                redChannel = (np.power(ia.array_nor(redChannel),
                                       self.matching_contrast) * 255).astype(
                                           np.uint8)
            elif type(self.MatchingVasMap) != type(None):
                redChannel = ia.resize_image(self.MatchingVasMap,
                                             (height, width))
                redChannel = (np.power(ia.array_nor(redChannel),
                                       self.matching_contrast) * 255).astype(
                                           np.uint8)
            else:
                redChannel = np.zeros((height, width)).astype(np.uint8)
        else:
            redChannel = np.zeros((height, width)).astype(np.uint8)

        blueChannel = np.zeros((height, width)).astype(np.uint8)
        pltImg = cv2.merge((redChannel, greenChannel, blueChannel))

        self.axes.imshow(pltImg)

        self.axes.set_xlim([0, width])
        self.axes.set_ylim([0, height])
        self.axes.invert_yaxis()

        self.canvas.draw()
data_folder = r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project\180404-M360495-2p\vasmap_2p"

zoom1_paths = [
    os.path.join(data_folder, f) for f in os.listdir(data_folder)
    if f[-12:] == '_rotated.tif' and '_zoom1_' in f
]

# zoom2_paths = [os.path.join(data_folder, f) for f in os.listdir(data_folder)
#                if f[-12:] == '_rotated.tif' and '_zoom2_' in f]

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

vas_map_zoom1 = []
# vas_map_zoom2 = []

for zoom1_path in zoom1_paths:
    curr_vasmap = np.mean(tf.imread(zoom1_path), axis=0)
    vas_map_zoom1.append(curr_vasmap)

# for zoom2_path in zoom2_paths:
#     curr_vasmap = np.mean(tf.imread(zoom2_path), axis=0)
#     vas_map_zoom2.append(curr_vasmap)

vas_map_zoom1 = ia.array_nor(np.mean(vas_map_zoom1, axis=0))
# vas_map_zoom2 = ia.array_nor(np.mean(vas_map_zoom2, axis=0))

tf.imsave('vas_map_focused_2p_zoom1.tif', vas_map_zoom1.astype(np.float32))
# tf.imsave('vas_map_focused_2p_zoom2.tif', vas_map_zoom2.astype(np.float32))
Esempio n. 3
0
filter_sigma = 0. # parameters only used if filter the rois
# dilation_iterations = 1. # parameters only used if filter the rois
cut_thr = 2.5 # low for more rois, high for less rois

bg_fn = "corrected_mean_projections.tif"
save_folder = 'figures'

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

data_f = h5py.File('caiman_segmentation_results.hdf5')
masks = data_f['masks'].value
data_f.close()

bg = ia.array_nor(np.max(tf.imread(bg_fn), axis=0))

final_roi_dict = {}

for i, mask in enumerate(masks):

    if is_filter:
        mask_nor = (mask - np.mean(mask.flatten())) / np.abs(np.std(mask.flatten()))
        mask_nor_f = ni.filters.gaussian_filter(mask_nor, filter_sigma)
        mask_bin = np.zeros(mask_nor_f.shape, dtype=np.uint8)
        mask_bin[mask_nor_f > cut_thr] = 1

    else:
        mask_bin = np.zeros(mask.shape, dtype=np.uint8)
        mask_bin[mask > 0] = 1
import os
import corticalmapping.NwbTools as nt
import corticalmapping.core.ImageAnalysis as ia
import matplotlib.pyplot as plt
import tifffile as tf

vasmap_name_wf = 'vas_map_focused_wf.tif'
vasmap_name_2p_zoom1 = 'vas_map_focused_2p_zoom1.tif'
# vasmap_name_2p_zoom2 = 'vas_map_focused_2p_zoom2.tif'

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

vasmap_wf = tf.imread(vasmap_name_wf)
vasmap_2p_zoom1 = ia.array_nor(tf.imread(vasmap_name_2p_zoom1))
# vasmap_2p_zoom2 = ia.array_nor(tf.imread(vasmap_name_2p_zoom2))

f = plt.figure(figsize=(15, 7))
ax1 = f.add_subplot(121)
ax1.imshow(vasmap_wf, cmap='gray', interpolation='nearest')
ax1.set_title('wide field surface vasculature')
ax2 = f.add_subplot(122)
ax2.imshow(vasmap_2p_zoom1,
           vmin=0,
           vmax=0.1,
           cmap='gray',
           interpolation='nearest')
ax2.set_title('two photon surface vasculature')
plt.show()

nwb_fn = [f for f in os.listdir(curr_folder) if f[-4:] == '.nwb'][0]
    if 'path_list' in offsets_keys:
        offsets_keys.remove('path_list')

    offsets_keys.sort()
    offsets = []
    for offsets_key in offsets_keys:
        offsets.append(offsets_f[offsets_key].value)
    offsets = np.concatenate(offsets, axis=0)
    offsets = np.array(zip(offsets[:, 1], offsets[:, 0]))
    offsets_f.close()

    mean_projection = tf.imread(
        os.path.join(plane_n, 'corrected_mean_projection.tif'))
    max_projection = tf.imread(
        os.path.join(plane_n, 'corrected_max_projections.tif'))
    max_projection = ia.array_nor(np.max(max_projection, axis=0))

    input_dict = {
        'field_name':
        plane_n,
        'original_timeseries_path':
        '/acquisition/timeseries/2p_movie_plane' + str(i),
        'corrected_file_path':
        movie_2p_fn,
        'corrected_dataset_path':
        plane_n,
        'xy_translation_offsets':
        offsets,
        'mean_projection':
        mean_projection,
        'max_projection':
    else:
        mask_bin = np.zeros(mask.shape, dtype=np.uint8)
        mask_bin[mask > 0] = 1

    mask_labeled, mask_num = ni.label(mask_bin)
    curr_mask_dict = ia.get_masks(labeled=mask_labeled,
                                  keyPrefix='caiman_mask_{:03d}'.format(i),
                                  labelLength=5)
    for roi_key, roi_mask in curr_mask_dict.items():
        final_roi_dict.update({roi_key: ia.WeightedROI(roi_mask * mask)})

print 'Total number of ROIs:', len(final_roi_dict)

f = plt.figure(figsize=(15, 8))
ax1 = f.add_subplot(121)
ax1.imshow(ia.array_nor(bg),
           vmin=0,
           vmax=0.5,
           cmap='gray',
           interpolation='nearest')
colors1 = pt.random_color(masks.shape[0])
for i, mask in enumerate(masks):
    pt.plot_mask_borders(mask, plotAxis=ax1, color=colors1[i])
ax1.set_title('original ROIs')
ax1.set_axis_off()
ax2 = f.add_subplot(122)
ax2.imshow(ia.array_nor(bg),
           vmin=0,
           vmax=0.5,
           cmap='gray',
           interpolation='nearest')
vas_map_paths = [
    r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project"
    r"\180404-M360495-2p\vasmap_wf\180404JCamF100",
    r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project"
    r"\180404-M360495-2p\vasmap_wf\180404JCamF101",
    r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project"
    r"\180404-M360495-2p\vasmap_wf\180404JCamF102",
]

saveFolder = os.path.dirname(os.path.realpath(__file__))
os.chdir(saveFolder)

vas_maps = []

for vas_map_path in vas_map_paths:

    vas_map_focused, _, _ = ft.importRawJCamF(vas_map_path,
                                              column=1024,
                                              row=1024,
                                              headerLength=116,
                                              tailerLength=452)
    vas_map_focused = vas_map_focused[2:]
    vas_map_focused = vas_map_focused[:, ::-1, :]
    vas_map_focused[vas_map_focused > 50000] = 400
    vas_map_focused = np.mean(vas_map_focused, axis=0)
    vas_maps.append(ia.array_nor(vas_map_focused))

vas_map = ia.array_nor(np.mean(vas_maps, axis=0))

tf.imsave('vas_map_focused_wf.tif', vas_map.astype(np.float32))
    if len(curr_vasmap.shape) == 2:
        if len(channels) == 1:
            vasmaps[channels[0]].append(np.array([curr_vasmap]))
        else:
            raise ValueError(
                'recorded file is 2d, cannot be deinterleved into {} channels.'
                .format(len(channels)))
    else:
        if len(curr_vasmap.shape) != 3:
            raise ValueError(
                'shape of recorded file: {}. should be either 2d or 3d.'.
                format(curr_vasmap.shape))

        for ch_i, ch_n in enumerate(channels):
            curr_vasmap_ch = curr_vasmap[ch_i::len(channels)]
            curr_vasmap_ch = ia.array_nor(np.mean(curr_vasmap_ch, axis=0))
            if is_equalize:
                curr_vasmap_ch = (curr_vasmap_ch * 255).astype(np.uint8)
                curr_vasmap_ch = cv2.equalizeHist(curr_vasmap_ch).astype(
                    np.float32)
            vasmaps[ch_n].append(curr_vasmap_ch)

for ch_n, ch_vasmap in vasmaps.items():
    # save_vasmap = np.concatenate(ch_vasmap, axis=0)
    # print(save_vasmap.shape)
    # save_vasmap = ia.array_nor(np.mean(save_vasmap, axis=0))
    # print(save_vasmap.shape)

    save_vasmap = ia.array_nor(np.mean(ch_vasmap, axis=0))

    if scope == 'scientifica':
Esempio n. 9
0
]

save_name = 'vasmap_2p_zoom1'

channels = ['green', 'red']

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

vasmaps = {}
for chn in channels:
    vasmaps.update({chn: []})

for file_n in file_ns:
    print(file_n)

    curr_vasmap = tf.imread(os.path.join(data_folder, file_n))

    for ch_i, ch_n in enumerate(channels):
        curr_vasmap_ch = curr_vasmap[ch_i::len(channels)]
        vasmaps[ch_n].append(curr_vasmap_ch.transpose((0, 2, 1))[:, ::-1, :])
        # print(curr_vasmap_ch.shape)

for ch_n, ch_vasmap in vasmaps.items():
    save_vasmap = np.concatenate(ch_vasmap, axis=0)
    # print(save_vasmap.shape)
    save_vasmap = ia.array_nor(np.mean(save_vasmap, axis=0))
    # print(save_vasmap.shape)
    tf.imsave('{}_{}.tif'.format(save_name, ch_n),
              save_vasmap.astype(np.float32))
import os
import numpy as np
import tifffile as tf
import corticalmapping.core.ImageAnalysis as ia
import matplotlib.pyplot as plt

data_folder = r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project\180322-M360495-2p"
file_name = "vasmap_zoom1_00001_00001.tif"
save_name = 'vasmap_2p_zoom1.tif'

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

vasmap = tf.imread(os.path.join(data_folder, file_name))
vasmap = np.mean(vasmap.transpose((0, 2, 1))[:, ::-1, :], axis=0)
vasmap = ia.array_nor(vasmap)

tf.imsave(save_name, vasmap.astype(np.float32))
Esempio n. 11
0
def run():
    isSave = True

    filter_sigma = 2.  # parameters only used if filter the rois
    thr_high = 0.0
    thr_low = 0.1

    bg_fn = "corrected_mean_projections.tif"
    save_folder = 'figures'

    curr_folder = os.path.dirname(os.path.realpath(__file__))
    os.chdir(curr_folder)

    data_f = h5py.File('caiman_segmentation_results.hdf5')
    masks = data_f['masks'].value
    data_f.close()

    bg = ia.array_nor(np.max(tf.imread(bg_fn), axis=0))

    final_roi_dict = {}

    roi_ind = 0
    for i, mask in enumerate(masks):
        mask_dict = hl.threshold_mask_by_energy(mask,
                                                sigma=filter_sigma,
                                                thr_high=thr_high,
                                                thr_low=thr_low)
        for mask_roi in mask_dict.values():
            final_roi_dict.update({'roi_{:04d}'.format(roi_ind): mask_roi})
            roi_ind += 1

    print 'Total number of ROIs:', len(final_roi_dict)

    f = plt.figure(figsize=(15, 8))
    ax1 = f.add_subplot(121)
    ax1.imshow(bg, vmin=0, vmax=0.5, cmap='gray', interpolation='nearest')
    colors1 = pt.random_color(masks.shape[0])
    for i, mask in enumerate(masks):
        pt.plot_mask_borders(mask, plotAxis=ax1, color=colors1[i])
    ax1.set_title('original ROIs')
    ax1.set_axis_off()
    ax2 = f.add_subplot(122)
    ax2.imshow(ia.array_nor(bg),
               vmin=0,
               vmax=0.5,
               cmap='gray',
               interpolation='nearest')
    colors2 = pt.random_color(len(final_roi_dict))
    i = 0
    for roi in final_roi_dict.values():
        pt.plot_mask_borders(roi.get_binary_mask(),
                             plotAxis=ax2,
                             color=colors2[i])
        i = i + 1
    ax2.set_title('filtered ROIs')
    ax2.set_axis_off()
    # plt.show()

    if isSave:

        if not os.path.isdir(save_folder):
            os.makedirs(save_folder)

        f.savefig(os.path.join(save_folder,
                               'caiman_segmentation_filtering.pdf'),
                  dpi=300)

        cell_file = h5py.File('cells.hdf5', 'w')

        i = 0
        for key, value in sorted(final_roi_dict.iteritems()):
            curr_grp = cell_file.create_group('cell{:04d}'.format(i))
            curr_grp.attrs['name'] = key
            value.to_h5_group(curr_grp)
            i += 1

        cell_file.close()
def run():
    # pixels, masks with center location within this pixel region at the image border will be discarded
    center_margin = [10, 20, 25, 10] # [top margin, bottom margin, left margin, right margin]

    # area range, range of number of pixels of a valid roi
    area_range = [150, 1000]

    # for the two masks that are overlapping, if the ratio between overlap and the area of the smaller mask is larger than
    # this value, the smaller mask will be discarded.
    overlap_thr = 0.2

    save_folder = 'figures'

    data_file_name = 'cells.hdf5'
    save_file_name = 'cells_refined.hdf5'
    background_file_name = "corrected_mean_projections.tif"

    curr_folder = os.path.dirname(os.path.realpath(__file__))
    os.chdir(curr_folder)

    if not os.path.isdir(save_folder):
        os.makedirs(save_folder)

    # read cells
    dfile = h5py.File(data_file_name)
    cells = {}
    for cellname in dfile.iterkeys():
        cells.update({cellname:ia.WeightedROI.from_h5_group(dfile[cellname])})

    print 'total number of cells:', len(cells)

    # get the names of cells which are on the edge
    edge_cells = []
    for cellname, cellmask in cells.iteritems():
        dimension = cellmask.dimension
        center = cellmask.get_center()
        if center[0] < center_margin[0] or \
           center[0] > dimension[0] - center_margin[1] or \
           center[1] < center_margin[2] or \
           center[1] > dimension[1] - center_margin[3]:

            # cellmask.plot_binary_mask_border(color='#ff0000', borderWidth=1)
            # plt.title(cellname)
            # plt.show()

            edge_cells.append(cellname)

    print '\ncells to be removed because they are on the edges:'
    print '\n'.join(edge_cells)

    # remove edge cells
    for edge_cell in edge_cells:
        _ = cells.pop(edge_cell)

    # get dictionary of cell areas
    cell_areas = {}
    for cellname, cellmask in cells.iteritems():
        cell_areas.update({cellname: cellmask.get_binary_area()})


    # remove cellnames that have area outside of the area_range
    invalid_cell_ns = []
    for cellname, cellarea in cell_areas.items():
        if cellarea < area_range[0] or cellarea > area_range[1]:
            invalid_cell_ns.append(cellname)
    print "cells to be removed because they do not meet area criterion:"
    print "\n".join(invalid_cell_ns)
    for invalid_cell_n in invalid_cell_ns:
        cell_areas.pop(invalid_cell_n)


    # sort cells with their binary area
    cell_areas_sorted = sorted(cell_areas.items(), key=operator.itemgetter(1))
    cell_areas_sorted.reverse()
    cell_names_sorted = [c[0] for c in cell_areas_sorted]
    # print '\n'.join([str(c) for c in cell_areas_sorted])

    # get the name of cells that needs to be removed because of overlapping
    retain_cells = []
    remove_cells = []
    for cell1_name in cell_names_sorted:
        cell1_mask = cells[cell1_name]
        is_remove = 0
        cell1_area = cell1_mask.get_binary_area()
        for cell2_name in retain_cells:
            cell2_mask = cells[cell2_name]
            cell2_area = cell2_mask.get_binary_area()
            curr_overlap = cell1_mask.binary_overlap(cell2_mask)

            if float(curr_overlap) / cell1_area > overlap_thr:
                remove_cells.append(cell1_name)
                is_remove = 1
                print cell1_name, ':', cell1_mask.get_binary_area(), ': removed'

                # f = plt.figure(figsize=(10,10))
                # ax = f.add_subplot(111)
                # cell1_mask.plot_binary_mask_border(plotAxis=ax, color='#ff0000', borderWidth=1)
                # cell2_mask.plot_binary_mask_border(plotAxis=ax, color='#0000ff', borderWidth=1)
                # ax.set_title('red:'+cell1_name+'; blue:'+cell2_name)
                # plt.show()
                break

        if is_remove == 0:
            retain_cells.append(cell1_name)
            print cell1_name, ':', cell1_mask.get_binary_area(), ': retained'

    print '\ncells to be removed because of overlapping:'
    print '\n'.join(remove_cells)

    print '\ntotal number of reatined cells:', len(retain_cells)

    # plotting
    colors = pt.random_color(len(cells.keys()))
    bgImg = ia.array_nor(np.max(tf.imread(background_file_name), axis=0))

    f = plt.figure(figsize=(10, 10))
    ax = f.add_subplot(111)
    ax.imshow(ia.array_nor(bgImg), cmap='gray', vmin=0, vmax=0.5, interpolation='nearest')

    f2 = plt.figure(figsize=(10, 10))
    ax2 = f2.add_subplot(111)
    ax2.imshow(np.zeros(bgImg.shape, dtype=np.uint8), vmin=0, vmax=1, cmap='gray', interpolation='nearest')

    i = 0
    for retain_cell in retain_cells:
        cells[retain_cell].plot_binary_mask_border(plotAxis=ax, color=colors[i], borderWidth=1)
        cells[retain_cell].plot_binary_mask_border(plotAxis=ax2, color=colors[i], borderWidth=1)
        i += 1
    # plt.show()

    # save figures
    pt.save_figure_without_borders(f, os.path.join(save_folder, '2P_refined_ROIs_with_background.png'), dpi=300)
    pt.save_figure_without_borders(f2, os.path.join(save_folder, '2P_refined_ROIs_without_background.png'), dpi=300)

    # save h5 file
    save_file = h5py.File(save_file_name, 'w')
    i = 0
    for retain_cell in retain_cells:
        print retain_cell, ':', cells[retain_cell].get_binary_area()

        currGroup = save_file.create_group('cell' + ft.int2str(i, 4))
        currGroup.attrs['name'] = retain_cell
        roiGroup = currGroup.create_group('roi')
        cells[retain_cell].to_h5_group(roiGroup)
        i += 1

    for attr, value in dfile.attrs.iteritems():
        save_file.attrs[attr] = value

    save_file.close()
    dfile.close()
import matplotlib.pyplot as plt
import corticalmapping.core.ImageAnalysis as ia

vasmap_wf_folder = r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data\181102-M412052-deepscope\vasmap_wf"

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

map_fns = [f for f in os.listdir(vasmap_wf_folder) if f[-4:]=='.tif']
map_fns.sort()
print('\n'.join(map_fns))

map_wf = []
for map_fn in map_fns:
    curr_map = tf.imread(os.path.join(vasmap_wf_folder, map_fn)).astype(np.float32)
    map_wf.append(ia.array_nor(curr_map))

map_wf = ia.array_nor(np.mean(map_wf, axis=0))
map_wf_r = ia.array_nor(ia.rigid_transform_cv2(map_wf, rotation=140)[:, ::-1])

f = plt.figure(figsize=(12, 6))
ax_wf = f.add_subplot(121)
ax_wf.imshow(map_wf, vmin=0., vmax=1., cmap='gray', interpolation='nearest')
ax_wf.set_axis_off()
ax_wf.set_title('vasmap widefield')

ax_wf_r = f.add_subplot(122)
ax_wf_r.imshow(map_wf_r, vmin=0., vmax=1., cmap='gray', interpolation='nearest')
ax_wf_r.set_axis_off()
ax_wf_r.set_title('vasmap widefield rotated')
import tifffile as tf
import matplotlib.pyplot as plt
import corticalmapping.core.ImageAnalysis as ia

data_fn = 'zstack_2p_zoom2_red_aligned.tif'
save_fn = '2018-08-16-M376019-depth-profile-red.png'
start_depth = 50 # micron
step_depth = 2 # micron
pix_size = 0.7 # sutter scope, zoom2, 512 x 512
resolution =  512

curr_folder = os.path.dirname(os.path.abspath(__file__))
os.chdir(curr_folder)

data = tf.imread(data_fn)
dp = ia.array_nor(np.mean(data, axis=1))

depth_i = np.array(range(0, dp.shape[0], 50))
depth_l = depth_i * step_depth + start_depth

f = plt.figure(figsize=(8, 8))
ax = f.add_subplot(111)
ax.imshow(dp, vmin=0, vmax=1, cmap='magma', aspect=step_depth / pix_size)
ax.set_xticks([0, resolution-1])
ax.set_xticklabels(['0', '{:7.2f}'.format(resolution*pix_size)])
ax.set_yticks(depth_i)
ax.set_yticklabels(depth_l)
ax.set_xlabel('horizontal dis (um)')
ax.set_ylabel('depth (um)')

plt.show()
def run():
    data_file_name = 'cells_refined.hdf5'
    background_file_name = "corrected_mean_projections.tif"
    save_folder = 'figures'

    overlap_threshold = 0.9
    surround_limit = [1, 8]

    curr_folder = os.path.dirname(os.path.realpath(__file__))
    os.chdir(curr_folder)

    if not os.path.isdir(save_folder):
        os.makedirs(save_folder)

    print 'reading cells file ...'
    data_f = h5py.File(data_file_name, 'r')

    cell_ns = data_f.keys()
    cell_ns.sort()

    binary_mask_array = []
    weight_mask_array = []

    for cell_n in cell_ns:
        curr_roi = ia.ROI.from_h5_group(data_f[cell_n]['roi'])
        binary_mask_array.append(curr_roi.get_binary_mask())
        weight_mask_array.append(curr_roi.get_weighted_mask())

    data_f.close()
    binary_mask_array = np.array(binary_mask_array)
    weight_mask_array = np.array(weight_mask_array)
    print 'starting mask_array shape:', weight_mask_array.shape

    print 'getting total mask ...'
    total_mask = np.zeros(
        (binary_mask_array.shape[1], binary_mask_array.shape[2]),
        dtype=np.uint8)
    for curr_mask in binary_mask_array:
        total_mask = np.logical_or(total_mask, curr_mask)
    total_mask = np.logical_not(total_mask)

    plt.imshow(total_mask, interpolation='nearest')
    plt.title('total_mask')
    # plt.show()

    print 'getting and surround masks ...'
    binary_surround_array = []
    for binary_center in binary_mask_array:
        curr_surround = np.logical_xor(
            ni.binary_dilation(binary_center, iterations=surround_limit[1]),
            ni.binary_dilation(binary_center, iterations=surround_limit[0]))
        curr_surround = np.logical_and(curr_surround,
                                       total_mask).astype(np.uint8)
        binary_surround_array.append(curr_surround)
        # plt.imshow(curr_surround)
        # plt.show()
    binary_surround_array = np.array(binary_surround_array)

    print "saving rois ..."
    center_areas = []
    surround_areas = []
    for mask_ind in range(binary_mask_array.shape[0]):
        center_areas.append(np.sum(binary_mask_array[mask_ind].flat))
        surround_areas.append(np.sum(binary_surround_array[mask_ind].flat))
    roi_f = h5py.File('rois_and_traces.hdf5')
    roi_f['masks_center'] = weight_mask_array
    roi_f['masks_surround'] = binary_surround_array

    roi_f.close()
    print 'minimum surround area:', min(surround_areas), 'pixels.'

    f = plt.figure(figsize=(10, 10))
    ax_center = f.add_subplot(211)
    ax_center.hist(center_areas, bins=30)
    ax_center.set_title('roi center area distribution')
    ax_surround = f.add_subplot(212)
    ax_surround.hist(surround_areas, bins=30)
    ax_surround.set_title('roi surround area distribution')
    # plt.show()

    print 'plotting ...'
    colors = pt.random_color(weight_mask_array.shape[0])
    bg = ia.array_nor(np.max(tf.imread(background_file_name), axis=0))

    f_c_bg = plt.figure(figsize=(10, 10))
    ax_c_bg = f_c_bg.add_subplot(111)
    ax_c_bg.imshow(bg, cmap='gray', vmin=0, vmax=0.5, interpolation='nearest')
    f_c_nbg = plt.figure(figsize=(10, 10))
    ax_c_nbg = f_c_nbg.add_subplot(111)
    ax_c_nbg.imshow(np.zeros(bg.shape, dtype=np.uint8),
                    vmin=0,
                    vmax=1,
                    cmap='gray',
                    interpolation='nearest')
    f_s_nbg = plt.figure(figsize=(10, 10))
    ax_s_nbg = f_s_nbg.add_subplot(111)
    ax_s_nbg.imshow(np.zeros(bg.shape, dtype=np.uint8),
                    vmin=0,
                    vmax=1,
                    cmap='gray',
                    interpolation='nearest')

    i = 0
    for mask_ind in range(binary_mask_array.shape[0]):
        pt.plot_mask_borders(binary_mask_array[mask_ind],
                             plotAxis=ax_c_bg,
                             color=colors[i],
                             borderWidth=1)
        pt.plot_mask_borders(binary_mask_array[mask_ind],
                             plotAxis=ax_c_nbg,
                             color=colors[i],
                             borderWidth=1)
        pt.plot_mask_borders(binary_surround_array[mask_ind],
                             plotAxis=ax_s_nbg,
                             color=colors[i],
                             borderWidth=1)
        i += 1

    # plt.show()

    print 'saving figures ...'
    pt.save_figure_without_borders(f_c_bg,
                                   os.path.join(save_folder,
                                                '2P_ROIs_with_background.png'),
                                   dpi=300)
    pt.save_figure_without_borders(f_c_nbg,
                                   os.path.join(
                                       save_folder,
                                       '2P_ROIs_without_background.png'),
                                   dpi=300)
    pt.save_figure_without_borders(f_s_nbg,
                                   os.path.join(
                                       save_folder,
                                       '2P_ROI_surrounds_background.png'),
                                   dpi=300)
    f.savefig(os.path.join(save_folder, 'roi_area_distribution.pdf'), dpi=300)
Esempio n. 16
0
for i, mask in enumerate(masks):
    mask_nor = (mask - np.mean(mask.flatten())) / np.abs(np.std(mask.flatten()))
    mask_nor_f = ni.filters.gaussian_filter(mask_nor, filter_sigma)
    mask_bin = np.zeros(mask_nor_f.shape, dtype=np.uint8)
    mask_bin[mask_nor_f > cut_thr] = 1
    mask_labeled, mask_num = ni.label(mask_bin)
    curr_mask_dict = ia.get_masks(labeled=mask_labeled, keyPrefix='caiman_mask_{:03d}'.format(i), labelLength=5)
    for roi_key, roi_mask in curr_mask_dict.items():
        final_roi_dict.update({roi_key: ia.WeightedROI(roi_mask * mask)})

print 'Total number of ROIs:',len(final_roi_dict)

f = plt.figure(figsize=(15, 8))
ax1 = f.add_subplot(121)
ax1.imshow(ia.array_nor(bg), vmin=0, vmax=0.1, cmap='gray', interpolation='nearest')
colors1 = pt.random_color(masks.shape[0])
for i, mask in enumerate(masks):
    pt.plot_mask_borders(mask, plotAxis=ax1, color=colors1[i])
ax1.set_title('original ROIs')
ax1.set_axis_off()
ax2 = f.add_subplot(122)
ax2.imshow(ia.array_nor(bg), vmin=0, vmax=0.1, cmap='gray', interpolation='nearest')
colors2 = pt.random_color(len(final_roi_dict))
i = 0
for roi in final_roi_dict.values():
    pt.plot_mask_borders(roi.get_binary_mask(), plotAxis=ax2, color=colors2[i])
    i = i + 1
ax2.set_title('filtered ROIs')
ax2.set_axis_off()
plt.show()
            # ax.set_title('red:'+cell1_name+'; blue:'+cell2_name)
            # plt.show()
            break

    if is_remove == 0:
        retain_cells.append(cell1_name)
        print cell1_name, ':', cell1_mask.get_binary_area(), ': retained'

print '\ncells to be removed because of overlapping:'
print '\n'.join(remove_cells)

print '\ntotal number of reatined cells:', len(retain_cells)

# plotting
colors = pt.random_color(len(cells.keys()))
bgImg = ia.array_nor(np.max(tf.imread(background_file_name), axis=0))

f = plt.figure(figsize=(10, 10))
ax = f.add_subplot(111)
ax.imshow(ia.array_nor(bgImg),
          cmap='gray',
          vmin=0,
          vmax=0.5,
          interpolation='nearest')

f2 = plt.figure(figsize=(10, 10))
ax2 = f2.add_subplot(111)
ax2.imshow(np.zeros(bgImg.shape, dtype=np.uint8),
           vmin=0,
           vmax=1,
           cmap='gray',
Esempio n. 18
0
data_folder = r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data\190822-M471944-deepscope\movie"
identifier = '110_LSNDGCUC'
start_ind = 121228
frame_num = 3

fns = []

for ind in np.arange(frame_num, dtype=np.int) + start_ind:

    if ind < 100000:
        fns.append('{}_{:05d}_00001.tif'.format(identifier, ind))
    elif ind < 1000000:
        fns.append('{}_{:06d}_00001.tif'.format(identifier, ind))
    elif ind < 10000000:
        fns.append('{}_{:07d}_00001.tif'.format(identifier, ind))

f = plt.figure(figsize=(5, 12))
for frame_i in range(frame_num):
    ax = f.add_subplot(frame_num, 1, frame_i + 1)
    ax.imshow(ia.array_nor(tf.imread(os.path.join(data_folder, fns[frame_i]))),
              cmap='gray',
              vmin=0,
              vmax=0.5,
              interpolation='nearest')
    ax.set_title(fns[frame_i])
    ax.set_axis_off()

plt.tight_layout()
plt.show()
Esempio n. 19
0
    if is_remove == 0:
        retain_cells.append(cell1_name)
        print cell1_name, ':', cell1_mask.get_binary_area(), ': retained'

print '\ncells to be removed because of overlapping:'
print '\n'.join(remove_cells)

print '\ntotal number of reatined cells:', len(retain_cells)

# plotting
colors = pt.random_color(len(cells.keys()))
bgImg = tf.imread(background_file_name)

f = plt.figure(figsize=(10, 10))
ax = f.add_subplot(111)
ax.imshow(ia.array_nor(bgImg),
          cmap='gray',
          vmin=0,
          vmax=0.5,
          interpolation='nearest')

f2 = plt.figure(figsize=(10, 10))
ax2 = f2.add_subplot(111)
ax2.imshow(np.zeros(bgImg.shape, dtype=np.uint8),
           vmin=0,
           vmax=1,
           cmap='gray',
           interpolation='nearest')

i = 0
for retain_cell in retain_cells:
isRectify=False # should the fft method be applied to a rectify signal or not

#wrap experiment parameters
isAnesthetized=False
visualStimType='KSstim'
visualStimBackground='gray'
analysisParams ={}

if vasMapPaths:
    vasMap = hl.getVasMap(vasMapPaths,dtype=vasMapDtype,headerLength=vasMapHeaderLength,tailerLength=vasMapTailerLength,
                          column=vasMapColumn,row=vasMapRow,frame=vasMapFrame,crop=vasMapCrop,mergeMethod=vasMapMergeMethod)
else:
    print 'No vasculature map find. Taking first frame of movie as vasculature map.'
    vasMap = BinarySlicer(movPath)[0,:,:]

vasMap = ia.array_nor(vasMap).astype(np.float32)

tf.imsave(os.path.join(saveFolder,dateRecorded+'_M'+mouseID+'_Trial'+trialNum+'_vasMap.tif'),vasMap)

_, jphys = ft.importRawNewJPhys(jphysPath,dtype=jphysDtype,headerLength=jphysHeaderLength,channels=jphysChannels,sf=jphysFs)

pd = jphys['photodiode']

displayOnsets = hl.segmentPhotodiodeSignal(pd, digitizeThr=pdDigitizeThr, filterSize=pdFilterSize, segmentThr=pdSegmentThr, Fs=jphysFs)

imgFrameTS = ta.get_onset_timeStamps(jphys['read'], Fs=jphysFs, threshold=readThreshold, onsetType=readOnsetType)

logPath = hl.findLogPath(date=dateRecorded,mouseID=mouseID,stimulus='KSstimAllDir',userID=userID,fileNumber=str(fileNum),displayFolder=dataFolder)

displayInfo = hl.analysisMappingDisplayLog(logPath)
Esempio n. 21
0
                node_grp0 = node_grp
            if node1 in node_grp:
                node_grp1 = node_grp

        if node_grp0 != node_grp1:
            node_grp0.update(node_grp1)
            node_grps.remove(node_grp1)

    return node_grps


save_plot_dir = os.path.join(curr_folder, 'figures', 'dff_extraction')
if not os.path.isdir(save_plot_dir):
    os.makedirs(save_plot_dir)

bg = ia.array_nor(tf.imread('corrected_mean_projection.tif'))

data_f = h5py.File('rois_and_traces.hdf5')
traces_subtracted = data_f['traces_center_subtracted'].value
masks = data_f['masks_center'].value

f, axs = plt.subplots(1, 2, figsize=(16, 5))

cor_mat = np.corrcoef(traces_subtracted)
fig = axs[0].imshow(cor_mat,
                    vmin=-1,
                    vmax=1,
                    cmap='jet',
                    interpolation='nearest')
axs[0].set_title('coriance matrix')
f.colorbar(fig, ax=axs[0])
vasmap_wf_path = r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project" \
                 r"\180502-M376019-deepscope\Widefield.tif"

vasmap_2p_zoom1_path = r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project" \
                       r"\180502-M376019-deepscope\01\01_00001.tif"

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

vasmap_wf = io.imread(vasmap_wf_path, as_grey=True)
vasmap_wf = vasmap_wf.transpose()[::-1, ::-1]

vasmap_2p_zoom1 = tf.imread(vasmap_2p_zoom1_path).astype(np.float32)
vasmap_2p_zoom1 = np.mean(vasmap_2p_zoom1, axis=0)
vasmap_2p_zoom1 = vasmap_2p_zoom1.transpose()[::-1, ::-1]

f = plt.figure(figsize=(12, 5))
ax_wf = f.add_subplot(121)
ax_wf.imshow(ia.array_nor(vasmap_wf), vmin=0., vmax=1., cmap='gray', interpolation='nearest')
ax_wf.set_title('vasmap wide field')
ax_wf.set_axis_off()
ax_2p = f.add_subplot(122)
ax_2p.imshow(ia.array_nor(vasmap_2p_zoom1), vmin=0., vmax=0.15, cmap='gray', interpolation='nearest')
ax_2p.set_title('vasmap 2p zoom1')
ax_2p.set_axis_off()

plt.show()

tf.imsave('vasmap_wf.tif', vasmap_wf)
tf.imsave('vasmap_2p_zoom1.tif', vasmap_2p_zoom1)
Esempio n. 23
0
vasmap_2p_zoom1_path = r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project" \
                       r"\180328-M360495-deepscope\01\01_00001.tif"

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

vasmap_wf = io.imread(vasmap_wf_path, as_grey=True)
vasmap_wf = vasmap_wf.transpose()[::-1, ::-1]

vasmap_2p_zoom1 = tf.imread(vasmap_2p_zoom1_path).astype(np.float32)
vasmap_2p_zoom1 = np.mean(vasmap_2p_zoom1, axis=0)
vasmap_2p_zoom1 = vasmap_2p_zoom1.transpose()[::-1, ::-1]

f = plt.figure(figsize=(12, 5))
ax_wf = f.add_subplot(121)
ax_wf.imshow(ia.array_nor(vasmap_wf),
             vmin=0.,
             vmax=1.,
             cmap='gray',
             interpolation='nearest')
ax_wf.set_title('vasmap wide field')
ax_wf.set_axis_off()
ax_2p = f.add_subplot(122)
ax_2p.imshow(ia.array_nor(vasmap_2p_zoom1),
             vmin=0.,
             vmax=0.15,
             cmap='gray',
             interpolation='nearest')
ax_2p.set_title('vasmap 2p zoom1')
ax_2p.set_axis_off()