def random_sampling_matlab(self): # Perform test via MATLAB from pymatbridge import Matlab mlab = Matlab() mlab.start() mlab.run_code('cvx_solver mosek;') mlab.run_code("addpath '~/mosek/7/toolbox/r2012a';") self.mlab = mlab if self.block_sizes is not None and len(self.block_sizes) == \ self.A.shape[1]: self.output['error'] = "Trivial example: nblocks == nroutes" logging.error(self.output['error']) self.mlab.stop() self.mlab = None return duration_time = time.time() p = self.mlab.run_func('%s/scenario_to_output.m' % self.CS_PATH, { 'filename' : self.fname, 'type': self.test, 'algorithm' : self.method, 'outpath' : self.OUT_PATH }) duration_time = time.time() - duration_time self.mlab.stop() return array(p['result'])
def worker(cls, args): mlab_path, reqid, respath, srcpath, subnets = args mlab = Matlab(executable=mlab_path) mlab.start() try: # setting variables log.error(msg="Setting Matlab variables", context="HTTP") success = True success &= mlab_setvar(mlab, "DH_Simulator_AllSubnets", ','.join(subnets)) success &= mlab_setvar(mlab, "DH_Simulator_SimulationID", reqid) success &= mlab_setvar(mlab, "DH_Simulator_ResultsPath", respath) success &= mlab_setvar(mlab, "DH_Simulator_ScriptPath", srcpath) if not success: log.error(msg="Unable to set some variables", context="HTTP") else: # running simulation log.error(msg="Starting simulation", context="HTTP") mlab.run_code("cd mlab") mlab.run_code("peak") log.error(msg="Simulation ended", context="HTTP") finally: mlab.stop() archive_dir = os.path.join(respath, reqid) shutil.make_archive(archive_dir, "zip", archive_dir) return reqid
def test_init_end_to_end_distance(run_dir): """ Plot the end-to-end distance distribution for a set of runs' r0 versus the theoretical distribution. """ if not os.path.isdir(os.path.join(run_dir, 'data', 'end_to_end_r0')): wdata.aggregate_r0_group_NP_L0(run_dir) m = Matlab() m.start() m.run_code("help pcalc") m.stop()
class MATLAB: def __init__(self): print "Initializing MATLAB." self.mlab = Matlab() print "Done initializing MATLAB." def connect(self): self.mlab.start() def disconnect(self): if(self.mlab.started): self.mlab.stop() else: print "Tried to disconnect from MATLAB without being connected." def run_code(self, code): try: r = self.mlab.run_code(code) except Exception as exc: raise RuntimeError("Problem executing matlab code: %s" % exc) else: if(not r['success']): raise RuntimeError( "Problem executing matlab code: %s: %s" % (code, r['content'])) def getvalue(self, var): return self.mlab.get_variable(var)
def process(self): from pymatbridge import Matlab hostname = socket.gethostname() if 'nt' in hostname: matlab_executable = str(matlab_r2015a / 'bin' / 'matlab') mlab_process = Matlab( 'nice -n 3 ' + matlab_executable + ' -c {}'.format(matlab_license) + ' -nodisplay -nosplash' ) else: matlab_executable = 'matlab' mlab_process = Matlab( f'nice -n 3 {matlab_executable} -nodisplay -nosplash') if shutil.which(matlab_executable) is None: # pymatbridge.Matlab has a long timeout and will fail with # "ValueError: MATLAB failed to start" when matlab does not exists. raise EnvironmentError( 'Could not find matlab.' ) mlab_process.start() ret = mlab_process.run_code('run ' + self.matlab_startup_path) if not ret['success']: print(ret) raise NameError('Matlab is not working!') return mlab_process
class MatlabEngine(object): def __init__(self): if 'OCTAVE_EXECUTABLE' in os.environ: self._engine = Octave(os.environ['OCTAVE_EXECUTABLE']) self._engine.start() self.name = 'octave' elif matlab_native: self._engine = matlab.engine.start_matlab() self.name = 'matlab' else: executable = os.environ.get('MATLAB_EXECUTABLE', 'matlab') self._engine = Matlab(executable) self._engine.start() self.name = 'pymatbridge' # add MATLAB-side helper functions to MATLAB's path if self.name != 'octave': kernel_path = os.path.dirname(os.path.realpath(__file__)) toolbox_path = os.path.join(kernel_path, 'toolbox') self.run_code("addpath('%s');" % toolbox_path) def run_code(self, code): if matlab_native: return self._run_native(code) return self._engine.run_code(code) def stop(self): if matlab_native: self._engine.exit() else: self._engine.stop() def _run_native(self, code): resp = dict(success=True, content=dict()) out = StringIO() err = StringIO() if sys.version_info[0] < 3: code = str(code) try: self._engine.eval(code, nargout=0, stdout=out, stderr=err) self._engine.eval(''' figures = {}; handles = get(0, 'children'); for hi = 1:length(handles) datadir = fullfile(tempdir(), 'MatlabData'); if ~exist(datadir, 'dir'); mkdir(datadir); end figures{hi} = [fullfile(datadir, ['MatlabFig', sprintf('%03d', hi)]), '.png']; saveas(handles(hi), figures{hi}); if (strcmp(get(handles(hi), 'visible'), 'off')); close(handles(hi)); end end''', nargout=0, stdout=out, stderr=err) figures = self._engine.workspace['figures'] except (SyntaxError, MatlabExecutionError) as exc: resp['content']['stdout'] = exc.args[0] resp['success'] = False else: resp['content']['stdout'] = out.getvalue() if figures: resp['content']['figures'] = figures return resp
class MatlabProcessor(PwebProcessor): """Runs Matlab code usinng python-matlab-brigde""" def __init__(self, parsed, source, mode, formatdict): from pymatbridge import Matlab self.matlab = Matlab() self.matlab.start() PwebProcessor.__init__(self, parsed, source, mode, formatdict) def getresults(self): self.matlab.stop() return copy.copy(self.executed) def loadstring(self, code_string, chunk=None, scope=PwebProcessorGlobals.globals): result = self.matlab.run_code(code_string) if chunk is not None and len(result["content"]["figures"]) > 0: chunk["matlab_figures"] = result["content"]["figures"] return result["content"]["stdout"] def loadterm(self, code_string, chunk=None): result = self.loadstring(code_string) return result def init_matplotlib(self): pass def savefigs(self, chunk): if not "matlab_figures" in chunk: return [] if chunk['name'] is None: prefix = self.basename + '_figure' + str(chunk['number']) else: prefix = self.basename + '_' + chunk['name'] figdir = os.path.join(self.cwd, rcParams["figdir"]) if not os.path.isdir(figdir): os.mkdir(figdir) fignames = [] i = 1 for fig in reversed( chunk["matlab_figures"] ): #python-matlab-bridge returns figures in reverse order #TODO See if its possible to get diffent figure formats. Either fork python-matlab-bridge or use imagemagick name = figdir + "/" + prefix + "_" + str( i) + ".png" #self.formatdict['figfmt'] shutil.copyfile(fig, name) fignames.append(name) i += 1 return fignames def add_echo(self, code_str): """Format inline chunk code to show results""" return "disp(%s)" % code_str
def startMatlab(nonnegative_dir, hardthresh_dir): mlab = Matlab() mlab.start() # I could do a cd here to make sure that the call functions are in the working dir status1 = mlab.run_code("addpath %s" % nonnegative_dir)['success'] status2 = mlab.run_code("addpath %s/Main" % hardthresh_dir) status3 = mlab.run_code("addpath %s/Auxiliary" % hardthresh_dir) if status1 and status2 and status3: print("Libraries succesfully loaded") else: print("Error loading libraries") return return mlab
class MatlabProcessor(PwebProcessor): """Runs Matlab code usinng python-matlab-brigde""" def __init__(self, parsed, source, mode, formatdict): from pymatbridge import Matlab self.matlab = Matlab() self.matlab.start() PwebProcessor.__init__(self, parsed, source, mode, formatdict) def getresults(self): self.matlab.stop() return copy.copy(self.executed) def loadstring(self, code_string, chunk=None, scope=PwebProcessorGlobals.globals): result = self.matlab.run_code(code_string) if chunk is not None and len(result["content"]["figures"]) > 0: chunk["matlab_figures"] = result["content"]["figures"] return result["content"]["stdout"] def loadterm(self, code_string, chunk=None): result = self.loadstring(code_string) return result def init_matplotlib(self): pass def savefigs(self, chunk): if not "matlab_figures" in chunk: return [] if chunk['name'] is None: prefix = self.basename + '_figure' + str(chunk['number']) else: prefix = self.basename + '_' + chunk['name'] figdir = os.path.join(self.cwd, rcParams["figdir"]) if not os.path.isdir(figdir): os.mkdir(figdir) fignames = [] i = 1 for fig in reversed(chunk["matlab_figures"]): #python-matlab-bridge returns figures in reverse order #TODO See if its possible to get diffent figure formats. Either fork python-matlab-bridge or use imagemagick name = figdir + "/" + prefix + "_" + str(i) + ".png" #self.formatdict['figfmt'] shutil.copyfile(fig, name) fignames.append(name) i += 1 return fignames def add_echo(self, code_str): """Format inline chunk code to show results""" return "disp(%s)" % code_str
class MatlabEngine(object): def __init__(self): if 'OCTAVE_EXECUTABLE' in os.environ: self._engine = Octave(os.environ['OCTAVE_EXECUTABLE']) self._engine.start() self.name = 'octave' elif matlab_native: self._engine = matlab.engine.start_matlab() self.name = 'matlab' else: executable = os.environ.get('MATLAB_EXECUTABLE', 'matlab') self._engine = Matlab(executable) self._engine.start() self.name = 'pymatbridge' # add MATLAB-side helper functions to MATLAB's path if self.name != 'octave': kernel_path = os.path.dirname(os.path.realpath(__file__)) toolbox_path = os.path.join(kernel_path, 'toolbox') self.run_code("addpath('%s');" % toolbox_path) def run_code(self, code): if matlab_native: return self._run_native(code) return self._engine.run_code(code) def stop(self): if matlab_native: self._engine.exit() else: self._engine.stop() def _run_native(self, code): out = StringIO() err = StringIO() if sys.version_info[0] < 3: code = str(code) try: self._engine.eval(code, nargout=0, stdout=out, stderr=err) except (SyntaxError, MatlabExecutionError) as exc: return dict(success=False, content=dict(stdout=exc.args[0])) return dict(success=True, content=dict(stdout=out.getvalue()))
# Parameters Setting filename = "./Input_data/example_lena.png" npixs = 512 Nstd = 0.4 NE = 20 seedNo = 1 numImf = 6 runCEEMD = 1 maxSift = 10 typeSpline = 2 toModifyBC = 1 randType = 2 checksignal = 1 ## mlab = Matlab() mlab.start() mlab.run_code("addpaths") # Fast 2dEEMD res = mlab.run_func( "meemd", filename, npixs, Nstd, NE, numImf, runCEEMD, maxSift, typeSpline, toModifyBC, randType, seedNo, checksignal ) imfs = res["result"] # Plot Results HHTplots.example_lena(filename, imfs) mlab.stop()
if __name__ == "__main__": output_queue_list = list() sys.stdout.write("Finding control VM\n") service_list = get_service_list(sys.argv) video_ip = service_list.get(SERVICE_META.VIDEO_TCP_STREAMING_ADDRESS) video_port = service_list.get(SERVICE_META.VIDEO_TCP_STREAMING_PORT) acc_ip = service_list.get(SERVICE_META.ACC_TCP_STREAMING_ADDRESS) acc_port = service_list.get(SERVICE_META.ACC_TCP_STREAMING_PORT) return_addresses = service_list.get(SERVICE_META.RESULT_RETURN_SERVER_LIST) #session = pymatlab.session_factory(); mlab = Matlab('/home/ivashish/Matlab/bin/matlab'); mlab.start(); mlab.run_code("tempModels = load('/home/ivashish/exemplarsvm-master/ketchups-final');"); mlab.run_code("ketchup_models = tempModels.combineModels"); mlab.run_code("tempModels = load('/home/ivashish/exemplarsvm-master/cup_comb.mat');"); mlab.run_code("cup_models = tempModels.allEsvms"); mlab.run_code("tempModels = load('/home/ivashish/voc-dpm-master/VOC2007/person_final.mat');"); mlab.run_code("dpm_model = tempModels.model"); LOG.info("loaded models"); # dummy video app image_queue = Queue.Queue(1) video_ip ='10.2.12.4' video_client = AppProxyStreamingClient((video_ip, video_port), image_queue)
from IPython.display import clear_output import matplotlib.pyplot as plt import numpy as np from numpy import sin, cos from pymatbridge import Matlab get_ipython().magic(u'matplotlib inline') execfile('../../matplotlibrc.py') # Run Matlab code and fetch relevant data mlab = Matlab() mlab.start() results = mlab.run_code(open('fem1d.m').read()) K = mlab.get_variable('K') U = mlab.get_variable('U') nodeLocs = mlab.get_variable('nodeLocs') mlab.stop() clear_output() print('K') print(K) print('U') print(U) x = nodeLocs[:,0] xex = np.linspace(0, 1); w = np.sqrt(2); uex = (cos(w) - 1)*sin(w*xex)/(2.0*sin(w)) - 0.5*cos(w*xex) + 0.5 fig, ax = plt.subplots(figsize=(14, 10)) ax.plot(xex, uex, 'k-', label='Exact') ax.plot(x, U, 'b-o', label='FEM')
import os import sys from pymatbridge import Matlab sys.path.append('/Users/stephane/Documents/git/takumi/fapm/cine_local') matlab_path = '/Applications/MATLAB_R2019a.app/bin/matlab' faqm_dir = os.path.split(os.path.realpath(__file__))[0] matlabdir = os.path.join(faqm_dir, 'matlab_codes') # DATA ARCHITECTURE scriptdir, scriptname = os.path.split(os.path.realpath(__file__)) mlab = Matlab(executable=matlab_path) mlabstart = mlab.start() mlab.run_code('addpath %s; savepath;' % matlabdir) print('DONE') print('... MATLAB may crash by this process, but the setup was successful.') mlab.quit()
im = utils.modcrop(im, UP_SCALE).astype(np.float32) im_gt += [im] im_l = [] if len(IMAGE_FILE) > 0: assert (len(im_gt) == 1) im_l = [np.array(Image.open(IMAGE_FILE)).astype(np.float32)] else: #down scale from ground truth using Matlab try: from pymatbridge import Matlab mlab = Matlab() mlab.start() for im in im_gt: mlab.set_variable('a', im) mlab.set_variable('s', 1.0 / UP_SCALE) mlab.run_code('b=imresize(a, s);') im_l += [mlab.get_variable('b')] mlab.stop() except: print 'failed to load Matlab!' assert (0) #im_l = utils.imresize(im_gt, 1.0/UP_SCALE) #upscaling #sr = Bicubic() sr = SCN(MODEL_FILE) res_all = [] for i in range(len(im_l)): t = time.time() im_h, im_h_y = sr.upscale(im_l[i], UP_SCALE) t = time.time() - t
print_matlab_code(code) def print_python_function(fun): code = inspect.getsource(fun) print_python_code(code) # ## Start Matlab matlab = Matlab() matlab.start() # ## Add `NoiseTools` to the Matlab's path matlab.run_code('addpath(\'{}\')'.format(noise_tools_dir)) # # Simulate data # Let's look at the example 1 code: print_matlab_script(example_1) # Let's create synthetic data in Matlab and transfer it here. example_1_code = open(example_1, 'r').readlines() synthethize_data_code = ''.join(example_1_code[9:21]) print_matlab_code(synthethize_data_code) matlab.run_code(synthethize_data_code) data = matlab.get_variable('data')
from pymatbridge import Matlab mlab = Matlab() mlab.start() res = mlab.run_code('a=10, b=a+3') mlab.stop()
from IPython.display import clear_output import matplotlib.pyplot as plt import numpy as np from numpy import sin, cos from pymatbridge import Matlab get_ipython().magic(u'matplotlib inline') execfile('../../matplotlibrc.py') # Run Matlab code and fetch relevant data mlab = Matlab() mlab.start() results = mlab.run_code(open('fem1d.m').read()) K = mlab.get_variable('K') U = mlab.get_variable('U') nodeLocs = mlab.get_variable('nodeLocs') mlab.stop() clear_output() print('K') print(K) print('U') print(U) x = nodeLocs[:, 0] xex = np.linspace(0, 1) w = np.sqrt(2) uex = (cos(w) - 1) * sin(w * xex) / (2.0 * sin(w)) - 0.5 * cos(w * xex) + 0.5 fig, ax = plt.subplots(figsize=(14, 10)) ax.plot(xex, uex, 'k-', label='Exact') ax.plot(x, U, 'b-o', label='FEM')
ground_truth_folder=args.ground_truth_path), batch_size=batch_size, shuffle=True, drop_last=True) test_loader = data.DataLoader(dataset=CleansingDataset( root=os.path.join(args.dataset, 'test_dataset'), transform=test_transform, dataset=args.dataset, special_list=[], filter_type='not_in', feature_folder=feature_floder, ground_truth_folder='annotation_folder'), batch_size=batch_size, shuffle=True, drop_last=True) train(model, train_loader, test_loader, epochs, args.logdir, savepath) get_resnet_result(model, args.dataset, savepath) endtime = datetime.datetime.now() print('code ends at ', endtime - starttime) if args.use_pymatbridge: from pymatbridge import Matlab mlab = Matlab() mlab.start() results = mlab.run_code('run {}/eva/Main.m'.format(args.dataset)) meanJacc, stdJacc, meanAcc, stdAcc = mlab.get_variable( 'meanJacc'), mlab.get_variable('stdJacc'), mlab.get_variable( 'meanAcc'), mlab.get_variable('stdAcc')
rt(n, s, :) = tempt; acc(n, s, :) = tempx; end end '''.format(nsims, nsubs, ntrials) # Write out code modelfile = 'simultrialparams.m' f = open(modelfile, 'w') f.write(matlabcode) f.close() # Simulate some data from simuldiff in MATLAB (find a way to do this in Python) mlab = Matlab(maxtime=240) # Increase max start time mlab.start() results2 = mlab.run_code('simultrialparams;') genparam = dict() genparam['tersub'] = mlab.get_variable('tersub') genparam['alphasub'] = mlab.get_variable('alphasub') genparam['deltasub'] = mlab.get_variable('deltasub') genparam['tertrialsd'] = mlab.get_variable('tertrialsd') genparam['deltatrialsd'] = mlab.get_variable('deltatrialsd') genparam['prob_mindwander'] = mlab.get_variable('prob_mindwander') genparam['rt'] = mlab.get_variable('rt') genparam['acc'] = mlab.get_variable('acc') genparam['y'] = mlab.get_variable('y') mlab.stop() sio.savemat('genparam_test.mat', genparam) # Send to JAGS nchains = 6
@author: scott """ from __future__ import print_function import sys, os import cobra, cobra.test, json from escher import Builder from gurobipy import * from os.path import join from cobra.io.mat import model_to_pymatbridge from pymatbridge import Matlab # if you're going to be working with both Matlab and Python: mlab = Matlab() mlab.start() results = mlab.run_code('a=1;') model.solver = 'gurobi' # redirect towards the datafiles data_dir = r'C:\\Users\\scott\\Google Drive\\Work\\UM\\Research\\Sriram\\Projects\\Lyssiotis_Proteomics' # we need to use a recon1 model with an objective function: recon1_model = cobra.io.load_matlab_model(join(data_dir, "model_human_duarte.mat")) model_to_pymatbridge(recon1_model, variable_name="metabolicmodel") recon1_model.metabolicmodel.S #Palsson_core = cobra.io.load_matlab_model(join(data_dir, "Core_Model_Palsson.mat"), variable_name="core_genecomb") def flux_activity_coeff(model, timecourse_metabolomics, sheetname='Sheet1', kappa=None, kappa2=None, genedelflag=None, rxndelflag=None, normquantile=None): '''docstring flux_activity_coeff
im = utils.modcrop(im, UP_SCALE).astype(np.float32) im_gt += [im] im_l = [] if len(IMAGE_FILE)>0: assert(len(im_gt)==1) im_l = [np.array(Image.open(IMAGE_FILE)).astype(np.float32)] else: #down scale from ground truth using Matlab try: from pymatbridge import Matlab mlab = Matlab() mlab.start() for im in im_gt: mlab.set_variable('a', im) mlab.set_variable('s', 1.0/UP_SCALE) mlab.run_code('b=imresize(a, s);') im_l += [mlab.get_variable('b')] mlab.stop() except: print 'failed to load Matlab!' assert(0) #im_l = utils.imresize(im_gt, 1.0/UP_SCALE) #upscaling #sr = Bicubic() sr = SCN(MODEL_FILE) res_all = [] for i in range(len(im_l)): t=time.time(); im_h, im_h_y=sr.upscale(im_l[i], UP_SCALE) t=time.time()-t;
for wavelet in wavelets: w = pywt.Wavelet(wavelet) mlab.set_variable('wavelet', wavelet) if size_set == 'full': data_sizes = list(range(w.dec_len, 40)) + \ [100, 200, 500, 1000, 50000] else: data_sizes = (w.dec_len, w.dec_len + 1) for N in data_sizes: data = rstate.randn(N) mlab.set_variable('data', data) for pmode, mmode in modes: # Matlab result mlab_code = ("[ma, md] = dwt(data, wavelet, " "'mode', '%s');" % mmode) res = mlab.run_code(mlab_code) if not res['success']: raise RuntimeError( "Matlab failed to execute the provided code. " "Check that the wavelet toolbox is installed.") # need np.asarray because sometimes the output is type float ma = np.asarray(mlab.get_variable('ma')) md = np.asarray(mlab.get_variable('md')) ma_key = '_'.join([mmode, wavelet, str(N), 'ma']) md_key = '_'.join([mmode, wavelet, str(N), 'md']) all_matlab_results[ma_key] = ma all_matlab_results[md_key] = md finally: mlab.stop() np.savez('dwt_matlabR2012a_result.npz', **all_matlab_results)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Wed Jan 9 15:53:25 2019 @author: rdamseh """ import matlab.engine as eng from pymatbridge import Matlab mlab = Matlab() mlab.start() mlab.run_code('x=[1,2,3,4,5,6].^2') x=mlab.get_variable('x')
#!/usr/bin/env python # coding: UTF-8 from pymatbridge import Matlab mlab = Matlab(executable='/Applications/MATLAB_R2014a.app/bin/matlab') mlab.start() results = mlab.run_code('a=1;') var = mlab.get_variable('a') print var mlab.stop()
from pymatbridge import Matlab # Initialise MATLAB mlab = Matlab() # Start the server mlab.start() # Run a test function: just adds 1 to the argument a res = [] for i in range(5): res.append(mlab.run_func('demo_func.m', {'a': i})['result']) print res[-1] # test the JSON parsing. # quotes, and \n res.append(mlab.run_code('fprintf(1,char([34,104,105,34,10]));')) print res[-1] # \b, \n res.append(mlab.run_code('fprintf(1,char([8,10]));')) print res[-1] # \f, \n res.append(mlab.run_code('fprintf(1,char([12,10]));')) print res[-1] # \r, \n res.append(mlab.run_code('fprintf(1,char([13,10]));')) print res[-1] # \t, \n res.append(mlab.run_code('fprintf(1,char([9,10]));')) print res[-1] # \\, \n res.append(mlab.run_code('fprintf(1,char([92,92,10]));'))
class MatlabKernel(MetaKernel): implementation = 'Matlab Kernel' implementation_version = __version__, language = 'matlab' language_version = '0.1', banner = "Matlab Kernel" language_info = { 'mimetype': 'text/x-matlab', 'name': 'octave', 'file_extension': '.m', 'help_links': MetaKernel.help_links, } _first = True def __init__(self, *args, **kwargs): super(MatlabKernel, self).__init__(*args, **kwargs) executable = os.environ.get('MATLAB_EXECUTABLE', 'matlab') subprocess.check_call([executable, '-e'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self._matlab = Matlab(executable) self._matlab.start() def get_usage(self): return "This is the Matlab kernel." def do_execute_direct(self, code): if self._first: self._first = False fig_code = "set(0, 'defaultfigurepaperunits', 'inches');" self._matlab.run_code(fig_code) self._matlab.run_code("set(0, 'defaultfigureunits', 'inches');") self.handle_plot_settings() self.log.debug('execute: %s' % code) resp = self._matlab.run_code(code.strip()) self.log.debug('execute done') if 'stdout' not in resp['content']: raise ValueError(resp) if 'figures' in resp['content']: for fname in resp['content']['figures']: try: im = Image(filename=fname) self.Display(im) except Exception as e: self.Error(e) if not resp['success']: self.Error(resp['content']['stdout'].strip()) else: return resp['content']['stdout'].strip() def get_kernel_help_on(self, info, level=0, none_on_fail=False): obj = info.get('help_obj', '') if not obj or len(obj.split()) > 1: if none_on_fail: return None else: return "" return self.do_execute_direct('help %s' % obj) def handle_plot_settings(self): """Handle the current plot settings""" settings = self.plot_settings settings.setdefault('size', '560,420') width, height = 560, 420 if isinstance(settings['size'], tuple): width, height = settings['size'] elif settings['size']: try: width, height = settings['size'].split(',') width, height = int(width), int(height) except Exception as e: self.Error(e) size = "set(0, 'defaultfigurepaperposition', [0 0 %s %s])\n;" self.do_execute_direct(size % (width / 150., height / 150.)) def repr(self, obj): return obj def restart_kernel(self): """Restart the kernel""" self._matlab.stop() def do_shutdown(self, restart): with open('test.txt', 'w') as fid: fid.write('hey hey\n') self._matlab.stop()
str(w.center_frequency)) elif wavelet == 'fbsp': mlab.set_variable( 'wavelet', wavelet + str(w.fbsp_order) + '-' + str(w.bandwidth_frequency) + '-' + str(w.center_frequency)) else: mlab.set_variable('wavelet', wavelet) if size_set == 'full': data_sizes = list(range(100, 101)) + \ [100, 200, 500, 1000, 50000] Scales = (1, np.arange(1, 3), np.arange(1, 4), np.arange(1, 5)) else: data_sizes = (1000, 1000 + 1) Scales = (1, np.arange(1, 3)) mlab_code = ("psi = wavefun(wavelet,10)") res = mlab.run_code(mlab_code) if not res['success']: raise RuntimeError("Matlab failed to execute the provided code. " "Check that the wavelet toolbox is installed.") psi = np.asarray(mlab.get_variable('psi')) psi_key = '_'.join([wavelet, 'psi']) all_matlab_results[psi_key] = psi for N in data_sizes: data = rstate.randn(N) mlab.set_variable('data', data) # Matlab result scale_count = 0 for scales in Scales: scale_count += 1 mlab.set_variable('scales', scales)
#!/usr/bin/python from pymatbridge import Matlab mlab = Matlab() mlab.start() print "Matlab started?", mlab.started print "Matlab is connected?", mlab.is_connected() mlab.run_code("conteo = 1:10") mlab.run_code("magica = magic(5)") mlab.stop()
maxSift = 10 typeSpline = 2 toModifyBC = 1 randType = 2 checksignal = 1 ## data = np.loadtxt(filename) time = data[:, 0] amp = data[:, 1] dt = time[1] - time[0] ## mlab = Matlab() mlab.start() mlab.run_code('addpaths') # Fast EEMD res = mlab.run_func('feemd_post_pro', amp, Nstd, NE, numImf, runCEEMD, maxSift, typeSpline, toModifyBC, randType, seedNo, checksignal) imfs = res['result'] # Orthogonality Checking oi = mlab.run_func('ratio1', imfs) oi_pair = mlab.run_func('ratioa', imfs) print('Non-orthogonal leakage of components:') print(oi['result']) print('Non-orthogonal leakage for pair of adjoining components:') print(oi_pair['result'])
class MatlabKernel(MetaKernel): implementation = "Matlab Kernel" implementation_version = (__version__,) language = "matlab" language_version = ("0.1",) banner = "Matlab Kernel" language_info = { "mimetype": "text/x-matlab", "name": "octave", "file_extension": ".m", "codemirror_mode": "Octave", "help_links": MetaKernel.help_links, } _first = True def __init__(self, *args, **kwargs): super(MatlabKernel, self).__init__(*args, **kwargs) executable = os.environ.get("MATLAB_EXECUTABLE", "matlab") subprocess.check_call([executable, "-e"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self._matlab = Matlab(executable) self._matlab.start() def get_usage(self): return "This is the Matlab kernel." def do_execute_direct(self, code): if self._first: self._first = False fig_code = "set(0, 'defaultfigurepaperunits', 'inches');" self._matlab.run_code(fig_code) self._matlab.run_code("set(0, 'defaultfigureunits', 'inches');") self.handle_plot_settings() self.log.debug("execute: %s" % code) resp = self._matlab.run_code(code.strip()) self.log.debug("execute done") if "stdout" not in resp["content"]: raise ValueError(resp) if "figures" in resp["content"]: for fname in resp["content"]["figures"]: try: im = Image(filename=fname) self.Display(im) except Exception as e: self.Error(e) if not resp["success"]: self.Error(resp["content"]["stdout"].strip()) else: return resp["content"]["stdout"].strip() or None def get_kernel_help_on(self, info, level=0, none_on_fail=False): obj = info.get("help_obj", "") if not obj or len(obj.split()) > 1: if none_on_fail: return None else: return "" return self.do_execute_direct("help %s" % obj) def handle_plot_settings(self): """Handle the current plot settings""" settings = self.plot_settings settings.setdefault("size", "560,420") width, height = 560, 420 if isinstance(settings["size"], tuple): width, height = settings["size"] elif settings["size"]: try: width, height = settings["size"].split(",") width, height = int(width), int(height) except Exception as e: self.Error(e) size = "set(0, 'defaultfigurepaperposition', [0 0 %s %s])\n;" self.do_execute_direct(size % (width / 150.0, height / 150.0)) def repr(self, obj): return obj def restart_kernel(self): """Restart the kernel""" self._matlab.stop() def do_shutdown(self, restart): with open("test.txt", "w") as fid: fid.write("hey hey\n") self._matlab.stop()
class MatlabKernel(MetaKernel): implementation = 'Matlab Kernel' implementation_version = __version__, language = 'matlab' language_version = '0.1', banner = "Matlab Kernel" language_info = { 'mimetype': 'text/x-matlab', 'name': 'matlab', 'file_extension': '.m', 'help_links': MetaKernel.help_links, } _first = True def __init__(self, *args, **kwargs): excecutable = kwargs.pop('excecutable', 'matlab') super(MatlabKernel, self).__init__(*args, **kwargs) self._matlab = Matlab(excecutable) self._matlab.start() def get_usage(self): return "This is the Matlab kernel." def do_execute_direct(self, code): if self._first: self._first = False fig_code = "set(0, 'defaultfigurepaperunits', 'inches');" self._matlab.run_code(fig_code) self._matlab.run_code("set(0, 'defaultfigureunits', 'inches');") self.handle_plot_settings() self.log.debug('execute: %s' % code) resp = self._matlab.run_code(code.strip()) self.log.debug('execute done') if 'stdout' not in resp['content']: raise ValueError(resp) if 'figures' in resp['content']: for fname in resp['content']['figures']: try: im = Image(filename=fname) self.Display(im) except Exception as e: self.Error(e) return resp['content']['stdout'].strip() def get_kernel_help_on(self, info, level=0, none_on_fail=False): obj = info.get('help_obj', '') if not obj or len(obj.split()) > 1: if none_on_fail: return None else: return "" return self.do_execute_direct('help %s' % obj) def handle_plot_settings(self): """Handle the current plot settings""" settings = self.plot_settings settings.setdefault('size', '560,420') width, height = 560, 420 if isinstance(settings['size'], tuple): width, height = settings['size'] elif settings['size']: try: width, height = settings['size'].split(',') width, height = int(width), int(height) except Exception as e: self.Error(e) size = "set(0, 'defaultfigurepaperposition', [0 0 %s %s])\n;" self.do_execute_direct(size % (width / 150., height / 150.)) def repr(self, obj): return obj def restart_kernel(self): """Restart the kernel""" self._matlab.stop()
h_test = np.matmul(np.conj(np.swapaxes(h_test, -2, -1)), h_test) y_test = np.reshape(y_test, (-1, num_rx)) h_test = np.reshape(h_test, h_test.shape[:-2] + (-1, )) h_test = np.reshape(h_test, (-1, num_rx * num_tx)) n_test = np.reshape(n_test, (-1, 1)) # Convert complex to reals y_test = y_test.view(np.float64) h_test = h_test.view(np.float64) # Start Matlab engine eng = Matlab() eng.start() # Move to right path eng.run_code( 'cd /home/yanni/marius/deep-llr-quantization-master/deep-llr-quantization-master/' ) # How many runs num_runs = 1 # Global metrics local_seed_collect = np.zeros((num_runs, )) bler_ae, ber_ae = np.zeros((num_runs, num_snr)), np.zeros((num_runs, num_snr)) bler_aeq, ber_aeq = np.zeros((num_runs, len(bits_per_dim), num_snr)), np.zeros( (num_runs, len(bits_per_dim), num_snr)) bler_enc, ber_enc = np.zeros((num_runs, num_snr)), np.zeros( (num_runs, num_snr)) bler_encq, ber_encq = np.zeros( (num_runs, len(bits_per_dim), num_snr)), np.zeros( (num_runs, len(bits_per_dim), num_snr))
HSA_mfile_list = os.listdir('./Matlab_runcode/') for files in HSA_mfile_list: shutil.move('./Matlab_runcode/' + files, HSA_dir) # Delete unnecessary directories/files os.remove('EEMD.zip') os.remove('Matlab_runcode.zip') os.rmdir('Matlab_runcode') print('...Done.') # Check the MATLAB version & replace the deprecated function with the new one. mlab = Matlab() print('* Checking your MATLAB version...') mlab.start() mlab.run_code('v = version;') version = mlab.get_variable('v') mlab.stop() print('Your MATLAB version is: ' + version) version = version.split('.') if int(version[0]) >= 8: print('The function "getDefaultStream" in eemd.m is no longer be used ' + 'in your MATLAB version.') print('* Replacing it with the function "getGlobalStream"...') with open(EEMD_dir + 'eemd.m', 'r', encoding='iso-8859-1') as infile: data = infile.read().replace('getDefaultStream', 'getGlobalStream') infile.close() with open(EEMD_dir + 'eemd2.m', 'w',encoding='iso-8859-1') as outfile: outfile.write(data) outfile.close() os.remove(EEMD_dir + 'eemd.m')
def train_save_sp_tensor(self, pmi=True): gatherer = self.get_pmi_gatherer(3) if pmi: print('creating PPMI tensor...') else: print('creating sparse count tensor...') indices, values = gatherer.create_pmi_tensor(positive=True, debug=True, symmetric=False, pmi=pmi, shift=-np.log2(15.)) matfile_name = 'sp_tensor_{}_{}_log15.mat'.format( self.num_sents, self.min_count) scipy.io.savemat(matfile_name, {'indices': indices, 'values': values}) print('saved {}. exiting.'.format(matfile_name)) sys.exit() from pymatbridge import Matlab session = Matlab('/usr/local/bin/matlab') print('starting matlab session...') session.start() #session.set_variable('indices', indices+1) #session.set_variable('vals', values) print('setting up variables...') session.run_code("d = load('/home/eric/code/gensim/sp_tensor.mat');") session.run_code("indices = d.indices + 1;") session.run_code("vals = d.values';") #session.run_code('size_ = [{0} {0} {0}];'.format(len(self.model.vocab))) session.run_code('size_ = [{0} {0} {0}];'.format(8)) session.run_code('R = {};'.format(self.embedding_dim)) import pdb pdb.set_trace() res = session.run_code("T = sptensor(indices, vals, size_);") print('running ALS...') t = time.time() res = session.run_code('[P, U0, out] = cp_als(T, R)') print('ALS took {} secs'.format(time.time() - t)) session.run_code('lambda = P.lambda;') session.run_code('U = P{1,1};') session.run_code('V = P{2,1};') session.run_code('W = P{3,1};') lambda_ = session.get_variable('lambda') U = session.get_variable('U') import pdb pdb.set_trace() '''
__author__ = 'bejar' from pymatbridge import Matlab from config.experiments import experiments, lexperiments import time # lexperiments = ['e130716', 'e130827', 'e130903', 'e141113', 'e141029', 'e141016', 'e140911', 'e140311', 'e140225', 'e140220'] lexperiments = ['130827']#'e130827','e140225', 'e140220', 'e141016', 'e140911'] mlab = Matlab(executable='/home/bejar/bin/MATLAB/R2014b/bin/matlab') mlab.start() a = mlab.run_code('cd(\'/home/bejar/PycharmProjects/PeakDataAnalysis/Matlab/\')') print a datasufix = ''#'-RawResampled' wtime = '120e-3' # Window length in miliseconds for expname in lexperiments: datainfo = experiments[expname] sampling = datainfo.sampling #/ 6.0 for file in [datainfo.datafiles[0]]: print time.ctime() nfile = '/home/bejar/Data/Cinvestav/' + file + datasufix + '.mat' nfiler = '/home/bejar/Data/Cinvestav/' + file + datasufix + '-peaks2.mat' print 'Processing ', file
def callMatlabFunc(mlab, funcName, inputArgs, nbOutputArg, debug=False, setupCode=""): if debug: print("Entering callMatlabFunc...") closeMatlab = False if mlab is None: if debug: print("Starting Matlab...") mlab = Matlab() #Matlab(matlab='C:/Program Files/MATLAB/R2015a/bin/matlab.exe') mlab.start() closeMatlab = True if len(setupCode): result = mlab.run_code(setupCode) if not result["success"]: raise RuntimeError(result["content"]["stdout"] ) if debug: print("Setting input variables...") inputStr = "" if len(inputArgs): for i, arg in enumerate(inputArgs): mlab.set_variable("in" + str(i), arg) inputStr += "in" + str(i) + "," inputStr = inputStr[:-1] if debug: print("Input variables set...") matlabCode = "" if nbOutputArg == 1: matlabCode += "out0 = " elif nbOutputArg > 1: matlabCode += "[" for i in range(nbOutputArg): matlabCode += "out" + str(i) + "," matlabCode = matlabCode[:-1] matlabCode += "] = " matlabCode += funcName + "(" + inputStr + ")" if debug: print("Matlab Code: ") print(matlabCode) result = mlab.run_code(matlabCode) if debug: print("run_code executed.") print(result) outArgs = [mlab.get_variable("out" + str(i)) for i in range(nbOutputArg)] if debug: print("Out args: ") print(outArgs) sucess = result["success"] stdout = result["content"]["stdout"] if closeMatlab : if debug: print("Stoping Matlab...") mlab.stop() if not sucess: raise RuntimeError(stdout) return outArgs
class MatlabBridgeDriver(MatlabDriver): """MATLAB driver which uses pymatbridge to do IPC with MATLAB.""" # TODO(andrei): Consider reusing MATLAB instances across iterations by # using process-level locals, if something like that exists. def __init__(self): super().__init__() self.matlab = Matlab() # As of July 2016, there seems to be a bug which wrecks the data # dimensionality when feeding it to MATLAB, causing a matrix dimension # mismatch to happen. raise ValueError("MATLAB interop via pymatbridge doesn't work.") def start(self): """Starts MATLAB so that we may send commands to it. Blocks until MATLAB is started and a ZMQ connection to it is established. This is a very sensitive piece of code which can fail due to numerous misconfigurations. For instance, on ETH's Euler cluster, one must ensure that the proper modules are loaded before starting MATLAB, and that the MATLAB one is the first one loaded because of PATH concerns. Getting this to run might not be straightforward, and may require installing 'libzmq', 'pyzmq', and 'pymatbridge' from scratch on Euler. The process has not been tested on regular commodity hardware, such as AWS, but it should be much easier to run there due to the increased access to installing new packages directly via a package manager. TODO(andrei): Write guide for this. TODO(andrei): Maybe have a retry mechanic in case something fails. """ super().start() self.matlab.start() self.matlab.run_code(r'''addpath(genpath('./matlab'))''') def _run_matlab_script(self, script, in_map): super()._run_matlab_script(script, in_map) start_ms = int(time.time() * 1000) logging.info("Have %d variables to set.", len(in_map)) for vn, v in in_map.items(): self.matlab.set_variable(vn, v) logging.info("Set all variables OK.") mlab_res = self.matlab.run_code('rungp_fn') print(mlab_res) if not mlab_res['success']: raise RuntimeError("Could not run MATLAB. Got error message: {0}" .format(mlab_res['content'])) result = self.matlab.get_variable('prob') print(result) # self.matlab.run_func('matlab/rungp_fn.m', # in_map['X'], # in_map['y'], # in_map['X_test']) # script_cmd = '{0} ; '.format(script) # self.matlab.run_code(script_cmd) end_ms = int(time.time() * 1000) time_ms = end_ms - start_ms logging.info("Ran MATLAB code using pymatbridge in %dms.", time_ms) # Dirty trick for testing # exit(-1) return result[:, 0]