def runAceCorrect(imgdict,params): imgname = imgdict['filename'] imgpath = os.path.join(imgdict['session']['image path'], imgname+'.mrc') voltage = (imgdict['scope']['high tension']) apix = apDatabase.getPixelSize(imgdict) ctfvalues = ctfdb.getBestCtfByResolution(imgdata) conf = ctfdb.calculateConfidenceScore(bestctfvalue) ctdimname = imgname ctdimpath = os.path.join(params['rundir'],ctdimname) print "Corrected Image written to " + ctdimpath #pdb.set_trace() acecorrectcommand=("ctfcorrect1('%s', '%s', '%.32f', '%.32f', '%f', '%f', '%f');" % \ (imgpath, ctdimpath, ctfvalues['defocus1'], ctfvalues['defocus2'], -ctfvalues['angle_astigmatism'], voltage, apix)) print acecorrectcommand try: matlab = pymat.open("matlab -nosplash") except: apDisplay.environmentError() raise pymat.eval(matlab, acecorrectcommand) pymat.close(matlab) return
def processImage(self, imgdata): self.ctfvalues = {} # RESTART MATLAB EVERY 500 IMAGES OR IT RUNS OUT OF MEMORY if self.stats['count'] % 500 == 0: apDisplay.printWarning("processed 500 images. restarting matlab...") pymat.close(self.matlab) time.sleep(5) self.matlab = pymat.open() scopeparams = { 'kv': imgdata['scope']['high tension']/1000, 'apix': apDatabase.getPixelSize(imgdata), 'cs': self.params['cs'], 'tempdir': self.params['tempdir'], } ### Scott's hack for FSU CM ### For CM high tension is given in kv instead of v if imgdata['scope']['tem']['name'] == "CM": scopeparams['kv'] = imgdata['scope']['high tension'] apMatlab.setScopeParams(self.matlab, scopeparams) ### RUN ACE self.ctfvalues = {} acevalues = apMatlab.runAce(self.matlab, imgdata, self.params) #check if ACE was successful if acevalues[0] == -1: self.badprocess = True return False acevaluelist = ('defocus1','defocus2','defocusinit','amplitude_contrast','angle_astigmatism', 'noise1','noise2','noise3','noise4','envelope1','envelope2','envelope3','envelope4', 'lowercutoff','uppercutoff','snr','confidence','confidence_d') for i in range(len(acevaluelist)): self.ctfvalues[ acevaluelist[i] ] = acevalues[i] ### override the astig angle to be in degrees self.ctfvalues['angle_astigmatism'] = - math.degrees(self.ctfvalues['angle_astigmatism']) self.ctfvalues['mat_file'] = imgdata['filename']+".mrc.mat" self.ctfvalues['cs'] = scopeparams['cs'] self.ctfvalues['volts'] = scopeparams['kv']*1000.0 ### RENAME/MOVE OUTPUT FILES imfile1 = os.path.join(self.params['tempdir'], "im1.png") imfile2 = os.path.join(self.params['tempdir'], "im2.png") opimfile1 = imgdata['filename']+".mrc1.png" opimfile2 = imgdata['filename']+".mrc2.png" opimfilepath1 = os.path.join(self.powerspecdir, opimfile1) opimfilepath2 = os.path.join(self.powerspecdir, opimfile2) if os.path.isfile(imfile1): shutil.copyfile(imfile1, opimfilepath1) else: apDisplay.printWarning("imfile1 is missing, %s"%(imfile1)) if os.path.isfile(imfile2): shutil.copyfile(imfile2, opimfilepath2) else: apDisplay.printWarning("imfile2 is missing, %s"%(imfile2)) self.ctfvalues['graph1'] = opimfilepath1 self.ctfvalues['graph2'] = opimfilepath2
def close(self): if not self._closed: self._closed = True try: mlabraw.close(self._session) except AssertionError: pass
def runAceCorrect(imgdict,params): imgname = imgdict['filename'] imgpath = os.path.join(imgdict['session']['image path'], imgname+'.mrc') voltage = (imgdict['scope']['high tension']) apix = apDatabase.getPixelSize(imgdict) ctfvalues, conf = ctfdb.getBestCtfValueForImage(imgdict) ctdimname = imgname ctdimpath = os.path.join(params['rundir'],ctdimname) print "Corrected Image written to " + ctdimpath #pdb.set_trace() acecorrectcommand=("ctfcorrect1('%s', '%s', '%.32f', '%.32f', '%f', '%f', '%f');" % \ (imgpath, ctdimpath, ctfvalues['defocus1'], ctfvalues['defocus2'], -ctfvalues['angle_astigmatism'], voltage, apix)) print acecorrectcommand try: matlab = pymat.open("matlab -nosplash") except: apDisplay.environmentError() raise pymat.eval(matlab, acecorrectcommand) pymat.close(matlab) return
def __init__(self): """Create a new matlab(tm) wrapper object. """ self._array_cast = None """specifies a cast for arrays. If the result of an operation is a numpy array, ``return_type(res)`` will be returned instead.""" self._autosync_dirs = True """`autosync_dirs` specifies whether the working directory of the matlab session should be kept in sync with that of python.""" self._flatten_row_vecs = False """Automatically return 1xn matrices as flat numeric arrays.""" self._flatten_col_vecs = False """Automatically return nx1 matrices as flat numeric arrays.""" self._clear_call_args = True """Remove the function args from matlab workspace after each function call. Otherwise they are left to be (partly) overwritten by the next function call. This saves a function call in matlab but means that the memory used up by the arguments will remain unreclaimed till overwritten.""" self._session = mlabraw.open() atexit.register(lambda handle=self._session: mlabraw.close(handle)) self._proxies = weakref.WeakValueDictionary() """Use ``mlab._proxies.values()`` for a list of matlab object's that are currently proxied.""" self._proxy_count = 0 self._mlabraw_can_convert = ('double', 'char') """The matlab(tm) types that mlabraw will automatically convert for us.""" self._dont_proxy = {'cell': False} """The matlab(tm) types we can handle ourselves with a bit of
def __init__(self): """Create a new matlab(tm) wrapper object. """ self._array_cast = None """specifies a cast for arrays. If the result of an operation is a numpy array, ``return_type(res)`` will be returned instead.""" self._autosync_dirs=True """`autosync_dirs` specifies whether the working directory of the matlab session should be kept in sync with that of python.""" self._flatten_row_vecs = False """Automatically return 1xn matrices as flat numeric arrays.""" self._flatten_col_vecs = False """Automatically return nx1 matrices as flat numeric arrays.""" self._clear_call_args = True """Remove the function args from matlab workspace after each function call. Otherwise they are left to be (partly) overwritten by the next function call. This saves a function call in matlab but means that the memory used up by the arguments will remain unreclaimed till overwritten.""" self._session = mlabraw.open(os.getenv("MLABRAW_CMD_STR", "")) atexit.register(lambda handle=self._session: mlabraw.close(handle)) self._proxies = weakref.WeakValueDictionary() """Use ``mlab._proxies.values()`` for a list of matlab object's that are currently proxied.""" self._proxy_count = 0 self._mlabraw_can_convert = ('double', 'char') """The matlab(tm) types that mlabraw will automatically convert for us.""" self._dont_proxy = {'cell' : False} """The matlab(tm) types we can handle ourselves with a bit of
def __init__(self): """Create a new matlab(tm) wrapper object. """ self._array_cast = None """specifies a cast for arrays. If the result of an operation is a numpy array, ``return_type(res)`` will be returned instead.""" self._autosync_dirs=True """`autosync_dirs` specifies whether the working directory of the matlab session should be kept in sync with that of python.""" self._flatten_row_vecs = True """Automatically return 1xn matrices as flat numeric arrays.""" self._flatten_col_vecs = True """Automatically return nx1 matrices as flat numeric arrays.""" self._clear_call_args = True """Remove the function args from matlab workspace after each function call. Otherwise they are left to be (partly) overwritten by the next function call. This saves a function call in matlab but means that the memory used up by the arguments will remain unreclaimed till overwritten.""" self._session = mlabraw.open(os.getenv("MLABRAW_CMD_STR", "")) atexit.register(lambda handle=self._session: mlabraw.close(handle)) self._proxies = weakref.WeakValueDictionary() """Use ``mlab._proxies.values()`` for a list of matlab object's that are currently proxied.""" self._proxy_count = 0 self._mlabraw_can_convert = ('double', 'char','single','int') """The matlab(tm) types that mlabraw will automatically convert for us.""" self._dont_proxy = {'cell' : True, 'struct' : True, 'logical' : True} """The matlab(tm) types we can handle ourselves with a bit of effort. To turn on autoconversion for e.g. cell arrays do: ``mlab._dont_proxy["cell"] = True``.""" if 'matlab' in self._session.matlab_process_path: # addpath to iFit location if using Matlab and distribution is not standalone self._eval('isd=double(isdeployed);') isdeployed = self._get('isd') # when not deployed, call 'addpath' if isdeployed == 0: print "Installing iFit" location = os.path.abspath( os.path.realpath(os.path.dirname(__file__))+os.sep+'..'+os.sep+'..' ) self._eval("addpath(genpath('"+location+"'));", print_expression=True) print self._eval('disp(version(iData));') print "To learn how to use Python iFit, type: mlab.doc(mlab.iData())\n";
def __init__(self, matlab_binary_path=None, matlab_version=None): """Create a new matlab(tm) wrapper object. :param matlab_binary_path: This is the full path to the main Matlab executable script on Linux and OSX, e.g. ``/opt/MATLAB/R2010a/matlab``. Not required on Windows. If set to None, mlabwrap will try to find matlab in your path. :param matlab_version: The version string, for example ``2010b``, or None if you want me to deduce it from the matlab path. """ self._array_cast = None """specifies a cast for arrays. If the result of an operation is a numpy array, ``return_type(res)`` will be returned instead.""" self._autosync_dirs=True """`autosync_dirs` specifies whether the working directory of the matlab session should be kept in sync with that of python.""" self._flatten_row_vecs = False """Automatically return 1xn matrices as flat numeric arrays.""" self._flatten_col_vecs = False """Automatically return nx1 matrices as flat numeric arrays.""" self._clear_call_args = True """Remove the function args from matlab workspace after each function call. Otherwise they are left to be (partly) overwritten by the next function call. This saves a function call in matlab but means that the memory used up by the arguments will remain unreclaimed till overwritten.""" #self._session = mlabraw.open(os.getenv("MLABRAW_CMD_STR", "")) self._session = mlabraw.open(matlab_binary_path) atexit.register(lambda handle=self._session: mlabraw.close(handle)) self._proxies = weakref.WeakValueDictionary() """Use ``mlab._proxies.values()`` for a list of matlab object's that are currently proxied.""" self._proxy_count = 0 # due to save stdio defaulting to matlab v4 files, we can unfortunately # only handle these types. sucks! self._mlabraw_can_convert = ('double', 'char') """The matlab(tm) types that mlabraw will automatically convert for us.""" self._dont_proxy = {'cell' : False} """The matlab(tm) types we can handle ourselves with a bit of
def __del__(self): mlabraw.close(self.session)
def postLoopFunctions(self): pymat.close(self.matlab) ctfdb.printCtfSummary(self.params, self.imgtree)
def _close_session(self): if self._session is not None: mlabraw.close(self._session) self._session = None
def close(self): try: mlabraw.close(self._engine) except: pass
def close(self): if not self._closed: self._closed = True mlabraw.close(self._session)
def __del__(self): try: mlabraw.close(self._session) except: pass
def processImage(self, imgdata): self.ctfvalues = {} # RESTART MATLAB EVERY 500 IMAGES OR IT RUNS OUT OF MEMORY if self.stats["count"] % 500 == 0: apDisplay.printWarning("processed 500 images. restarting matlab...") pymat.close(self.matlab) time.sleep(5) self.matlab = pymat.open() scopeparams = { "kv": imgdata["scope"]["high tension"] / 1000, "apix": apDatabase.getPixelSize(imgdata), "cs": self.params["cs"], "tempdir": self.params["tempdir"], } ### Scott's hack for FSU CM ### For CM high tension is given in kv instead of v if imgdata["scope"]["tem"]["name"] == "CM": scopeparams["kv"] = imgdata["scope"]["high tension"] apMatlab.setScopeParams(self.matlab, scopeparams) ### RUN ACE self.ctfvalues = {} acevalues = apMatlab.runAce(self.matlab, imgdata, self.params) # check if ACE was successful if acevalues[0] == -1: self.badprocess = True return False acevaluelist = ( "defocus1", "defocus2", "defocusinit", "amplitude_contrast", "angle_astigmatism", "confidence", "confidence_d", ) for i in range(len(acevaluelist)): self.ctfvalues[acevaluelist[i]] = acevalues[i] ### override the astig angle to be in degrees self.ctfvalues["angle_astigmatism"] = -math.degrees(self.ctfvalues["angle_astigmatism"]) self.ctfvalues["mat_file"] = imgdata["filename"] + ".mrc.mat" self.ctfvalues["cs"] = scopeparams["cs"] self.ctfvalues["volts"] = scopeparams["kv"] * 1000.0 ### RENAME/MOVE OUTPUT FILES imfile1 = os.path.join(self.params["tempdir"], "im1.png") imfile2 = os.path.join(self.params["tempdir"], "im2.png") opimfile1 = imgdata["filename"] + ".mrc1.png" opimfile2 = imgdata["filename"] + ".mrc2.png" opimfilepath1 = os.path.join(self.powerspecdir, opimfile1) opimfilepath2 = os.path.join(self.powerspecdir, opimfile2) if os.path.isfile(imfile1): shutil.copyfile(imfile1, opimfilepath1) else: apDisplay.printWarning("imfile1 is missing, %s" % (imfile1)) if os.path.isfile(imfile2): shutil.copyfile(imfile2, opimfilepath2) else: apDisplay.printWarning("imfile2 is missing, %s" % (imfile2)) self.ctfvalues["graph1"] = opimfilepath1 self.ctfvalues["graph2"] = opimfilepath2