def test_mergeROIs(self): roi1 = ia.WeightedROI(np.arange(9).reshape((3, 3))) roi2 = ia.WeightedROI(np.arange(1, 10).reshape((3, 3))) merged_ROI = sca.merge_weighted_rois(roi1, roi2) merged_ROI2 = sca.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))))
def test_SpatialReceptiveField_reverse_locations(self): alts = [-2., -1., 0., 1., 2.] azis = [-2., -1., 0., 1., 2.] map = np.array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 1., 1., 0.], [0., 0., 1., 1., 0.], [0., 0., 0., 0., 0.]]) srf = sca.SpatialReceptiveField(mask=map, altPos=alts, aziPos=azis, sign='ON') # print(srf.get_weighted_rf_center()) assert (srf.get_weighted_rf_center() == [0.5, 0.5]) srf2 = sca.SpatialReceptiveField(mask=map, altPos=alts[::-1], aziPos=azis, sign='ON') assert (srf2.get_weighted_rf_center() == [-0.5, 0.5]) srf_f = srf.gaussian_filter(sigma=1) # print(srf_f.get_weighted_rf_center()) srf2_f = srf2.gaussian_filter(sigma=1) # print(srf2_f.get_weighted_rf_center()) assert (abs(srf_f.get_weighted_rf_center()[0] + srf2_f.get_weighted_rf_center()[0]) < 1e-10) srf_fi = srf_f.interpolate(ratio=2, method='linear') # print(srf_fi.get_weighted_mask()) # print(srf_fi.get_weighted_rf_center()) # import matplotlib.pyplot as plt # f=plt.figure() # ax=f.add_subplot(111) # srf_fi.plot_rf(plot_axis=ax, tick_spacing=1) # plt.show() srf_fit = srf_fi.threshold(thr=0.4) # print(srf_fit.get_weighted_rf_center()) assert (srf_fit.get_weighted_rf_center()[0] - 0.5 < 0.05) srf2_fi = srf2_f.interpolate(ratio=2, method='linear') # print(srf2_fi.get_weighted_mask()) # print(srf2_fi.get_weighted_rf_center()) assert (np.array_equal(srf_fi.get_weighted_mask(), srf2_fi.get_weighted_mask())) assert (abs(srf_fi.get_weighted_rf_center()[0] + srf2_fi.get_weighted_rf_center()[0]) < 1e-10)
def test_SpatialTemporalReceptiveField_plot_traces(self): locations = [ [2., 0.], [2., 1.], [0., 0.], [0., 1.], [2., 0.], [2., 1.], [0., 0.], [0., 1.], ] signs = [1, 1, 1, 1, -1, -1, -1, -1] traces = [ np.array([[0., 1., 2.]]), np.array([[2., 1., 0.]]), np.array([[1., 0., 2.]]), np.array([[2., 0., 1.]]), np.array([[0., 0., 2.]]), np.array([[0., 0., 1.]]), np.array([[2., 0., 0.]]), np.array([[1., 0., 0.]]), ] time = [-1., 0., 1.] strf = sca.SpatialTemporalReceptiveField(locations=locations, signs=signs, traces=traces, time=time) strf.plot_traces(figSize=(5, 5), yRange=[-1, 3]) import matplotlib.pyplot as plt plt.show()
def test_get_local_similarity_index(self): mask1 = np.zeros((5, 5)) mask1[1:4, 3] = 1 # print(mask1) mask2 = mask1 * 3 mask3 = mask1 * (-4) mask4 = np.zeros((5, 5)) mask4[1, 2] = 2 mask4[2, 1] = 5 # print(sca.get_local_similarity_index(mask1, mask2)) # # print(sca.get_local_similarity_index(mask1, mask3)) # # print(sca.get_local_similarity_index(mask1, mask4)) assert (np.allclose(sca.get_local_similarity_index(mask1, mask2), 1)) assert (np.allclose(sca.get_local_similarity_index(mask1, mask3), -1)) assert (np.allclose(sca.get_local_similarity_index(mask1, mask4), 0))
def test_DirectionTuning_get_opposite_ind(self): dire = np.linspace(0, 360, 12, endpoint=False) resp = np.array(list(range(8)) + list(range(4)), dtype=np.float) dt = pd.DataFrame(dire, columns=['dire']) dt['resp_mean'] = resp # print(dt) # print(dt.columns) # print('dire' in dt.columns) dt = sca.DirectionTuning(data=dt) print(dt) assert (dt.get_opposite_ind() == 1) assert (dt.get_opposite_ind(dire=30) == 7) assert (dt.get_opposite_ind(dire=225) == 1) dfe = dt.elevate(bias=5) print(dfe.trace_type)
def test_SpatialTemporalReceptiveField_IO(self): locations = [[3.0, 4.0], [3.0, 5.0], [2.0, 4.0], [2.0, 5.0], [3.0, 4.0], [3.0, 5.0], [2.0, 4.0], [2.0, 5.0]] signs = [1, 1, 1, 1, -1, -1, -1, -1] traces = [[np.arange(4)], [np.arange(1, 5)], [np.arange(2, 6)], [np.arange(3, 7)], [np.arange(5, 9)], [np.arange(6, 10)], [np.arange(7, 11)], [np.arange(8, 12)]] time = np.arange(4, 8) STRF = sca.SpatialTemporalReceptiveField(locations, signs, traces, time) # print(STRF.data) if os.path.isfile(self.testH5Path): os.remove(self.testH5Path) testFile = h5py.File(self.testH5Path, 'a') STRFGroup = testFile.create_group('spatial_temporal_receptive_field') STRF.to_h5_group(STRFGroup) testFile.close() h5File = h5py.File(self.testH5Path, 'r') STRF2 = sca.SpatialTemporalReceptiveField.from_h5_group( h5File['spatial_temporal_receptive_field']) h5File.close() assert (STRF2.data.altitude.equals(STRF.data.altitude)) assert (STRF2.data.azimuth.equals(STRF.data.azimuth)) assert (STRF2.data.sign.equals(STRF.data.sign)) # print(STRF.data.traces) # print(STRF2.data.traces) assert (np.array_equal( np.array([np.array(t) for t in STRF.data.traces]), np.array([np.array(t) for t in STRF2.data.traces])))
def test_SpatialTemporalReceptiveField(self): locations = [[3.0, 4.0], [3.0, 5.0], [2.0, 4.0], [2.0, 5.0], [3.0, 4.0], [3.0, 5.0], [2.0, 4.0], [2.0, 5.0]] signs = [1, 1, 1, 1, -1, -1, -1, -1] traces = [[np.arange(4)], [np.arange(1, 5)], [np.arange(2, 6)], [np.arange(3, 7)], [np.arange(5, 9)], [np.arange(6, 10)], [np.arange(7, 11)], [np.arange(8, 12)]] traces = [np.array(t) for t in traces] time = np.arange(4, 8) STRF = sca.SpatialTemporalReceptiveField(locations, signs, traces, time) # print(signs) # print(locations) # print(traces) # print(STRF.data) assert (STRF.data['traces'].iloc[0][0, 1] == 8) assert (STRF.data['sign'].loc[4] == -1) newLocations = [[location[0] + 1, location[1] + 1] for location in locations[0:4]] newSigns = [1, 1, 1, 1] STRF.add_traces(newLocations, newSigns, traces[0:4]) assert (STRF.data['traces'][7][1][2] == 4)
def test_getSparseNoiseOnsetIndex(self): display_log = ft.loadFile(self.sparseNoiseDisplayLogPath) # print(display_log.keys()) allOnsetInd, onsetIndWithLocationSign = sca.get_sparse_noise_onset_index( display_log) # print(list(allOnsetInd[0:10])) # print(onsetIndWithLocationSign[2]) assert (list( allOnsetInd[0:10]) == [0, 6, 12, 18, 24, 30, 36, 42, 48, 54]) for probe in onsetIndWithLocationSign: if np.array_equal(probe[0], np.array([0., 70. ])) and probe[1] == -1.: # print(probe[2]) assert (np.array_equal( probe[2], np.array([ 960, 1158, 2280, 3816, 4578, 5586, 6546, 7008, 8496, 9270 ])))
def test_SpatialReceptiveField_interpolate(self): SRF = sca.SpatialReceptiveField(np.random.rand(5, 5), np.arange(5)[::-1], np.arange(5)) SRF_i = SRF.interpolate(5) assert (SRF_i.get_weighted_mask().shape == (20, 20))
def test_SpatialReceptiveField_thresholdReceptiveField(self): SRF = sca.SpatialReceptiveField( np.arange(9).reshape((3, 3)), np.arange(3), np.arange(3)) thresholdedSRF = SRF.threshold(thr=4) assert (np.array_equal(thresholdedSRF.weights, np.arange(4, 9)))
def test_SpatialReceptiveField(self): SRF = sca.SpatialReceptiveField( np.arange(9).reshape((3, 3)), np.arange(3), np.arange(3)) assert (np.array_equal(SRF.weights, np.arange(1, 9)))
os_diff_on = [] os_diff_off = [] for roi_i, roi_row in osdf.iterrows(): date = int(roi_row['date']) mid = roi_row['mouse_id'] plane_n = roi_row['plane_n'] roi_n = roi_row['roi_n'] map_fn = '{}_{}_{}_{}'.format(date, mid, plane_n, response_dir) map_f = h5py.File(os.path.join(rf_maps_folder, map_fn + '.hdf5'), 'r') on_grp = map_f['{}_ON'.format(map_fn)] off_grp = map_f['{}_OFF'.format(map_fn)] dire = roi_row['dgc_{}_{}_{}_{}'.format(response_dir, dire_type, dire_pp, response_type)] ori = sca.dire2ori(dire) if roi_n in on_grp.keys() and roi_n in off_grp.keys(): rf_on = sca.SpatialReceptiveField.from_h5_group(on_grp[roi_n]) rf_off = sca.SpatialReceptiveField.from_h5_group(off_grp[roi_n]) c_alt_on, c_azi_on = rf_on.get_weighted_rf_center() c_alt_off, c_azi_off = rf_off.get_weighted_rf_center() onoff_ang = np.arctan((c_alt_on - c_alt_off) / (c_azi_on - c_azi_off)) onoff_ang = onoff_ang * 180. / np.pi onoff_ang = sca.dire2ori(onoff_ang) curr_diff = abs(onoff_ang - ori) if curr_diff > 90.: curr_diff = 180 - curr_diff
pdff = PdfPages(os.path.join(save_folder, 'STRFs_' + plane_n + '.pdf')) roi_lst = nwb_f['processing/rois_and_traces_' + plane_n + '/ImageSegmentation/imaging_plane/roi_list'].value roi_lst = [r for r in roi_lst if r[:4] == 'roi_'] roi_lst.sort() for roi_ind, roi_n in enumerate(roi_lst): print('roi: {} / {}'.format(roi_ind + 1, len(roi_lst))) curr_trace, _ = dt.get_single_trace(nwb_f=nwb_f, plane_n=plane_n, roi_n=roi_n) if np.min(curr_trace) < bias: add_to_trace = -np.min(curr_trace) + bias else: add_to_trace = 0. curr_strf = sca.get_strf_from_nwb(h5_grp=strf_grp[plane_n], roi_ind=roi_ind, trace_type=trace_type) curr_strf_dff = curr_strf.get_local_dff_strf(is_collaps_before_normalize=True, add_to_trace=add_to_trace) v_min, v_max = curr_strf_dff.get_data_range() f = curr_strf_dff.plot_traces(yRange=(v_min, v_max * 1.1), figSize=(16, 10), columnSpacing=0.002, rowSpacing=0.002) # plt.show() pdff.savefig(f) f.clear() plt.close(f) pdff.close() nwb_f.close()
_, rf_off_new = dt.get_rf_properties( srf=srf_off, polarity='negative', sigma=analysis_params['gaussian_filter_sigma_rf'], interpolate_rate=analysis_params[ 'interpolate_rate_rf'], z_thr_abs=analysis_params['rf_z_thr_abs'], z_thr_rel=analysis_params['rf_z_thr_rel']) else: raise ValueError rf_on_mask = rf_on_new.get_weighted_mask() rf_off_mask = rf_off_new.get_weighted_mask() rf_onoff_new = sca.SpatialReceptiveField( mask=np.max([rf_on_mask, rf_off_mask], axis=0), altPos=rf_on_new.altPos, aziPos=rf_on_new.aziPos, sign='ON_OFF', thr=analysis_params['rf_z_thr_abs']) curr_s2_grp = s2_grp.create_group(roi_row['roi_n']) rf_onoff_new.to_h5_group(curr_s2_grp) curr_s1_on_grp = s1_on_grp.create_group(roi_row['roi_n']) rf_on_new.to_h5_group(curr_s1_on_grp) curr_s1_off_grp = s1_off_grp.create_group(roi_row['roi_n']) rf_off_new.to_h5_group(curr_s1_off_grp) # positive S1 ON s1_on_df = subdf[(subdf['rf_{}_on_peak_z'.format(response_dir)] >= analysis_params['rf_z_thr_abs'])