def localali(im1, im2, psref, maxdx): """Takes an fft.amplitude() psref to use as a basis for optimal alignment. Exhaustive search +-maxdx pixels. returns (dx,dy) required to bring im2 into alignment with im1""" nx = im1["nx"] mask = EMAN2.EMData(nx, nx, 1) mask.to_one() mask.process_inplace("mask.gaussian", { "inner_radius": nx / 4, "outer_radius": nx / 12 }) # EMAN2.display(mask) maxdx = int(maxdx) out = EMAN2.EMData(2 * maxdx + 1, 2 * maxdx + 1, 1) for dx in xrange(-maxdx, maxdx + 1): for dy in xrange(-maxdx, maxdx + 1): av = im1 + im2.process("xform.translate.int", {"trans": (dx, dy, 0)}) av.mult( mask ) # to prevent edge effects and slightly smooth the powspec avf = av.do_fft() avf.ri2ap() avf = avf.amplitude() out[dx + maxdx, dy + maxdx] = avf.cmp("ccc", psref) # print dx,dy,out[dx+20,dy+20] ret = out.calc_max_location() return (int(ret[0] - maxdx), int(ret[1] - maxdx)), out
def orients_one_unit(self, delta_deg): sym = EMAN2.parsesym(self._symtype) drad = np.pi * delta_deg / 180.0 # generate uniform orientations on the sphere vecs = np.float32(Symmetry.calc_points(drad)) nvec = vecs.shape[0] # convert unit vectors to azimuth and altitude az = 180.0 * np.arctan2(vecs[:, 0], vecs[:, 2]) / np.pi + 180.0 alt = 180.0 * np.arcsin(vecs[:, 1]) / np.pi + 90.0 # zero out azimuth on poles aloc = np.logical_or(alt == 0.0, alt == 180.0) az[aloc] = 0.0 # select orientations inside the asymmetric unit, including mirrors (True) isin = np.array([ sym.is_in_asym_unit(float(az[v]), float(alt[v]), True) for v in range(nvec) ]) azin = az[isin] altin = alt[isin] ors = [ EMAN2.Transform({ "type": "eman", "az": float(a), "alt": float(al), "phi": 0.0 }) for a, al in zip(azin, altin) ] return ors
def setup_nn4(image_size, npad=2, sym='c1', weighting=1, **extra): ''' Initalize a reconstruction object :Parameters: image_size : int Size of the input image and output volume npad : int, optional Number of times to pad the input image, default: 2 sym : str, optional Type of symmetry, default: 'c1' weighting : int Amount of weight to give projections :Returns: recon : tuple Reconstructor, Fourier volume, Weight Volume, and numpy versions ''' if EMAN2 is None: raise ImportError, "EMAN2/Sparx library not available, setup_nn4 requires EMAN2/Sparx" fftvol = EMAN2.EMData() weight = EMAN2.EMData() param = { "size": image_size, "npad": npad, "symmetry": sym, "weighting": weighting, "fftvol": fftvol, "weight": weight } r = EMAN2.Reconstructors.get("nn4", param) r.setup() return (r, fftvol, weight), em2numpy(fftvol), em2numpy(weight), image_size
def backproject_nn4_new(img, align=None, recon=None, **extra): ''' Add the given image and alignment or generator of image/alignment pairs to the current reconstruction :Parameters: img : array or EMData Image of projection to backproject into reconstruction align : array, optional Array of alignment parameters (not required if img is generator of images and alignments) recon : tuple Reconstructor, Fourier volume, Weight Volume, and numpy versions extra : dict Keyword arguments to be passed to setup_nn4 if recon is None :Returns: recon : tuple Reconstructor, Fourier volume, Weight Volume, and numpy versions ''' if EMAN2 is None: raise ImportError, "EMAN2/Sparx library not available, backproject_nn4_new requires EMAN2/Sparx" npad, sym, weighting = extra.get('npad', 2), extra.get('sym', 'c1'), extra.get( 'weighting', 1) e = EMAN2.EMData() if not hasattr(img, 'ndim'): for i, val in img: a = align[i] ''' if a[4] != (i+1): _logger.error("mismatch: %d != %d -- %s"%(a[4], i+1, str(align[:5]))) assert(a[4]==(i+1)) ''' xform_proj = EMAN2.Transform({ "type": "spider", "phi": a[2], "theta": a[1], "psi": a[0] }) if not is_em(val): val = numpy2em(val, e) if recon is None: recon = setup_nn4(val.get_xsize(), npad, sym, weighting) recon[0][0].insert_slice(val, xform_proj) else: xform_proj = EMAN2.Transform({ "type": "spider", "phi": align[2], "theta": align[1], "psi": align[0] }) if not is_em(img): img = numpy2em(img) if recon is None: recon = setup_nn4(val.get_xsize(), npad, sym, weighting) recon[0][0].insert_slice(img, xform_proj) val1 = recon[1].copy() val2 = recon[2].copy() return val1, val2
def writeParticles(): line = sys.stdin.readline() fnHdf = "" while line: objDict = json.loads(line) index = int(objDict['_index']) filename = str(objDict['_filename']) imageData = eman.EMData() imageData.read_image(filename, index) if '_ctfModel._defocusU' in objDict: ctf = eman.EMAN2Ctf() defU = objDict['_ctfModel._defocusU'] defV = objDict['_ctfModel._defocusV'] ctf.from_dict({"defocus": (defU + defV) / 20000.0, "dfang": objDict['_ctfModel._defocusAngle'], "dfdiff": (defU - defV) / 10000.0, "voltage": objDict['_acquisition._voltage'], "cs": max(objDict['_acquisition._sphericalAberration'], 0.0001), "ampcont": objDict['_acquisition._amplitudeContrast'] * 100.0, "apix": objDict['_samplingRate']}) imageData.set_attr('ctf', ctf) imageData.set_attr('apix_x', objDict['_samplingRate']) imageData.set_attr('apix_y', objDict['_samplingRate']) transformation = None if '_angles' in objDict: # TODO: convert to vector not matrix angles = objDict['_angles'] shifts = objDict['_shifts'] transformation = eman.Transform({"type": "spider", "phi": angles[0], "theta": angles[1], "psi": angles[2], "tx": -shifts[0], "ty": -shifts[1], "tz": -shifts[2], "mirror": 0, # TODO: test flip "scale": 1.0}) if transformation is not None: imageData.set_attr('xform.projection', transformation) outputFile = str(objDict['hdfFn']) if outputFile != fnHdf: i = 0 fnHdf = outputFile imageData.write_image(outputFile, i, eman.EMUtil.ImageType.IMAGE_HDF, False) i += 1 print("OK") # it is necessary to add newline sys.stdout.flush() line = sys.stdin.readline()
def backproject_nn4_queue(qin, qout, shmem, shape, process_limit, process_number, align=None, **extra): ''' Add the given image and alignment or generator of image/alignment pairs to the current reconstruction :Parameters: img : array or EMData Image of projection to backproject into reconstruction align : array, optional Array of alignment parameters (not required if img is generator of images and alignments) recon : tuple Reconstructor, Fourier volume, Weight Volume, and numpy versions extra : dict Keyword arguments to be passed to setup_nn4 if recon is None :Returns: recon : tuple Reconstructor, Fourier volume, Weight Volume, and numpy versions ''' if EMAN2 is None: raise ImportError, "EMAN2/Sparx library not available, backproject_nn4_queue requires EMAN2/Sparx" npad, sym, weighting = extra.get('npad', 2), extra.get('sym', 'c1'), extra.get( 'weighting', 1) e = EMAN2.EMData() recon = None while True: pos = qin.get() if pos is None or pos[0] == -1: if hasattr(qin, "task_done"): qin.task_done() break pos, idx = pos a = align[idx] img = shmem[pos].reshape(shape) xform_proj = EMAN2.Transform({ "type": "spider", "phi": a[2], "theta": a[1], "psi": a[0] }) if not is_em(img): img = numpy2em(img, e) if recon is None: recon = setup_nn4(img.get_xsize(), npad, sym, weighting) recon[0][0].insert_slice(img, xform_proj) qout.put((process_number, idx)) return recon[0], recon[1], recon[2]
def do_dataset(names): ''' Supposing we have marked data laying in folder data, in two subfolders 'good' and 'ice' and only this way. For now way of keeping data is hardcoded. #TODO:Make set creator more flexible. So we got list of full paths to mrc files lying in both direcories. Func assembles dataset,labels encoding dictionary matching classifier labels to words 'good' and 'ice' and returning it. Args: names - list of full paths to mrc files lying in a directories as described above. returns: dataset(list of feature vectors) labels(list of labels) encoding_dict dictionary via label:label_name. ''' labels = [name.split('/')[-2] for name in names] labels = np.array(labels) ice_mask = labels == 'ice' good_mask = labels == 'good' names = np.array(names) ice_names = names[ice_mask] good_names = names[good_mask] good_labels = [0] * len(good_names) ice_labels = [1] * len(good_names) encoding_dict = {0: 'good', 1: 'ice'} goods = [] ices = [] for name in good_names: img = EMAN2.EMData(name, 0) img_np = EMAN2.EMNumPy.em2numpy(img) sample = ft.preprocess_img(img_np) goods.append(sample) for name in ice_names: img = EMAN2.EMData(name, 0) img_np = EMAN2.EMNumPy.em2numpy(img) sample = ft.preprocess_img(img_np) ices.append(sample) dataset = goods + ices labels = good_labels + ice_labels return dataset, labels, encoding_dict
def stackToHDF(infile, outfile, apix, pinfo=None): """ convert stack to an hdf stack pinfo contains CTF information from apFrealign.getPerParticleCTF pinfo may also contain helical information """ from utilities import generate_ctf a = EMAN2.EMData() imn = EMAN2.EMUtil.get_image_count(infile) if pinfo is not None: if len(pinfo) != imn: apDisplay.printError("insufficient particle info for stack") # output must end with hdf outf, ext = os.path.splitext(outfile) if ext != '.hdf': outstack = outf + ".hdf" apDisplay.printMsg("Generating '%s' with %i particles" % (outstack, imn)) for i in xrange(imn): a.read_image(infile, i) a.set_attr_dict({'active': 1}) t2 = EMAN2.Transform({ "type": "spider", "phi": 0, "theta": 0, "psi": 0 }) a.set_attr("xform.projection", t2) a.set_attr("apix_x", apix) if pinfo is not None: pdata = pinfo[i + 1] df1 = pdata['df1'] df2 = pdata['df2'] astig = pdata['angastig'] kv = pdata['kev'] cs = pdata['cs'] ampc = pdata['ampc'] * 100 # save CTF dict (bfactor is hard coded to 0.0) df = (float(df1) + float(df2)) / 2 ctfgen = generate_ctf([df, cs, kv, apix, 0.0, ampc]) a.set_attr("ctf", ctfgen) # if helical info is present if pdata['hnum'] is not None: a.set_attr("h_angle", pdata['hangle']) a.write_image(outstack, i) return outstack
def backproject_nn4(img, align=None, recon=None, **extra): ''' Add the given image and alignment or generator of image/alignment pairs to the current reconstruction :Parameters: img : array or EMData Image of projection to backproject into reconstruction align : array, optional Array of alignment parameters (not required if img is generator of images and alignments) recon : tuple Reconstructor, Fourier volume, Weight Volume, and numpy versions extra : dict Keyword arguments to be passed to setup_nn4 if recon is None :Returns: recon : tuple Reconstructor, Fourier volume, Weight Volume, and numpy versions ''' if EMAN2 is None: raise ImportError, "EMAN2/Sparx library not available, backproject_nn4 requires EMAN2/Sparx" npad, sym, weighting = extra.get('npad', 2), extra.get('sym', 'c1'), extra.get( 'weighting', 1) if not hasattr(img, 'ndim'): for i, val in enumerate(img): if isinstance(val, tuple): val, a = val else: a = align[i] xform_proj = EMAN2.Transform({ "type": "spider", "phi": a[2], "theta": a[1], "psi": a[0] }) if not is_em(val): val = numpy2em(val) if recon is None: recon = setup_nn4(val.get_xsize(), npad, sym, weighting) recon[0][0].insert_slice(val, xform_proj) else: xform_proj = EMAN2.Transform({ "type": "spider", "phi": align[2], "theta": align[1], "psi": align[0] }) if not is_em(img): img = numpy2em(img) if recon is None: recon = setup_nn4(val.get_xsize(), npad, sym, weighting) recon[0][0].insert_slice(img, xform_proj) return recon
def _build(self, index): '''Build a single image in the file.''' # print "...index:", index header = {} # Read the header img = EMAN2.EMData() img.read_image(self.workfile, 0, True) h = img.get_attr_dict() # Copy basic header information header['nx'] = h['nx'] header['ny'] = h['ny'] header['nz'] = h['nz'] header['slices'] = [] if header['nz'] == 1: # 2D Image img2 = EMAN2.EMData() img2.read_image(self.workfile, index, False) img2.process_inplace("normalize") if self.nimg > 1: # ... stack of 2D images. header['slices'].append( self.build_slice(img2, index=index, fixed=[128, 512, 1024])) else: # regular old 2D image -- also generate power spectrum and tiles. header['slices'].append( self.build_slice(img2, index=index, tile=True, pspec=True, fixed=[128, 512, 1024])) else: # 3D Image -- read region for each Z slice for i in range(header['nz']): region = EMAN2.Region(0, 0, i, header['nx'], header['ny'], 1) img2 = EMAN2.EMData() img2.read_image(self.workfile, 0, False, region) header['slices'].append( self.build_slice(img2, index=index, nz=i, fixed=[128, 512, 1024])) return header
def preload_images(micrographs, img_buffer): ''' Preloads IMG_BUFFER_SIZE number of images into the memory. :param micrographs: list of all micrographs :param img_buffer: deque as buffer for images :return: None ''' while True: index = GUI.fileList.row(GUI.fileList.currentItem()) max_index = index + IMG_BUFFER_SIZE + 1 if max_index > len(micrographs): max_index = len(micrographs) should_be_inside = set(range(index + 1, max_index)) is_inside = set([tuble[1] for tuble in img_buffer]) load = should_be_inside - is_inside if len(img_buffer) == 0: load.add(index) for index_to_load in load: filename = str(micrographs[index_to_load]) image = EMAN2.EMData(filename) fft_img = image.do_fft() fft_img.set_value_at(0, 0, 0, 0) fft_img.set_value_at(1, 0, 0, 0) fft_img.process_inplace("xform.phaseorigin.tocorner") fft_img.process_inplace("xform.fourierorigin.tocenter") fft_img = fft_img.get_fft_amplitude() img_buffer.append((filename, index_to_load, image, fft_img)) print("Put new image:", filename) time.sleep(1) '''
def create_ctf_list(current_ctf, def_search_range, def_step_size): """ Creates a set of CTFs with different defoci based on a defocus search range arround the defocus of the current ctf estimate. :param current_ctf: Current ctf :param def_search_range: Defocus search range :param def_step_size: Defocus step size :return: Set of CTFs """ current_defocus = current_ctf.defocus ctfs = [] lower_defocus = current_defocus - def_search_range if lower_defocus < 0: lower_defocus = 0 upper_defocus = current_defocus + def_search_range + def_step_size for defocus in numpy.arange(lower_defocus, upper_defocus, def_step_size): ctf_parameter = EMAN2.EMAN2Ctf() removed_dict = dict([(key, value) for key , value in current_ctf.to_dict().items() if key not in ('background', 'snr')]) ctf_parameter.from_dict(removed_dict) # ctf_parameter.from_dict(current_ctf.to_dict()) ctf_parameter.defocus = defocus ctfs.append(ctf_parameter) return ctfs
def create_particle_stack(particle_stack, output_dir, particle_data): """ Create a mrcs particle stack based on the bdb/hdf input stack. Arguments: particle_stack - Particle stack name output_dir - Output directory particle_data - Particle_data array Returns: None """ print('|_Get particle ID and particle names') ptcl_ids = [ int(entry.split('@')[0]) for entry in particle_data['_rlnImageName'] ] ptcl_names = [ entry.split('@')[1] for entry in particle_data['_rlnImageName'] ] print('|_Write images') for particle_idx in range(particle_data.shape[0]): if particle_idx % 10000 == 0: print(particle_idx, ' of ', particle_data.shape[0]) emdata = EMAN2.EMData(particle_stack, particle_idx) output_name = os.path.join(output_dir, ptcl_names[particle_idx]) try: os.makedirs(os.path.dirname(output_name)) except OSError: pass emdata.write_image(output_name, -1)
def main(): GUIUSE = True try: if EMAN2.get_platform() == "Linux" and os.getenv("DISPLAY") == None: raise Exception # import IPython.lib.inputhook app = eman2_gui.emapplication.EMApp() # IPython.lib.inputhook.enable_qt4(app) def ipy_on_timer(): eman2_gui.emimage.image_update() ipytimer = PyQt5.QtCore.QTimer() ipytimer.timeout.connect(ipy_on_timer) ipytimer.start(200) EMAN2.GUIMode = True EMAN2.app = app except: GUIUSE = False if GUIUSE: print("Welcome to the interactive SPARX-GUI Python interface, provided by ipython") else: print( "Welcome to the interactive SPARX-NoGUI Python interface, provided by ipython" ) print(" ", sp_global_def.SPARXVERSION)
def convert(self, name, format, normalize=False): import EMAN2 if format not in [ 'tif', 'tiff', 'tif8', 'mrc', 'hdf', 'jpg', 'jpeg', 'png' ]: raise ValueError, "Invalid format: %s" % format depth = None if format in ['tif8']: depth = 8 format = 'tif' bdo = self.db.binary.get(name) img = EMAN2.EMData() img.read_image(str(bdo.filepath)) if normalize: img.process_inplace("normalize") outfile = tempfile.NamedTemporaryFile(delete=False, suffix='.%s' % format) if depth == 8: img['render_min'] = -1 img['render_max'] = 256 img.write_image(str(outfile.name), -1, EMAN2.EMUtil.ImageType.IMAGE_UNKNOWN, False, None, EMAN2.EMUtil.EMDataType.EM_UCHAR, False) else: img.write_image(str(outfile.name)) filename = os.path.splitext(bdo.filename)[0] filename = '%s.%s' % (filename, format) return filename, outfile.name
def create_ctf_list(current_ctf, def_search_range, def_step_size): """ Creates a set of CTFs with different defoci based on a defocus search range arround the defocus of the current ctf estimate. :param current_ctf: Current ctf :param def_search_range: Defocus search range :param def_step_size: Defocus step size :return: Set of CTFs """ current_defocus = current_ctf.defocus ctfs = [] lower_defocus = current_defocus - def_search_range if lower_defocus < 0: lower_defocus = 0 upper_defocus = current_defocus + def_search_range + def_step_size for defocus in np.arange(lower_defocus, upper_defocus, def_step_size): ctf_parameter = EMAN2.EMAN2Ctf() ctf_parameter.from_dict(current_ctf.to_dict()) ctf_parameter.defocus = defocus ctfs.append(ctf_parameter) return ctfs
def numpy2em(im, e=None): '''Convert NumPy array to an EMAN2 image object This convenience method converts a numpy.ndarray object into an EMAN2.EMData .. sourcecode:: py >>> from core.image.eman2_utility import * >>> ar = numpy.zeros((2,2)) >>> numpy2em(ar) <libpyEMData2.EMData object at 0xdd61b0> :Parameters: img : numpy.ndarray A numpy array :Returns: return_val : EMAN2.EMData An EMAN2 image object ''' if EMAN2 is None: raise ImportError, "EMAN2/Sparx library not available, numpy2em requires EMAN2/Sparx" try: im = numpy.require(im, numpy.float32) if e is None: e = EMAN2.EMData() EMAN2.EMNumPy.numpy2em(im, e) return e except: return EMAN2.EMNumPy.numpy2em(im)
def createComponents(self): self.buttonLoad = QPushButton('Load') self.buttonSave = QPushButton('Save') self.buttonKeep = QPushButton('Keep (Right arrow key)') self.buttonKeep.setStyleSheet('QPushButton {color: green;}') self.buttonDiscard = QPushButton('Discard (Left arrow key)') self.buttonDiscard.setStyleSheet('QPushButton {color: red;}') self.fileList = QListWidget() self.micrograph = emimage2d.EMImage2DWidget(image=EMAN2.EMData( 700, 700), application=app, parent=self) self.powerSpectrum = emimage2d.EMImage2DWidget(image=EMAN2.EMData( 700, 700), application=app, parent=self) self.all_items = []
def ccfs(ref,stack): "compute and center CCFs between ref and each member of stack" ret=[i.calc_ccf(ref) for i in stack] f=old_div(ref["nx"],4) for i in ret: i.process_inplace("xform.phaseorigin.tocenter") ret=[i.get_clip(EMAN2.Region(f,f,f*2,f*2)) for i in ret] return ret
def loadMicrographsFromItemList(self, image_index): """creates an EMImage-Widget""" if self.current_image is None: image_path = itemName[image_index] self.current_image = EMAN2.EMData(image_path) self.micrograph.set_data( self.current_image ) # in project descriptopn as 'aaa' instead of 'micrograph'
def emdata_header(self): """Get header information from EMAN2.""" if not EMAN2: return # EMAN2 only works with str filenames; no unicode. img = EMAN2.EMData() img.read_image(str(self.filename), 0, True) header = img.get_attr_dict() return header
def orients2mats_all_units(self, orients): ''' returns rotation matrices corresponding to orientations in each symmetric unit mats: nsym,nrots,3,3 ''' nrots = len(orients) nsym = EMAN2.parsesym(self._symtype).get_nsym() mats = np.zeros([nsym, nrots, 3, 3], dtype='float32') for n in range(nsym): for k in range(nrots): m = orients[k].get_sym(self._symtype, n).get_matrix() mats[n, k] = np.reshape(m, [3, 4])[..., :3] return mats
def read_particle(path, index, header_only=False): """ Reads single particle from stack. :param path: Stack from path :param index: Index of the particle in the stack :param header_only: If true, only the header information will be read :return: 2D particle image """ particle_image = EMAN2.EMData() particle_image.read_image(path, index, header_only) return particle_image
def readstack(n): "read the data for the nth particle. Return ref,ptclframes,psref" ref=EMAN2.EMData("tmp2.hdf",n*33) ptcls=EMAN2.EMData.read_images("tmp2.hdf",list(range(n*33+1,(n+1)*33))) ctfim=ref.do_fft() ctf=ref["ctf"] ctf.compute_2d_complex(ctfim,EMAN2.Ctf.CtfType.CTF_POWEVAL) ctfim.ri2ap() ctfim=ctfim.amplitude() # ctfim.process_inplace("normalize") return ref,ptcls,ctfim
def main(): usage = """ Usage... """ parser = EMAN2.EMArgumentParser(usage=usage,version=EMAN2.EMANVERSION) parser.add_argument("--output", type=str,help="Output pdb file") parser.add_argument("--mrcin", type=str,help="mrc file for input",default=None) parser.add_argument("--pdbin", type=str,help="pdb file for input",default=None) parser.add_argument("--lenthr", type=int,help="length threshold of helixes",default=13) parser.add_argument("--denthr", type=float,help="density threshold of helixes",default=4) parser.add_argument("--mapwohelix", type=str,help="Write a map without helix density",default=None) parser.add_argument("--dirs", type=int,help="Counting from one direction?",default=0) parser.add_argument("--edgefile", type=str,help="Existing helixes file",default=None) (options, args) = parser.parse_args() eg=[] if options.edgefile<>None: edge=read_fixed(options.edgefile) eg.append(edge[0][0]) for i in range(1,len(edge)): if edge[i][0]<>edge[i-1][1]: eg.append(edge[i-1][1]) eg.append(edge[i][0]) eg.append(edge[len(edge)-1][1]) atomnumber=read_pdb(options.pdbin) print eg for i in range(len(eg)): for j in range(len(atomnumber)): if atomnumber[j]==eg[i]: eg[i]=j break print eg #exit() mrc=EMData(options.mrcin) atoms=PointArray() atoms.read_from_pdb(options.pdbin) #atoms.reverse_chain() #mrc.process_inplace("filter.lowpass.gauss",{"cutoff_abs":0.5}) hlx=atoms.fit_helix(mrc,options.lenthr,options.denthr,eg,options.dirs) for i in range(len(hlx)/8): print hlx[i*8],hlx[i*8+1] atoms.save_pdb_with_helix(options.output,hlx) #atoms.save_to_pdb(options.output) if options.mapwohelix<>None: atoms.remove_helix_from_map(mrc,hlx) mrc.write_image(options.mapwohelix)
def check_output_format(input_var, filename): out_data_3d = EMAN2.EMData(10, 10, 10) out_data_3d += 1 try: out_data_3d.write_image("test_" + filename) except Exception as e: msg = "The parameter '" + input_var + "' has a not valid extension or the file is not writable. Actual namefile is: '" + filename sp_global_def.ERROR(msg, action=1) return False # i cannot just remove it because in case of multiple cpu use it could crash try: os.remove("test_" + filename) except Exception as useless_e: pass return True
def preload_images(self, micrograph_list, img_buffer): """ Preloads IMG_BUFFER_SIZE number of images into the memory. :param micrograph_list: list of all micrographs ######## mistake --> here it gets just a string not a list :param img_buffer: deque as buffer for images :return: None """ print("preload_images micrograph_list", micrograph_list) offset = 0 last_index = -1 while True: index = self.fileList.row(self.fileList.currentItem()) if last_index != -1: if index - last_index == 1: offset = offset - 1 elif index - last_index != 0: offset = 0 if len(img_buffer) < IMG_BUFFER_SIZE and ( index + offset) < len(micrograph_list): start = time.time() print("in", index + offset) print("micrograph_list", micrograph_list) filename = str(micrograph_list[index + offset]) print("filename", filename) image = EMAN2.EMData(filename) fft_img = image.do_fft() fft_img.set_value_at(0, 0, 0, 0) fft_img.set_value_at(1, 0, 0, 0) fft_img.process_inplace("xform.phaseorigin.tocorner") fft_img.process_inplace("xform.fourierorigin.tocenter") fft_img = fft_img.get_fft_amplitude() img_buffer.append((filename, index + offset, image, fft_img)) end = time.time() print("Put new image:", filename, "Current index", index, "Image+offset:", index + offset, "offset", offset, "time", end - start) offset = offset + 1 else: time.sleep(1) last_index = index
def create_raw_hdf(self, outfile): # This is a very expensive process and may take a while. # This runs in the main thread :( Blocks the UI. Must fix. files = self.find('RawImage') if not files: return self.log("Creating raw HDF with %s frames: %s" % (len(files), outfile)) outfile = str(outfile) # Sort the raw frames files = sorted(files, key=lambda x: int(x.split("_")[-1].split(".")[0])) for f in files: f = str(f) self.log("Adding %s to raw HDF" % (os.path.basename(f))) e = EMAN2.EMData() e.read_image(f) e['render_min'] = -1 e['render_max'] = 256 * 256 e.write_image(outfile, -1, EMAN2.EMUtil.ImageType.IMAGE_HDF, False, None, EMAN2.EMUtil.EMDataType.EM_USHORT, False)
def check_options(options,args): ''' A way to check the options, arg as returned by parser.parse_args() in e2tomoaverage ''' error = [] error.extend(check_tomo_options(options)) # there is a big bunch of generic options if len(args) < 2: error.append("Error - to average you must supply atleast two images") else: for arg in args: if not file_exists(arg): error.append("%s does not exist" %arg) if options.path != None: if not os.path.exists(options.path): error.append( "Error: the path %s does not exist" %options.path) else: options.path = EMAN2.numbered_path(tomo_ave_path_root,True) return error
def build_tiles(self, img, nz=1, index=0, tilesize=256): '''Build tiles for a 2D slice.''' # Work with a copy of the EMData img2 = img.copy() # Calculate the number of zoom levels based on the tile size levels = math.ceil( math.log(max(img.get_xsize(), img.get_ysize()) / tilesize) / math.log(2.0)) # Tile header header = img.get_attr_dict() tile_dict = {} # Step through shrink range creating tiles for level in range(int(levels)): scale = 2**level rmin = img2.get_attr("mean") - img2.get_attr("sigma") * 3.0 rmax = img2.get_attr("mean") + img2.get_attr("sigma") * 3.0 for x in range(0, img2.get_xsize(), tilesize): for y in range(0, img2.get_ysize(), tilesize): i = img2.get_clip(EMAN2.Region(x, y, tilesize, tilesize), fill=rmax) i.set_attr("render_min", rmin) i.set_attr("render_max", rmax) i.set_attr("jpeg_quality", 80) # Write output fsp = "tile.index-%d.scale-%d.z-%d.x-%d.y-%d.jpg" % ( index, scale, nz, x / tilesize, y / tilesize) fsp = os.path.join(self.tmpdir, fsp) i.write_image(fsp) tile_dict[(scale, x / tilesize, y / tilesize)] = [ fsp, None, 'jpg', tilesize, tilesize ] # Shrink by 2 for next round. img2.process_inplace("math.meanshrink", {"n": 2}) return tile_dict
def execute(self,alignment_jobs,files,caller): ''' The main function @param alignment_jobs a list of alignment pair indices like this [[0,1],[2,1],[2,3],[0,5],...] etc the indices pair represent images to be aligned and correspond to the order of the files argument @param files a list of filenames - used to read image based on the indices present in alignment_jobs @param caller - the calling object - it needs to have a function called process_output that takes a dictionary as the argument ''' options = self.options align_data = EMAN2.parsemodopt(options.align) align_cmp_data = EMAN2.parsemodopt(options.aligncmp) cmp_data = EMAN2.parsemodopt(options.cmp) ralign_data = None if options.ralign != None: ralign_data = EMAN2.parsemodopt(options.ralign) ralign_cmp_data = EMAN2.parsemodopt(options.raligncmp) data = {} data["align"] = align_data data["aligncmp"] = align_cmp_data data["cmp"] = cmp_data if ralign_data: data["ralign"] = ralign_data data["raligncmp"] = ralign_cmp_data data["using_cuda"] = self.using_cuda data["nsoln"] = self.nsoln if self.options.parallel : task_customers = [] tids = [] if options.shrink: scratch_name_1 = numbered_bdb("bdb:tomo_scratch#scratch_shrink") scratch_name_2 = numbered_bdb("bdb:tomo_scratch##scratch_shrink") else: print "no shrink" for i,j in alignment_jobs: if options.shrink or options.filter: a = EMData(files[i],0) if options.filter: filter_params = EMAN2.parsemodopt(options.filter) a.process_inplace(filter_params[0],filter_params[1]) if options.shrink: a.process_inplace("math.meanshrink",{"n":options.shrink}) a.set_attr("src_image",files[i]) a.write_image(scratch_name_1,0) a = EMData(files[j],0) if options.filter: filter_params = EMAN2.parsemodopt(options.filter) a.process_inplace(filter_params[0],filter_params[1]) if options.shrink: a.process_inplace("math.meanshrink",{"n":options.shrink}) a.set_attr("src_image",files[j]) a.write_image(scratch_name_2,0) data["probe"] = ("cache",scratch_name_1,0) data["target"] = ("cache",scratch_name_2,0) else: data["probe"] = ("cache",files[i],0) data["target"] = ("cache",files[j],0) data["target_idx"] = j data["probe_idx"] = i task = EMTomoAlignTaskDC(data=data) from EMAN2PAR import EMTaskCustomer etc=EMTaskCustomer(self.options.parallel) #print "Est %d CPUs"%etc.cpu_est() tid=etc.send_task(task) #print "Task submitted tid=",tid task_customers.append(etc) tids.append(tid) self.dc_monitor(task_customers,tids,caller) else: n = len(alignment_jobs) p = 0.0 for i,j in alignment_jobs: probe = EMData(files[i],0) target = EMData(files[j],0) if options.filter: print "filtered" filter_params = EMAN2.parsemodopt(options.filter) probe.process_inplace(filter_params[0],filter_params[1]) target.process_inplace(filter_params[0],filter_params[1]) if options.shrink: probe.process_inplace("math.meanshrink",{"n":options.shrink}) target.process_inplace("math.meanshrink",{"n":options.shrink}) else: print "no shrink" data["target"] = target data["probe"] = probe data["target_idx"] = j data["probe_idx"] = i task = EMTomoAlignTask(data=data) rslts = task.execute(self.progress_callback) if options.shrink: self.correction_translation(rslts,options.shrink) caller.process_output(rslts) p += 1.0