def pick(self, mrc_filename): """Do the picking job through tensorflow. This function read the micrograph data information based on the given filename of micrograph. Then do the auto picking based on pre-trained CNN model. Args: mrc_filename: string, it is the filename of the target micrograph. Returns: return list_coordinate list_coordinate: a list, the length of this list stands for the number of picked particles. Each element in the list is also a list, the length is 4, the first one is y-axis, the second one is x-axis, the third one is the predicted score, the fourth is the micrograph filename. """ # read the micrograph image data print(mrc_filename) header, body = DataLoader.readMrcFile(mrc_filename) num_col = header[0] num_row = header[1] body_2d = np.array(body, dtype = np.float32).reshape(num_row, num_col) # do process to micrograph body_2d, bin_size = DataLoader.preprocess_micrograph(body_2d) # Edge detection to get the ice noise mask # a binary matrix, 1 stands for the ice noise site # mask = edge_detection_ice(body_2d) step_size = 4 candidate_patches = None candidate_patches_exist = False num_total_patch = 0 patch_size = int(self.particle_size/bin_size) # the size to do peak detection local_window_size = int(0.6*patch_size/step_size) #print("image_col:", body_2d.shape[0]) #print("particle_size:", patch_size) #print("step_size:", step_size) map_col = int((body_2d.shape[0]-patch_size)/step_size) map_row = int((body_2d.shape[1]-patch_size)/step_size) #prediction = np.zeros((map_col, map_row), dtype = float) time1 = time.time() particle_candidate_all = [] map_index_col = 0 for col in range(0, body_2d.shape[0]-patch_size+1, step_size): for row in range(0, body_2d.shape[1]-patch_size+1, step_size): # extract the particle patch patch = np.copy(body_2d[col:(col+patch_size), row:(row+patch_size)]) # do preprocess to the particle patch = DataLoader.preprocess_particle(patch, self.model_input_size) particle_candidate_all.append(patch) num_total_patch = num_total_patch + 1 map_index_col = map_index_col + 1 map_index_row = map_index_col-map_col+map_row #print("map_col:",map_col) #print("map_row:",map_row) #print(len(particle_candidate_all)) #print("map_index_col:",map_index_col) #print("map_index_row:",map_index_row) #print("col*row:",map_index_col*map_index_row) # reshape it to fit the input format of the model particle_candidate_all = np.array(particle_candidate_all).reshape(num_total_patch, self.model_input_size[1], self.model_input_size[2], 1) # predict predictions = self.deepModel.evaluation(particle_candidate_all, self.sess) predictions = predictions[:, 1:2] predictions = predictions.reshape(map_index_col, map_index_row) time_cost = time.time() - time1 print("time cost: %d s"%time_cost) #display.save_image(prediction, "prediction.png") # get the prediction value to be a positive sample, it is a value between 0~1 # the following code not tested # do a connected component analysis # prediction = detete_large_component(prediction) # do a local peak detection to get the best coordinate # list_coordinate is a 2D list of shape (number_particle, 3) # element in list_coordinate is [x_coordinate, y_coordinate, prediction_value] list_coordinate = self.peak_detection(predictions, local_window_size) # add the mrc filename to the list of each coordinate for i in range(len(list_coordinate)): list_coordinate[i].append(mrc_filename) # transform the coordinates to the original size list_coordinate[i][0] = (list_coordinate[i][0]*step_size+patch_size/2)*bin_size list_coordinate[i][1] = (list_coordinate[i][1]*step_size+patch_size/2)*bin_size return list_coordinate
def pick(self, mrc_filename): if mrc_filename.endswith('.rec'): header, body = DataLoader.readRecFile(mrc_filename) else: header, body = DataLoader.readMrcFile(mrc_filename) if header == None or body == None: return [] num_col = header[0] num_row = header[1] body_2d = np.array(body, dtype=np.float32).reshape(num_row, num_col) body_2d_ori = body_2d body_2d, bin_size = DataLoader.preprocess_micrograph(body_2d) step_size = 4 candidate_patches = None candidate_patches_exist = False num_total_patch = 0 patch_size = int(self.particle_size / bin_size) local_window_size = int(patch_size / step_size) #local_window_size = int(0.6*patch_size) map_col = int((body_2d.shape[0] - patch_size + 1) / step_size) map_row = int((body_2d.shape[1] - patch_size + 1) / step_size) time1 = time.time() particle_candidate_all = [] map_index_col = 0 for col in range(0, body_2d.shape[0] - patch_size, step_size): for row in range(0, body_2d.shape[1] - patch_size, step_size): patch = np.copy(body_2d[col:(col + patch_size), row:(row + patch_size)]) #patch = DataLoader.preprocess_particle(patch, self.model_input_size) particle_candidate_all.append(patch) num_total_patch = num_total_patch + 1 map_index_col = map_index_col + 1 map_index_row = map_index_col - map_col + map_row #particle_candidate_all = np.array(particle_candidate_all).reshape( # num_total_patch, self.model_input_size[1], self.model_input_size[2], 1) particle_candidate_all = np.array(particle_candidate_all).reshape( num_total_patch, patch_size, patch_size, 1) predictions = self.deepModel.evaluation(particle_candidate_all, self.sess) predictions = predictions[:, 1:2] predictions = predictions.reshape(map_index_col, map_index_row) time_cost = time.time() - time1 if self.verbose: print("gpu time: %.1f s" % time_cost) list_coordinate = self.peak_detection(predictions, local_window_size) for i in range(len(list_coordinate)): list_coordinate[i].append(mrc_filename) list_coordinate[i][0] = (list_coordinate[i][0] * step_size + patch_size / 2) * bin_size list_coordinate[i][1] = (list_coordinate[i][1] * step_size + patch_size / 2) * bin_size #return all coordinate list_coordinate_all = [i for i in list_coordinate if i[2] > 0.0] list_coordinate_all = sorted(list_coordinate_all, key=lambda x: x[2], reverse=True) #print ("size = ", len(list_coordinate)) list_coordinate = [i for i in list_coordinate if i[2] > self.threshold] list_coordinate = sorted(list_coordinate, key=lambda x: x[2], reverse=True) #print ("filtered size = ", len(list_coordinate)) #list_coordinate = list_coordinate[:100] print("#candidate:%d, #picked:%d" % (num_total_patch, len(list_coordinate))) plot_list_coordinate = copy.deepcopy(list_coordinate) for i in range(len(plot_list_coordinate)): plot_list_coordinate[i][0] = plot_list_coordinate[i][0] / bin_size plot_list_coordinate[i][1] = plot_list_coordinate[i][1] / bin_size if self.plot_picking_result: #print ">>>>>>>>>>>>>>>", body_2d.shape reference_coordinate_file = mrc_filename.replace( '.mrc', '_DW_recentered.star') reference_coordinate_file = os.path.join( '/data00/Data/piezo/train', reference_coordinate_file) #print(reference_coordinate_file) if os.path.isfile(reference_coordinate_file): reference_coordinate = DataLoader.read_coordinate_from_star( reference_coordinate_file) for i in range(len(reference_coordinate)): reference_coordinate[i][ 0] = reference_coordinate[i][0] / bin_size reference_coordinate[i][ 1] = reference_coordinate[i][1] / bin_size #display.plot_circle_in_micrograph(body_2d, reference_coordinate, plot_list_coordinate, patch_size, "plot/micro_circle_%s.png" % (os.path.basename(mrc_filename))) #plot_dir = os.path.basename(self.output_dir) plot_dir = os.path.join(os.path.abspath(self.output_dir), "plot") #pos = plot_dir.rfind('/') #plot_dir = os.path.join(plot_dir[:pos], 'plot') if self.verbose: print "plot_dir >>>>>>>>>> ", plot_dir if self.plot_picking_result and os.path.exists(plot_dir) == False: os.makedirs(plot_dir) display.plot_circle_in_micrograph( body_2d, plot_list_coordinate, patch_size, plot_dir + "/micro_circle_%s.png" % (os.path.basename(mrc_filename))) #display.plot_circle_in_micrograph(body_2d_ori, list_coordinate, self.particle_size, "plot/micro_circle_%s.png" % (os.path.basename(mrc_filename))) return list_coordinate, list_coordinate_all
unique_name = 'stack_2406_2x_SumCorr_movie_DW' #unique_name = 'stack_3025_2x_SumCorr_movie_DW' coordinates = [] class_number = [] starfile = os.path.join(basepath, unique_name + new + '.star') with open(starfile) as fin: idx = 0 for l in fin: idx += 1 if idx <= 5 or l.strip() == '': continue t = map(float, l.strip().split()) coordinates.append([int(t[0]), int(t[1])]) class_number.append(int(t[2])) plot = 'test_plot_%d' % peek_cls + new + '.png' filename = os.path.join(mrcpath, unique_name + '.mrc') header, body = DataLoader.readMrcFile(filename) n_col = header[0] n_row = header[1] print n_col, n_row body_2d = np.array(body, dtype=np.float32).reshape(n_row, n_col, 1) body_2d, bin_size = DataLoader.preprocess_micrograph(body_2d) coordinates = np.array(coordinates) coordinates = coordinates / bin_size plot_circle_in_micrograph(body_2d, coordinates, class_number, 180 / bin_size, plot, color='white')
def pick(self, mrc_filename): """Do the picking job through tensorflow. This function read the micrograph data information based on the given filename of micrograph. Then do the auto picking based on pre-trained CNN model. Args: mrc_filename: string, it is the filename of the target micrograph. Returns: return list_coordinate list_coordinate: a list, the length of this list stands for the number of picked particles. Each element in the list is also a list, the length is 4, the first one is y-axis, the second one is x-axis, the third one is the predicted score, the fourth is the micrograph filename. """ # read the micrograph image data print(mrc_filename) header, body = DataLoader.readMrcFile(mrc_filename) num_col = header[0] num_row = header[1] body_2d = np.array(body, dtype=np.float32).reshape(num_row, num_col) # do process to micrograph body_2d, bin_size = DataLoader.preprocess_micrograph(body_2d) # Edge detection to get the ice noise mask # a binary matrix, 1 stands for the ice noise site # mask = edge_detection_ice(body_2d) step_size = 4 candidate_patches = None candidate_patches_exist = False num_total_patch = 0 patch_size = int(self.particle_size / bin_size) # the size to do peak detection local_window_size = int(0.6 * patch_size / step_size) #print("image_col:", body_2d.shape[0]) #print("particle_size:", patch_size) #print("step_size:", step_size) map_col = int((body_2d.shape[0] - patch_size) / step_size) map_row = int((body_2d.shape[1] - patch_size) / step_size) #prediction = np.zeros((map_col, map_row), dtype = float) time1 = time.time() particle_candidate_all = [] map_index_col = 0 for col in range(0, body_2d.shape[0] - patch_size + 1, step_size): for row in range(0, body_2d.shape[1] - patch_size + 1, step_size): # extract the particle patch patch = np.copy(body_2d[col:(col + patch_size), row:(row + patch_size)]) # do preprocess to the particle patch = DataLoader.preprocess_particle(patch, self.model_input_size) particle_candidate_all.append(patch) num_total_patch = num_total_patch + 1 map_index_col = map_index_col + 1 map_index_row = map_index_col - map_col + map_row #print("map_col:",map_col) #print("map_row:",map_row) #print(len(particle_candidate_all)) #print("map_index_col:",map_index_col) #print("map_index_row:",map_index_row) #print("col*row:",map_index_col*map_index_row) # reshape it to fit the input format of the model particle_candidate_all = np.array(particle_candidate_all).reshape( num_total_patch, self.model_input_size[1], self.model_input_size[2], 1) # predict predictions = self.deepModel.evaluation(particle_candidate_all, self.sess) predictions = predictions[:, 1:2] predictions = predictions.reshape(map_index_col, map_index_row) time_cost = time.time() - time1 print("time cost: %d s" % time_cost) #display.save_image(prediction, "prediction.png") # get the prediction value to be a positive sample, it is a value between 0~1 # the following code not tested # do a connected component analysis # prediction = detete_large_component(prediction) # do a local peak detection to get the best coordinate # list_coordinate is a 2D list of shape (number_particle, 3) # element in list_coordinate is [x_coordinate, y_coordinate, prediction_value] list_coordinate = self.peak_detection(predictions, local_window_size) # add the mrc filename to the list of each coordinate for i in range(len(list_coordinate)): list_coordinate[i].append(mrc_filename) # transform the coordinates to the original size list_coordinate[i][0] = (list_coordinate[i][0] * step_size + patch_size / 2) * bin_size list_coordinate[i][1] = (list_coordinate[i][1] * step_size + patch_size / 2) * bin_size return list_coordinate