def main(): matlab = matlab_wrapper.MatlabSession() print("matlab.version:", matlab.version) print("raw version string from MATLAB workspace:", matlab.workspace.version())
def sample_dual_dpp(L, q, k=None): ''' Wrapper function for the sample_dual_dpp Matlab code written by Alex Kulesza Given a kernel matrix L, returns a sample from a k-DPP. L is the kernel matrix q is the number of used eigenvalues k is the number of elements in the sample from the DPP ''' # Matlab link global mtb if mtb == None: import matlab_wrapper mtb = matlab_wrapper.MatlabSession() # Extract the feature matrix from the kernel evals, evecs = largest_eigsh(L, q, which='LM') B = np.dot(evecs, np.diag(evals)) # load values in Matlab and get sample mtb.put('B', B) if k != None: k = np.array([[k]]) # matlab only undernstand matrices mtb.put('k', k) mtb.eval("dpp_sample = sample_dual_dpp(B,decompose_kernel(B'*B),k)") else: mtb.eval("dpp_sample = sample_dual_dpp(B,decompose_kernel(B'*B))") dpp_sample = mtb.get('dpp_sample') return dpp_sample.astype(int)
def initialize(cmd, cm): if cmd == 'ss': model_dirname = '20160828_SS_train0.8' elif cmd == 'ed': model_dirname = '20160808_EAR0.4.2_train0.8' elif cmd == 'BING': model_dirname = '20160807_BING800_train0.8' else: raise IOError('Wrong cmd name, choose from ss, ed, BING') # configuration for the caffe net prototxt = os.path.join(cfg.ROOT_DIR, 'models', NETS['caffenet'][0], 'test.prototxt') caffemodel = os.path.join(cfg.ROOT_DIR, 'output', model_dirname, 'soton_ear', NETS['caffenet'][cm]) caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(prototxt, caffemodel, caffe.TEST) print '\nLoaded network {:s}'.format(caffemodel) # initialize the MATLAB server print '\nMATLAB Connected' import matlab_wrapper matlab = matlab_wrapper.MatlabSession() print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' return net, matlab
def avg_score(test_dir, gt_dir, metric_name='ssim', smooth=False, sigma=75, verbose=False): """ Read images from two folders and calculate the average score. """ metric_name = metric_name.lower() all_score = [] if metric_name == 'fsim': matlab = matlab_wrapper.MatlabSession() for name in sorted(sorted(os.listdir(gt_dir))): test_img = Image.open(os.path.join(test_dir, name)).convert('L') gt_img = Image.open(os.path.join(gt_dir, name)).convert('L') if smooth: test_img = cv.bilateralFilter(np.array(test_img), 7, sigma, sigma) if metric_name == 'ssim': tmp_score = SSIM(gt_img, test_img) elif metric_name == 'fsim': tmp_score = FSIM(matlab, gt_img, test_img) if verbose: print('Image: {}, Metric: {}, Smooth: {}, Score: {}'.format( name, metric_name, smooth, tmp_score)) all_score.append(tmp_score) return np.mean(np.array(all_score))
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_matlab_engine.log"): os.remove("./logfiles/test_matlab_engine.log") logging.basicConfig(filename="./logfiles/test_matlab_engine.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 500") print("Sampling method: CandidateDYCORS") print("Experimental design: Latin Hypercube") print("Surrogate: Cubic RBF") nthreads = 4 maxeval = 500 data = Ackley(dim=10) print(data.info) # Use the serial controller (uses only one thread) controller = ThreadController() controller.strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nthreads, exp_design=LatinHypercube(dim=data.dim, npts=2*(data.dim+1)), response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim)) print("\nNOTE: You may need to specify the matlab_root keyword in " "order \n to start a MATLAB session using the matlab_wrapper " "module\n") # We need to tell MATLAB where the script is mfile_location = os.getcwd() # Launch the threads for _ in range(nthreads): worker = MatlabWorker(controller) try: worker.matlab = matlab_wrapper.MatlabSession(options='-nojvm') except Exception as err: print("\nERROR: Failed to initialize a MATLAB session.\n") exit() worker.matlab.workspace.addpath(mfile_location) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() # Print the final result print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True)))
def sample_dpp(L, k=None): ''' Wrapper function for the sample_dpp Matlab code written by Alex Kulesza Given a kernel matrix L, returns a sample from a k-DPP. The code is hacked in a way that if a set A is provied, samples from a conditional dpp given A are produced L: kernel matrix k: size of the sample from the DPP set: index of the conditional elements. Integer numpy array containing the locations (starting in zero) relative to the rows of L. ''' # Matlab link global mtb if mtb == None: import matlab_wrapper mtb = matlab_wrapper.MatlabSession() # load values in Matlab and get sample mtb.put('L', L) if k != None: k = np.array([[k]]) # matlab only undenstand matrices mtb.put('k', k) mtb.eval("dpp_sample = sample_dpp(decompose_kernel(L),k)") else: mtb.eval("dpp_sample = sample_dpp(decompose_kernel(L))") #dpp_sample = mtb.getvalue('dpp_sample') dpp_sample = mtb.get('dpp_sample') return dpp_sample.astype(int) - 1 # index start in zero in python
def main(): matlab = matlab_wrapper.MatlabSession() x = np.random.randn(100) matlab.workspace.plot(x) raw_input("Press enter to finish...")
def imResample(img, hs, ws): matlab = matlab_wrapper.MatlabSession() matlab.put('img', img) matlab.put('hs', hs) matlab.put('ws', ws) matlab.eval('script') im_data = matlab.get('im_data') return im_data
def main(): matlab = matlab_wrapper.MatlabSession() matlab.put('x', 2.) matlab.eval('my_script') y = matlab.get('y') print("And the winner is:", y)
def main(): matlab = matlab_wrapper.MatlabSession() matlab.put('x', 2.) matlab.eval('demo') y = matlab.get('y') print("And the winner is:", y) for i in y: print(int(i))
def main(): matlab = matlab_wrapper.MatlabSession() matlab.put('x', 2.) matlab.eval('my_script') # matlab.eval('scenarios/cartPole/cartPole_learn') #doesnt understand directory navigation!!! y = matlab.get('y') a = matlab.get('a') print("And the winner is:", y) print(a)
def main(): matlab = matlab_wrapper.MatlabSession() kaiser = matlab.workspace.kaiser(81., 8.) b = matlab.workspace.fir1(80., 0.5, kaiser) matlab.workspace.freqz(b, 1., nout=0) raw_input("Press enter to finish...")
def main(): p = psutil.Process() i = 0 while True: print(i, p.memory_percent(), p.memory_info()) matlab = matlab_wrapper.MatlabSession() i += 1
def face(self, file): matlab = matlab_wrapper.MatlabSession() matlab.put('filename', file) matlab.put('IMAGE_DIR', IMAGE_DIR) matlab.eval('face') count = matlab.get('count') has_crop = matlab.get('has_crop') return int(count), int(has_crop) == 1
def initialize_mtb(seed): print("starting matlab..."), sys.stdout.flush() import matlab_wrapper matlb = matlab_wrapper.MatlabSession() matlb.put('current_dir', os.path.dirname(os.path.realpath(__file__))) matlb.eval("addpath(current_dir)") if not seed is None: matlb.put('seed', seed) matlb.eval("rng(seed)") print("done!") sys.stdout.flush() return matlb
def __init__(self): print("Loading forest model...") tic() self.forest = utilites.loadVariableFromFile( "static/Corel5K/forest/forest_128.pkl") print("Done.") toc() self.train_vectors = loadmat( utilites.getAbsPath('static/Corel5K/train_vectors_original.mat')) self.train_vectors = self.train_vectors['train_vectors'] self.train_file_path = utilites.loadVariableFromFile( "static/Corel5K/train_file_path.pkl") # load contents of concepts self.concepts = utilites.loadVariableFromFile( "static/Corel5K/cluster_contents.pkl") self.tag_scores = utilites.loadVariableFromFile( "static/Corel5K/all_tags_scores.pkl") self.train_vectors_classic = loadmat( utilites.getAbsPath( 'static/Corel5K/baseline_features/corel5k_train_feats_classic.mat' )) self.train_vectors_classic = self.train_vectors_classic[ 'corel5k_train_feats_classic'] self.test_vectors_classic = loadmat( utilites.getAbsPath( 'static/Corel5K/baseline_features/corel5k_test_feats_classic.mat' )) self.test_vectors_classic = self.test_vectors_classic[ 'corel5k_test_feats_classic'] self.test_file_name = utilites.loadVariableFromFile( 'static/Corel5K/corel5k_test_file_name.pkl') self.feat_dict_classic = dict( zip(self.test_file_name, self.test_vectors_classic)) # start a matlab session for feature extraction self.matlab = matlab_wrapper.MatlabSession( matlab_root="/Applications/MATLAB_R2015b.app") # start matlab self.matlab.eval('run MatConvNet/matlab/vl_setupnn') # basic config self.matlab.eval('run vlfeat/toolbox/vl_setup') ## basic config self.matlab.eval("feature('DefaultCharacterSet', 'UTF8')") print("Loading cnn model...") tic() self.matlab.eval( "net = load('/Users/TONYSUN/Desktop/SIR_Corel5K_demo/static/cnnmodel/imagenet-matconvnet-vgg-verydeep-16.mat')" ) toc() print("Matlab session started.") print("Ready for work ^_^.")
def main(): matlab = matlab_wrapper.MatlabSession() p = psutil.Process() i = 0 while True: print(i, p.memory_percent(), p.memory_info()) a = np.random.randn(1e7) matlab.put('a', a) matlab.get('a') i += 1
def test_nnresample(): """ Compare matlab and nnresample resample : FAILING """ from nnresample import resample from pystoi.stoi import FS import matlab_wrapper matlab = matlab_wrapper.MatlabSession() matlab.put('FS', float(FS)) RTOL = 1e-4 for fs in [8000, 11025, 16000, 22050, 32000, 44100, 48000]: x = np.random.randn(2*fs,) x_r = resample(x, FS, fs) matlab.put('x', x) matlab.put('fs', float(fs)) matlab.eval('x_r = resample(x, FS, fs)') assert_allclose(x_r, matlab.get('x_r'), atol=ATOL, rtol=RTOL)
def initialize_matlab(): matlab = matlab_wrapper.MatlabSession() # edge_detector OP_method matlab.eval("cd('/home/harrysocool/Github/fast-rcnn/OP_methods/edges')") matlab.eval( "addpath(genpath('/home/harrysocool/Github/fast-rcnn/OP_methods/edges'))" ) matlab.eval("toolboxCompile") # # selective_search OP_method # matlab.eval("cd('/home/harrysocool/Github/fast-rcnn/OP_methods/selective_search_ijcv_with_python')") # matlab.eval("addpath(genpath('/home/harrysocool/Github/fast-rcnn/OP_methods/selective_search_ijcv_with_python'))") return matlab
def __init__ ( self,options = '-nosplash', scoperoot = '~/Simulations/Matlab/SCOPE/SCOPE_v1.xx/code/'): import matlab_wrapper # matlab = matlab_wrapper.MatlabSession(options=options,matlab_root='/usr/local/matlab_r2014a/') import socket hostname = socket.gethostname() if hostname=='hephaestus.geog.ucl.ac.uk': matlab_root='/usr/local/matlab_r2014a/' else: matlab_root='/usr/local/matlab_r2016a/' matlab = matlab_wrapper.MatlabSession(options=options,matlab_root=matlab_root) # options=options, buffer_size=buffer_size ###################################################### ############# Initialize SCOPE model ################# ###################################################### # change matlab working directory to SCOPE directory matlab.put('scoperoot',scoperoot) matlab.eval('cd(scoperoot)') # Import default values to run scope matlab.eval('SCOPE') # Translate the Parameter input-structures to arrays in matlab workspace matlab.eval('SCOPE_translate_fwd') # Retrieve values Input Parameters from initial set Names = ['Ta', 'p', 'ea', 'u', 'Rin', 'Rli', 'Cab', 'Cca', 'Cdm', 'Cw', 'Cs', 'N' , 'emissivity_leaf', 'emissivity_soil', 'Vcmo', 'lai', 'hc', 'lw', 'M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'tts','tto','psi'] Input_real = OrderedDict() for varname in Names: Input_real[varname] = matlab.get(varname) # input_parameters = ['Ta', 'p', 'ea', 'u', 'Rin', 'Rli', 'Cab', 'Cca','Cdm', 'Cw', 'Cs', 'N', 'emissivity_leaf', 'emissivity_soil', 'Vcmo', 'lai', 'hc', 'lw'] # default_values = [22.05,993.8/1.0e3, 67.8, 2.3, 440./1.0e3, 381.69/1.0e3, 0.89, 0.99, 0.38, 0.78, 0.60, 1.6, 0.98, 0.95, 30., 0.95, 49.55,0.054] #transformed self.matlab = matlab self.Input_real = Input_real self.Input_transformed = self.forward_transform(copy.deepcopy(Input_real)) self.wl = matlab.get('wlS') self.names = Names
def matlab_fmincon_optimization(ifs, distortion, bounds, verbose=True): matlab = matlab_wrapper.MatlabSession() matlab.put('solver_path', axro_opt_matlab_func) matlab.eval('addpath(solver_path)') matlab.put('ifs', ifs) matlab.put('distortion', fliperroo(distortion)) matlab.put('lb', fliperroo(np.asarray(bounds)[:, 0])) matlab.put('ub', fliperroo(np.asarray(bounds)[:, 1])) matlab.eval('[x,fval,exitflag] = matlab_run_fmincon(ifs,distortion,lb,ub)') optv = matlab.get('x') if verbose is True: print 'Optimization achieved, Merit Function Value: ' + str( matlab.get('fval')) return optv
def main(): matlab = matlab_wrapper.MatlabSession( options='-nojvm', buffer_size=1000, ) matlab.eval("c = {1, 2, 3; 'text', eye(2,3), {11; 22; 33}}") print(matlab.get('c')) matlab.put( 'a', np.array([[1, 'asdf'], [3, np.array([1, 2, 3], dtype='O')]], dtype='O')) matlab.eval('a') print(matlab.output_buffer)
def start_engines(self): '''Starts engines required by non-Python modules in the pipeline. This should be done only once, since engines may have long startup times, which would otherwise slow down the execution of the pipeline. Note ---- For Matlab, you need to set the MATLABPATH environment variable in order to add module dependencies to the Matlab path. Warning ------- Matlab will be started with the ``"-nojvm"`` option. ''' # TODO: JVM for java code languages = [m.language for m in self.pipeline] if 'Matlab' in languages: logger.info('start Matlab engine') try: import matlab_wrapper as matlab except ImportError: raise ImportError('Matlab engine cannot be started, because ' '"matlab-wrapper" package is not installed.') # NOTE: It is absolutely necessary to specify these startup options # for use parallel processing on the cluster. Otherwise some jobs # hang up and get killed due to timeout. startup_ops = '-nosplash -singleCompThread -nojvm -nosoftwareopengl' logger.debug('Matlab startup options: %s', startup_ops) self._engines['Matlab'] = matlab.MatlabSession(options=startup_ops) # We have to make sure that code which may be called by a module, # are actually on the MATLAB path. # To this end, the MATLABPATH environment variable can be used. # However, this only adds the folder specified # by the environment variable, but not its subfolders. To enable # this, we add each directory specified in the environment variable # to the path. matlab_path = os.environ['MATLABPATH'] matlab_path = matlab_path.split(':') for p in matlab_path: if not p: continue logger.debug('add "%s" to MATLABPATH', p) self._engines['Matlab'].eval( 'addpath(genpath(\'{0}\'));'.format(p))
def main(): matlab = matlab_wrapper.MatlabSession( options='-nojvm', buffer_size=1000, ) matlab.eval(""" s = struct() s(1,1).x = 1 s(1,1).y = 'a' s(2,2).x = 2 s(2,2).y = [1,2,3] """) print(matlab.get('s')) matlab.eval(""" s = struct() s(1).x = 1 s(1).y = [1 2] s(2).x = 2 s(2).y = [3 5] """) s = matlab.get('s') print(s, s.dtype) print("=" * 70) a = np.array([(1, 'a'), (2, 'b')], dtype=[('x', '<i8'), ('y', 'S1')]) matlab.put('a', a) matlab.eval('a\n a(1)\n a(2)') print(matlab.output_buffer)
def test_stoi_matlab_resample(): """ Test STOI with any sampling frequency, where Matlab is doing all the resampling. Successful test.""" from pystoi.stoi import FS import matlab_wrapper matlab = matlab_wrapper.MatlabSession() matlab.workspace.cd('matlab/') matlab.put('FS', float(FS)) for fs in [8000, 11025, 16000, 22050, 32000, 44100, 48000]: matlab.put('fs', float(fs)) x = np.random.randn(2*fs,) y = np.random.randn(2*fs, ) matlab.put('x', x) matlab.put('y', y) matlab.eval('x_r = resample(x, FS, fs)') matlab.eval('y_r = resample(y, FS, fs)') x_r = matlab.get('x_r') y_r = matlab.get('y_r') stoi_out = stoi(x_r, y_r, FS) stoi_out_m = matlab.eval('stoi_out_m = stoi(x_r, y_r, FS)') assert_allclose(stoi_out, matlab.get('stoi_out_m'), atol=ATOL, rtol=RTOL)
def main(): matlab = matlab_wrapper.MatlabSession( options='-nojvm', buffer_size=1000, ) print(matlab.version) print() print('=' * 70) print() matlab.eval("a = pi * 2") print(matlab.output_buffer) a = matlab.get('a') print(a) print() print('=' * 70) print() matlab.eval("s = 'asdf'") s = matlab.get('s') print(s) print() print('=' * 70) print() matlab.eval("l = logical([1 1 0])") l = matlab.get('l') print(l) print(matlab.output_buffer) print() print('=' * 70) print() matlab.put('m', 'asdf') m = matlab.get('m') print(m) print() print('=' * 70) print() # matlab.put('m', np.array('asdf')) # m = matlab.get('m') # print(m) # r = np.arange(9) + np.ones(9) * 1j # while True: # matlab.put('r', r) # out = matlab.get('r') # print(out) a = matlab.workspace.sin(np.arange(10.)) print(a) print() print('=' * 70) print() y, i = matlab.workspace.sort([2, 1, 3], nout=2) print(y, i) print() print('=' * 70) print() print(matlab.workspace.pi()) print() print('=' * 70) print() matlab.workspace.a = [123, 12, 1] matlab.eval("b = a*2") print(matlab.workspace.b) #help(matlab.workspace.sin) print() print('=' * 70) print() matlab.eval("whos") print(matlab.output_buffer) print() print('=' * 70) print() print(matlab) print() print('=' * 70) print() matlab.workspace.a = ['asdf', 'strings'] matlab.eval('a') print(matlab.output_buffer)
def _session_hook(self): import matlab_wrapper startup_ops = '-nodesktop -nodisplay' if self._num_threads != 1 else \ '-nodesktop -nodisplay -singleCompThread' return matlab_wrapper.MatlabSession(matlab_root=self._engine_location, options=startup_ops)
print('Processing recorded IR finished!') ################################################################################################ # SIMULATED IRS if compute_simulated: print('Processing simulated IR...') ## Load matlab interface try: import matlab_wrapper except ImportError: print('matlab_wrapper not available') ## Start Matlab Session matlab = matlab_wrapper.MatlabSession(matlab_root=matlab_root) ## Add script into the matlab path matlab.eval('addpath ' + rir_root) ## Pass parameters to matlab matlab.eval('clear') matlab.put('c', float(sound_speed)) matlab.put('procFs', float(recorded_sample_rate)) matlab.put('sphLocation', listener_position) # Listener position matlab.put('s',emitter_position) # Source position matlab.put('L', wall1-wall0) # Room dimensions matlab.put('beta',float(recorded_t60)) matlab.put('sphType',sphType) matlab.put('sphRadius',float(sphRadius)) matlab.put('mic',capsule_positions)
def matlab(): matlab = matlab_wrapper.MatlabSession( options='-nojvm', buffer_size=1024 ) return matlab
matlab.eval('clf(1)') matlab.eval('draw_rollout_cp') # 1. Initialization: cartpole environment in gym env = gym.make('CartPole-v0') ob = env.reset() # print("cartpole-v0 is on!") # for _ in range(10): # env.render() # # x, reward, done, info = env.step(action) # ob, reward, done, _ = env.step(env.action_space.sample()) #random action # 1. Initialization: pilco in matlab N = 2.0 #number controller optimizations matlab = matlab_wrapper.MatlabSession() matlab.eval('clear all') matlab.eval('close all') matlab.workspace.N = N # Override Nr controller optimizations # matlab.put('N', N) # Same as line above # matN = matlab.get('N') # matN = matlab.workspace.N # print("Overriding Nr controller optimizations from here:", matN) # os.system("settings_cp.py") # :( CREATES A SUBSHELL and loses all memory. BETTER WITH FUNCTIONS! # =================================== # settings_cp # As follows: scp.init(matlab) scp.def_state_indices(matlab) scp.def_scenario(matlab) scp.def_plant_st(matlab)