def processImage(self, imgdata, filtarray): #Ignore array filter array = imgdata['image'] imgmax = self.params['max'] imgmin = self.params['min'] outfile = os.path.join(self.params['rundir'], imgdata['filename']) if self.params['fft'] is True: outfile += ".fft" outfile += ".jpg" if imgmin > imgmax: imgmax = array.max() imgmin = array.min() shape = array.shape maxdim = max(shape) if self.params['fft'] is True: ## fft's are binned by 2 array = imagefun.bin(array, 2) array = imagefun.power(array) maxdim /= 2 bin = int(math.ceil(float(maxdim / self.params['imgsize']))) array = imagefun.bin(array, bin) jpg.write(array, outfile, min=imgmin, max=imgmax, quality=self.params['quality'], stdval=self.params['stdval'])
def manualCheckLoop(self, presetname=None, emtarget=None): ## copied and simplified from focuser.py ## go to preset and target self.logger.info('Starting manual focus loop, please confirm defocus...') self.beep() camdata1 = {} # configure camera using settings, but only 512x512 to save time origcam = self.settings['camera settings'] camdata1 = self.makeCenterImageCamSettings(origcam) camdata1['exposure time']=self.focexptime self.instrument.ccdcamera.Settings = camdata1 pixelsize,center = self.getReciprocalPixelSizeFromInstrument() self.ht = self.instrument.tem.HighTension self.panel.onNewPixelSize(pixelsize,center,self.ht) self.manualplayer.play() self.onManualCheck() while True: t0 = time.time() state = self.manualplayer.state() if state == 'stop': break elif state == 'pause': if self.manualplayer.wait() == 'stop': break # acquire image, show image and power spectrum # allow user to adjust defocus and stig correction = self.settings['correct image'] camdata1['exposure time'] = self.focexptime self.manualchecklock.acquire() self.instrument.ccdcamera.Settings = camdata1 try: if correction: imagedata = self.acquireCorrectedCameraImageData() else: imagedata = self.acquireCameraImageData() imarray = imagedata['image'] except: raise self.manualchecklock.release() self.manualplayer.pause() self.logger.error('Failed to acquire image, pausing...') continue self.manualchecklock.release() pow = imagefun.power(imarray, self.maskradius) man_power = pow.astype(numpy.float32) self.man_power = numpy.clip(man_power,self.powmin,self.powmax) self.man_image = imarray.astype(numpy.float32) self.panel.setManualImage(self.man_image, 'Image') self.panel.setManualImage(self.man_power, 'Power') #sleep if too fast in simulation t1 = time.time() if t1-t0 < 0.5: time.sleep(0.5-(t1-t0)) self.onManualCheckDone() self.logger.info('Manual focus check completed')
def processImage(self, imgdata): stackedname = os.path.join(self.params['rundir'], imgdata['filename'] + "power.jpg") if os.path.isfile(stackedname) and apFile.fileSize(stackedname) > 100: return ### make the power spectra powerspectra = imagefun.power(imgdata['image'], mask_radius=1.0, thresh=4) binpowerspectra = imagefun.bin2(powerspectra, self.params['bin']) del powerspectra if self.params['hp'] is True: binpowerspectra = apImage.fermiHighPassFilter(binpowerspectra, apix=4.0, radius=2000.0) binpowerspectra = apImage.normStdevMed(binpowerspectra, size=5) binpowerspectra = apImage.pixelLimitFilter(binpowerspectra, pixlimit=4) binpowerspectra = apImage.normRange(binpowerspectra) ### filter the image imagedata = imagefun.bin2(imgdata['image'], self.params['bin']) del imgdata['image'] imagedata = apImage.normStdevMed(imagedata, size=5) imagedata = apImage.pixelLimitFilter(imagedata, pixlimit=2) imagedata = apImage.normRange(imagedata) ### write to file stacked = numpy.hstack([imagedata, binpowerspectra]) del imagedata, binpowerspectra apImage.arrayToJpeg(stacked, stackedname)
def binAndAddCTFlabel(self, image, ht, rpixelsize, binning=1, defocus=None): pow = imagefun.power(image) binned = imagefun.bin(pow, binning) # No ctf estimation until it works better so that this node does not # depend on coma beam-tilt calibration s = None ctfdata = None ''' try: ctfdata = fftfun.fitFirstCTFNode(pow,rpixelsize['x'], defocus, ht) except Exception, e: self.logger.error("ctf fitting failed: %s" % e) ctfdata = None if ctfdata: self.logger.info('z0 %.3f um, zast %.3f um (%.0f ), angle= %.1f deg' % (ctfdata[0]*1e6,ctfdata[1]*1e6,ctfdata[2]*100, ctfdata[3]*180.0/math.pi)) s = '%d' % int(ctfdata[0]*1e9) #elif self.ace2exe: elif False: ctfdata = self.estimateCTF(imagedata) z0 = (ctfdata['defocus1'] + ctfdata['defocus2']) / 2 s = '%d' % (int(z0*1e9),) ''' if s: t = numpil.textArray(s) t = ndimage.zoom(t, (min(binned.shape)-40.0)*0.08/(t.shape)[0]) minvalue = arraystats.min(binned) maxvalue = arraystats.max(binned) t = minvalue + t * (maxvalue-minvalue) imagefun.pasteInto(t, binned, (20,20)) return binned, ctfdata
def processImage(self, imgdata): stackedname = os.path.join(self.params['rundir'], imgdata['filename']+"power.jpg") if os.path.isfile(stackedname) and apFile.fileSize(stackedname) > 100: return ### make the power spectra powerspectra = imagefun.power(imgdata['image'], mask_radius=1.0, thresh=4) binpowerspectra = imagefun.bin2(powerspectra, self.params['bin']) del powerspectra if self.params['hp'] is True: binpowerspectra = apImage.fermiHighPassFilter(binpowerspectra, apix=4.0, radius=2000.0) binpowerspectra = apImage.normStdevMed(binpowerspectra, size=5) binpowerspectra = apImage.pixelLimitFilter(binpowerspectra, pixlimit=4) binpowerspectra = apImage.normRange(binpowerspectra) ### filter the image imagedata = imagefun.bin2(imgdata['image'], self.params['bin']) del imgdata['image'] imagedata = apImage.normStdevMed(imagedata, size=5) imagedata = apImage.pixelLimitFilter(imagedata, pixlimit=2) imagedata = apImage.normRange(imagedata) ### write to file stacked = numpy.hstack([imagedata, binpowerspectra]) del imagedata, binpowerspectra apImage.arrayToJpeg(stacked, stackedname)
def addCTFlabel(self, image, ht, rpixelsize, binning=1, defocus=None): pow = imagefun.power(image) binned = imagefun.bin(pow, binning) s = None try: ctfdata = fftfun.fitFirstCTFNode(pow,rpixelsize['x'], defocus, ht) except Exception, e: self.logger.error("ctf fitting failed: %s" % e) ctfdata = None
def splitTableau(image, split): tabimage = numpy.zeros(image.shape, image.dtype) splitsize = int(math.floor(image.shape[0]*0.5/int(split)))*2, int(math.floor(image.shape[1]*0.5/int(split)))*2 for row in range(0,image.shape[0],splitsize[0]): rowslice = slice(row,row+splitsize[0]) for col in range(0,image.shape[1],splitsize[1]): colslice = slice(col,col+splitsize[1]) if col+splitsize[1] <= image.shape[1] and row+splitsize[0] <= image.shape[0]: tabimage[rowslice,colslice] = imagefun.power(image[rowslice,colslice]) else: tabimage[rowslice,colslice] = tabimage[max(0,row-1),max(0,col-1)] return tabimage
def calculatePowerImage(self, imagedata): imarray = imagedata["image"] imageshape = imarray.shape if imageshape[0] != imageshape[1]: new_dim = min(imageshape) imarray = imagefun.crop_at(imarray, (imageshape[0] / 2, imageshape[1] / 2), (new_dim, new_dim)) imageshape = imarray.shape if self.settings["reduced"]: size = max(imageshape) if size > 1024: imarray = scipy.ndimage.zoom(imarray, 1024.0 / imageshape[0]) self.logger.info("Calculating power spectrum for image") pow = imagefun.power(imarray, self.settings["mask radius"]) self.setImage(numpy.asarray(pow, numpy.float32), "Power") self.imageshape = imageshape return pow
def calculatePowerImage(self, imagedata): imarray = imagedata['image'] imageshape = imarray.shape if imageshape[0] != imageshape[1]: new_dim = min(imageshape) imarray = imagefun.crop_at(imarray, (imageshape[0] / 2, imageshape[1] / 2), (new_dim, new_dim)) imageshape = imarray.shape if self.settings['reduced']: size = max(imageshape) if size > 1024: imarray = scipy.ndimage.zoom(imarray, 1024.0 / imageshape[0]) self.logger.info('Calculating power spectrum for image') pow = imagefun.power(imarray, self.settings['mask radius']) self.setImage(numpy.asarray(pow, numpy.float32), 'Power') self.imageshape = imageshape return pow
def padpower(image, pixelsize, fieldsize=None, mask_radius=0.5): """ computes power spectra of image using padding """ t0 = time.time() if fieldsize is None: fieldsize = getFieldSize(image.shape) maxDim = max(image.shape) powerTwo = math.ceil(math.log(maxDim)/math.log(2.0)) powerTwoDim = int(2**(powerTwo)) squareImage = imagefilter.frame_constant(image, (powerTwoDim,powerTwoDim)) envelop = twodHann(powerTwoDim) poweravg = imagefun.power(squareImage*envelop, mask_radius) binning = int(powerTwoDim/fieldsize) imagefun.bin2(poweravg, binning) freq = 1.0/(poweravg.shape[0]*pixelsize) apDisplay.printMsg("Fast compute PSD with size %d -> %d complete in %s" %(powerTwoDim, fieldsize, apDisplay.timeString(time.time()-t0))) return poweravg, freq
def acquireManualFocusImage(self): t0 = time.time() correction = self.settings['correct image'] self.manualchecklock.acquire() if correction: imdata = self.acquireCorrectedCameraImageData() else: imdata = self.acquireCameraImageData() imarray = imdata['image'] self.manualchecklock.release() pow = imagefun.power(imarray, self.maskradius) self.man_power = pow.astype(numpy.float32) self.man_image = imarray.astype(numpy.float32) self.panel.setManualImage(self.man_image, 'Image') self.panel.setManualImage(self.man_power, 'Power') #sleep if too fast in simulation safetime = 1.0 t1 = time.time() if t1-t0 < safetime: time.sleep(safetime-(t1-t0))
def acquireImage(self): imagedata = calibrator.Calibrator.acquireImage(self) # fake acquire image for testing #imagedata = self.fakeImage() if imagedata is None: return scope = imagedata['scope'] camera = imagedata['camera'] self.mag = scope['magnification'] self.bin = camera['binning']['x'] newimage = imagedata['image'] self.shape = newimage.shape self.imagepixelsize = self.getImagePixelSize() size = max(self.shape) #if size > 2048: # newimage = scipy.ndimage.zoom(newimage, 2048.0/max(self.shape)) pow = imagefun.power(newimage, 1, 10) pow[(0, 0)] = pow.max() * 2 self.setImage(pow, 'Power') self.panel.acquisitionDone()
def acquireImage(self): imagedata = calibrator.Calibrator.acquireImage(self) # fake acquire image for testing #imagedata = self.fakeImage() if imagedata is None: return scope = imagedata['scope'] camera = imagedata['camera'] self.mag = scope['magnification'] self.bin = camera['binning']['x'] newimage = imagedata['image'] self.shape = newimage.shape self.imagepixelsize = self.getImagePixelSize() size = max(self.shape) if size > 2048: newimage = scipy.ndimage.zoom(newimage, 2048.0/max(self.shape)) pow = imagefun.power(newimage, 1,10) pow[(0,0)] = pow.max()*2 self.setImage(pow, 'Power') self.panel.acquisitionDone()
def processImage(self, imgdata, filtarray): #Ignore array filter array = imgdata['image'] imgmax = self.params['max'] imgmin = self.params['min'] outfile = os.path.join(self.params['rundir'], imgdata['filename']) if self.params['fft'] is True: outfile+=".fft" outfile+=".jpg" if imgmin > imgmax: imgmax = array.max() imgmin = array.min() shape = array.shape maxdim = max(shape) if self.params['fft'] is True: ## fft's are binned by 2 array = imagefun.bin(array,2) array = imagefun.power(array) maxdim/=2 bin = int(math.ceil(float(maxdim/self.params['imgsize']))) array = imagefun.bin(array,bin) jpg.write(array, outfile, min=imgmin, max=imgmax, quality=self.params['quality'], stdval=self.params['stdval'])
def binAndAddCTFlabel(self, image, ht, cs, rpixelsize, binning=1, defocus=None): pow = imagefun.power(image) binned = imagefun.bin(pow, binning) # No ctf estimation until it works better so that this node does not # depend on coma beam-tilt calibration s = None ctfdata = None ''' try: ctfdata = fftfun.fitFirstCTFNode(pow,rpixelsize['x'], defocus, ht, cs) except Exception, e: self.logger.error("ctf fitting failed: %s" % e) ctfdata = None if ctfdata: self.logger.info('z0 %.3f um, zast %.3f um (%.0f ), angle= %.1f deg' % (ctfdata[0]*1e6,ctfdata[1]*1e6,ctfdata[2]*100, ctfdata[3]*180.0/math.pi)) s = '%d' % int(ctfdata[0]*1e9) #elif self.ace2exe: elif False: ctfdata = self.estimateCTF(imagedata) z0 = (ctfdata['defocus1'] + ctfdata['defocus2']) / 2 s = '%d' % (int(z0*1e9),) ''' if s: t = numpil.textArray(s) t = ndimage.zoom(t, (min(binned.shape) - 40.0) * 0.08 / (t.shape)[0]) minvalue = arraystats.min(binned) maxvalue = arraystats.max(binned) t = minvalue + t * (maxvalue - minvalue) imagefun.pasteInto(t, binned, (20, 20)) return binned, ctfdata
x = args[0] y = args[1] fig = self.get_figure() axes = fig.gca() axes.plot(x,y) def clear(self): fig = self.get_figure() axes = fig.gca() axes.cla() if __name__ == '__main__': import wx import numextension from pyami import mrc from pyami import imagefun import sys im = mrc.read(sys.argv[1]) pow = imagefun.power(im) b = numextension.radialPower(pow, 5, 20) app = wx.PySimpleApp() frame = wx.Frame(None, -1, title='My Plot') plotpanel = PlotPanel(frame, -1) #b = [2,3,5,4,3] plotpanel.plot(b) frame.Show() app.MainLoop()
#print phaseshift[c[0],:] * 180.0 / math.pi correction = numpy.cos(phaseshift)+numpy.sin(phaseshift)*complex(0,1) return correction def correctBeamTiltPhaseShift(imgarray,pixelsize,beamtilt,Cs,ht): fft = fftpack.fft2(imgarray) beamtilt = (0.0,1e-4) Cs = 2e-3 pixelsize = 1e-10 ht = 120000 wavelength = getElectronWavelength(ht) correction = getBeamTiltPhaseShiftCorrection(fft.shape,beamtilt,Cs,wavelength,pixelsize) cfft = fft * correction corrected_image = fftpack.ifft2(cfft) return corrected_image if __name__ == '__main__': a = mrc.read('test.mrc') # pixel size in meters pixelsize = 6e-10 Cs=2e-3 ht=300000 pow = imagefun.power(a) mrc.write(pow,'pow.mrc') dimension = {'x':a.shape[1],'y':a.shape[0]} imagepixelsize = {'x':pixelsize,'y':pixelsize} rpixelsize = {'x':1.0/(imagepixelsize['x']*dimension['x']),'y':1.0/(imagepixelsize['y']*dimension['y'])} ctfdata = fitFirstCTFNode(pow,rpixelsize['x'], None, ht) print ctfdata
def power(image, pixelsize, fieldsize=None, mask_radius=0.5, msg=True): """ computes power spectra of image using sub-field averaging image - (2d numpy float array) image to compute power spectra pixelsize - (float) used to compute frequency, freq. can be either Angstroms or meters, but freq will have same inverse units fieldsize - (integer) size of box mask_radius - (float) passed to imagefun.power(), creates a mask of size mask_radius in the center TODO: add median flag, requires saving individual power spectra rather than summing """ if fieldsize is None: fieldsize = getFieldSize(image.shape) t0 = time.time() xsize, ysize = image.shape xnumstep = int(math.floor(xsize/float(fieldsize)))*2-1 ynumstep = int(math.floor(ysize/float(fieldsize)))*2-1 f = fieldsize #powersum = numpy.zeros((fieldsize,fieldsize)) #envelop = numpy.ones((fieldsize,fieldsize)) envelop = twodHann(fieldsize) count = 0 psdlist = [] if msg is True: sys.stderr.write("Computing power spectra in %dx%d blocks"%(fieldsize,fieldsize)) for i in range(xnumstep): for j in range(ynumstep): count += 1 x1 = f*i/2 x2 = x1 + f y1 = f*j/2 y2 = y1 + f if debug is True: print "%03d: %d:%d, %d:%d"%(count, x1, x2, y1, y2) elif msg is True: sys.stderr.write(".") cutout = image[x1:x2, y1:y2] powerspec = imagefun.power(cutout*envelop, mask_radius) psdlist.append(powerspec) if xsize%fieldsize > fieldsize*0.1: for j in range(ynumstep): count += 1 x1 = xsize-f x2 = xsize y1 = f*j/2 y2 = y1 + f if debug is True: print "%03d: %d:%d, %d:%d"%(count, x1, x2, y1, y2) elif msg is True: sys.stderr.write(".") cutout = image[x1:x2, y1:y2] powerspec = imagefun.power(cutout*envelop, mask_radius) psdlist.append(powerspec) if ysize%fieldsize > fieldsize*0.1: for i in range(xnumstep): count += 1 x1 = f*i/2 x2 = x1 + f y1 = ysize-f y2 = ysize if debug is True: print "%03d: %d:%d, %d:%d"%(count, x1, x2, y1, y2) elif msg is True: sys.stderr.write(".") cutout = image[x1:x2, y1:y2] powerspec = imagefun.power(cutout*envelop, mask_radius) psdlist.append(powerspec) sys.stderr.write("\n") freq = 1.0/(powerspec.shape[0]*pixelsize) #poweravg = numpy.array(psdlist).mean(0) apDisplay.printMsg("Computing median of power spectra series") poweravg = numpy.median(psdlist, axis=0) if msg is True: apDisplay.printMsg("Compute PSD with fieldsize %d and %d images complete in %s" %(fieldsize, count, apDisplay.timeString(time.time()-t0))) return poweravg, freq
x = args[0] y = args[1] fig = self.get_figure() axes = fig.gca() axes.plot(x, y) def clear(self): fig = self.get_figure() axes = fig.gca() axes.cla() if __name__ == '__main__': import wx import numextension from pyami import mrc from pyami import imagefun import sys im = mrc.read(sys.argv[1]) pow = imagefun.power(im) b = numextension.radialPower(pow, 5, 20) app = wx.PySimpleApp() frame = wx.Frame(None, -1, title='My Plot') plotpanel = PlotPanel(frame, -1) #b = [2,3,5,4,3] plotpanel.plot(b) frame.Show() app.MainLoop()
#poweravg = numpy.array(psdlist).mean(0) apDisplay.printMsg("Computing median of power spectra series") poweravg = numpy.median(psdlist, axis=0) if msg is True: apDisplay.printMsg("Compute PSD with fieldsize %d and %d images complete in %s" %(fieldsize, count, apDisplay.timeString(time.time()-t0))) return poweravg, freq #=================== #=================== #=================== if __name__ == "__main__": a = mrc.read("/home/vosslab/test.mrc") a = imagefilter.planeRegression(a) fullpower = imagefun.power(a) #imagestat.printImageInfo(a) t0 = time.time() x = numpy.arange(6, 13) N = 2**x print N for n in N: print "=====================================" b = power(a, n) b = imagefilter.frame_cut(b, numpy.array(b.shape)/2) imagefile.arrayToPng(b, "%04d-field.png"%(n)) imagestat.printImageInfo(b) bin = int(round(2**12/n)) b = imagefun.bin2(fullpower, bin) b = imagefilter.frame_cut(b, numpy.array(b.shape)/2)
def manualCheckLoop(self, presetname=None, emtarget=None): ## copied and simplified from focuser.py ## go to preset and target self.logger.info( 'Starting manual focus loop, please confirm defocus...') self.beep() camdata1 = {} # configure camera using settings, but only 512x512 to save time origcam = self.settings['camera settings'] camdata1 = self.makeCenterImageCamSettings(origcam) camdata1['exposure time'] = self.focexptime self.instrument.ccdcamera.Settings = camdata1 pixelsize, center = self.getReciprocalPixelSizeFromInstrument() self.ht = self.instrument.tem.HighTension self.cs = self.getTEMCsValue() self.panel.onNewPixelSize(pixelsize, center, self.ht, self.cs) self.manualplayer.play() self.onManualCheck() while True: t0 = time.time() state = self.manualplayer.state() if state == 'stop': break elif state == 'pause': if self.manualplayer.wait() == 'stop': break # acquire image, show image and power spectrum # allow user to adjust defocus and stig correction = self.settings['correct image'] camdata1['exposure time'] = self.focexptime self.manualchecklock.acquire() self.instrument.ccdcamera.Settings = camdata1 try: if correction: imagedata = self.acquireCorrectedCameraImageData() else: imagedata = self.acquireCameraImageData() imarray = imagedata['image'] except: raise self.manualchecklock.release() self.manualplayer.pause() self.logger.error('Failed to acquire image, pausing...') continue self.manualchecklock.release() pow = imagefun.power(imarray, self.maskradius) man_power = pow.astype(numpy.float32) self.man_power = numpy.clip(man_power, self.powmin, self.powmax) self.man_image = imarray.astype(numpy.float32) self.panel.setManualImage(self.man_image, 'Image') self.panel.setManualImage(self.man_power, 'Power') #sleep if too fast in simulation t1 = time.time() if t1 - t0 < 0.5: time.sleep(0.5 - (t1 - t0)) self.onManualCheckDone() self.logger.info('Manual focus check completed')