def match(a, c): matched_ROIs1, matched_ROIs2, non_matched1, non_matched2, performance, A2 = register_ROIs( A, a, dims, align_flag=False, thresh_cost=.7) cor = [ pearsonr(c1, c2)[0] for (c1, c2) in np.transpose([C[matched_ROIs1], c[matched_ROIs2]], (1, 0, 2)) ] print(np.mean(cor), np.median(cor)) return matched_ROIs2[[list(matched_ROIs1).index(i) for i in ids]]
max_thr = 0.1 A_forw, assign_forw, match_forw = register_multisession(A, dims, Cns, max_thr=max_thr) A_back, assign_back, match_back = register_multisession(A[::-1], dims, Cns[::-1], max_thr=max_thr) #%% align all pairs separately N = len(A) pairs = list(itertools.combinations(range(N), 2)) pairs_matches = [] for pair in pairs: match_1, match_2, non_1, non_, perf_, _ = register_ROIs(A[pair[0]], A[pair[1]], dims, template1=Cns[pair[0]], template2=Cns[pair[1]], plot_results=False, max_thr=max_thr) pairs_matches.append(np.vstack((match_1, match_2))) #%% read all pairing through the union pairs_forw = [] pairs_back = [] assign_back_rev = assign_back[:,::-1] for pair in pairs: match_f = extract_active_components(assign_forw, list(pair), only=False) match_f1 = assign_forw[list(match_f)][:,list(pair)] pairs_forw.append(match_f1.astype(int).T) match_b = extract_active_components(assign_back_rev, list(pair), only=False) match_b1 = assign_back_rev[list(match_b)][:,list(pair)]
#%% normalize matrices A1 = csc_matrix(A1 / A1.sum(0)) A2 = csc_matrix(A2 / A2.sum(0)) A3 = csc_matrix(A3 / A3.sum(0)) #%% match consecutive pairs from caiman.base.rois import register_ROIs import matplotlib.pyplot as plt import matplotlib.lines as mlines import numpy as np dims = 512, 512 match1_12, match2_12, mis1_12, mis2_12, perf_12 = register_ROIs( A1, A2, dims, plot_results=True, template1=data1['Cn'], template2=data2['Cn']) plt.figure() match2_23, match3_23, mis2_23, mis3_23, perf_23 = register_ROIs( A2, A3, dims, plot_results=True, template1=data2['Cn'], template2=data3['Cn']) #%% match2_12 = list(match2_12) match2_23 = list(match2_23) # ROIs in session 2 that are registered against both session 1 and session 3 ind_2 = list(set(match2_12).intersection(match2_23)) ind_1 = [match1_12[match2_12.index(x)] for x in ind_2] ind_3 = [match3_23[match2_23.index(x)] for x in ind_2] #%% make figure
dims, Cns[::-1], max_thr=max_thr) #%% align all pairs separately N = len(A) pairs = list(itertools.combinations(range(N), 2)) pairs_matches = [] for pair in pairs: match_1, match_2, non_1, non_, perf_, _ = register_ROIs( A[pair[0]], A[pair[1]], dims, template1=Cns[pair[0]], template2=Cns[pair[1]], plot_results=False, max_thr=max_thr) pairs_matches.append(np.vstack((match_1, match_2))) #%% read all pairing through the union pairs_forw = [] pairs_back = [] assign_back_rev = assign_back[:, ::-1] for pair in pairs: match_f = extract_active_components(assign_forw, list(pair), only=False) match_f1 = assign_forw[list(match_f)][:, list(pair)] pairs_forw.append(match_f1.astype(int).T) match_b = extract_active_components(assign_back_rev,
max_thr=0) #%% N = len(A) trip_forw = extract_active_components(assignments, list(range(N)), only=False) trip_back = extract_active_components(assignments_back, list(range(N)), only=True) #%% matched_ROIs1, matched_ROIs2, non_matched1, non_matched2, performan, _ = register_ROIs( A_union, A_back, dims, template1=Cns[-1], template2=Cns[0], plot_results=False, thresh_cost=.8, max_thr=0) #%% A_union_01, assignments_01, matchings_01 = register_multisession(A, dims, Cns, max_thr=0.1) A_back_01, assignments_back_01, matchings_back_01 = register_multisession( A[::-1], dims, Cns[::-1], max_thr=0.1) trip_forw_01 = extract_active_components(assignments_01, list(range(N)),
for mm in masks_3[triplets[:,2]]] # plt.legend(('Day1','Day2','Day3')) plt.title('Matched components across multiple days') plt.axis('off') day1 = mlines.Line2D([], [], color='b', label='Day 1') day2 = mlines.Line2D([], [], color='r', label='Day 2') day3 = mlines.Line2D([], [], color='y', label='Day 3') plt.legend(handles=[day1, day2, day3], loc=4) plt.show() #%% dims = 512, 512 plt.figure() match1_12, match2_12, mis1_12, mis2_12, perf_12, A2_12 = register_ROIs( A1, A2, dims, plot_results = True, max_thr = 0.0, align_flag = True, template1=Cn1, template2=Cn2) plt.figure() match1b_12, match2b_12, mis1b_12, mis2b_12, perfb_12, A2b_12 = register_ROIs( A1, A2, dims, plot_results=True, max_thr = 0.0, align_flag = True, template1=Cn1, template2=Cn2) #plt.figure() #%% match2_23, match3_23, mis2_23, mis3_23, perf_23, _ = register_ROIs( A3, A2, dims, plot_results=False, template1=Cn3, template2=Cn2) match2b_23, match3b_23, mis2_23, mis3_23, perf_23, _ = register_ROIs( A2, A3, dims, plot_results=False, template1=Cn2, template2=Cn3)
day1 = mlines.Line2D([], [], color='b', label='Day 1') day2 = mlines.Line2D([], [], color='r', label='Day 2') day3 = mlines.Line2D([], [], color='y', label='Day 3') plt.legend(handles=[day1, day2, day3], loc=4) plt.show() #%% dims = 512, 512 plt.figure() match1_12, match2_12, mis1_12, mis2_12, perf_12, A2_12 = register_ROIs( A1, A2, dims, plot_results=True, max_thr=0.0, align_flag=True, template1=Cn1, template2=Cn2) plt.figure() match1b_12, match2b_12, mis1b_12, mis2b_12, perfb_12, A2b_12 = register_ROIs( A1, A2, dims, plot_results=True, max_thr=0.0, align_flag=True, template1=Cn1, template2=Cn2)
#cors[cell_i] = np.correlate(a, v) cors[cell_i] = np.correlate(A1[:, cell_i], A2[:, cell_i]) return cors pairs = list(itertools.combinations(range(len(templates)), 2)) stats = pd.DataFrame() for pair in pairs: match_1, match_2, non_1, non_2, perf_, A2 = register_ROIs( spatial[pair[0]], spatial[pair[1]], dims, template1=templates[pair[0]], template2=templates[pair[1]], plot_results=False, max_thr=max_thr, thresh_cost=thresh_cost, max_dist=max_dist, Cn=templates[pair[0]]) plt.suptitle(cnm_titles[pair[0]] + ' vs ' + cnm_titles[pair[1]]) my_roi_image(spatial[pair[0]].toarray(), A2, dims, match_1, match_2, non_1, non_2, max_thr) # Calculate centroid distances for the matched cells cm_1 = com(spatial[pair[0]], dims[0], dims[1])[match_1] cm_2 = com(spatial[pair[1]], dims[0], dims[1])[match_2] cm_2_registered = com(A2, dims[0], dims[1])[match_2] distances = [0] * len(cm_1)