def view_tomo(self,sigma=2,R=10): # d = {v_siz:(32,32,32), vs:{uuid0:{center, v, id}, uuid1:{center, v, id} ... }} subvols_loc = os.path.join(self.dump_path,"demo_single_particle_subvolumes.pickle") d = io_file.pickle_load(subvols_loc) a = io_file.read_mrc_data(self.path) if 'self.centers' not in dir(): centers = [] uuids = [] for k,v in d['vs'].items(): if v['v'] is not None: centers.append(v['center']) uuids.append(k) self.centers = centers self.uuids = uuids # denoise a_smooth = smooth(a,sigma) for slice_num in range(a_smooth.shape[2]): centers = np.array(centers) slice_centers = centers[(centers[:,2]-slice_num)**2<R**2] img = a_smooth[:,:,slice_num] plt.rcParams['figure.figsize'] = (15.0, 12.0) fig = plt.figure() ax = fig.add_subplot(111) plt.axis('off') for center_num in range(len(slice_centers)): y, x = slice_centers[center_num][0:2] r = np.sqrt(R**2 - (slice_centers[center_num][2]-slice_num)**2) circle = plt.Circle((x, y), r, color='b', fill=False) plt.gcf().gca().add_artist(circle) ax_u = ax.imshow(img, cmap = 'gray')
def view_subtom(self,subvol_num,sigma=2,R=10): subvols_loc = os.path.join(self.dump_path,"demo_single_particle_subvolumes.pickle") d = io_file.pickle_load(subvols_loc) a = io_file.read_mrc_data(self.path) # denoise a_smooth = smooth(a,sigma) if 'self.centers' not in dir(): centers = [] uuids = [] for k,v in d['vs'].items(): if v['v'] is not None: centers.append(v['center']) uuids.append(k) self.centers = centers self.uuids = uuids y, x, z = self.centers[subvol_num] img = a_smooth[:,:,z] plt.rcParams['figure.figsize'] = (10.0, 8.0) fig = plt.figure() ax = fig.add_subplot(111) circle = plt.Circle((x, y), R, color='b', fill=False) plt.gcf().gca().add_artist(circle) plt.axis('off') output_str = '%d of %d, uuid = %s' % ( subvol_num, len(centers), self.uuids[subvol_num]) plt.title(output_str) ax_u = ax.imshow(img, cmap = 'gray') save_path = 'tmp_sub.png' # plt.imsave(save_path, img, cmap='gray') plt.savefig(save_path) return save_path
def g_denoising(G_type, a, name, gaussian_sigma, save_flag=False): b_time = time.time() if G_type == 1: a = G.smooth(a, gaussian_sigma) elif G_type == 2: a = G.dog_smooth(a, gaussian_sigma) elif G_type == 3: a = G.dog_smooth__large_map(a, gaussian_sigma) end_time = time.time() print('Gaussian de-noise takes', end_time - b_time, 's', ' sigma=', gaussian_sigma) if save_flag: img = (a[:, :, int(a.shape[2] / 2)]).copy() # TODO: Change the image and tomogram saving path img_path = '/Users/apple/Desktop/Lab/Zach_Project/Denoising_Result/Gaussian/' + str(name) + '_G=' + \ str(gaussian_sigma) + '_type=' + str(G_type) + '.png' plt.imsave(img_path, img, cmap='gray') mrc_path = '/Users/apple/Desktop/Lab/Zach_Project/Denoising_Result/Gaussian/' + str(name) + '_G=' + \ str(gaussian_sigma) + '_type=' + str(G_type) + '.mrc' io_file.put_mrc_data(a, mrc_path) return img
def plot_slice(self, z, z_lower, z_upper, NewBox, FinishCurrentBox, label, sigma=2, R=10): time1 = time.time() a = self.volume # Smooth if sigma == 0: a_smooth = a else: a_smooth = smooth(a, sigma) # Get image img = a_smooth[:, :, z] plt.rcParams['figure.figsize'] = self.figsize self.current_ax.imshow(img, cmap=self.cmap) # Change box color based on lower and upper bound of z box_c = 'green' if z <= z_lower or z >= z_upper: box_c = 'red' # Store label and bounding box info when done with the current box if FinishCurrentBox: coordinates = toggle_selector.RS.extents # Return (xmin, xmax, ymin, ymax) coordinates = coordinates * 5 xmin = coordinates[0] xmax = coordinates[1] ymin = coordinates[2] ymax = coordinates[3] boxInfo = [ xmin, ymin, z_lower, xmax - xmin, ymax - ymin, z_upper - z_lower ] self.annotations[label] = boxInfo # Adding a new bounding box if NewBox: toggle_selector.RS = RectangleSelector( self.current_ax, self.line_select_callback, drawtype='box', useblit=True, button=[1, 3], # don't use middle button minspanx=5, minspany=5, spancoords='pixels', interactive=True, rectprops=dict(edgecolor=box_c, alpha=1, fill=False)) plt.connect('key_press_event', toggle_selector) print('plot_slice time', time.time() - time1)
def active_contour_slice(v, sigma=3.5, membrane_thickness=5, display_slice=-1, out_dir = './output', save_flag=True): if os.path.exists(out_dir): shutil.rmtree(out_dir) os.makedirs(out_dir) mask_v = np.zeros(v.shape) print(v.shape) vg = FG.smooth(v, sigma) for slice_num in range(mask_v.shape[2]): save_flag_slice = False if slice_num == display_slice and save_flag: save_flag_slice = True print('processing slice ', slice_num) ori_img = (v[:, :, slice_num]).copy() if save_flag_slice: plt.imsave(os.path.join(out_dir, 'original.png'), ori_img, cmap='gray') img = (vg[:, :, slice_num]).copy() if save_flag_slice: plt.imsave(os.path.join(out_dir, 'original_smooth.png'), img, cmap='gray') # generate init circle s = np.linspace(0, 2*np.pi, 400) x = 80 + 60*np.sin(s) y = 80 + 60*np.cos(s) init = np.array([x, y]).T # init = create_sphere(20,20,20,10) # sphere for 3d active contour snake = active_contour(img, init, alpha=0.015, beta=10, gamma=0.001) # Note: The default format of the returned coordinates is (x,y) instead of (row,col) in skimage 0.15.x - 0.17.x snake[:,[0,1]]= snake[:, [1,0]] r = snake[:,0].astype(int) c = snake[:,1].astype(int) img2 = img.copy() colour = np.min(img2) img2[r,c] = colour if save_flag_slice: plt.imsave(os.path.join(out_dir, 'contour.png'), img2, cmap='gray') mask = np.zeros(ori_img.shape) rr,cc = polygon(r, c) mask[rr,cc] = 2 mask[r,c] = 1 for i in range(membrane_thickness): mask = contour_shrink(mask) mask[mask==0] = 3 if save_flag_slice: plt.imsave(os.path.join(out_dir, 'mask.png'), mask, cmap='gray') mask_v [:,:,slice_num] = mask mask_im = AIVU.cub_img(mask_v)['im'] # better visualize the results for i in range(0,mask_im.shape[0],mask_v.shape[0]): mask_im[i,:]=0 for i in range(0,mask_im.shape[1],mask_v.shape[1]): mask_im[:,i]=0 if save_flag: plt.imsave(os.path.join(out_dir, 'result.png'), mask_im, cmap='gray') return mask_v
se = N.array(op['debug']['start_end']) v = v[se[0, 0]:se[0, 1], se[1, 0]:se[1, 1], se[2, 0]:se[2, 1]] v = v.astype(N.float) v -= v.mean() IF.put_mrc(v, '/tmp/v.mrc', overwrite=True) if op['inverse_intensity']: v = -v print('intensity distribution of v', 'max', v.max(), 'min', v.min(), 'mean', v.mean()) print('# gaussian smoothing') vg = FG.smooth(v, sigma=float(op['sigma']) / voxel_spacing) print('intensity distribution of vg', 'max', vg.max(), 'min', vg.min(), 'mean', vg.mean()) IF.put_mrc(vg, '/tmp/vg.mrc', overwrite=True) R = feature(vg) IF.put_mrc(R, op['out_file'], overwrite=True) ''' IF.put_mrc(R, '/tmp/r.mrc', overwrite=True) S = FD.gradient_magnitude_square(dif) print '# calculate directional dirvative along thee gradient' Rdd = FD.directional_derivative_along_gradient(R, d=dif) Sdd = FD.directional_derivative_along_gradient(S, d=dif)
def sphere_seg(v, sigma, init_level_set=None, out_dir='./output', save_flag=False): """ sphere_seg: membrane segmentation by sphere fitting @params: v: volume data sigma: gaussian sigma for denoising init_level_set: initial level set for morphsnake @return: [x, y, z, R]: coordinates and radius of the sphere """ np.random.seed(12345) if save_flag: if os.path.exists(out_dir): shutil.rmtree(out_dir) os.makedirs(out_dir) # morphsnake if init_level_set: init = init_level_set else: circle_set = circle_level_set(v.shape[:2], (80, 80), 60) init_mask = np.zeros(v.shape) for i in range(init_mask.shape[2]): init_mask[:, :, i] = circle_set init = checkerboard_level_set(v.shape) init = np.multiply(init, init_mask) v_im = AIVU.cub_img(v)['im'] if save_flag: plt.imsave(os.path.join(out_dir, 'original.png'), v_im, cmap='gray') vg = FG.smooth(v, sigma) mask_v = ACWE(image=vg, iterations=25, init_level_set=init) mask_im = AIVU.cub_img(mask_v)['im'] if save_flag: plt.imsave(os.path.join(out_dir, 'morphsnake_result.png'), mask_im, cmap='gray') # RANSC coords = np.array(np.where(mask_v == 1)) coords = coords.T # (N,3) # robustly fit line only using inlier data with RANSAC algorithm xyz = coords model_robust, inliers = ransac(xyz, CircleModel3D, min_samples=20, residual_threshold=3, max_trials=50) outliers = inliers == False # print('inliers_num = ', sum(inliers), 'inliers_num = ', sum(outliers)) x, y, z, R = model_robust.params v_RANSC = np.zeros(mask_v.shape) assert len(inliers) == coords.shape[0] if save_flag: for i in range(len(inliers)): if inliers[i]: v_RANSC[coords[i][0]][coords[i][1]][coords[i][2]] = 2 else: v_RANSC[coords[i][0]][coords[i][1]][coords[i][2]] = 1 vim_RANSC = AIVU.cub_img(v_RANSC)['im'] plt.imsave(os.path.join(out_dir, 'RANSC_result.png'), vim_RANSC, cmap='gray') a = FG.smooth(v, sigma) a.flags.writeable = True color = np.min(a) thickness = 1 center = (x, y, z) grid = np.mgrid[[slice(i) for i in a.shape]] grid = (grid.T - center).T phi1 = R - np.sqrt(np.sum((grid)**2, 0)) phi2 = np.max(R - thickness, 0) - np.sqrt(np.sum((grid)**2, 0)) res = np.int8(phi1 > 0) - np.int8(phi2 > 0) a[res == 1] = color vim = AIVU.cub_img(a)['im'] plt.imsave(os.path.join(out_dir, 'result.png'), vim, cmap='gray') return model_robust.params
def encoder_simple_conv_test(d, pose, img_org_file, out_dir, clus_num): if pose: assert img_org_file is not None tom0 = auto.read_mrc_numpy_vol(img_org_file) tom = AFG.smooth(tom0, 2.0) x_keys = [_ for _ in d['vs'] if d['vs'][_]['v'] is not None] x_train_no_pose = [N.expand_dims(d['vs'][_]['v'], -1) for _ in x_keys] x_train_no_pose = N.array(x_train_no_pose) x_center = [d['vs'][_]['center'] for _ in x_keys] x_train = [] default_val = tom.mean() x_train_no_pose -= x_train_no_pose.max() x_train_no_pose = N.abs(x_train_no_pose) print('pose normalizing') for i in range(len(x_train_no_pose)): center = x_center[i] v = x_train_no_pose[i][:, :, :, 0] c = auto.center_mass(v) # calculate principal directions rm = auto.pca(v=v, c=c)['v'] mid_co = (N.array(v.shape) - 1) / 2.0 loc_r__pn = rm.T.dot(mid_co - c) # pose normalize so that the major axis is along x-axis vr = auto.rotate_retrieve(v, tom=tom, rm=rm, center=center, loc_r=loc_r__pn, default_val=default_val) x_train.append(vr) x_train = N.array(x_train) x_train = N.expand_dims(x_train, axis=4) print('pose normalization finished') else: x_keys = [_ for _ in d['vs'] if d['vs'][_]['v'] is not None] x_train = [N.expand_dims(d['vs'][_]['v'], -1) for _ in x_keys] x_train = N.array(x_train) if False: # warning, if you normalize here, you need also to normalize when decoding. # so it is better not normalize. Use batch normalization in the network instead if True: x_train -= x_train.mean() x_train /= x_train.std() else: x_train -= x_train.min() x_train /= x_train.max() x_train -= 0.5 x_train *= 2 # print('x_train.shape', x_train.shape) model_dir = op_join(out_dir, 'model') if not os.path.isdir(model_dir): os.makedirs(model_dir) model_autoencoder_checkpoint_file = op_join( model_dir, 'model-autoencoder--weights--best.h5') model_autoencoder_file = op_join(model_dir, 'model-autoencoder.h5') model_encoder_file = op_join(model_dir, 'model-encoder.h5') model_decoder_file = op_join(model_dir, 'model-decoder.h5') if not os.path.isfile(model_autoencoder_file): enc = encoder_simple_conv(img_shape=d['v_siz']) autoencoder = enc['autoencoder'] autoencoder_p = autoencoder from keras.optimizers import Adam # choose a proper lr to control convergance speed, and val_loss adam = Adam(lr=0.001, beta_1=0.9, decay=0.001 / 500) # sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) autoencoder_p.compile(optimizer=adam, loss='mean_squared_error') if os.path.isfile(model_autoencoder_checkpoint_file): print('loading previous best weights', model_autoencoder_checkpoint_file) autoencoder_p.load_weights(model_autoencoder_checkpoint_file) from keras.callbacks import EarlyStopping, ModelCheckpoint earlyStopping = EarlyStopping(monitor='val_loss', patience=20, verbose=0, mode='auto') checkpoint = ModelCheckpoint(model_autoencoder_checkpoint_file, monitor='val_loss', verbose=1, save_best_only=True, mode='auto') # use a large batch size when batch normalization is used autoencoder_p.fit(x_train, x_train, nb_epoch=100, batch_size=128, shuffle=True, validation_split=0.1, callbacks=[checkpoint, earlyStopping]) # we use the best weights for subsequent analysis autoencoder_p.load_weights(model_autoencoder_checkpoint_file) enc['autoencoder'].save(model_autoencoder_file) enc['encoder'].save(model_encoder_file) enc['decoder'].save(model_decoder_file) else: import keras.models as KM enc = dict() enc['autoencoder'] = KM.load_model(model_autoencoder_file) enc['encoder'] = KM.load_model(model_encoder_file) enc['decoder'] = KM.load_model(model_decoder_file) x_enc = enc['encoder'].predict(x_train) # use kmeans to seperate x_enc into a specific number of clusters, # then decode cluster centers, and patch the decoded cluster centers back to the image, # can use mayavi??? import multiprocessing from sklearn.cluster import KMeans kmeans_n_init = multiprocessing.cpu_count() kmeans = KMeans(n_clusters=clus_num, n_jobs=-1, n_init=100).fit(x_enc) x_km_cent = N.array( [_.reshape(x_enc[0].shape) for _ in kmeans.cluster_centers_]) x_km_cent_pred = enc['decoder'].predict(x_km_cent) # save cluster info and cluster centers clus_center_dir = op_join(out_dir, 'clus-center') if not os.path.isdir(clus_center_dir): os.makedirs(clus_center_dir) kmeans_clus = defaultdict(list) for i, l in enumerate(kmeans.labels_): kmeans_clus[l].append(x_keys[i]) AIF.pickle_dump(kmeans_clus, op_join(clus_center_dir, 'kmeans.pickle')) ccents = {} for i in range(len(x_km_cent_pred)): ccents[i] = x_km_cent_pred[i].reshape(d['v_siz']) AIF.pickle_dump(ccents, op_join(clus_center_dir, 'ccents.pickle')) AIF.pickle_dump(x_km_cent, op_join(clus_center_dir, 'ccents_d.pickle'))
Load Volume and Display image¶ change to parent directory to use aitom library """ import os os.chdir("..") # load mrc file using io module # example data: # http://ftp.ebi.ac.uk/pub/databases/empiar/archive/10045/data/ribosomes/AnticipatedResults/Particles/Tomograms/05/IS002_291013_005_subtomo000001.mrc import aitom.io.file as IF a = IF.read_mrc_data("data/IS002_291013_005_subtomo000001.mrc") # denoising using gaussian filter for visualization from aitom.filter.gaussian import smooth a = smooth(a, sigma=8) # display image using image module import aitom.image.vol.util as IVU ''' a_im is a dict: 'im': image data, type of numpy.ndarray, elements in [0, 1] 'vt': volume data ''' a_im = IVU.cub_img(a) print(type(a_im['im'])) print(a_im['im'].shape) print(a_im['im'][1][1]) import matplotlib.pyplot as plt plt.imshow(a_im['im'], cmap='gray')