Exemplo n.º 1
0
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)
Exemplo n.º 2
0
    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()
        '''
Exemplo n.º 3
0
        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 'time elapsed:', t
Exemplo n.º 4
0
                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)
                mlab_code = ("coefs = cwt(data, scales, wavelet)")
                res = mlab.run_code(mlab_code)
                if not res['success']:
                    raise RuntimeError(
Exemplo n.º 5
0
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')
print(data.shape)

# That is 300 time points, 30 channels and 100 trials.

# # Calculate covariances

# ## Inspect the `NoiseTools` code

# Here is how the covariance matrices are calculated:

covariances_code = ''.join(example_1_code[22:24])
print_matlab_code(covariances_code)

# Let's see what `nt_cov` does.
Exemplo n.º 6
0
                # Matlab result
                if np.any((wavelet == np.array(['coif6', 'coif7', 'coif8', 'coif9', 'coif10', 'coif11', 'coif12', 'coif13', 'coif14', 'coif15', 'coif16', 'coif17'])),axis=0):
                    mlab.set_variable('Lo_D', w.dec_lo)
                    mlab.set_variable('Hi_D', w.dec_hi)
                    mlab_code = ("[ma, md] = dwt(data, Lo_D, Hi_D, "
                                 "'mode', '%s');" % mmode)
                else:
                    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

                # Matlab result
                mlab.set_variable('Lo_D', w.dec_lo)
                mlab.set_variable('Hi_D', w.dec_hi)
                mlab_code = ("[ma, md] = dwt(data, Lo_D, Hi_D, "
                             "'mode', '%s');" % mmode)
                res = mlab.run_code(mlab_code)
                if not res['success']:
                    raise RuntimeError(
                        "Matlab failed to execute the provided code. "
Exemplo n.º 7
0
#!/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')
Exemplo n.º 8
0
        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 'time elapsed:', t
        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)
                mlab_code = ("coefs = cwt(data, scales, wavelet)")
                res = mlab.run_code(mlab_code)
                if not res['success']:
                    raise RuntimeError(
Exemplo n.º 10
0
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')
    os.rename(EEMD_dir + 'eemd2.m', EEMD_dir + 'eemd.m')
Exemplo n.º 11
0
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')
Exemplo n.º 12
0
            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')
Exemplo n.º 13
0
	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
burnin = 2000  # Note that scientific notation breaks pyjags
nsamps = 10000
Exemplo n.º 14
0
#!/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()
Exemplo n.º 15
0
        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)
Exemplo n.º 16
0
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')
Exemplo n.º 17
0
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
Exemplo n.º 18
0
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]