def fix_model(self,
               W,
               intMat,
               drugMat,
               targetMat,
               num,
               cvs,
               dataset,
               seed=None):
     self.dataset = dataset
     self.num = num
     self.cvs = cvs
     self.seed = seed
     R = W * intMat
     drugMat = (drugMat + drugMat.T) / 2
     targetMat = (targetMat + targetMat.T) / 2
     mlab = Matlab()
     mlab.start()
     # print os.getcwd()
     # self.predictR = mlab.run_func(os.sep.join([os.getcwd(), "kbmf2k", "kbmf.m"]), {'Kx': drugMat, 'Kz': targetMat, 'Y': R, 'R': self.num_factors})['result']
     res = mlab.run_func(
         os.path.realpath(os.sep.join(['kbmf2k', "kbmf.m"])), {
             'Kx': drugMat,
             'Kz': targetMat,
             'Y': R,
             'R': self.num_factors
         })
     self.predictR = res['result']
     # print os.path.realpath(os.sep.join(['../kbmf2k', "kbmf.m"]))
     mlab.stop()
Exemplo n.º 2
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.º 3
0
    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 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'])
Exemplo n.º 5
0
class MatlabCaller:
    """
    bridging class with matlab
    """

    def __init__(self, port=14001, id=None):
        if id is None:
            id = numpy.random.RandomState(1234)

        # Initialise MATLAB
        self.mlab = Matlab(port=port, id=id)

    def start(self):
        # Start the server
        self.mlab.start()

    def stop(self):
        self.mlab.stop()
        os.system('pkill MATLAB')


    def call_fn(self, path=None, params=None):
        """
        path: the path to your .m function
        params: dictionary contains all parameters
        """
        assert path is not None
        assert isinstance(params, dict)

        return self.mlab.run(path, params)['result']
Exemplo n.º 6
0
 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 mc_mat(gym.Env):
    def __init__(self):
        self.mlab = Matlab(matlab='/Applications/MATLAB_R2014a.app/bin/matlab')
        self.mlab.stop()
        self.mlab.start()

        self.action_space = spaces.Discrete(4)
        self.observation_space = spaces.Discrete(4)
        self.round = 2

        self.LAM = round(random.uniform(0.1, 20), 3)
        self.D = round(np.random.uniform(5, 40), 2)
        self.N_n = np.random.randint(5, 20)
        self.N_0 = round(random.uniform(5, 20), 3)
        # self.observation = np.array([self.LAM, self.D, self.N_n, self.N_0])#.reshape(1,4)
        self.observation = (self.LAM, self.D, self.N_n, self.N_0
                            )  #.reshape(1,4)

        self._reset()

    def _step(self, action):
        assert self.action_space.contains(action)

        res = self.mlab.run(
            '/Users/zonemercy/Documents/MATLAB/mc_gym/Copy_of_oraginal.m', {
                'arg1': 1,
                'arg2': self.LAM,
                'arg3': self.D,
                'arg4': self.N_0,
                'arg5': self.N_n
            })

        # esb = res['result']
        esb = [round(elm, self.round) for elm in res['result']]
        result = np.where(esb == np.min(esb))[0]

        if action in result:
            reward = 10
            done = True
        else:
            reward = 0
            done = False

        # return self.observation, reward, done, {"action": action, "result": result,'esb':esb}
        return self.observation, reward, done, {}

    def _reset(self):
        self.LAM = round(random.uniform(0.1, 20), 3)
        self.D = round(np.random.uniform(5, 40), 2)
        self.N_n = np.random.randint(5, 20)
        self.N_0 = round(random.uniform(5, 20), 3)
        # self.observation = np.array([self.LAM, self.D, self.N_n, self.N_0])#.reshape(1,4)
        self.observation = (self.LAM, self.D, self.N_n, self.N_0)

        return self.observation

    def _configure(self, nround=3, close_mat=False):
        self.round = nround
        if close_mat == True:
            self.mlab.stop()
Exemplo n.º 8
0
 def tucker(self, coreNway, lam, init=None):
     # 初始化
     if init is None:
         A0, C0 = self.tucker_init(*coreNway)
     else:
         assert len(init) == 4
         A0 = init[:3]
         C0 = init[-1]
     mlab = Matlab()
     mlab.start()
     while True:
         res = mlab.run_func(
             'tucker.m', self.R, coreNway, {
                 'hosvd': 1,
                 'lam': lam,
                 'maxit': 4000,
                 'A0': A0,
                 'C0': C0,
                 'tol': 1e-5
             })
         if res['success']:
             break
     mlab.stop()
     O, D, T, C = res['result']
     return TuckerResult(self.R, O, D, T, C)
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
Exemplo n.º 10
0
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
Exemplo n.º 11
0
def decision(data):
	image=data.save_to_disk('/home/user1/Downloads/Carla9.5/PythonAPI/my projects/camera_result/%06d.jpg' % data.frame_number);
	print('this is the path to send to matlab: ',image); 
	mlab = Matlab()
	mlab.start()
	overlap = mlab.run_func('parking.m', {'img': image})

	return overlap
Exemplo n.º 12
0
    def convertnctotiff(self):
        self.cancelbtn.config(state="disabled")

        matlabpath = "\"C:/Program Files/MATLAB/R2016b/bin/matlab\""
        mlab = Matlab(executable=matlabpath)

        ncfilefullpath = self.inputncdirtxtfield.get("1.0",
                                                     tk.END).encode("ascii")
        ncfilefullpath = ncfilefullpath[0:-1]
        #ncfilefullpathst = 'D:/NARSS/Research Project/2018-2019/01-01-2020/Task_NC-2-TIFF/input/GRCTellus.JPL.200204_201603.GLO.RL05M_1.MSCNv02CRIv02.nc'
        #print('ncfilefullpathdyn: '+ncfilefullpath, type(ncfilefullpath))

        tiffoutputdir = self.outputtiffdirtxtfield.get("1.0",
                                                       tk.END).encode("ascii")
        tiffoutputdir = tiffoutputdir[0:-1]
        tiffoutputdir = os.path.join(tiffoutputdir, '')
        #tiffoutputdirst = 'D:/NARSS/Research Project/2018-2019/01-01-2020/Task_NC-2-TIFF/output/'
        #print('tiffoutputdir: '+tiffoutputdir, type(tiffoutputdir))

        ncvar = self.ncvartxtfield.get("1.0", tk.END).encode("ascii")
        ncvar = ncvar[0:-1]
        #ncvar = 'lwe_thickness'
        #print('ncvar: '+ncvar, type(ncvar))

        nctimes = self.nctimestxtfield.get("1.0", tk.END)
        nctimes = int(nctimes)
        #nctimes = 152
        #print('nctimes: '+nctimes, type(nctimes))

        mlab.start()

        mlab.run_func(
            'C:/Users/akotb/PycharmProjects/AGP_System/resources/netcdf_to_tiff.m',
            {
                'arg1': ncfilefullpath,
                'arg2': ncvar,
                'arg3': nctimes,
                'arg4': tiffoutputdir
            })

        #self.conversionstartedlbl.grid_forget()
        self.startconvertingnctotiffbtn.grid_forget()
        self.cancelbtn.grid_forget()

        # task status
        self.conversioncompletedlbl = tk.Label(self.master,
                                               text="Conversion Completed")
        self.conversioncompletedlbl.grid(sticky='W',
                                         padx=10,
                                         pady=10,
                                         row=5,
                                         column=1)
        self.closebtn = tk.Button(self.master,
                                  text="Close",
                                  width=15,
                                  command=self.exit)
        self.closebtn.grid(sticky='E', padx=10, pady=10, row=5, column=2)
        mlab.stop()
Exemplo n.º 13
0
def start_mlab():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    dir_list = ['/HHT_MATLAB_package', '/HHT_MATLAB_package/EEMD',
                '/HHT_MATLAB_package/checkIMFs', '/HHT_MATLAB_package/HSA']
    mlab = Matlab()
    mlab.start()
    for d in dir_list:
        res = mlab.run_func('addpath', dir_path + d)
    return mlab
Exemplo n.º 14
0
def network_hill(panel, prior_graph=[], lambdas=[], max_indegree=3, reg_mode='full', stdise=1, silent=0, maxtime=120):
    '''
    run_hill(panel)

    input: dataframe
    should be a T x N dataframe with T time points and N samples.

    output: dict containing key 'e' and key 'i' from Hill's code
    '''
    from scipy.io import savemat
    from scipy.io import loadmat

    # start matlab
    mlab = Matlab(maxtime=maxtime)
    mlab.start()

    # .mat shuttle files
    # add path check
    inPath = os.path.join('..', 'cache', 'dbn_wrapper_in.mat')
    outPath = os.path.join('..', 'cache', 'dbn_wrapper_out.mat')
    
    D = np.transpose(panel.values)
    num_rows = np.shape(D)[0]
    num_cols = np.shape(D)[1]
    D = np.reshape(D, (num_rows, num_cols, 1))
    
    #D = np.transpose(panel, (2,1,0))
    

    # save the matlab object that the DBN wrapper will load
    # contains all the required parameters for the DBN code
    savemat(inPath, {"D" : D,
                     "max_indegree" : max_indegree,
                     "prior_graph" : prior_graph,
                     "lambdas" : lambdas,
                     "reg_mode" : reg_mode,
                     "stdise" : stdise,
                     "silent" : silent})

    # DBN wrapper just needs an input and output path
    args = {"inPath" : inPath, "outPath" : outPath}

    # call DBN code
    res = mlab.run_func('dbn_wrapper.m', args, maxtime=maxtime)

    mlab.stop()

    out = loadmat(outPath)
    
    edge_prob = pd.DataFrame(out['e'], index=panel.columns, columns=panel.columns)
    edge_sign = pd.DataFrame(out['i'], index=panel.columns, columns=panel.columns)
    
    #edge_prob = out['e']
    #edge_sign = out['i']

    return (edge_prob, edge_sign)
Exemplo n.º 15
0
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
Exemplo n.º 16
0
 def fix_model(self, W, intMat, drugMat, targetMat, seed=None):
     R = W*intMat
     drugMat = (drugMat+drugMat.T)/2
     targetMat = (targetMat+targetMat.T)/2
     mlab = Matlab()
     mlab.start()
     # print os.getcwd()
     # self.predictR = mlab.run_func(os.sep.join([os.getcwd(), "kbmf2k", "kbmf.m"]), {'Kx': drugMat, 'Kz': targetMat, 'Y': R, 'R': self.num_factors})['result']
     self.predictR = mlab.run_func(os.path.realpath(os.sep.join(['../kbmf2k', "kbmf.m"])), {'Kx': drugMat, 'Kz': targetMat, 'Y': R, 'R': self.num_factors})['result']
     # print os.path.realpath(os.sep.join(['../kbmf2k', "kbmf.m"]))
     mlab.stop()
Exemplo n.º 17
0
def start_mlab():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    dir_list = [
        '/HHT_MATLAB_package', '/HHT_MATLAB_package/EEMD',
        '/HHT_MATLAB_package/checkIMFs', '/HHT_MATLAB_package/HSA'
    ]
    mlab = Matlab()
    mlab.start()
    for d in dir_list:
        res = mlab.run_func('addpath', dir_path + d)
    return mlab
Exemplo n.º 18
0
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()
Exemplo n.º 19
0
    def mask_to_region(self, path):
        """
		Converts mask image into corresponding regions list

		Arguments
		---------
		path : string
			Path to mask image

		Returns
		-------
		regions : list
			"regions" for the dataset that the mask belongs to
		"""
        def remove_interiors(L, region):
            """
			Removes interior pixels in neuron regions

			Arguments
			---------
			L : 2D numpy array
				Matrix containing labels for each neuron
			region : list
				List of all pixels in a neuron label
			"""
            for pixel in region:
                # Creating grid around pixel
                grid = L[pixel[0] - 1:pixel[0] + 2, pixel[1] - 1:pixel[1] + 2]
                # Removing pixels which are surrounded by similar values
                if np.unique(grid).size == 1:
                    region.remove(pixel)
            return region

        # Initializes Matlab to get labels for neurons using bwboundaries() method
        cwd = os.getcwd()
        matlab_file_path = os.path.join(cwd, 'utils',
                                        'get_region_boundaries.m')
        mlab = Matlab()
        mlab.start()
        matlab_result = mlab.run_func(matlab_file_path, {'arg1': path})
        mlab.stop()
        # `L` is 2D array with each neuron carrying a label 1 -> n
        L = matlab_result['result']
        n = int(L.max())  # Number of neurons
        # Getting coordinates of pixels of neuron regions
        # This includes interior pixels as well
        regions = [{
            "coordinates": list(zip(*np.where(L == float(i))))
        } for i in range(1, n)]
        # Removing interior pixels in neuron regions
        for region in regions:
            remove_interiors(L, region["coordinates"])

        return regions
Exemplo n.º 20
0
def plot_deltahist(fileName1, fileName2):

    prog = find_executable('matlab')
    if not prog:
        sys.exit("Could not find MATLAB on the PATH. Exiting...")
    else:
        print("Found MATLAB in "+prog) 

    mlab = Matlab(executable=prog)
    mlab.start()
    results = mlab.run_func('./plot_deltahist.m', {'arg1': fileName1, 'arg2': fileName2})
    mlab.stop()
Exemplo n.º 21
0
def start_matlab():

    prog = find_executable('matlab')
    if not prog:
        sys.exit("Could not find MATLAB on the PATH. Exiting...")
    else:
        print("Found MATLAB in "+prog) 

    mlab = Matlab(executable=prog)
    mlab.start()

    return mlab
    def fix_model(self, W, intMat, drugMat, targetMat,num, cvs, dataset, seed=None):
        self.dataset = dataset
        self.num = num
        self.cvs = cvs
        self.seed = seed
        R = W * intMat
        #R = np.transpose(R)
        drugMat = (drugMat + drugMat.T) / 2
        targetMat = (targetMat + targetMat.T) / 2
        mlab = Matlab()
        mlab.start()
        res = mlab.run_func(os.path.realpath(os.sep.join(['GRMF',"runGRMF.m"])),{'Kx': drugMat, 'Kz': targetMat, 'Y': R, 'cv': self.cv})
        self.predictR = res['result']

        mlab.stop()
        1+1
Exemplo n.º 23
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.º 24
0
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
Exemplo n.º 25
0
 def fix_model(self, W, intMat, drugMat, targetMat, seed=None):
     R = W * intMat
     drugMat = (drugMat + drugMat.T) / 2
     targetMat = (targetMat + targetMat.T) / 2
     mlab = Matlab('/Applications/MATLAB_R2014b.app/bin/matlab')
     mlab.start()
     # print os.getcwd()
     # self.predictR = mlab.run_func(os.sep.join([os.getcwd(), "kbmf2k", "kbmf.m"]), {'Kx': drugMat, 'Kz': targetMat, 'Y': R, 'R': self.num_factors})['result']
     self.predictR = mlab.run_func(
         os.path.realpath(os.sep.join(['./kbmf2k', "kbmf.m"])), {
             'Kx': drugMat,
             'Kz': targetMat,
             'Y': R,
             'R': self.num_factors
         })['result']
     # print os.path.realpath(os.sep.join(['./kbmf2k', "kbmf.m"]))
     mlab.stop()
Exemplo n.º 26
0
def call_peak_picking(filepath, scriptpath):
    """ Method connects to local Matlab via ZeroMQ and calls peak picking function there implemented in script path. """

    start_time = time.time()

    mlab = Matlab(
        executable='/Applications/MATLAB_R2018a_floating.app/bin/matlab')

    mlab.start()
    response = mlab.run_func(scriptpath, {'path': filepath})
    mlab.stop()

    peaks = response['result']

    print(time.time() - start_time, " seconds elapsed for peak picking")

    print("Total number of peaks", len(peaks))

    return peaks
Exemplo n.º 27
0
    def objective_function(self, x):
        '''
        matlabpath: we need the path to matlab since we need to run it.
        
        IMPORTANT: walker_simulation.m must be included in the 
        WGCCM_three_link_walker_example file in order to work. 
        So simply modify the path below.
        '''
		mlab = Matlab(executable= matlabpath)
		mlab.start()
		output = mlab.run_func(os.path.join(self.matlab_code_directory, 'walker_simulation.m'), 
						   {'arg1': x[:, 0],'arg2': x[:, 1],'arg3': x[:, 2],
						   'arg4': x[:, 3],'arg5':x[:, 4],'arg6': x[:, 5],
						   'arg7': x[:, 6],'arg8': x[:, 7],'arg9': self.num_steps})
						   

		answer = output['result']
		simul_output = answer['speed']
		mlab.stop()
Exemplo n.º 28
0
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()))
Exemplo n.º 29
0
    def evaluation(self, test_data, test_label):

        mlab = Matlab()
        mlab.start()
        res = mlab.run_func(
            os.path.realpath(os.sep.join([os.getcwd(), "pudt", "PUDT.m"])), {
                'w': self.w,
                'dataset': self.dataset
            })
        self.predictR = res['result']

        mlab.stop()
        # score = self.predictR[test_data[:, 0], test_data[:, 1]]
        score = self.predictR
        import pandas as pd
        score = pd.DataFrame(score)
        score.to_csv('../data/datasets/EnsambleDTI/pudt_' + str(self.dataset) +
                     '_s' + str(self.cvs) + '_' + str(self.seed) + '_' +
                     str(self.num) + '.csv',
                     index=False)
        prec, rec, thr = precision_recall_curve(np.array(score['test_label']),
                                                np.array(score['score']))
        aupr_val = auc(rec, prec)
        # plt.step(rec, prec, color='b', alpha=0.2,
        #          where='post')
        # plt.fill_between(rec, prec, step='post', alpha=0.2,
        #                  color='b')
        #
        # plt.xlabel('Recall')
        # plt.ylabel('Precision')
        # plt.ylim([0.0, 1.05])
        # plt.xlim([0.0, 1.0])
        # plt.title('2-class Precision-Recall curve: AP={0:0.2f}')
        fpr, tpr, thr = roc_curve(np.array(score['test_label']),
                                  np.array(score['score']))
        auc_val = auc(fpr, tpr)
        print("AUPR: " + str(aupr_val) + ", AUC: " + str(auc_val))

        return aupr_val, auc_val
Exemplo n.º 30
0
class cvWorker (threading.Thread):

    def __init__(self, worker_id, mlab_path, socket_prefix, id_prefix):
        threading.Thread.__init__(self)
        self.redis_obj = redis.Redis()
        self.worker_id = worker_id

        #Create a matlab instance; ie one matlab instance is created per worker
        cv_socket = socket_prefix + worker_id
        mlab_id = id_prefix + worker_id
        self.mlab_inst = Matlab(matlab=mlab_path, socket_addr=cv_socket, id=mlab_id)
        self.mlab_inst.start()

        time.sleep(2)
        self.createHash()

    def createHash(self):
        self.redis_obj.hmset('cloudcv_worker:'+self.worker_id, {'status' : 'Started'})

    def run(self):
        # Keep polling the redis queue for jobs
        # If present, execute it else keep polling till instructed to stop
        loop = 1
        self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Running')

        while loop == 1:
            json_args = self.redis_obj.lpop('cloudcv_jobs')
            if json_args is not None:
                parsed_dict = json.loads(json_args)
                re.run(parsed_dict, self.mlab_inst)
                print json_args
            

            '''
            if task_id is not None:
                # Set status to "Executing Job<ID> @ Time" and execute the task
                json_args = self.redis_obj.get('cloudcv:'+task_id+':args')
                exec_path = self.redis_obj.get('cloudcv:'+task_id+':exec')
                ## HAVE TO CLEAN UP REDIS AFTER THIS
                task_args = json.loads(json_args)
                #task_args = {'imagePath': '/home/mclint/pythonbridge/testin','outputPath': '/home/mclint/pythonbridge/testout','featList': 'lbp','verbosity': '2' }
                ## HAVE TO ADD TIME
                time_str = time.asctime(time.localtime(time.time()))
                self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Executing Job: ' + task_id + ' at ' + time_str)
                self.redis_obj.set('cloudcv:'+task_id+':status', 'Executing on Worker: ' + self.worker_id + ' at ' + time_str)

                res = self.mlab_inst.run_func(exec_path, task_args)
                print res

                self.redis_obj.set('cloudcv:'+task_id+':result', res)
                ## HAVE TO ADD TIME
                time_str = time.asctime(time.localtime(time.time()))
                if res['result'] == 0:
                    # Set status to "Completed Job<ID> @ Time"
                    print task_id + ' Completed Successfully by Worker: ' + self.worker_id + ' at ' + time_str
                    self.redis_obj.set('cloudcv:'+task_id+':status', 'Completed by Worker: ' + self.worker_id + ' at ' + time_str)
                    self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Completed Job: ' + task_id + ' at ' + time_str)
                else:
                    # Set status to "FAILED Job<ID> @ Time"
                    print task_id + ' FAILED - worker: ' + self.worker_id + ' at ' + time_str
                    self.redis_obj.set('cloudcv:'+task_id+':status', 'Job FAILED - Worker: ' + self.worker_id + ' at ' + time_str)
                    self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Job FAILED: ' + task_id + ' at ' + time_str)
            '''
            if self.redis_obj.get('cloudcv:stop') != 'False':
                loop = 0
                self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Stopping Matlab')
                print 'Quit command issued'
            time.sleep(0.1)
        # Go to terminate here
        self.terminate()

    def terminate(self):
        # Stop Matlab instance and set status
        self.mlab_inst.stop()
        self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Stoped Matlab')
        self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Exiting Thread')
Exemplo n.º 31
0
def main():
    #start matlab connection
    mlab = Matlab()
    mlab.start()
    vehicle = None
    data=None
    vehicle1=None
    vehicle2=None
    vehicle3=None
    vehicle4=None
    vehicle5=None
    actor_list = []
    sensors = []

   
    argparser = argparse.ArgumentParser(
        description=__doc__)
    argparser.add_argument(
        '--host',
        metavar='H',
        default='127.0.0.1',
        help='IP of the host server (default: 127.0.0.1)')
    argparser.add_argument(
        '-p', '--port',
        metavar='P',
        default=2000,
        type=int,
        help='TCP port to listen to (default: 2000)')
    args = argparser.parse_args()
    
    try:
       client = carla.Client(args.host, args.port)

       client.set_timeout(2.0)
       

       world = client.get_world()
       ourMap = world.get_map()
       debug = world.debug;
#********* setting fixed time stamp for simulation **************
       settings = world.get_settings();
       settings.fixed_delta_seconds = 0.05;
       world.apply_settings(settings);
       spectator = world.get_spectator();

#*************** deffinition of sensors ****************************
       
       camera_blueprint = world.get_blueprint_library().find('sensor.camera.rgb')
      
#*********** definition of blueprints for vehicles *******************
       blueprint = world.get_blueprint_library().find('vehicle.audi.tt') #for main(ego) vehicle
       blueprint.set_attribute('role_name', 'hero')
#*********** for non-player vehicles *********************************
       non_playerBlueprint1 = random.choice(world.get_blueprint_library().filter('vehicle.bmw.grandtourer'))
       non_playerBlueprint2 = random.choice(world.get_blueprint_library().filter('vehicle.tesla.*'))
       non_playerBlueprint4 = random.choice(world.get_blueprint_library().filter('vehicle.mercedes-benz.coupe'))
       non_playerBlueprint3 = random.choice(world.get_blueprint_library().filter('vehicle.toyota.*'))


#******************************* set weather **********************************
       weather = carla.WeatherParameters(
        cloudyness=0.0,
        precipitation=0.0,
        sun_altitude_angle=70.0,
        sun_azimuth_angle=50.0)

       world.set_weather(weather)


#************************ spawn non-player vehicles ******************************

       class spawn_vehicle:
        def __init__(self, blueprint, x, y):
            self.vehicle=None;
            spawn_points = ourMap.get_spawn_points()
            spawn_point = spawn_points[30];
            spawn_point.location.x += x;
            spawn_point.location.y += y;
            self.vehicle = world.spawn_actor(blueprint, spawn_point)

     
       


       #first non-player vehicle
       data=spawn_vehicle(non_playerBlueprint1, -20, 1.0);
       vehicle1=data.vehicle;
       actor_list.append(vehicle1)


       #second non-player vehicle
       data=spawn_vehicle(non_playerBlueprint2, -12, 1.0);
       vehicle2=data.vehicle;
       actor_list.append(vehicle2)

       # 3rd non-player vehicle
       data=spawn_vehicle(non_playerBlueprint3, 2.0, 1.0);
       vehicle3=data.vehicle;
       actor_list.append(vehicle3)

       #4th nonplayer vehicle
       data=spawn_vehicle(non_playerBlueprint4, 10.0, 1.0);
       vehicle4=data.vehicle;
       actor_list.append(vehicle4)
      


       #********************** spawn main vehicle *****************************
       data=spawn_vehicle(blueprint, -23.0, -1.5);
       vehicle=data.vehicle;
       actor_list.append(vehicle)
       #set the first movement of ego car
       control = carla.VehicleControl(throttle=0.1)
       vehicle.apply_control(control)
      
       
       #************************ camera-sensor settings ***********************
       camera_location = carla.Transform(carla.Location(x=1.3, z=2.0))
       camera_blueprint.set_attribute('sensor_tick', '0.4');
       camera_sensor = world.spawn_actor(camera_blueprint, camera_location, attach_to=vehicle);


       #=======================================obstacle sensors for maneuver==============================================================


       class ObstacleSensor(object):
        def __init__(self, parent_actor,x,y,z,angle):
          self.sensor = None
          self._parent = parent_actor
          self.actor = 0.0
          self.distance = 0.0
          self.x=x
          self.y=y
          self.z=z
          self.angle=angle;
          world = self._parent.get_world()
          bp = world.get_blueprint_library().find('sensor.other.obstacle')
          bp.set_attribute('debug_linetrace', 'false');
          self.sensor = world.spawn_actor(bp, carla.Transform(carla.Location(x=self.x, y=self.y, z=self.z), carla.Rotation(yaw=self.angle)), attach_to=self._parent);
          sensors.append(self.sensor);
          weak_self = weakref.ref(self)
          self.sensor.listen(lambda event: ObstacleSensor._on_event(weak_self, event))

        @staticmethod
        def _on_event(weak_self, event):
         self = weak_self()
         if not self:
            return
         self.actorId = event.other_actor.id
         self.actorName = event.other_actor.type_id
         self.distance = event.distance



       forward_sensor = ObstacleSensor(vehicle, x=1.3, y=0.9, z=0.6, angle=90);
       rearSide_sensor = ObstacleSensor(vehicle, x=-1.3, y=0.9, z=0.6, angle=90);
       center_sensor = ObstacleSensor(vehicle, x=0.0, y=0.9, z=0.6, angle=90);
       back_sensor = ObstacleSensor(vehicle, x=-1.3, y=0.0, z=0.6, angle=180);
       
      
  


       #====================================step1:Parking Decision==========================================================
       global image;
       time.sleep(5.0);
       textLocation = vehicle.get_location();
       textLocation.x +=5.0;
       textLocation.y -=0.8;
       textLocation.z +=1.8;
       startText = "Search for parking place"
       debug.draw_string(textLocation, startText, True, carla.Color(255,255,0), 3, True);
       textLocation.y +=1.0;
       debug.draw_box(carla.BoundingBox(textLocation,carla.Vector3D(0.2,1.5,0.5)),carla.Rotation(yaw=180), 0.1, carla.Color(255,0,0),3, True);
       camera_sensor.listen(lambda image: parking_decision(image));


       
       def parking_decision(data):
        camera_sensor.stop();
        cameraControl = carla.VehicleControl(throttle=0);
        vehicle.apply_control(cameraControl);
        output=interface.decision(data,mlab);
        overlapRatio = output['result'];
        print('rate of Overlap:', overlapRatio);
        if overlapRatio < 0.1:
          print('parking place is between the next 2 vehicles');
          location = vehicle.get_location();
          location.x +=15;
          location.y +=1.0;
          location.z +=1.8;
          text = 'Parking vacancy detected';
          debug.draw_string(location, text, True, carla.Color(255,255,0), 4.0, True);
          location.y +=1.5;
          debug.draw_box(carla.BoundingBox(location,carla.Vector3D(0.2,2.0,0.8)),carla.Rotation(yaw=180), 0.1, carla.Color(255,0,0),4.0, True);
          time.sleep(3.0);
          mlab.end();
          if hasattr(center_sensor, 'actorId'):
            print('center_sensor info in detection time:', center_sensor.actorName, center_sensor.actorId);
            if center_sensor.actorId != 0:
              number=2;
            else:
              number=1;
          print('matlab disconnected');
          parking_preparation(number);
        else:
          print('no parking place has been detected right now');
          cameraControl = carla.VehicleControl(throttle=0.2);
          vehicle.apply_control(cameraControl);
          print('car is moving');
          camera_sensor.listen(lambda image: parking_decision(image));

  
      

       #==================================step2:Parking Preparation(position phase)=============================================
       def parking_preparation(limit):
        print('limit',limit);
        x1=x2=0;
        vehicle.apply_control(carla.VehicleControl(throttle=0.3));
        while config.count < limit:
          current_vhcl = center_sensor.actorId;
          if config.nonplayer != current_vhcl and current_vhcl != 0:
            config.nonplayer = current_vhcl;
            config.count += 1;
            continue;
        
        else:
          first_parked_car = forward_sensor.actorId;
          print('first_parked_car', first_parked_car);
          while config.count == limit:
            if forward_sensor.actorId == 0:
                  print('first_parked_car passed');
                  config.parkingWidth = forward_sensor.distance; #lateral distance of the parking bay
                  x1 = vehicle.get_location().x; #vehicle first place in parking bay
                  x1 -= 1.3; #because this location is from center of the car
                  print('parkingWidth:', config.parkingWidth, 'x1:', x1);
                  break;
          while forward_sensor.actorId == 0:
            continue;

          else:
            if forward_sensor.actorId != 0 and forward_sensor.actorId != first_parked_car:
                  print('we reached second static car:', forward_sensor.actorId);
                  print('second_parked_car', forward_sensor.actorId);
                  x2 = vehicle.get_location().x; #vehicle second place in parking bay
                  x2 += 1.3; 
                  config.parkingLength = abs(x2 - x1); #length of the parking
                  print('parkingLength:', config.parkingLength, 'x2:', x2);
                  while rearSide_sensor.distance > 1.0:
                    vehicle.apply_control(carla.VehicleControl(steer=0.0, throttle=0.2));
                    time.sleep(1.0);
                    if rearSide_sensor.actorId != 0: #when the static vehicle detected
                      vehicle.apply_control(carla.VehicleControl(steer=0.0, throttle=0.0, brake=1.0));
                      parking_maneuver();
                      break;
                  else:
                    vehicle.apply_control(carla.VehicleControl(steer=0.0, throttle=0.0, brake=1.0));
                    time.sleep(3.0);
                    parking_maneuver();


       #====================================step3:Parking Maneuver=================================================================
       def parking_maneuver():
        time.sleep(3.0);
        maneuver.calculate_maneuverTime(vehicle);
        maneuver.calculate_max_steeringAng(vehicle);
        time.sleep(3.0);
        for t in numpy.arange(0,config.T,config.sampling_period):
          time.sleep(0.08);
          if (hasattr(back_sensor, 'actorId') and (back_sensor.actorId != 0) and (back_sensor.distance < 4)): #or (hasattr(rearSide_sensor, 'actorId') and (rearSide_sensor.actorId == 0) and (rearSide_sensor.distance < 5.0)):
            print('1****vehicle should be stopped!', back_sensor.distance);
            vehicle.apply_control(carla.VehicleControl(throttle=0, steer=0.0, brake=1.0));
            control=vehicle.get_control();
            print('throttle', control.throttle, 'brake', control.brake, 'steer', control.steer)
          else:
            print('2****move');
            maneuver.parking(t,vehicle); #call parking function from maneuver.py
            control=vehicle.get_control();
            print('throttle', control.throttle, 'brake', control.brake, 'steer', control.steer)
        

       

       #set simulator view to the location of the vehicle
       while True:
        time.sleep(1.0)
        viewLocation = vehicle.get_location();
        viewLocation.z += 1.0;
        spectator.set_transform(get_transform(viewLocation, -180))



    finally:
      print('\ndestroying %d actors' % len(actor_list))
      for actor in actor_list:
        if actor is not None: 
          actor.destroy()
      for sensor in sensors:
        if sensor is not None:
          sensor.destroy()

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'
Exemplo n.º 33
0
class DataPreprocess:
    def __init__(self):
        file = xlrd.open_workbook('excel/0.xls')
        table = file.sheets()[sheet]  #通过索引顺序获取工作表
        nrows = table.nrows  #行数
        self.mlab = Matlab()
        self.mlab.start()
        # 该文件无用,只是为了避免一个路径引起的bug
        res = self.mlab.run_func(
            'C:/Users/ovewa/Desktop/git-storage/OS-ELM-matlab/test.m', 1)
        self.ditu = []
        for i in range(1, nrows):
            self.ditu.append(table.row_values(i)[1:])

    # 生成中间数据
    def get_median(self):
        for j in range(0, 20):
            file = xlrd.open_workbook('excel/' + str(j) + '.xls')
            table = file.sheets()[sheet]  #通过索引顺序获取工作表
            nrows = table.nrows  #行数
            data = []
            for i in range(1, nrows):
                data.append(table.row_values(i)[1:])
            if os.path.isdir('middata' + str(sheet) + '/'):
                pass
            else:
                os.mkdir('middata' + str(sheet) + '/')
            with open('middata' + str(sheet) + '/' + str(j) + '.txt',
                      'wt') as f:
                for y in range(len(data)):
                    for x in range(len(data[0])):
                        if int(data[y][x]) != 0 and int(self.ditu[y][x]) != 0:
                            f.write(
                                str(data[y][x] / self.ditu[y][x] - 1) + " " +
                                str(x) + " " + str(y) + "\n")

    def get_none(self):
        with open('middata' + str(sheet) + '/none.txt', 'wt') as f:
            for y in range(13):
                for x in range(10):
                    f.write(str(0) + " " + str(x) + " " + str(y) + "\n")

    # 训练过程
    # 可优化
    # 1是训练条件不同结果不同
    # 2是随机参数不同结果不同
    # 多次训练取最优解
    def training(self, model_num):
        # 初始训练
        res = self.mlab.run_func('OSELM_initial_training.m',
                                 'middata' + str(sheet) + '/0.txt',
                                 10,
                                 'sin',
                                 nargout=5)
        IW = res['result'][0]
        Bias = res['result'][1]
        M = res['result'][2]
        beta = res['result'][3]
        # 增量学习
        # ####!!!添加将 每一次增量学习结果的误差输出出来并可视化
        for x in range(1, model_num + 1):
            res = self.mlab.run_func('OSELM_increase_study.m',
                                     'middata' + str(sheet) + '/' + str(x) +
                                     '.txt',
                                     IW,
                                     Bias,
                                     M,
                                     beta,
                                     'sin',
                                     1,
                                     nargout=4)
            IW = res['result'][0]
            Bias = res['result'][1]
            M = res['result'][2]
            beta = res['result'][3]
        # 获取完整指纹库
        res = self.mlab.run_func('OSELM_test_value.m',
                                 'middata' + str(sheet) + '/none.txt', IW,
                                 Bias, beta, 'sin')
        result = res['result']

        y = 0
        data = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

        for x in range(len(result)):
            ###重点,现在直接将初次训练结果保存,没有进行比较分析,待完善
            data[x % 10][y] = int(self.ditu[y][x % 10] * (result[x] + 1))
            if (x + 1) % 10 == 0:
                y = y + 1
        return data

    def mlab_stop(self):
        self.mlab.stop()
Exemplo n.º 34
0
def print_matlab_script(path):
    with open(path, 'r') as f:
        code = f.read()
    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])
Exemplo n.º 35
0
def custom_filter(source, destination):
	mlab = Matlab(matlab='/usr/local/MATLAB/R2013a/bin/matlab')
	mlab.start()
	res = mlab.run_func('./lineDect.m', {'arg1': source, 'arg2':destination})
Exemplo n.º 36
0
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()
Exemplo n.º 37
0
"""

__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'
Exemplo n.º 38
0
class cvWorker(threading.Thread):
    def __init__(self, worker_id, mlab_path, socket_prefix, id_prefix):
        threading.Thread.__init__(self)
        self.redis_obj = redis.Redis()
        self.worker_id = worker_id

        #Create a matlab instance; ie one matlab instance is created per worker
        cv_socket = socket_prefix + worker_id
        mlab_id = id_prefix + worker_id
        self.mlab_inst = Matlab(matlab=mlab_path,
                                socket_addr=cv_socket,
                                id=mlab_id)
        self.mlab_inst.start()

        time.sleep(2)
        self.createHash()

    def createHash(self):
        self.redis_obj.hmset('cloudcv_worker:' + self.worker_id,
                             {'status': 'Started'})

    def run(self):
        # Keep polling the redis queue for jobs
        # If present, execute it else keep polling till instructed to stop
        loop = 1
        self.redis_obj.rpush('cloudcv:' + self.worker_id + ':status',
                             'Running')

        while loop == 1:
            json_args = self.redis_obj.lpop('cloudcv_jobs')
            if json_args is not None:
                parsed_dict = json.loads(json_args)
                re.run(parsed_dict, self.mlab_inst)
                print json_args
            '''
            if task_id is not None:
                # Set status to "Executing Job<ID> @ Time" and execute the task
                json_args = self.redis_obj.get('cloudcv:'+task_id+':args')
                exec_path = self.redis_obj.get('cloudcv:'+task_id+':exec')
                ## HAVE TO CLEAN UP REDIS AFTER THIS
                task_args = json.loads(json_args)
                #task_args = {'imagePath': '/home/mclint/pythonbridge/testin','outputPath': '/home/mclint/pythonbridge/testout','featList': 'lbp','verbosity': '2' }
                ## HAVE TO ADD TIME
                time_str = time.asctime(time.localtime(time.time()))
                self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Executing Job: ' + task_id + ' at ' + time_str)
                self.redis_obj.set('cloudcv:'+task_id+':status', 'Executing on Worker: ' + self.worker_id + ' at ' + time_str)

                res = self.mlab_inst.run_func(exec_path, task_args)
                print res

                self.redis_obj.set('cloudcv:'+task_id+':result', res)
                ## HAVE TO ADD TIME
                time_str = time.asctime(time.localtime(time.time()))
                if res['result'] == 0:
                    # Set status to "Completed Job<ID> @ Time"
                    print task_id + ' Completed Successfully by Worker: ' + self.worker_id + ' at ' + time_str
                    self.redis_obj.set('cloudcv:'+task_id+':status', 'Completed by Worker: ' + self.worker_id + ' at ' + time_str)
                    self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Completed Job: ' + task_id + ' at ' + time_str)
                else:
                    # Set status to "FAILED Job<ID> @ Time"
                    print task_id + ' FAILED - worker: ' + self.worker_id + ' at ' + time_str
                    self.redis_obj.set('cloudcv:'+task_id+':status', 'Job FAILED - Worker: ' + self.worker_id + ' at ' + time_str)
                    self.redis_obj.rpush('cloudcv:'+self.worker_id+':status', 'Job FAILED: ' + task_id + ' at ' + time_str)
            '''
            if self.redis_obj.get('cloudcv:stop') != 'False':
                loop = 0
                self.redis_obj.rpush('cloudcv:' + self.worker_id + ':status',
                                     'Stopping Matlab')
                print 'Quit command issued'
            time.sleep(0.1)
        # Go to terminate here
        self.terminate()

    def terminate(self):
        # Stop Matlab instance and set status
        self.mlab_inst.stop()
        self.redis_obj.rpush('cloudcv:' + self.worker_id + ':status',
                             'Stoped Matlab')
        self.redis_obj.rpush('cloudcv:' + self.worker_id + ':status',
                             'Exiting Thread')
Exemplo n.º 39
0
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()
Exemplo n.º 40
0
 def process(self):
     mlab = Matlab()
     mlab.start()
     return mlab
Exemplo n.º 41
0
# Apply conjugate operator to features
if enc_cfg['conj_inputs']:
    y_test = np.matmul(np.conj(np.swapaxes(h_test, -2, -1)), y_test[..., None])
    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(
Exemplo n.º 42
0
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()
Exemplo n.º 43
0
def backtest(filename, data, schedule_data):
  f = open(filename)
  f.readline()
  player_data = {}
  time_data = []
  for i in xrange(50):
    line = f.readline()
    if line is None or len(line) == 0:
      break
    date = int(line[:3])
    print date
    jsonvalue = "{"+f.readline()+"}"
    value = json.loads(jsonvalue)
    time_data.insert(0,(date,value))
    for p in value:
      if not p in player_data:
        player_data[p] = [0]
      player_data[p].insert(0,value[p])

  time_data2 = convertToPlayersTimeData(time_data, data)

  teams = set([i.team for i in data])

  for i in xrange(len(time_data2)):
    stamp_data = time_data2[i][1]
  Tracer()()
  portfolio = ["rohit sharma", "ajinkya rahane", "david warner", "glenn maxwell", "robin uthappa", "shane watson", "sandeep sharma", "sunil narine", "pravin tambe", "yuzvendra chahal", "bhuvneshwar kumar"]
  # portfolio = ["yuzvendra chahal", "shakib al hasan", "shane watson", "rohit sharma", "sandeep sharma", "sunil narine", "ajinkya rahane", "jacques kallis", "robin uthappa", "jayant yadav","bhuvneshwar kumar"]
  # portfolio = ["manish pandey", "rohit sharma","jacques kallis","robin uthappa", "aditya tare", "ambati rayudu", "morne morkel","piyush chawla","sunil narine","lasith malinga","pragyan ojha"]
  power_player = "glenn maxwell"
  # power_player = "bhuvneshwar kumar"
  portfolio_p = set([getPlayer(data, p)[0] for p in portfolio])
  power_player_p = getPlayer(data, power_player)[0]
  points = 0
  subs = 75

  mlab = Matlab(matlab='/Applications/MATLAB_R2013a.app/bin/matlab')
  mlab.start()
  for i in xrange(4,len(time_data2)):
    # START = str(time_data2[i][0])
    # CURRENT_TEAM = set(portfolio)
    # SUBSTITUTIONS = subs
    # PAST_STATS = time_data[i][1]
    print "\n\n\n\n\n\n"
    print (subs, str(time_data2[i][0]))
    print set(portfolio_p)
    print points
    print "\n\n\n\n\n\n"
    # print time_data[i-1][1]
    # Tracer()()

    inp = (subs, str(time_data2[i][0]), set(portfolio), time_data[i-1][1])
    backtest_pickteam.pickTeam(data, schedule_data, inp)



    res = mlab.run_func('/Users/deedy/Dev/FantasyIPL-Moneyball/python2matlab.m', {}, maxtime = 500)
    changes = backtest_results.getResults(data, schedule_data, res['result'])
    subs -= changes[2]
    portfolio_p = changes[0]
    power_player_p = changes[1]
    # Tracer()()
    # update portfolio
    # update subs
    # update power player
    # Tracer()()

    teams = [(p,time_data2[i][1][p] - time_data2[i-1][1][p] ) for p in time_data2[i][1] if p in portfolio_p]
    print teams
    pthis = 0
    for i in teams:
      if power_player_p == i[0]:
        pthis += 2*i[1]
      else:
        pthis += i[1]
    points+= pthis
    print "{0}\t{1}\t{2}\n\n".format(points, pthis, subs)


    # print "{0}\t{1}".format(time_data2[i][0] , teams)

  mlab.stop()
  Tracer()()
  f.close()
def readSUN3D_singleData(list_dir, threshold_PCE, threshold_camDist,
                         threshold_overlap, threshold_iter, choose_secondFrame,
                         output):
    mlab = Matlab(executable='/usr/bin/matlab')
    mlab.start()
    dataDir = list_dir[random.randint(0, len(list_dir) - 1)]
    print(dataDir)

    minFrame = 1
    maxFrame = len(glob(dataDir + 'image/*'))
    frameLength = maxFrame

    #print (minFrame,maxFrame)
    frameID = random.randint(minFrame, maxFrame)
    frameIDAnother = -1
    count = 1
    while (True):

        if (frameID == frameIDAnother):
            frameID = random.randint(1, frameLength)
        else:
            while (True):
                frameIDAnother = random.randint(
                    max(minFrame, frameID - choose_secondFrame),
                    min(maxFrame, frameID + choose_secondFrame))
                if (frameID != frameIDAnother):
                    break

            output_tmp = dataLoad_SUN3D(mlab, dataDir, frameID, frameIDAnother,
                                        '', './SUN3Dflow_py.m', False)

            #print(output_tmp['camDist'], output_tmp['pce'] ,output_tmp['overlapRatio'])
            if (output_tmp['camDist'] > threshold_camDist
                    and output_tmp['pce'] < threshold_PCE
                    and output_tmp['overlapRatio'] > threshold_overlap
                    and output_tmp['overlapRatio'] < 1):
                """
                egomotion_extended = np.zeros(7)

                egomotion_tmp = output_tmp['egomotion']

                scale_tmp = np.linalg.norm(egomotion_tmp[0:3])
                egomotion_tmp[0:3] = egomotion_tmp[0:3]/scale_tmp

                egomotion_extended[0:6] = egomotion_tmp
                egomotion_extended[6] = scale_tmp

                output_tmp['egomotion'] = egomotion_extended
                """
                print('finished')

                output.put(output_tmp)

                break
                """
                scale_tmp = np.linalg.norm(egomotion_tmp[0:3])
                target_egomotion[iterBatch,0:3] = egomotion_tmp[0:3]/scale_tmp
                target_egomotion[iterBatch,3:3] = egomotion_tmp[3:3]
                target_egomotion[iterBatch,6] = scale_tmp
                """

            else:
                # print(count)
                count = count + 1
                if (output_tmp['overlapRatio'] <= threshold_overlap):
                    minFrame = min(frameID, frameIDAnother)
                    maxFrame = max(frameID, frameIDAnother)

                if (count == threshold_iter):
                    count = 1
                    frameIDAnother = frameID
                    minFrame = 1
                    maxFrame = frameLength
from sklearn.metrics import average_precision_score
import matplotlib.pyplot as plt

with open("exp.txt", "r") as inf:
    inf.readline()
    int_array = [line.strip("\n").split()[:] for line in inf]

predictR = np.array(int_array, dtype=np.float64)

with open("exp1.txt", "r") as inf1:
    inf1.readline()
    int_array1 = [line.strip("\n").split()[:] for line in inf1]
final_lable = np.array(int_array1, dtype=np.float64)

mlab = Matlab()
mlab.start()
# print os.getcwd()
# self.predictR = mlab.run_func(os.sep.join([os.getcwd(), "kbmf2k", "kbmf.m"]), {'Kx': drugMat, 'Kz': targetMat, 'Y': R, 'R': self.num_factors})['result']
res = mlab.run_func(os.path.realpath(os.sep.join(["..\pudt", "AUC.m"])), {
    'test_targets': final_lable,
    'output': predictR
})
predictAUC = res['result']
# print os.path.realpath(os.sep.join(['../kbmf2k', "kbmf.m"]))
mlab.stop()
# score = self.predictR[test_data[:, 0], test_data[:, 1]]
score = predictAUC
fpr, tpr, thr = roc_curve(final_lable, np.array(predictR))
auc_val = auc(fpr, tpr)

print("predictAUC: " + str(predictAUC) + ", AUC: " + str(auc_val))
Exemplo n.º 46
0
class FingerprintUpdate:
    def __init__(self):
        self.mlab = Matlab()
        self.mlab.start()
        # 该文件无用,只是为了避免一个路径引起的bug
        res = self.mlab.run_func(
            'C:/Users/ovewa/Desktop/git-storage/OS-ELM-matlab/test.m', 1)

    # 生成中间数据
    def get_median(self, ditu, data, model_num, ap_num):
        if os.path.isdir('middata' + str(ap_num) + '/'):
            pass
        else:
            os.mkdir('middata' + str(ap_num) + '/')
        with open('middata' + str(ap_num) + '/' + str(model_num) + '.txt',
                  'wt') as f:
            for x in range(len(data)):
                for y in range(len(data[0])):
                    if int(data[x][y]) != 0 and int(ditu[x][y]) != 0:
                        f.write(
                            str(data[x][y] / ditu[x][y] - 1) + " " + str(x) +
                            " " + str(y) + "\n")

    def get_none(self, ditu, ap_num):
        if os.path.isdir('middata' + str(ap_num) + '/'):
            pass
        else:
            os.mkdir('middata' + str(ap_num) + '/')
        with open('middata' + str(ap_num) + '/none.txt', 'wt') as f:
            for x in range(len(ditu)):
                for y in range(len(ditu[0])):
                    f.write(str(0) + " " + str(x) + " " + str(y) + "\n")

    # 训练过程
    # 可优化
    # 1是训练条件不同结果不同
    # 2是随机参数不同结果不同
    # 多次训练取最优解
    def training(self, model_num, ditu, ap_num):
        # 初始训练
        res = self.mlab.run_func('OSELM_initial_training.m',
                                 'middata' + str(ap_num) + '/1.txt',
                                 10,
                                 'sin',
                                 nargout=5)
        IW = res['result'][0]
        Bias = res['result'][1]
        M = res['result'][2]
        beta = res['result'][3]
        # 增量学习
        # ####!!!添加将每一次增量学习结果的误差输出出来并可视化
        for x in range(2, (model_num + 1)):
            res = self.mlab.run_func('OSELM_increase_study.m',
                                     'middata' + str(ap_num) + '/' + str(x) +
                                     '.txt',
                                     IW,
                                     Bias,
                                     M,
                                     beta,
                                     'sin',
                                     1,
                                     nargout=4)
            IW = res['result'][0]
            Bias = res['result'][1]
            M = res['result'][2]
            beta = res['result'][3]
        # 获取完整指纹库
        res = self.mlab.run_func('OSELM_test_value.m',
                                 'middata' + str(ap_num) + '/none.txt', IW,
                                 Bias, beta, 'sin')
        result = res['result']
        y = 0
        data = []
        for i in range(len(ditu)):
            data.append([])
            for j in range(len(ditu[0])):
                data[i].append(-1)

        for x in range(len(result)):
            ###重点,现在直接将初次训练结果保存,没有进行比较分析,待完善
            data[x % 10][y] = int(ditu[x % 10][y] * (result[x] + 1))
            if (x + 1) % 10 == 0:
                y = y + 1
        return data

    def mlab_stop(self, ap_mac):
        self.mlab.stop()
        # 删除中间文件
        for x in range(len(ap_mac)):
            if os.path.isdir('middata' + str(x) + '/'):
                shutil.rmtree('middata' + str(x) + '/')
Exemplo n.º 47
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.º 48
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]