def create_particles_plot_map( stack_file_path, indices, values=None, vmin=None, vmax=None, colmap="viridis", title=None, colorlabel=None, plotpos=(), ): """Multiline Comment0""" x_coordinates = [] y_coordinates = [] value_for_coloring = [] for value_index, particle_index in enumerate(indices): particle = sp_ctf_refine_io.read_particle(stack_file_path, particle_index, header_only=True) particle_coord = particle.get_attr("ptcl_source_coord") x_coordinates.append(particle_coord[0]) y_coordinates.append(particle_coord[1]) if values is None: particle_ctf = particle.get_attr("ctf") value_for_coloring.append(particle_ctf.defocus) else: val_for_col = values[value_index] value_for_coloring.append(val_for_col) axis = matplotlib.pyplot.subplot(*plotpos) scatter = axis.scatter( x_coordinates, y_coordinates, c=value_for_coloring, vmin=vmin, vmax=vmax, marker="o", cmap=colmap, s=50, lw=0, ) matplotlib.pyplot.xlabel("Image dimension x") matplotlib.pyplot.ylabel("Image dimension y") cbar = matplotlib.pyplot.colorbar(scatter) if colorlabel: cbar.set_label(colorlabel, rotation=270) if title: axis.set_title(title, fontsize=7) if vmin != None or vmax != None: ticks = [t.get_text() for t in cbar.ax.get_yticklabels()] ticks[0] = "<" + ticks[0] ticks[-1] = ">" + ticks[-1] cbar.ax.set_yticklabels(ticks) matplotlib.pyplot.setp(axis.get_xticklabels(), rotation=30, horizontalalignment="right") return axis
def _main_(): argparser = setup_argparser() args = argparser.parse_args() path_to_resultsfile = args.resultsfile mode = args.mode lower_then = args.lower_then greater_then = args.greater_then path_output = args.output path_stack = args.stack field = args.field field_index_error = 1 field_index_dr_ratio = 3 if field == "ERROR": field = field_index_error elif field == "DR_RATIO": field = field_index_dr_ratio # Read in error file results = np.loadtxt(path_to_resultsfile, delimiter=",") # Identify relevant particles relevant_selection = [ a and b for a, b in zip(results[:, field] <= lower_then, results[:, field] >= greater_then) ] # Write stack number_of_particles = EMAN2.EMUtil.get_image_count(path_stack) local_bdb_stack = EMAN2db.db_open_dict(path_output) sp_global_def.write_command(EMAN2db.db_parse_path(path_output)[0]) num_particles_relevant = 0 for particle_index in range(number_of_particles): particle = sp_ctf_refine_io.read_particle(path_stack, particle_index, header_only=True) particle_header = particle.get_attr_dict() if relevant_selection[particle_index]: pctf = particle_header["ctf"] pctf.defocus = results[particle_index, 2] particle_header["ctf"] = pctf num_particles_relevant = num_particles_relevant + 1 if mode == "UPDATE": local_bdb_stack[particle_index] = particle_header elif mode == "EXTRACT" and relevant_selection[particle_index]: local_bdb_stack[num_particles_relevant - 1] = particle_header EMAN2db.db_close_dict(local_bdb_stack) sp_global_def.sxprint("Particles updated/extracted", num_particles_relevant)
def refine_set(particle_indices): """ Runs the defocus refinement for a set of particles. :param particle_indices: Indicies of the particles inside the stack :return: List of refined CTFs. """ try: refined_ctfs = [] particle_micrographs = {} for particle_index in particle_indices: particle = sp_ctf_refine_io.read_particle(STACK_FILE_PATH, particle_index) # particle.write_image("/home/twagner/temp/refine/"+str(particle_index)+"particle.hdf") if RESOLUTION is not None and PIXEL_SIZE is not None: particle = particle.process( "filter.lowpass.gauss", {"cutoff_freq": 1.0 / RESOLUTION, "apix": PIXEL_SIZE}, ) # In case of phase plate a high pass might be necessary # if PARTICLE_DIAMETER is not None # particle = particle.process("filter.highpass.gauss",{"cutoff_freq":1.0/160}) particle_projection_params = PROJECTION_PARAMETERS[particle_index].tolist() # As i shift the volume and not the particle, multiply by -1 particle_projection_params[3] = -1 * particle_projection_params[3] particle_projection_params[4] = -1 * particle_projection_params[4] # particle_projection_params[3] = add_proj_error(particle_projection_params, 3, 2) # particle_projection_params[4] = add_proj_error(particle_projection_params, 4, 2) # particle_projection_params[2] = add_proj_error(particle_projection_params, 2, 2) particle_ctf = particle.get_attr("ctf") volume = VOLUME1 if HALF_MAP_ASSIGNEMENTS is not None: if HALF_MAP_ASSIGNEMENTS[particle_index] != 0: volume = VOLUME2 best_ctf, error, drratio = refine_defocus_with_error_est( particle_volume=volume, particle_image=particle, projection_angle_and_shift=particle_projection_params, current_ctf=particle_ctf, def_search_range=DEFOCUS_SEARCH_RANGE, def_step_size=DEFOCUS_STEP_SIZE, ) particle_micrograph_filename = particle.get_attr("ptcl_source_image") if particle_micrograph_filename in particle_micrographs: particle_micrographs[particle_micrograph_filename]["indices"].append( particle_index ) particle_micrographs[particle_micrograph_filename]["defocus"].append( best_ctf.defocus ) particle_micrographs[particle_micrograph_filename]["diff"].append( particle_ctf.defocus - best_ctf.defocus ) particle_micrographs[particle_micrograph_filename]["error"].append( error ) particle_micrographs[particle_micrograph_filename]["drratio"].append( drratio ) else: particle_micrographs[particle_micrograph_filename] = { "indices": [particle_index], "diff": [particle_ctf.defocus - best_ctf.defocus], "error": [error], "defocus": [best_ctf.defocus], "drratio": [drratio], } refined_ctfs.append(best_ctf) del particle_projection_params del volume del particle_ctf del particle except Exception as err: import traceback sp_global_def.sxprint( "Exception happend for particles in range ", np.min(particle_indices), "-", np.max(particle_indices), ) traceback.print_exc() raise err return refined_ctfs, particle_micrographs