def __init__(self, display = False): import mlabraw if display: self.handle = mlabraw.open("matlab -logfile /tmp/matlablog") else: self.handle = mlabraw.open("matlab -nodesktop -nodisplay -nojvm -logfile /tmp/matlablog") mlabraw.eval(self.handle, "addpath('%s');"%os.path.join(os.path.dirname(lfd.__file__), "matlab"))
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 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 preLoopFunctions(self): self.powerspecdir = os.path.join(self.params['rundir'], "opimages") apParam.createDirectory(self.powerspecdir, warning=False) self.acerunq = None self.params['matdir'] = os.path.join(self.params['rundir'],"matfiles") self.params['tempdir'] = os.path.join(self.params['rundir'],"temp") apParam.createDirectory(os.path.join(self.params['matdir']), warning=False) apParam.createDirectory(os.path.join(self.params['tempdir']), warning=False) if self.params['sessionname'] is not None: self.params['outtextfile']=os.path.join(self.params['rundir'], self.params['sessionname']+".txt") if self.params['xvfb'] is True: apParam.resetVirtualFrameBuffer() apMatlab.checkMatlabPath(self.params) acepath = os.path.join(os.getcwd(), self.functionname+".py") #if not os.path.isfile(acepath): # apDisplay.printWarning("'"+self.functionname+".py' usually needs to be run in the "+\ # "same directory as all of its matlab files") print "Connecting to matlab ... " if self.params['xvfb'] is True: apParam.resetVirtualFrameBuffer() try: self.matlab = pymat.open() #self.matlab = pymat.open('matlab -nodisplay') except: apDisplay.environmentError() raise apMatlab.setAceConfig(self.matlab, self.params)
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", "")) # Ensure session get's closed on exit. atexit.register(self._close_session) 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 preLoopFunctions(self): self.powerspecdir = os.path.join(self.params['rundir'], "opimages") apParam.createDirectory(self.powerspecdir, warning=False) self.acerunq = None self.params['matdir'] = os.path.join(self.params['rundir'], "matfiles") self.params['tempdir'] = os.path.join(self.params['rundir'], "temp") apParam.createDirectory(os.path.join(self.params['matdir']), warning=False) apParam.createDirectory(os.path.join(self.params['tempdir']), warning=False) if self.params['sessionname'] is not None: self.params['outtextfile'] = os.path.join( self.params['rundir'], self.params['sessionname'] + ".txt") if self.params['xvfb'] is True: apParam.resetVirtualFrameBuffer() apMatlab.checkMatlabPath(self.params) acepath = os.path.join(os.getcwd(), self.functionname + ".py") #if not os.path.isfile(acepath): # apDisplay.printWarning("'"+self.functionname+".py' usually needs to be run in the "+\ # "same directory as all of its matlab files") print "Connecting to matlab ... " if self.params['xvfb'] is True: apParam.resetVirtualFrameBuffer() try: self.matlab = pymat.open() #self.matlab = pymat.open('matlab -nodisplay') except: apDisplay.environmentError() raise apMatlab.setAceConfig(self.matlab, self.params)
def targetTestImage(self): usercheck = self.settings['user check'] self.settings['user check'] = False filename = self.settings['test image'] try: image2 = mrc.read(filename) image = numpy.asarray(image2,dtype=numpy.float) except: self.logger.error('Failed to load test image') raise return self.setImage(image, 'Image') if self.handle is None: self.handle = pymat.open() pymat.put(self.handle, 'image', image) imdata_id = 0 pymat.put(self.handle, 'image_id',imdata_id) self.matlabFindTargets() self.settings['user check'] = usercheck
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 findTargets(self, imdata, targetlist): imdata_id = imdata.dbid self.logger.info(imdata_id) # convert to float image2 = imdata['image'] image = numpy.asarray(image2,dtype=numpy.float) self.setImage(image, 'Image') if self.handle is None: self.handle = pymat.open() pymat.put(self.handle, 'image', image) pymat.put(self.handle, 'image_id',imdata_id) self.matlabFindTargets() if self.settings['user check']: # user now clicks on targets self.notifyUserSubmit() self.userpause.clear() self.setStatus('user input') self.userpause.wait() self.setStatus('processing') pymat.put(self.handle, 'image', []) pymat.put(self.handle, 'focus', []) pymat.put(self.handle, 'acquisition', []) self.publishTargets(imdata, 'focus', targetlist) self.publishTargets(imdata, 'acquisition', targetlist) self.logger.info('Targets have been submitted')
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 test_mlab_3d(): data = np.arange(24) x = data.reshape(2,3,4) handle = mlabraw.open("matlab -nodesktop -nodisplay -nojvm") registration_matlab.put3d(handle, "x", x) hopefully_x = registration_matlab.get3d(handle, "x") print hopefully_x assert np.allclose(x, hopefully_x)
def test_mlab_3d(): data = np.arange(24) x = data.reshape(2, 3, 4) handle = mlabraw.open("matlab -nodesktop -nodisplay -nojvm") registration_matlab.put3d(handle, "x", x) hopefully_x = registration_matlab.get3d(handle, "x") print hopefully_x assert np.allclose(x, hopefully_x)
def onRunParamGUI(self, evt): # added for GUI parametrization: self.handle = pymat.open() d, f = os.path.split(self.widgets["parametergui path"].GetValue()) # self.settings['parametergui path']) if d: pymat.eval(self.handle, "path(path, '%s')" % d) if not f[:-2]: raise RuntimeError pymat.eval(self.handle, "%s" % f[:-2])
def onRunParamGUI(self, evt): # added for GUI parametrization: self.handle = pymat.open() d, f = os.path.split(self.widgets['parametergui path'].GetValue() ) #self.settings['parametergui path']) if d: pymat.eval(self.handle, 'path(path, \'%s\')' % d) if not f[:-2]: raise RuntimeError pymat.eval(self.handle, '%s' % f[:-2])
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 __init__(self, matlab_root=find_matlab_root(), use_jvm=False, use_display=False): """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 self._closed = False # 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. cmd_str = os.path.join(matlab_root, 'bin', 'matlab') cmd_str += ' -nodesktop' if not use_jvm: cmd_str += ' -nojvm' if not use_display: cmd_str += ' -nodisplay' self._session = mlabraw.open(cmd_str) atexit.register(self.close) 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 = ('single', 'double', 'char', 'cell', 'struct') # The matlab(tm) types that mlabraw will automatically convert for us. self._dont_proxy = {'cell': False}
def __init__(self): #get matlab session try: self.session = mlabraw.open() except Exception as e: print "Error: %s" % str(e)
def initialize(): global MATLAB if MATLAB is None: print "starting matlab..." MATLAB = mlabraw.open("matlab -nodisplay -nosplash -nojvm -nodesktop") print "done"
def __init__(self): self._engine = mlabraw.open(os.getenv("MLABRAW_CMD_STR", "")) atexit.register(lambda self=self: self.close())
try: from mlabwrap import mlab except ImportError: print 'Unable to import mlab module. Attempting to install...' os.system('cd %s; python setup.py build' % MLABWRAPDIR) basedir = '%s/build/' % MLABWRAPDIR sys.path.extend([os.path.join(basedir, x) for x in os.listdir(basedir) if x.startswith('lib')]) from mlabwrap import mlab try: mlab.sin(1) except: # Re-initialize the broken connection to the Matlab engine. import mlabraw mlab._session = mlabraw.open() mlab.addpath(os.path.join(CURRDIR, 'coversongs')) def extract_features(track, fctr=400, fsd=1.0, type=1): """Computes beat-synchronous chroma features. Uses Dan Ellis' chrombeatftrs Matlab function (via the mlabwrap module, which is included with this feature extractor). See http://labrosa.ee.columbia.edu/projects/coversongs for more details. Parameters ----------
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