def main(): usage="[prog] <2D image file> " parser = EMArgumentParser(usage=usage,version=EMANVERSION) (options, args) = parser.parse_args() logid=E2init(sys.argv) filename=args[0] app = EMApp() img=EMData(filename) #print img[0]["mean"] w=EMBreakBrick(img,app) #w.set_data(img,filename) app.show_specific(w) app.exec_() E2end(logid)
def main(): progname = os.path.basename(sys.argv[0]) usage = """prog Presents a graphical representation of the orientation distribution of the particles in a single particle reconstruction. This is displayed as a single asymmetric unit on a sphere, with cylinders of varying height representing the number of particles found in each orientation. Middle-click will produce a control-panel as usual, and clicking on a single peak will permit viewing the class-average and related projection (and particles). Normally launched without arguments from a e2workflow project directory. """ parser = EMArgumentParser(usage=usage, version=EMANVERSION) parser.add_header(name="e2eulerxplorheader", help="Click Launch to Run e2eulerxplor.py", title="### Click Launch to Run e2eulerxplor.py ###", row=0, col=0, rowspan=1, colspan=3) parser.add_argument( "--eulerdata", "-e", type=str, help= "File for Eulerdata, Ryan style, if none is given, data is read from the DB.", default=None) parser.add_argument( "--ppid", type=int, help="Set the PID of the parent process, used for cross platform PPID", default=-1) parser.add_argument( "--verbose", "-v", dest="verbose", action="store", metavar="n", type=int, default=0, help= "verbose level [0-9], higner number means higher level of verboseness") global options (options, args) = parser.parse_args() logid = E2init(sys.argv, options.ppid) em_app = EMApp() window = EMEulerWidget(file_name=options.eulerdata, read_from=args) em_app.show_specific(window) em_app.execute() E2end(logid)
def main(): usage = "Generate training set for tomogram segmentation. Please run this program from the GUI in e2projectmanager.py." #print usage parser = EMArgumentParser(usage=usage, version=EMANVERSION) #parser.add_header(name="tmpheader", help='temp label', title="### This program is NOT avaliable yet... ###", row=0, col=0, rowspan=1, colspan=2, mode="box,seg,set") #### boxing #### parser.add_argument("--boxing", action="store_true", help="Boxing particles.", default=False, guitype='boolbox', row=4, col=0, rowspan=1, colspan=1, mode='box[True]') parser.add_pos_argument( name="micrographs", help="List the file to process with e2boxer here.", default="", guitype='filebox', browser="EMRawDataTable(withmodal=True,startpath=\"rawtomograms\")", row=1, col=0, rowspan=1, colspan=3, mode="box") parser.add_argument("--boxsize", "-B", type=int, help="Box size in pixels", default=-1, guitype='intbox', row=3, col=0, rowspan=1, colspan=3, mode="box") #### segment #### #parser.add_header(name="instruction", help='instruction', title="### Mark the target features white ###", row=0, col=0, rowspan=1, colspan=2, mode="seg") parser.add_argument("--segment", action="store_true", help="Segment particles.", default=False, guitype='boolbox', row=4, col=0, rowspan=1, colspan=1, mode='seg[True]') parser.add_pos_argument(name="particles", help="Particle file.", default="", guitype='filebox', browser="EMParticlesTable(withmodal=True)", row=1, col=0, rowspan=1, colspan=3, mode="seg") parser.add_argument( "--output", type=str, help= "output file name. Default is the input particle file name plus _seg.hdf", default=None, guitype='strbox', row=3, col=0, rowspan=1, colspan=3, mode="seg") #### build set #### parser.add_argument("--buildset", action="store_true", help="Segment particles.", default=False, guitype='boolbox', row=7, col=0, rowspan=1, colspan=1, mode='set[True]') parser.add_argument("--particles_raw", type=str, help="Input raw particle file", default=None, guitype='filebox', browser="EMParticlesTable(withmodal=True)", row=1, col=0, rowspan=1, colspan=3, mode="set") parser.add_argument("--particles_label", type=str, help="Input labels for particle file", default=None, guitype='filebox', browser="EMParticlesTable(withmodal=True)", row=2, col=0, rowspan=1, colspan=3, mode="set") parser.add_argument("--boxes_negative", type=str, help="Input boxes of negative samples", default=None, guitype='filebox', browser="EMParticlesTable(withmodal=True)", row=3, col=0, rowspan=1, colspan=3, mode="set") parser.add_argument( "--ncopy", type=int, help= "Number of copies for NEGATIVE samples. (number of copies of particles is calculated accordingly) ", default=10, guitype='intbox', row=5, col=0, rowspan=1, colspan=1, mode="set") parser.add_argument( "--trainset_output", type=str, help= "output file name of the training set.Default is the input particle file name plus _trainset.hdf", default=None, guitype='strbox', row=4, col=0, rowspan=1, colspan=3, mode="set") parser.add_argument("--zthick", type=int, help="Thickness in z ", default=0, guitype='intbox', row=5, col=1, rowspan=1, colspan=1, mode="set") parser.add_argument( "--validset", type=float, help="Propotion of particles in validation set. Default is 0.2 ", default=0.0, guitype='floatbox', row=7, col=1, rowspan=1, colspan=1, mode="set") ################## parser.add_argument( "--ppid", type=int, help="Set the PID of the parent process, used for cross platform PPID", default=-1) (options, args) = parser.parse_args() logid = E2init(sys.argv) #### boxing ### if options.boxing: db = js_open_dict(EMBOXERBASE_DB) cache_box_size = True if options.boxsize == -1: cache_box_size = False options.boxsize = db.setdefault("box_size", 64) application = EMApp() module = EMBoxerModule(args, options.boxsize) module.show_interfaces() application.execute(logid) #### segment ### if options.segment: filename = args[0] if options.output == None: options.output = filename[:-4] + "_seg.hdf" app = EMApp() img = EMData.read_images(filename) #print img[0]["mean"] w = EMImageWidget(data=img, old=None, app=app, force_2d=True) #w = EMWidgetFromFile(filename,application=app,force_2d=True) w.setWindowTitle(base_name(filename)) w.show_inspector(1) ins = w.get_inspector() ins.mmtab.setCurrentIndex(5) ins.dtpenv.setText('100') w.set_mouse_mode(5) app.show_specific(w) app.exec_() try: os.remove(options.output) except: pass for e in img: e.process_inplace("threshold.belowtozero", {"minval": 99}) e.process_inplace("threshold.binary", {"value": 1}) e.write_image(options.output, -1) #### build set ### if options.buildset: tomo_in = options.particles_raw seg_in = options.particles_label neg_in = options.boxes_negative if tomo_in and neg_in and seg_in: n_ptcl = EMUtil.get_image_count(tomo_in) n_neg = EMUtil.get_image_count(neg_in) if options.trainset_output == None: options.trainset_output = tomo_in[:-4] + "_trainset.hdf" p_copy = options.ncopy * n_neg / n_ptcl else: p_copy = options.ncopy try: os.remove(options.trainset_output) except: pass print( "making {} copies for particles, and {} copies for negative samples" .format(p_copy, options.ncopy)) imgs = [] if tomo_in and seg_in: n_ptcl = EMUtil.get_image_count(tomo_in) for i in range(n_ptcl): #t=EMData(tomo_in,i) t = get_box(tomo_in, i, options.zthick) if t == None: continue s = EMData(seg_in, i) for c in range(p_copy): tr = Transform() rd = random.random() * 360 tr.set_rotation({"type": "2d", "alpha": rd}) e = t.process("xform", {"transform": tr}) #e.process_inplace("normalize") imgs.append(e) #e.write_image(options.trainset_output,-1) e = s.process("xform", {"transform": tr}) #e.write_image(options.trainset_output,-1) imgs.append(e) ngood = len(imgs) if neg_in: s = EMData(neg_in, 0) s.to_zero() n_neg = EMUtil.get_image_count(neg_in) for i in range(n_neg): t = get_box(neg_in, i, options.zthick) if t == None: continue for c in range(options.ncopy): tr = Transform() rd = random.random() * 360 tr.set_rotation({"type": "2d", "alpha": rd}) e = t.process("xform", {"transform": tr}) #e.process_inplace("normalize") #e.write_image(options.trainset_output,-1) imgs.append(e) e = s.process("xform", {"transform": tr}) #e.write_image(options.trainset_output,-1) imgs.append(e) print("Shuffling particles...") ### randomize n = len(imgs) #n=EMUtil.get_image_count(options.trainset_output) idx = range(int((ngood / 2) * (1 - options.validset))) + range( (ngood / 2), int(n / 2 * (1 - options.validset))) random.shuffle(idx) for i in idx: imgs[i * 2]["valid_set"] = 0 imgs[i * 2].write_image(options.trainset_output, -1) imgs[i * 2 + 1]["valid_set"] = 0 imgs[i * 2 + 1].write_image(options.trainset_output, -1) idx = range(int( (ngood / 2) * (1 - options.validset)), ngood / 2) + range( int(n / 2 * (1 - options.validset)), n / 2) random.shuffle(idx) for i in idx: imgs[i * 2]["valid_set"] = 1 imgs[i * 2].write_image(options.trainset_output, -1) imgs[i * 2 + 1]["valid_set"] = 1 imgs[i * 2 + 1].write_image(options.trainset_output, -1) #e=EMData(options.trainset_output,i*2) #e.process_inplace("normalize") #e.write_image(tmpfile,-1) #e=EMData(options.trainset_output,i*2+1) #e.write_image(tmpfile,-1) #shutil.move(tmpfile,options.trainset_output) print("Generate a training set of {:d} samples.".format(n / 2)) print("Done") E2end(logid)
def main(): progname = os.path.basename(sys.argv[0]) usage = """e2findlines sets/img.lst ** EXPERIMENTAL ** this program looks for ~ straight line segments in images, such as wrinkles in graphene oxide films or possible C-film edges """ parser = EMArgumentParser(usage=usage, version=EMANVERSION) parser.add_argument("--threshold", type=float, help="Threshold for separating particles, default=3", default=3.0) parser.add_argument("--newsets", default=False, action="store_true", help="Split lines/nolines into 2 new sets") #parser.add_argument("--output",type=str,help="Output filename (text file)", default="ptclplot.txt") parser.add_argument("--gui", default=False, action="store_true", help="show histogram of values") parser.add_argument( "--threads", default=4, type=int, help="Number of threads to run in parallel on the local computer") parser.add_argument( "--verbose", "-v", dest="verbose", action="store", metavar="n", type=int, default=0, help= "verbose level [0-9], higher number means higher level of verboseness") parser.add_argument( "--ppid", type=int, help="Set the PID of the parent process, used for cross platform PPID", default=-1) parser.add_argument( "--invar", default=False, action="store_true", help= "create the invar file for the newsets. The newsets option must be used." ) parser.add_argument("--zscore", default=True, action="store_true", help="run Z-score-based line finding.") parser.add_argument("--rdnxform", default=False, action="store_true", help="detect lines via radon transform") parser.add_argument( "--rthreshold", default=25, help="see scikit-image.transform.radon() parameter documentation.") parser.add_argument( "--rsigma", default=3, help="see scikit-image.transform.radon() parameter documentation.") (options, args) = parser.parse_args() if (len(args) < 1): parser.error("Please specify an input stack/set to operate on") E2n = E2init(sys.argv, options.ppid) options.threads += 1 # one extra thread for storing results if options.rdnxform: options.zscore = False print("running e2findlines.py with Radon transform method.") n = EMUtil.get_image_count(args[0]) lines = [] if options.threads - 1 == 0: t1 = time.time() for i in range(n): im = EMData(args[0], i) radon_im = radon(im.numpy(), preserve_range=True) laplacian = blob_log(radon_im, threshold=options.rthreshold, min_sigma=options.rsigma) if len(laplacian) == 0: lines.append(0) else: lines.append(1) print(f"{i} out of {n} images analyzed" + ' ' * 20, end='\b' * (len(str(f"{i} out of {n} images analyzed")) + 20), flush=True) t2 = time.time() print(f"Total time for rdnxform (nonthreaded): {t2-t1}s") if options.threads - 1 > 0 and options.threads <= n: t1 = time.time() print("running threaded rdnxform") threaded_indices = [[] for x in range(options.threads - 1)] for i in range(n): threaded_indices[i % (options.threads - 1)].append(i) #completed = 0 class ImageBatch(threading.Thread): def __init__(self, threadId, name, data_indices): threading.Thread.__init__(self) self.threadId = threadId self.name = name self.data_indices = data_indices def run(self): for i in self.data_indices: im = EMData(args[0], i) radon_im = radon(im.numpy(), preserve_range=True) laplacian = blob_log(radon_im, threshold=options.rthreshold, min_sigma=options.rsigma) if len(laplacian) == 0: lines.append(0) else: lines.append(1) print( f"{i} out of {n} images analyzed" + ' ' * 20, end='\b' * (len(str(f"{i} out of {n} images analyzed")) + 20), flush=True) threads = [ ImageBatch(x, "thread_%s" % x, threaded_indices[x]) for x in range(options.threads - 1) ] for thread in threads: thread.start() for thread in threads: thread.join() t2 = time.time() print(f"Total time rdnxform (threaded): {t2-t1}s") """for i in range(n): im=EMData(args[0],i) radon_im=radon(im.numpy(), preserve_range=True) laplacian=blob_log(radon_im, threshold=options.rthreshold, min_sigma=options.rsigma) if len(laplacian)==0: lines.append(0) else: lines.append(1) print(f"{i} out of {n} images analyzed"+' '*20, end ='\b'*(len(str(f"{i} out of {n} images analyzed"))+20), flush=True)""" if options.zscore: print("running e2findlines.py with Z-score method.") im0 = EMData(args[0], 0) # first image r2 = im0["ny"] / 4 # outer radius # we build up a list of 'Z scores' which should be larger for images containing one or more parallel lines. # if 2 lines aren't parallel the number may be lower, even if the lines are strong, but should still be higher # than images without lines in most cases n = EMUtil.get_image_count(args[0]) step = max(n // 500, 1) Z = [] im2d = [] for i in range(n): im = EMData(args[0], i) a = im.do_fft().calc_az_dist(60, -88.5, 3, 4, r2) d = np.array(a) Z.append((d.max() - d.mean()) / d.std()) if i % step == 0: im["zscore"] = (d.max() - d.mean()) / d.std() im2d.append(im) if options.gui: # GUI display of a histogram of the Z scores from eman2_gui.emhist import EMHistogramWidget from eman2_gui.emimagemx import EMImageMXWidget from eman2_gui.emapplication import EMApp app = EMApp() histw = EMHistogramWidget(application=app) histw.set_data(Z) app.show_specific(histw) imd = EMImageMXWidget(application=app) im2d.sort(key=lambda x: x["zscore"]) imd.set_data(im2d) app.show_specific(imd) app.exec_() """ if options.newsets: lstin=LSXFile(args[0]) # output containing images with lines linesfsp=args[0].rsplit(".",1)[0]+"_lines.lst" try: os.unlink(linesfsp) except: pass lstlines=LSXFile(linesfsp) # output containin images without lines nolinesfsp=args[0].rsplit(".",1)[0]+"_nolines.lst" try: os.unlink(nolinesfsp) except: pass lstnolines=LSXFile(nolinesfsp) for i,z in enumerate(Z): if z>options.threshold: lstlines[-1]=lstin[i] else: lstnolines[-1]=lstin[i] """ if options.newsets and not options.invar: lstin = LSXFile(args[0]) # output containing images with lines linesfsp = args[0].split("__", 1)[0] + "_lines__" + args[0].split( "__", 1)[1] try: os.unlink(linesfsp) except: pass lstlines = LSXFile(linesfsp) # output containin images without lines nolinesfsp = args[0].split("__", 1)[0] + "_nolines__" + args[0].split( "__", 1)[1] try: os.unlink(nolinesfsp) except: pass lstnolines = LSXFile(nolinesfsp) if options.zscore: for i, z in enumerate(Z): if z > options.threshold: lstlines[-1] = lstin[i] else: lstnolines[-1] = lstin[i] if options.rdnxform: for i, r in enumerate(lines): if r != 0: lstlines[-1] = lstin[i] else: lstnolines[-1] = lstin[i] if options.newsets and options.invar: lstin = LSXFile(args[0]) # output containing images with lines fnamemod = input("Type the filename modifier: ") linesfsp = args[0].split( "__", 1)[0] + f"_lines_{fnamemod}__" + args[0].split("__", 1)[1] try: os.unlink(linesfsp) except: pass lstlines = LSXFile(linesfsp) # output containing images without lines nolinesfsp = args[0].split( "__", 1)[0] + f"_nolines_{fnamemod}__" + args[0].split("__", 1)[1] try: os.unlink(nolinesfsp) except: pass lstnolines = LSXFile(nolinesfsp) # output copy of nolines folder invarfsp = nolinesfsp.rsplit("_", 1)[0] + "_invar.lst" try: os.unlink(invarfsp) except: pass lstinvar = LSXFile(invarfsp) if options.zscore: for i, z in enumerate(Z): if z > options.threshold: lstlines[-1] = lstin[i] else: lstnolines[-1] = lstin[i] lstinvar[-1] = lstin[i] if options.rdnxform: for i, r in enumerate(lines): if r != 0: lstlines[-1] = lstin[i] else: lstnolines[-1] = lstin[i] lstinvar[-1] = lstin[i] print(f"running: e2proclst.py {invarfsp} --retype ctf_flip_invar") os.system(f"e2proclst.py {invarfsp} --retype ctf_flip_invar") print("invar file created.") E2end(E2n)
def main(): progname = os.path.basename(sys.argv[0]) usage = """e2findlines sets/img.lst ** EXPERIMENTAL ** this program looks for ~ straight line segments in images, such as wrinkles in graphene oxide films or possible C-film edges """ parser = EMArgumentParser(usage=usage, version=EMANVERSION) parser.add_argument("--threshold", type=float, help="Threshold for separating particles, default=3", default=3.0) parser.add_argument("--newsets", default=False, action="store_true", help="Split lines/nolines into 2 new sets") #parser.add_argument("--output",type=str,help="Output filename (text file)", default="ptclplot.txt") parser.add_argument("--gui", default=False, action="store_true", help="show histogram of values") parser.add_argument( "--threads", default=4, type=int, help="Number of threads to run in parallel on the local computer") parser.add_argument( "--verbose", "-v", dest="verbose", action="store", metavar="n", type=int, default=0, help= "verbose level [0-9], higher number means higher level of verboseness") parser.add_argument( "--ppid", type=int, help="Set the PID of the parent process, used for cross platform PPID", default=-1) (options, args) = parser.parse_args() if (len(args) < 1): parser.error("Please specify an input stack/set to operate on") E2n = E2init(sys.argv, options.ppid) options.threads += 1 # one extra thread for storing results im0 = EMData(args[0], 0) # first image r2 = im0["ny"] / 4 # outer radius # we build up a list of 'Z scores' which should be larger for images containing one or more parallel lines. # if 2 lines aren't parallel the number may be lower, even if the lines are strong, but should still be higher # than images without lines in most cases n = EMUtil.get_image_count(args[0]) step = max(n // 500, 1) Z = [] im2d = [] for i in range(n): im = EMData(args[0], i) a = im.do_fft().calc_az_dist(60, -88.5, 3, 4, r2) d = np.array(a) Z.append((d.max() - d.mean()) / d.std()) if i % step == 0: im["zscore"] = (d.max() - d.mean()) / d.std() im2d.append(im) if options.gui: # GUI display of a histogram of the Z scores from eman2_gui.emhist import EMHistogramWidget from eman2_gui.emimagemx import EMImageMXWidget from eman2_gui.emapplication import EMApp app = EMApp() histw = EMHistogramWidget(application=app) histw.set_data(Z) app.show_specific(histw) imd = EMImageMXWidget(application=app) im2d.sort(key=lambda x: x["zscore"]) imd.set_data(im2d) app.show_specific(imd) app.exec_() if options.newsets: lstin = LSXFile(args[0]) # output containing images with lines linesfsp = args[0].rsplit(".", 1)[0] + "_lines.lst" try: os.unlink(linesfsp) except: pass lstlines = LSXFile(linesfsp) # output containin images without lines nolinesfsp = args[0].rsplit(".", 1)[0] + "_nolines.lst" try: os.unlink(nolinesfsp) except: pass lstnolines = LSXFile(nolinesfsp) for i, z in enumerate(Z): if z > options.threshold: lstlines[-1] = lstin[i] else: lstnolines[-1] = lstin[i] E2end(E2n)