def run(self): t0 = time.time() imgarray = self.openFile() self.imgshape = imgarray.shape self.printImageInfo(imgarray) coordlist = self.setupCoordList() ctfdict = {} self.count = 0 ### process image pieces print "processing %d image pieces" % (len(coordlist)) keys = coordlist.keys() keys.sort() for key in keys: (xstr, ystr) = key.split('x') x = int(xstr) y = int(ystr) self.count += 1 subarray = self.getSubImage(imgarray, x, y) #self.printImageInfo(subarray) imgfile = "splitimage-" + key + ".dwn.mrc" mrc.write(subarray, imgfile) ctfvalues = None reproc = 0 edgeblur = 8 while ctfvalues is None and reproc < 3: ctfvalues = self.processImage(imgfile, edgeblur) reproc += 1 edgeblur += 2 if ctfvalues is not None: sys.stderr.write("#") else: sys.stderr.write("!") ctfdict[key] = ctfvalues apFile.removeFilePattern(imgfile + "*", False) sys.stderr.write("\n") ### calculate ctf parameters self.fitPlaneToCtf(ctfdict) apDisplay.printMsg("Time: %s" % (apDisplay.timeString(time.time() - t0)))
def run(self): t0 = time.time() imgarray = self.openFile() self.imgshape = imgarray.shape self.printImageInfo(imgarray) coordlist = self.setupCoordList() ctfdict = {} self.count = 0 ### process image pieces print "processing %d image pieces"%(len(coordlist)) keys = coordlist.keys() keys.sort() for key in keys: (xstr,ystr) = key.split('x') x = int(xstr) y = int(ystr) self.count += 1 subarray = self.getSubImage(imgarray, x, y) #self.printImageInfo(subarray) imgfile = "splitimage-"+key+".dwn.mrc" mrc.write(subarray, imgfile) ctfvalues = None reproc = 0 edgeblur = 8 while ctfvalues is None and reproc < 3: ctfvalues = self.processImage(imgfile, edgeblur) reproc += 1 edgeblur += 2 if ctfvalues is not None: sys.stderr.write("#") else: sys.stderr.write("!") ctfdict[key] = ctfvalues apFile.removeFilePattern(imgfile+"*", False) sys.stderr.write("\n") ### calculate ctf parameters self.fitPlaneToCtf(ctfdict) apDisplay.printMsg("Time: %s"%(apDisplay.timeString(time.time()-t0)))
def processImage(self, imgfile, edgeblur=8, msg=False): ### make command line acecmd = ("%s -i %s -c %.2f -k %d -a %.3f -e %d,0.001 -b 1" % (self.ace2exe, imgfile, self.params['cs'], self.params['kv'], self.params['apix'], edgeblur)) ### run ace2 aceoutf = open("ace2.stdout", "w") #print acecmd ace2proc = subprocess.Popen(acecmd, shell=True, stdout=aceoutf, stderr=aceoutf) ace2proc.wait() ### check if ace2 worked imagelog = imgfile + ".ctf.txt" if not os.path.isfile(imagelog) and self.count <= 1: ### ace2 always crashes on first image??? .fft_wisdom file?? time.sleep(1) ace2proc = subprocess.Popen(acecmd, shell=True, stdout=aceoutf, stderr=aceoutf) ace2proc.wait() aceoutf.close() ### die if not os.path.isfile(imagelog): apDisplay.printError("ace2 did not run") ### parse log file ctfvalues = {} logf = open(imagelog, "r") for line in logf: sline = line.strip() if re.search("^Final Defocus:", sline): parts = sline.split() ctfvalues['defocus1'] = float(parts[2]) ctfvalues['defocus2'] = float(parts[3]) ### convert to degrees ctfvalues['angle_astigmatism'] = math.degrees(float(parts[4])) elif re.search("^Amplitude Contrast:", sline): parts = sline.split() ctfvalues['amplitude_contrast'] = float(parts[2]) elif re.search("^Confidence:", sline): parts = sline.split() ctfvalues['confidence'] = float(parts[1]) ctfvalues['confidence_d'] = float(parts[1]) logf.close() ### summary stats avgdf = (ctfvalues['defocus1'] + ctfvalues['defocus2']) / 2.0 ampconst = 100.0 * ctfvalues['amplitude_contrast'] pererror = 100.0 * (ctfvalues['defocus1'] - ctfvalues['defocus2']) / avgdf if msg is True: apDisplay.printMsg("============") apDisplay.printMsg("Defocus: %.3f x %.3f um (%.2f percent error)" % (ctfvalues['defocus1'] * 1.0e6, ctfvalues['defocus2'] * 1.0e6, pererror)) apDisplay.printMsg("Angle astigmatism: %.2f degrees" % (ctfvalues['angle_astigmatism'])) apDisplay.printMsg("Amplitude contrast: %.2f percent" % (ampconst)) apDisplay.printMsg("Final confidence: %.3f" % (ctfvalues['confidence'])) ### double check that the values are reasonable if avgdf < -1.0e-3 or avgdf > -1.0e-9: apDisplay.printWarning( "bad defocus estimate, not committing values to database") return None if ampconst < -0.01 or ampconst > 80.0: apDisplay.printWarning( "bad amplitude contrast, not committing values to database") return None if ctfvalues['confidence'] < 0.6: sys.stderr.write("c") #apDisplay.printWarning("bad confidence") return None return ctfvalues
def processImage(self, imgfile, edgeblur=8, msg=False): ### make command line acecmd = ("%s -i %s -c %.2f -k %d -a %.3f -e %d,0.001 -b 1" %(self.ace2exe, imgfile, self.params['cs'], self.params['kv'], self.params['apix'], edgeblur)) ### run ace2 aceoutf = open("ace2.stdout", "w") #print acecmd ace2proc = subprocess.Popen(acecmd, shell=True, stdout=aceoutf, stderr=aceoutf) ace2proc.wait() ### check if ace2 worked imagelog = imgfile+".ctf.txt" if not os.path.isfile(imagelog) and self.count <= 1: ### ace2 always crashes on first image??? .fft_wisdom file?? time.sleep(1) ace2proc = subprocess.Popen(acecmd, shell=True, stdout=aceoutf, stderr=aceoutf) ace2proc.wait() aceoutf.close() ### die if not os.path.isfile(imagelog): apDisplay.printError("ace2 did not run") ### parse log file ctfvalues = {} logf = open(imagelog, "r") for line in logf: sline = line.strip() if re.search("^Final Defocus:", sline): parts = sline.split() ctfvalues['defocus1'] = float(parts[2]) ctfvalues['defocus2'] = float(parts[3]) ### convert to degrees ctfvalues['angle_astigmatism'] = math.degrees(float(parts[4])) elif re.search("^Amplitude Contrast:",sline): parts = sline.split() ctfvalues['amplitude_contrast'] = float(parts[2]) elif re.search("^Confidence:",sline): parts = sline.split() ctfvalues['confidence'] = float(parts[1]) ctfvalues['confidence_d'] = float(parts[1]) logf.close() ### summary stats avgdf = (ctfvalues['defocus1']+ctfvalues['defocus2'])/2.0 ampconst = 100.0*ctfvalues['amplitude_contrast'] pererror = 100.0 * (ctfvalues['defocus1']-ctfvalues['defocus2']) / avgdf if msg is True: apDisplay.printMsg("============") apDisplay.printMsg("Defocus: %.3f x %.3f um (%.2f percent error)"% (ctfvalues['defocus1']*1.0e6, ctfvalues['defocus2']*1.0e6, pererror )) apDisplay.printMsg("Angle astigmatism: %.2f degrees"%(ctfvalues['angle_astigmatism'])) apDisplay.printMsg("Amplitude contrast: %.2f percent"%(ampconst)) apDisplay.printMsg("Final confidence: %.3f"%(ctfvalues['confidence'])) ### double check that the values are reasonable if avgdf < -1.0e-3 or avgdf > -1.0e-9: apDisplay.printWarning("bad defocus estimate, not committing values to database") return None if ampconst < -0.01 or ampconst > 80.0: apDisplay.printWarning("bad amplitude contrast, not committing values to database") return None if ctfvalues['confidence'] < 0.6: sys.stderr.write("c") #apDisplay.printWarning("bad confidence") return None return ctfvalues