Y = f['LogNcountsMmus'][:] # gene expression matrix tech_noise = f['LogVar_techMmus'][:] # technical noise genes_het_bool=f['genes_heterogen'][:] # index of heterogeneous(??!??) genes geneID = f['sym_names'][:] # gene names cellcyclegenes_filter = SP.unique(f['cellcyclegenes_filter'][:].ravel() -1) # idx of cell cycle genes cellcyclegenes_filterCB600 = f['ccCBall_gene_indices'][:].ravel() -1 # idxof cell cycle genes ... #ground truth from Hoechst staining phase_vec = f['labels'][:].ravel() if max(phase_vec)==4: phase_vec[phase_vec==1]=2 phase_vec = phase_vec-1 KG1 = SP.zeros((Y.shape[0],Y.shape[0])) for iph in range(Y.shape[0]): for jph in range(Y.shape[0]): if SP.bitwise_and(phase_vec[iph]==phase_vec[jph], phase_vec[iph]==1): KG1[iph,jph]=1 KS = SP.zeros((Y.shape[0],Y.shape[0])) for iph in range(Y.shape[0]): for jph in range(Y.shape[0]): if SP.bitwise_and(phase_vec[iph]==phase_vec[jph], phase_vec[iph]==2): KS[iph,jph]=1 KG2M = SP.zeros((Y.shape[0],Y.shape[0])) for iph in range(Y.shape[0]): for jph in range(Y.shape[0]): if SP.bitwise_and(phase_vec[iph]==phase_vec[jph], phase_vec[iph]==3): KG2M[iph,jph]=1 #intra-phase variations in cell size
fnames = sp.array(fnames).reshape(len(fnames)/2, 2) pos_items = [(a,b) for a in 'tp','fn' for b in 'tp','fn'] neg_items = [(a,b) for a in 'tn','fp' for b in 'tn','fp'] all_items = pos_items + neg_items for xitem, yitem in all_items: idx, x = d1[xitem] idy, y = d2[yitem] assert (idx==idy).all() sel = sp.bitwise_and(x, y) mfnames = fnames[idx][sel] nmfnames = len(mfnames) if nmfnames <= 0: continue mpath = path.join(output_path, xitem+'_'+yitem) os.makedirs(mpath) for n, (fname1, fname2) in enumerate(mfnames): arr1 = sp.atleast_3d(sp.misc.imread(fname1).astype('float32')) arr2 = sp.atleast_3d(sp.misc.imread(fname2).astype('float32')) # grayscale? if arr1.shape[2] == 1: arr1 = sp.concatenate([arr1,arr1,arr1], 2)
'genes_heterogen'][:] # index of heterogeneous(??!??) genes geneID = f['sym_names'][:] # gene names cellcyclegenes_filter = SP.unique(f['cellcyclegenes_filter'][:].ravel() - 1) # idx of cell cycle genes cellcyclegenes_filterCB600 = f['ccCBall_gene_indices'][:].ravel( ) - 1 # idxof cell cycle genes ... #ground truth from Hoechst staining phase_vec = f['labels'][:].ravel() if max(phase_vec) == 4: phase_vec[phase_vec == 1] = 2 phase_vec = phase_vec - 1 KG1 = SP.zeros((Y.shape[0], Y.shape[0])) for iph in range(Y.shape[0]): for jph in range(Y.shape[0]): if SP.bitwise_and(phase_vec[iph] == phase_vec[jph], phase_vec[iph] == 1): KG1[iph, jph] = 1 KS = SP.zeros((Y.shape[0], Y.shape[0])) for iph in range(Y.shape[0]): for jph in range(Y.shape[0]): if SP.bitwise_and(phase_vec[iph] == phase_vec[jph], phase_vec[iph] == 2): KS[iph, jph] = 1 KG2M = SP.zeros((Y.shape[0], Y.shape[0])) for iph in range(Y.shape[0]): for jph in range(Y.shape[0]): if SP.bitwise_and(phase_vec[iph] == phase_vec[jph], phase_vec[iph] == 3): KG2M[iph, jph] = 1
print "bad file: %s" % (fn) continue #store #pdb.set_trace() RV['K'] = K RV['cc_genes_het'] = cc_genes_het dumpDictHdf5(RV,fout) fout.close() Ycorr = RV['Ycorr'][:] SP.savetxt(out_file_base + '.Ycorr.txt', Ycorr) SP.savetxt(out_file_base + '.isConverged.txt', RV['is_converged']) #plot variance decomposition from scLVM_cfg import * indsconv = RV['is_converged'].ravel() == 1 is_nocc_conv = SP.bitwise_and(indsconv, cc_genes_het.ravel() == 1) vars_normConv = RV['varsnorm'][indsconv == 1, :] # ['hidden_0', 'biol_noise', 'tech_noise'] # ==> # ['hidden_0', 'tech_noise', 'biol_noise'] #pdb.set_trace() vars_normConv = vars_normConv[:, [0, 1, 2]] H2 = 1 - vars_normConv[:, 2] out_file_pdf = out_file_base+'.pdf' #pdb.set_trace() var_plot(vars_normConv, H2, CFG['var_comp_fields'][SP.array([0,1,2])],V_range=SP.linspace(0,1,11),plot_element_count=False, filename=out_file_pdf, normalize=True, figsize=[6,5]) # var_plot(vars_normConv, H2, CFG['var_comp_fields'][SP.array([0,1,2])],V_range=SP.linspace(0,1,8),plot_element_count=True, # filename=out_file_pdf, normalize=True, figsize=[6,5]) # calculate average variance components across all genes and visualize var_mean = vars_normConv.mean(0)
def extract_palm_mask(input_image, sample_spacing=20, debug=False, visual_debug=False, vd_last_step=True): centre_xy, inner_circle_radius, outer_circle_radius = detect_palm_circle(input_image, visual_debug=visual_debug and not vd_last_step, debug=debug) outer_circle_points = bresenhamCircle(centre_xy[0], centre_xy[1], outer_circle_radius) #sorting points in clockwise direction outer_circle_points = sorted(outer_circle_points, key = lambda c:atan2(c[0] - centre_xy[0], c[1] - centre_xy[1])) spacing = max(int((sample_spacing*len(outer_circle_points))/float(360)), 1) #subset the sampling space sample_points = [] for i in range(0, len(outer_circle_points), spacing): sample_points.append([outer_circle_points[i][0], outer_circle_points[i][1]]) if debug: print("[DEBUG] extract_palm_mask : outer circle point - %d, sample space - %d and sample points - %d" % (len(outer_circle_points), spacing, len(sample_points))) boundary_points = get_boundary_points(sample_points, input_image, debug=debug) convex_hull, palm_mask = np.zeros(input_image.shape, dtype=np.uint8), np.zeros(input_image.shape, dtype=np.uint8) for points in boundary_points: convex_hull[points[0], points[1]] = 255 convex_hull = convex_hull_image(convex_hull) palm_mask[convex_hull==True] = 255 palm_mask[convex_hull==False] = 0 wrist_point_one, wrist_point_two = wrist_detection(boundary_points) palm_mask = scipy.bitwise_and(input_image, palm_mask) display_image = np.stack([input_image, input_image, input_image], axis=2) #remove the pixel below the wristine remove_pixel_below_wrist_line(input_image, wrist_point_one, wrist_point_two) #image rotation #slope of wristline and angle of rotation m = (wrist_point_two[0]-wrist_point_one[0])/(wrist_point_two[1]-wrist_point_one[1]) theta = atan(m)*180/pi rotation_center = ((wrist_point_one[0]+wrist_point_two[0])/2, (wrist_point_one[1]+wrist_point_two[1])/2) #executing rotation with above parameters rotated_image, rotation_marix = img_rotation(input_image, theta, rotation_center, debug=debug) palm_mask, _ = img_rotation(palm_mask, theta, rotation_center, debug=debug) if visual_debug: # displaying the palm centre rr, cc = circle_perimeter(centre_xy[0], centre_xy[1], 3) rr, cc = valid_circle_points(rr, cc, display_image.shape[0], display_image.shape[1]) display_image[rr, cc] = [0, 0, 255] # displaying the palm inner circle rr, cc = circle_perimeter(centre_xy[0], centre_xy[1], inner_circle_radius) rr, cc = valid_circle_points(rr, cc, display_image.shape[0], display_image.shape[1]) display_image[rr, cc] = [0, 255, 0] # displaying the palm outer circle rr, cc = circle_perimeter(centre_xy[0], centre_xy[1], outer_circle_radius) rr, cc = valid_circle_points(rr, cc, display_image.shape[0], display_image.shape[1]) display_image[rr, cc] = [0, 255, 128] # displaying the wirst line rr, cc = line(wrist_point_one[0], wrist_point_one[1], wrist_point_two[0], wrist_point_two[1]) display_image[rr, cc] = [255, 0, 0] # imgnew = cv2.rotate(img,theta) fig = plt.figure() ax0 = fig.add_subplot(2, 2, 1) ax0.set_title("original input image") ax0.imshow(display_image) ax1 = fig.add_subplot(2, 2, 2) ax1.set_title("rotated image wrt wrist line") ax1.imshow(rotated_image, cmap='gray') ax2 = fig.add_subplot(2, 2, 3) ax2.set_title("palm mask with wrist line") ax2.imshow(palm_mask, cmap='gray') ax3 = fig.add_subplot(2, 2, 4) ax3.set_title("segmentation") ax3.imshow((rotated_image - palm_mask), cmap='gray') # wristline_len = np.sqrt(np.square(wrist_point_one[0] - wrist_point_two[0]) + np.square(wrist_point_one[1] - wrist_point_two[1])) # ax3.imshow(img_rotation(input_image, theta, (wrist_point_one[0]-wristline_len, wrist_point_one[0]-wristline_len), debug=debug), cmap='gray') plt.show() return palm_mask, rotated_image