def test_getTrace(self):
        mov = np.arange(64).reshape((4, 4, 4))
        # print mov

        mask1 = np.zeros((4, 4))
        mask1[2, 2] = 1
        mask1[1, 1] = 1
        trace1 = ia.get_trace(mov, mask1, maskMode='binary')
        assert (trace1[2] == 39.5)

        mask2 = np.zeros((4, 4), dtype=np.float)
        mask2[:] = np.nan
        mask2[2, 2] = 1
        mask2[1, 1] = 1
        trace2 = ia.get_trace(mov, mask2, maskMode='binaryNan')
        assert (trace2[2] == 39.5)

        mask3 = np.zeros((4, 4), dtype=np.float)
        mask3[2, 2] = 1
        mask3[1, 1] = 2
        trace3 = ia.get_trace(mov, mask3, maskMode='weighted')
        assert (trace3[2] == 58)

        mask4 = np.zeros((4, 4), dtype=np.float)
        mask4[:] = np.nan
        mask4[2, 2] = 1
        mask4[1, 1] = 2
        trace4 = ia.get_trace(mov, mask4, maskMode='weightedNan')
        assert (trace4[2] == 58)
Example #2
0
 def test_ROI_binary_overlap(self):
     roi1 = np.zeros((10, 10))
     roi1[4:8, 3:7] = 1
     roi1 = ia.ROI(roi1)
     roi2 = np.zeros((10, 10))
     roi2[5:9, 5:8] = 1
     roi2 = ia.ROI(roi2)
     assert(roi1.binary_overlap(roi2) == 6)
Example #3
0
 def test_plot_ROIs(self):
     aa = np.zeros((50, 50));
     aa[15:20, 30:35] = np.random.rand(5, 5)
     roi1 = ia.ROI(aa)
     _ = roi1.plot_binary_mask_border();
     _ = roi1.plot_binary_mask()
     roi2 = ia.WeightedROI(aa)
     _ = roi2.plot_binary_mask_border();
     _ = roi2.plot_binary_mask();
     _ = roi2.plot_weighted_mask()
Example #4
0
    def test_mergeROIs(self):

        import corticalmapping.core.ImageAnalysis as ia
        roi1 = ia.WeightedROI(np.arange(9).reshape((3, 3)))
        roi2 = ia.WeightedROI(np.arange(1, 10).reshape((3, 3)))

        merged_ROI = ia.merge_weighted_rois(roi1, roi2)
        merged_ROI2 = ia.merge_binary_rois(roi1, roi2)

        assert (np.array_equal(merged_ROI.get_weighted_mask(), np.arange(1, 18, 2).reshape((3, 3))))
        assert (np.array_equal(merged_ROI2.get_binary_mask(), np.ones((3, 3))))
Example #5
0
    def test_get_circularity(self):
        aa = np.zeros((10, 10))
        aa[3:5, 3:5] = 1
        cir1 = ia.get_circularity(aa, is_skimage=False)
        # print(cir1)
        assert(0.7853981633974483 - 1e-15 < cir1 < 0.7853981633974483 + 1e-15)

        print(ia.get_circularity(aa, is_skimage=True))

        aa[3:5, 5] = 1
        cir2 = ia.get_circularity(aa, is_skimage=False)
        # print(cir2)
        assert (0.7539822368615503 - 1e-15 < cir2 < 0.7539822368615503 + 1e-15)
Example #6
0
 def test_ROI(self):
     a = np.zeros((10, 10))
     a[5:7, 3:6] = 1
     a[8:9, 7:10] = np.nan
     roi = ia.ROI(a)
     # plt.imshow(roi.get_binary_mask(),interpolation='nearest')
     assert (list(roi.get_center()) == [5.5, 4.])
Example #7
0
    def test_fit_ellipse(self):

        mask = np.zeros((100, 100), dtype=np.uint8)
        mask[20:50, 30:80] = 255
        mask[40:50, 70:80] = 0
        mask[20:30, 30:40] = 0

        # import matplotlib.pyplot as plt
        # f = plt.figure(figsize=(4, 4))
        # ax = f.add_subplot(111)
        # ax.set_aspect('equal')
        # ax.imshow(mask, interpolation='nearest')
        # plt.show()

        ell = ia.fit_ellipse(mask)
        print(ell.info())

        assert((np.round(ell.angle * 100) / 100) % 180. == 44.16)

        import cv2
        img = np.array([mask, mask, mask]).transpose((1, 2, 0)).copy()
        img = ell.draw(img=img, thickness=1)
        img = cv2.cvtColor(img, code=cv2.COLOR_BGR2RGB)
        import matplotlib.pyplot as plt
        plt.imshow(img, interpolation='nearest')
        plt.show()
Example #8
0
 def test_ROI_getBinaryTrace(self):
     mov = np.random.rand(5, 4, 4)
     mask = np.zeros((4, 4))
     mask[2, 3] = 1
     trace1 = mov[:, 2, 3]
     roi = ia.ROI(mask)
     trace2 = roi.get_binary_trace(mov)
     assert (np.array_equal(trace1, trace2))
Example #9
0
 def test_WeigthedROI_getWeightedCenter(self):
     aa = np.random.rand(5, 5);
     mask = np.zeros((5, 5))
     mask[2, 3] = aa[2, 3];
     mask[1, 4] = aa[1, 4];
     mask[3, 4] = aa[3, 4]
     roi = ia.WeightedROI(mask)
     center = roi.get_weighted_center()
     assert (center[0] == (2 * aa[2, 3] + 1 * aa[1, 4] + 3 * aa[3, 4]) / (aa[2, 3] + aa[1, 4] + aa[3, 4]))
def get_traces(params):
    t0 = time.time()

    chunk_ind, chunk_start, chunk_end, nwb_path, data_path, curr_folder, center_array, surround_array = params

    nwb_f = h5py.File(nwb_path, 'r')
    print('\nstart analyzing chunk: {}'.format(chunk_ind))
    curr_mov = nwb_f[data_path][chunk_start:chunk_end]
    nwb_f.close()

    # print 'extracting traces'
    curr_traces_center = np.empty((center_array.shape[0], curr_mov.shape[0]),
                                  dtype=np.float32)
    curr_traces_surround = np.empty((center_array.shape[0], curr_mov.shape[0]),
                                    dtype=np.float32)
    for i in range(center_array.shape[0]):
        curr_center = ia.WeightedROI(center_array[i])
        curr_surround = ia.ROI(surround_array[i])
        curr_traces_center[i, :] = curr_center.get_weighted_trace_pixelwise(
            curr_mov)

        # scale surround trace to be similar as center trace
        mean_center_weight = curr_center.get_mean_weight()
        curr_traces_surround[i, :] = curr_surround.get_binary_trace_pixelwise(
            curr_mov) * mean_center_weight

    # print 'saveing chunk {} ...'.format(chunk_ind)
    chunk_folder = os.path.join(curr_folder, 'chunks')
    if not os.path.isdir(chunk_folder):
        os.mkdir(chunk_folder)
    chunk_f = h5py.File(
        os.path.join(chunk_folder,
                     'chunk_temp_' + ft.int2str(chunk_ind, 4) + '.hdf5'))
    chunk_f['traces_center'] = curr_traces_center
    chunk_f['traces_surround'] = curr_traces_surround
    chunk_f.close()

    print('\n\t{:06d} seconds: chunk: {}; demixing finished.'.format(
        int(time.time() - t0), chunk_ind))

    return None
    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()
Example #12
0
data_f = h5py.File('caiman_segmentation_results.hdf5')
masks = data_f['masks'].value
data_f.close()

bg = tf.imread(bg_fn)

final_roi_dict = {}

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')
Example #13
0
def add_rois_and_traces(
        data_folder,
        nwb_f,
        plane_n,
        imaging_depth,
        mov_path='/processing/motion_correction/MotionCorrection'):

    mov_grp = nwb_f.file_pointer[mov_path + '/' + plane_n + '/corrected']

    data_f = h5py.File(os.path.join(data_folder, 'rois_and_traces.hdf5'), 'r')
    mask_arr_c = data_f['masks_center'].value
    mask_arr_s = data_f['masks_surround'].value
    traces_center_raw = data_f['traces_center_raw'].value
    # traces_center_demixed = data_f['traces_center_demixed'].value
    traces_center_subtracted = data_f['traces_center_subtracted'].value
    # traces_center_dff = data_f['traces_center_dff'].value
    traces_surround_raw = data_f['traces_surround_raw'].value
    neuropil_r = data_f['neuropil_r'].value
    neuropil_err = data_f['neuropil_err'].value
    data_f.close()

    if traces_center_raw.shape[1] != mov_grp['num_samples'].value:
        raise ValueError(
            'number of trace time points ({}) does not match frame number of '
            'corresponding movie ({}).'.format(traces_center_raw.shape[1],
                                               mov_grp['num_samples'].value))

    # traces_center_raw = traces_center_raw[:, :mov_grp['num_samples'].value]
    # traces_center_subtracted = traces_center_subtracted[:, :mov_grp['num_samples'].value]
    # traces_surround_raw = traces_surround_raw[:, :mov_grp['num_samples'].value]

    rf_img_max = tf.imread(
        os.path.join(data_folder, 'corrected_max_projection.tif'))
    rf_img_mean = tf.imread(
        os.path.join(data_folder, 'corrected_mean_projection.tif'))

    print 'adding segmentation results ...'
    rt_mo = nwb_f.create_module('rois_and_traces_' + plane_n)
    rt_mo.set_value('imaging_depth_micron', imaging_depth)
    is_if = rt_mo.create_interface('ImageSegmentation')
    is_if.create_imaging_plane('imaging_plane', description='')
    is_if.add_reference_image('imaging_plane', 'max_projection', rf_img_max)
    is_if.add_reference_image('imaging_plane', 'mean_projection', rf_img_mean)

    for i in range(mask_arr_c.shape[0]):
        curr_cen = mask_arr_c[i]
        curr_cen_n = 'roi_' + ft.int2str(i, 4)
        curr_cen_roi = ia.WeightedROI(curr_cen)
        curr_cen_pixels_yx = curr_cen_roi.get_pixel_array()
        curr_cen_pixels_xy = np.array(
            [curr_cen_pixels_yx[:, 1], curr_cen_pixels_yx[:, 0]]).transpose()
        is_if.add_roi_mask_pixels(image_plane='imaging_plane',
                                  roi_name=curr_cen_n,
                                  desc='',
                                  pixel_list=curr_cen_pixels_xy,
                                  weights=curr_cen_roi.weights,
                                  width=512,
                                  height=512)

        curr_sur = mask_arr_s[i]
        curr_sur_n = 'surround_' + ft.int2str(i, 4)
        curr_sur_roi = ia.ROI(curr_sur)
        curr_sur_pixels_yx = curr_sur_roi.get_pixel_array()
        curr_sur_pixels_xy = np.array(
            [curr_sur_pixels_yx[:, 1], curr_sur_pixels_yx[:, 0]]).transpose()
        is_if.add_roi_mask_pixels(image_plane='imaging_plane',
                                  roi_name=curr_sur_n,
                                  desc='',
                                  pixel_list=curr_sur_pixels_xy,
                                  weights=None,
                                  width=512,
                                  height=512)
    is_if.finalize()

    trace_f_if = rt_mo.create_interface('Fluorescence')
    seg_if_path = '/processing/rois_and_traces_' + plane_n + '/ImageSegmentation/imaging_plane'
    # print seg_if_path
    ts_path = mov_path + '/' + plane_n + '/corrected'

    print 'adding center fluorescence raw'
    trace_raw_ts = nwb_f.create_timeseries('RoiResponseSeries', 'f_center_raw')
    trace_raw_ts.set_data(traces_center_raw,
                          unit='au',
                          conversion=np.nan,
                          resolution=np.nan)
    trace_raw_ts.set_value('data_format', 'roi (row) x time (column)')
    trace_raw_ts.set_value('data_range', '[-8192, 8191]')
    trace_raw_ts.set_description(
        'fluorescence traces extracted from the center region of each roi')
    trace_raw_ts.set_time_as_link(ts_path)
    trace_raw_ts.set_value_as_link('segmentation_interface', seg_if_path)
    roi_names = [
        'roi_' + ft.int2str(ind, 4)
        for ind in range(traces_center_raw.shape[0])
    ]
    trace_raw_ts.set_value('roi_names', roi_names)
    trace_raw_ts.set_value('num_samples', traces_center_raw.shape[1])
    trace_f_if.add_timeseries(trace_raw_ts)
    trace_raw_ts.finalize()

    print 'adding neuropil fluorescence raw'
    trace_sur_ts = nwb_f.create_timeseries('RoiResponseSeries',
                                           'f_surround_raw')
    trace_sur_ts.set_data(traces_surround_raw,
                          unit='au',
                          conversion=np.nan,
                          resolution=np.nan)
    trace_sur_ts.set_value('data_format', 'roi (row) x time (column)')
    trace_sur_ts.set_value('data_range', '[-8192, 8191]')
    trace_sur_ts.set_description(
        'neuropil traces extracted from the surroud region of each roi')
    trace_sur_ts.set_time_as_link(ts_path)
    trace_sur_ts.set_value_as_link('segmentation_interface', seg_if_path)
    sur_names = [
        'surround_' + ft.int2str(ind, 4)
        for ind in range(traces_center_raw.shape[0])
    ]
    trace_sur_ts.set_value('roi_names', sur_names)
    trace_sur_ts.set_value('num_samples', traces_surround_raw.shape[1])
    trace_f_if.add_timeseries(trace_sur_ts)
    trace_sur_ts.finalize()

    roi_center_n_path = '/processing/rois_and_traces_' + plane_n + '/Fluorescence/f_center_raw/roi_names'
    # print 'adding center fluorescence demixed'
    # trace_demix_ts = nwb_f.create_timeseries('RoiResponseSeries', 'f_center_demixed')
    # trace_demix_ts.set_data(traces_center_demixed, unit='au', conversion=np.nan, resolution=np.nan)
    # trace_demix_ts.set_value('data_format', 'roi (row) x time (column)')
    # trace_demix_ts.set_description('center traces after overlapping demixing for each roi')
    # trace_demix_ts.set_time_as_link(mov_path + '/' + plane_n + '/corrected')
    # trace_demix_ts.set_value_as_link('segmentation_interface', seg_if_path)
    # trace_demix_ts.set_value('roi_names', roi_names)
    # trace_demix_ts.set_value('num_samples', traces_center_demixed.shape[1])
    # trace_f_if.add_timeseries(trace_demix_ts)
    # trace_demix_ts.finalize()

    print 'adding center fluorescence after neuropil subtraction'
    trace_sub_ts = nwb_f.create_timeseries('RoiResponseSeries',
                                           'f_center_subtracted')
    trace_sub_ts.set_data(traces_center_subtracted,
                          unit='au',
                          conversion=np.nan,
                          resolution=np.nan)
    trace_sub_ts.set_value('data_format', 'roi (row) x time (column)')
    trace_sub_ts.set_description(
        'center traces after overlap demixing and neuropil subtraction for each roi'
    )
    trace_sub_ts.set_time_as_link(mov_path + '/' + plane_n + '/corrected')
    trace_sub_ts.set_value_as_link('segmentation_interface', seg_if_path)
    trace_sub_ts.set_value_as_link('roi_names', roi_center_n_path)
    trace_sub_ts.set_value('num_samples', traces_center_subtracted.shape[1])
    trace_sub_ts.set_value('r', neuropil_r, dtype='float32')
    trace_sub_ts.set_value('rmse', neuropil_err, dtype='float32')
    trace_sub_ts.set_comments(
        'value "r": neuropil contribution ratio for each roi. '
        'value "rmse": RMS error of neuropil subtraction for each roi')
    trace_f_if.add_timeseries(trace_sub_ts)
    trace_sub_ts.finalize()

    trace_f_if.finalize()

    # print 'adding global dF/F traces for each roi'
    # trace_dff_if = rt_mo.create_interface('DfOverF')
    #
    # trace_dff_ts = nwb_f.create_timeseries('RoiResponseSeries', 'dff_center')
    # trace_dff_ts.set_data(traces_center_dff, unit='au', conversion=np.nan, resolution=np.nan)
    # trace_dff_ts.set_value('data_format', 'roi (row) x time (column)')
    # trace_dff_ts.set_description('global df/f traces for each roi center, input fluorescence is the trace after demixing'
    #                              ' and neuropil subtraction. global df/f is calculated by '
    #                              'allensdk.brain_observatory.dff.compute_dff() function.')
    # trace_dff_ts.set_time_as_link(ts_path)
    # trace_dff_ts.set_value_as_link('segmentation_interface', seg_if_path)
    # trace_dff_ts.set_value('roi_names', roi_names)
    # trace_dff_ts.set_value('num_samples', traces_center_dff.shape[1])
    # trace_dff_if.add_timeseries(trace_dff_ts)
    # trace_dff_ts.finalize()
    # trace_dff_if.finalize()

    rt_mo.finalize()
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

    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):
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)
Example #16
0
import os
import numpy as np
import tifffile as tf
import corticalmapping.core.ImageAnalysis as ia

data_folder = r"\\allen\programs\braintv\workgroups\nc-ophys\Jun\raw_data_rabies_project" \
              r"\180404-M360495-2p\2p_movie\reorged"
xy_downsample_rate = 2
t_downsample_rate = 10

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

corr_folder = os.path.join(data_folder, 'corrected')

f_ns = [f for f in os.listdir(corr_folder) if f[-14:] == '_corrected.tif']
f_ns.sort()
print('\n'.join(f_ns))

mov_d = []

for f_n in f_ns:
    print('processing {} ...'.format(f_n))
    curr_mov = tf.imread(os.path.join(corr_folder, f_n))
    curr_mov_d = ia.rigid_transform_cv2(img=curr_mov,
                                        zoom=(1. / xy_downsample_rate))
    curr_mov_d = ia.z_downsample(curr_mov_d, downSampleRate=t_downsample_rate)
    mov_d.append(curr_mov_d)

mov_d = np.concatenate(mov_d, axis=0).astype(np.int16)
tf.imsave('2p_movie_downsampled.tif', mov_d)
channels = ['DAPI', 'GCaMP', 'mRuby', 'NeuN']
downsample_rate = 0.1

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

fns = [f for f in os.listdir(curr_folder) if f[-4:] == '.btf']
fns.sort()
print('\n'.join(fns))

for fn in fns:
    print('\nprocessing {} ...'.format(fn))
    big_img = tf.imread(fn)
    fname = os.path.splitext(fn)[0]
    print('shape: {}'.format(big_img.shape))
    print('dtype: {}'.format(big_img.dtype))

    # comb_img = []

    for chi, chn in enumerate(channels):
        print('\tchannel: {}'.format(chn))
        down_img_ch = ia.rigid_transform_cv2(big_img[chi],
                                             zoom=downsample_rate).astype(
                                                 np.uint16)[::-1, :]
        tf.imsave('thumbnail_{}_{:02d}_{}.tif'.format(fname, chi, chn),
                  down_img_ch)
        # comb_img.append(down_img_ch)

    # comb_img = np.array(comb_img)
    # tf.imsave('{}_downsampled.tif'.format(fname), comb_img)
    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':
                                                             td_rate)

            for save_file_id in range(num_file_to_save):
                save_chunk = total_movs[ch_i][save_file_id *
                                              (frames_per_file *
                                               td_rate):(save_file_id + 1) *
                                              (frames_per_file * td_rate)]
                save_path = os.path.join(
                    save_folders[ch_i],
                    '{}_{:05d}_reorged.tif'.format(file_identifier,
                                                   save_ids[ch_i]))
                if td_rate != 1:
                    print('\tdown sampling for {} ...'.format(
                        os.path.split(save_path)[1]))
                    save_chunk = ia.z_downsample(save_chunk,
                                                 downSampleRate=td_rate,
                                                 is_verbose=False)

                print('\tsaving {} ...'.format(os.path.split(save_path)[1]))
                tf.imsave(save_path, save_chunk)
                save_ids[ch_i] = save_ids[ch_i] + 1

            if total_movs[ch_i].shape[0] % (frames_per_file * td_rate) == 0:
                total_movs[ch_i] = None
            else:
                frame_num_left = total_movs[ch_i].shape[0] % (frames_per_file *
                                                              td_rate)
                total_movs[ch_i] = total_movs[ch_i][-frame_num_left:]

print('\nprocessing residual frames ...')
    curr_save_folder = os.path.join(data_folder, file_identifier, ch_n)
    if not os.path.isdir(curr_save_folder):
        os.makedirs(curr_save_folder)
    save_folders.append(curr_save_folder)

curr_step = 0

for file_n in file_ns:
    curr_mov = tf.imread(os.path.join(data_folder, file_n))

    # reorient movie
    if is_rotate:
        h_new = int(curr_mov.shape[1] * np.sqrt(2))
        w_new = int(curr_mov.shape[2] * np.sqrt(2))
        curr_mov = ia.rigid_transform_cv2(curr_mov,
                                          rotation=140,
                                          outputShape=(h_new,
                                                       w_new))[:, :, ::-1]

    curr_frame_num = curr_mov.shape[0] / len(ch_ns)

    if curr_frame_num % frames_per_step != 0:
        raise ValueError(
            '{}: total frame number is not divisible by frames per step.'.
            format(file_n))

    curr_mov_chs = []
    for ch_i in range(len(ch_ns)):
        curr_mov_chs.append(curr_mov[ch_i::len(ch_ns)])

    steps = curr_frame_num // frames_per_step
    for step_ind in range(steps):
Example #21
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()
Example #22
0
def align_mapping_img(mapping_img, alignment_json, zoom=0.5):
    return ia.rigidTransform(align_image(mapping_img, alignment_json, 1),
                             zoom=zoom)
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))
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))
print('\n'.join(file_ns))

save_folders = []
for ch_n in ch_ns:
    curr_save_folder = os.path.join(data_folder, file_identifier, ch_n)
    if not os.path.isdir(curr_save_folder):
        os.makedirs(curr_save_folder)
    save_folders.append(curr_save_folder)

curr_step = 0

for file_n in file_ns:
    curr_mov = tf.imread(os.path.join(data_folder, file_n))

    # reorient movie
    curr_mov = ia.rigid_transform_cv2_2d(curr_mov[:, ::-1, :], rotation=135)

    curr_frame_num = curr_mov.shape[0] / len(ch_ns)

    if curr_frame_num % frames_per_step != 0:
        raise ValueError(
            '{}: total frame number is not divisible by frames per step.'.
            format(file_n))

    curr_mov_chs = []
    for ch_i in range(len(ch_ns)):
        curr_mov_chs.append(curr_mov[ch_i::len(ch_ns)])

    steps = curr_frame_num // frames_per_step
    for step_ind in range(steps):
Example #26
0
import numpy as np
import os

mov_name = "160525_M235516_aveMov_102.tif"

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)
mov_file_name, mov_file_ext = os.path.splitext(mov_name)
save_name = mov_file_name + '_detrend' + mov_file_ext

mov = tf.imread(mov_name).astype(np.float32)

height = mov.shape[1]
width = mov.shape[2]

roi = ia.generate_oval_mask((height, width), (height / 2, width / 2),
                            int(height * 0.6), int(width * 0.6))

f = plt.figure(figsize=(10, 10))
ax = f.add_subplot(111)
ax.imshow(mov[0, :, :], cmap='gray', interpolation='nearest')
pt.plot_mask_borders(roi, plotAxis=ax)

plt.show()

mov_detrend, trend, amp, rvalue = hl.regression_detrend(mov, roi)

f = plt.figure(figsize=(15, 4))
ax = f.add_subplot(111)
ax.plot(trend)
ax.set_title('trend')
    f_ns = [f for f in os.listdir(plane_folder) if f[-14:] == '_corrected.tif']
    f_ns.sort()
    print('\n'.join(f_ns))

    mov_join = []
    for f_n in f_ns:

        curr_mov = tf.imread(os.path.join(plane_folder, f_n))

        if curr_mov.shape[0] % t_downsample_rate != 0:
            print(
                'the frame number of {} ({}) is not divisible by t_downsample_rate ({}).'
                .format(f_n, curr_mov.shape[0], t_downsample_rate))

        curr_mov_d = ia.z_downsample(curr_mov,
                                     downSampleRate=t_downsample_rate)
        mov_join.append(curr_mov_d)

    mov_join = np.concatenate(mov_join, axis=0)
    add_to_mov = 10 - np.amin(mov_join)

    save_name = '{}_d1_{}_d2_{}_d3_1_order_C_frames_{}_.mmap'\
        .format(base_name, mov_join.shape[2], mov_join.shape[1], mov_join.shape[0])

    mov_join = mov_join.reshape(
        (mov_join.shape[0], mov_join.shape[1] * mov_join.shape[2]),
        order='F').transpose()
    mov_join_mmap = np.memmap(os.path.join(plane_folder, save_name),
                              shape=mov_join.shape,
                              order='C',
                              dtype=np.float32,
Example #28
0
file_id_save = 0
total_mov = None
base_name = '_'.join(file_list[0].split('_')[:-1])
save_folder = os.path.join(data_folder, 'reorged')
if not os.path.isdir(save_folder):
    os.makedirs(save_folder)

for file_path in file_paths:
    print('\nprocessing {} ...'.format(os.path.split(file_path)[1]))

    curr_mov = tf.imread(file_path)
    curr_mov = curr_mov.transpose((0, 2, 1))[:, ::-1, :]

    if temporal_downsample_rate != 1:
        curr_mov = ia.z_downsample(curr_mov,
                                   downSampleRate=temporal_downsample_rate)

    if total_mov is None:
        total_mov = curr_mov
    else:
        total_mov = np.concatenate((total_mov, curr_mov), axis=0)

    while (total_mov is not None) and (total_mov.shape[0] >= frames_per_file):

        num_file_to_save = total_mov.shape[0] // frames_per_file

        for save_file_id in range(num_file_to_save):
            save_chunk = total_mov[save_file_id *
                                   frames_per_file:(save_file_id + 1) *
                                   frames_per_file]
            save_path = os.path.join(
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)
Example #30
0
        ax_and_scatter = f.add_subplot(4, 5, 16)
        ax_and_scatter.plot(df_and['rf_{}_on_center_azi'.format(response_dir)],
                            df_and['rf_{}_on_center_alt'.format(response_dir)],
                            '.',
                            color='#ff0000')
        ax_and_scatter.plot(
            df_and['rf_{}_off_center_azi'.format(response_dir)],
            df_and['rf_{}_off_center_alt'.format(response_dir)],
            '.',
            color='#0000ff')
        ax_and_scatter.set_xlim([azi_min, azi_max])
        ax_and_scatter.set_ylim([alt_min, alt_max])

        # =============================pairwise distance=============================================
        dis_or = ia.pairwise_distance(df_or[[
            'rf_{}_onoff_center_azi'.format(response_dir),
            'rf_{}_onoff_center_alt'.format(response_dir)
        ]].values)
        ax_or_pd = f.add_subplot(4, 5, 2)
        if len(dis_or) > 0:
            ax_or_pd.hist(dis_or,
                          range=[0, 80],
                          bins=20,
                          facecolor='#aaaaaa',
                          edgecolor='none')
        ax_or_pd.get_yaxis().set_ticks([])
        ax_or_pd.set_title(
            'pw RF dis')  # pairwise receptive field center distance

        dis_on = ia.pairwise_distance(df_on[[
            'rf_{}_on_center_azi'.format(response_dir),
            'rf_{}_on_center_alt'.format(response_dir)