def T_Test_Map(self): t_graph = np.zeros(shape=(512, 512, 3), dtype=np.uint8) cell_t = [] #定义每个细胞的AB之差t值。效应量= t/sqrt(N)t值越大A越强,为负则越小B越强。 cell_p = [] #定义每个细胞AB差异的显著性p cell_effect_size = [] #定义每个细胞的AB效应量。 for i in range(0, np.shape(self.spike_train)[0]): #全部细胞循环 set_size = min(len(self.frame_set_A), len(self.frame_set_B)) #先定义配对的样本大小 temp_A_set = random.sample( list(self.spike_train[i, self.frame_set_A[:]]), set_size) #在两个刺激下,都随机选择N个 temp_B_set = random.sample( list(self.spike_train[i, self.frame_set_B[:]]), set_size) [temp_t, temp_p] = stats.ttest_rel(temp_A_set, temp_B_set) cell_t.append(temp_t) cell_p.append(temp_p) #按顺序把p和t加进来 if temp_p < 0.05: #显著检验 cell_effect_size.append(temp_t / np.sqrt(set_size)) else: cell_effect_size.append(0) norm_cell_effect_size = cell_effect_size / abs( np.asarray(cell_effect_size)).max() for i in range(0, len(norm_cell_effect_size)): if norm_cell_effect_size[i] > 0: #大于零为红,小于零为蓝 x_list, y_list = pp.cell_location(self.cell_group[i]) t_graph[ y_list, x_list, 2] = norm_cell_effect_size[i] * 255 #注意CV2读写颜色顺序是BGR= = #index_graph[y_list,x_list,2] = norm_preference_index[i]*255 else: x_list, y_list = pp.cell_location(self.cell_group[i]) t_graph[y_list, x_list, 0] = abs(norm_cell_effect_size[i]) * 255 pp.save_graph(self.map_name + r'_T_Graph', t_graph, self.map_folder, '.png', 8, 1)
def Tuning_Index(self): preference_index = np.zeros(shape=(np.shape(self.spike_train)[0], 1), dtype=np.float64) for i in range(0, np.shape(self.spike_train)[0]): #全部细胞循环 temp_cell_A = 0 for j in range(0, len(self.frame_set_A)): #叠加平均刺激setA下的细胞反应 temp_cell_A = temp_cell_A + self.spike_train[ i, self.frame_set_A[j]] / len(self.frame_set_A) temp_cell_B = 0 for j in range(0, len(self.frame_set_B)): temp_cell_B = temp_cell_B + self.spike_train[ i, self.frame_set_B[j]] / len(self.frame_set_B) preference_index[i] = temp_cell_A - temp_cell_B norm_preference_index = preference_index / abs( preference_index).max() index_graph = np.zeros(shape=(512, 512, 3), dtype=np.uint8) for i in range(0, len(preference_index)): if preference_index[i] > 0: x_list, y_list = pp.cell_location(self.cell_group[i]) index_graph[ y_list, x_list, 2] = norm_preference_index[i] * 255 #注意CV2读写颜色顺序是BGR= = #index_graph[y_list,x_list,2] = norm_preference_index[i]*255 else: x_list, y_list = pp.cell_location(self.cell_group[i]) index_graph[y_list, x_list, 0] = abs(norm_preference_index[i]) * 255 #绘图 pp.save_graph(self.map_name + r'_Tuning_Index', index_graph, self.map_folder, '.png', 8, 1)
def Cell_Graph(self): # ============================================================================= # 定义在一开始进行 # self.spike_train = pp.read_variable(save_folder+r'\\spike_train.pkl') # self.cell_group = pp.read_variable(save_folder+r'\\Cell_Group.pkl') # ============================================================================= cell_tuning = np.zeros(shape=(np.shape(self.spike_train)[0], 1), dtype=np.float64) for i in range(np.shape(self.spike_train)[0]): #全部细胞循环 temp_cell_A = 0 for j in range(len(self.frame_set_A)): #叠加平均刺激setA下的细胞反应 temp_cell_A = temp_cell_A + self.spike_train[ i, self.frame_set_A[j]] / len(self.frame_set_A) temp_cell_B = 0 for j in range(len(self.frame_set_B)): temp_cell_B = temp_cell_B + self.spike_train[ i, self.frame_set_B[j]] / len(self.frame_set_B) cell_tuning[i] = temp_cell_A - temp_cell_B pp.save_variable(cell_tuning, self.map_folder + r'\\' + self.map_name + '_Cells.pkl') #把这个刺激的cell data存下来 #至此得到的cell tuning是有正负的,我们按照绝对值最大把它放到-1~1的范围里 norm_cell_tuning = cell_tuning / abs(cell_tuning).max() clip_min_cell = norm_cell_tuning.mean() - 3 * norm_cell_tuning.std() clip_max_cell = norm_cell_tuning.mean() + 3 * norm_cell_tuning.std() cell_clipped = np.clip(norm_cell_tuning, clip_min_cell, clip_max_cell) #对减图进行最大和最小值的clip #接下来plot出来细胞减图 sub_graph_cell = np.ones(shape=(512, 512), dtype=np.float64) * 127 for i in range(len(cell_clipped)): x_list, y_list = pp.cell_location(self.cell_group[i]) sub_graph_cell[y_list, x_list] = (cell_clipped[i] + 1) * 127 pp.save_graph(self.map_name + '_Cell', sub_graph_cell, self.map_folder, '.png', 8, 1)
def show_cell(self): #由于前面筛掉了一些cell,所以这里图需要重新画。 thres_graph = np.zeros(shape=(512, 512), dtype=np.uint8) for i in range(len(self.cell_group)): x_list, y_list = pp.cell_location(self.cell_group[i]) thres_graph[y_list, x_list] = 255 RGB_graph = cv2.cvtColor(thres_graph, cv2.COLOR_GRAY2BGR) #转灰度为RGB base_graph_path = self.save_folder + '\\' + self.save_name cv2.imwrite(base_graph_path + '.tif', RGB_graph) cv2.imwrite(base_graph_path + '_resized.tif', cv2.resize(RGB_graph, (1024, 1024))) #把细胞图放大一倍并保存起来 # pp.show_cell(base_graph_path+'.tif',self.cell_group)# 在细胞图上标上细胞的编号。 pp.show_cell(base_graph_path + '_resized.tif', self.cell_group) # 在细胞图上标上细胞的编号。
def cell_plot(self): thres_graph = np.zeros(shape=(512, 512), dtype=np.uint8) for i in range(len(self.cell_group)): x_list, y_list = pp.cell_location(self.cell_group[i]) thres_graph[y_list, x_list] = 255 RGB_graph = cv2.cvtColor(thres_graph, cv2.COLOR_GRAY2BGR) #转灰度为RGB for i in range(len(self.save_folder)): cv2.imwrite(self.save_folder[i] + r'\\' + self.save_name + '.tif', RGB_graph) cv2.imwrite( self.save_folder[i] + r'\\' + self.save_name + '_resized.tif', cv2.resize(RGB_graph, (1024, 1024))) pp.show_cell(self.save_folder[i] + r'\\' + self.save_name + '_resized.tif', self.cell_group) # 在细胞图上标上细胞的编号。 pp.save_variable( self.cell_group, self.save_folder[i] + r'\\' + self.save_name + '_Groups.pkl')
def cell_graph_plot(self,name,features):#注意输入格式,需要features第一个维度是样本数 graph_folder = save_folder+r'\\'+name pp.mkdir(graph_folder) for i in range(np.shape(features)[0]): temp_graph = np.ones(shape = (512,512),dtype = np.uint8)*127#基底图 temp_feature = features[i] if temp_feature.max() !=0: norm_temp_feature = (temp_feature/(abs(temp_feature).max())+1)/2#保持0不变,并拉伸至0-1的图 else: norm_temp_feature = (temp_feature+1)/2 for j in range(len(self.cell_group)):#循环细胞 x_list,y_list = pp.cell_location(self.cell_group[j]) temp_graph[y_list,x_list] = norm_temp_feature[j]*255 temp_graph_name = graph_folder+r'\\'+name+str(i+1) current_graph_labled = cv2.cvtColor(np.uint8(temp_graph),cv2.COLOR_GRAY2BGR) cv2.imwrite(temp_graph_name+'.png',np.uint8(cv2.resize(current_graph_labled,(1024,1024)))) pp.show_cell(temp_graph_name+'.png',self.cell_group)
def sub_video(self): #dF video is based on 0.5 graph, use 0 as gray. #Clip First, Attention Here, data <0 is set as 0, with data >0 clip at 2std self.sub_series = np.clip( self.sub_series, 0, self.sub_series.mean() + 2 * self.sub_series.std()) #Then normalize all data,keep 0 as 0 unchanged. self.sub_series = self.sub_series / abs(self.sub_series).max() #Recover cell data to frame sub_video_context = np.zeros(shape=(self.frame_Num, 512, 512), dtype=np.uint8) for i in range(self.frame_Num): current_frame = np.ones(shape=(512, 512), dtype=np.uint8) * 127 for j in range(len(self.cell_group)): x_list, y_list = pp.cell_location(self.cell_group[j]) current_frame[y_list, x_list] = np.uint8( (self.sub_series[i, j] + 1) * 127) sub_video_context[i, :, :] = current_frame self.video_plot('Sub_Video_Cell.avi', sub_video_context)
def F_video( self ): #F_video is 0-1 video on black back ground, show avtivations. #clip F_series to +-2std self.F_series = np.clip(self.F_series, self.F_series.mean() - 2 * self.F_series.std(), self.F_series.mean() + 2 * self.F_series.std()) # Then normalize F series to (0,1) self.F_series = (self.F_series - self.F_series.min()) / ( self.F_series.max() - self.F_series.min()) #recover cell data to graph datas. F_video_context = np.zeros(shape=(self.frame_Num, 512, 512), dtype=np.uint8) for i in range(self.frame_Num): current_frame = np.zeros(shape=(512, 512), dtype=np.uint8) for j in range(len(self.cell_group)): x_list, y_list = pp.cell_location(self.cell_group[j]) current_frame[y_list, x_list] = np.uint8(self.F_series[i, j] * 255) F_video_context[i, :, :] = current_frame self.video_plot('F_Video_Cell.avi', F_video_context)