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()
Esempio n. 2
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']
Esempio 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
Esempio n. 4
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)
Esempio n. 5
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)
 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()
Esempio n. 7
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
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
Esempio n. 9
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()
Esempio n. 10
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)
Esempio n. 11
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
Esempio n. 12
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()
Esempio n. 13
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
Esempio n. 14
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()
Esempio n. 15
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()
    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
Esempio n. 17
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()
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
Esempio n. 19
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()
Esempio n. 20
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()))
Esempio n. 21
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
Esempio n. 22
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()
Esempio n. 23
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
Esempio n. 24
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')
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))
Esempio n. 26
0
a = mlab.run_code('cd(\'/home/bejar/PycharmProjects/PeakDataAnalysis/Matlab/\')')
print a

datasufix = ''#'-RawResampled'

wtime = '120e-3' # Window length in miliseconds

for expname in lexperiments:
    datainfo = experiments[expname]
    sampling = datainfo.sampling #/ 6.0

    for file in [datainfo.datafiles[0]]:
        print time.ctime()
        nfile = '/home/bejar/Data/Cinvestav/' + file + datasufix + '.mat'
        nfiler = '/home/bejar/Data/Cinvestav/' + file + datasufix + '-peaks2.mat'
        print 'Processing ', file
        print 'IN= ', nfile
        print 'OUT= ', nfiler
        a = mlab.run_code('cdp_identification(\'' + nfile + '\', \'' + nfiler + '\', '+ wtime + ',' + str(sampling) + ')')
        print a
        print '************************************************'
        print time.ctime()

mlab.stop()


# print results
# print mlab.get_variable('a')

#res = mlab.run_func('jk.m', {'arg1': 3, 'arg2': 5})
Esempio n. 27
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()
Esempio n. 28
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()
Esempio n. 29
0
def Run(C, R, mic, Plot):
    CHUNK = 44100  # number of data points to read at a time 4096
    CHUNK = C
    # 4096 byte
    # the number of frames
    RATE = 44100  # 176400  # time resolution for reading device (Hz) 44100 samples/second
    RATE = R
    # sampling rate i.e the number of frames per second
    serSignal = 'S'
    KnockSignal = 'K'
    Input_Device_Index = 2
    Input_Device_Index = mic
    plot = Plot

    # Define the serial port
    ser_port = "COM8"  # for window computer, int must be used COM1 = 0,COM2=1 ...
    baud_rate = 9600
    count = 0
    flag = False
    signal = False

    mlab = Matlab(executable=r"D:\MATLAB\bin\matlab.exe")
    mlab.start()
    p = pyaudio.PyAudio()

    # while True:
    #     ser.write(serSignal.encode('utf-8'))
    #     if ser.readline().decode('utf-8') != "Spray":
    #         break

    stream = p.open(format=pyaudio.paInt16,
                    channels=1,
                    rate=RATE,
                    input=True,
                    input_device_index=None,
                    frames_per_buffer=CHUNK)
    ser = serial.Serial(ser_port, baud_rate)
    print(ser.readline().decode("utf-8"))
    print("Input delay is %f" % stream.get_input_latency())
    while (True):
        for i in range(int(3)):  #only loop forA int(??) times
            #if(count>1):
            #    sleep(1)
            if (count == 1):
                ser.write(KnockSignal.encode(
                    "utf-8"))  # encode is used for string.encode()
                sleep(.32)  # **change here (0.1s per 5000samples)
                flag = True
                print("Must Knock Here")
            # The input device id "2"   => built-in microphone
            # info = p.get_host_api_info_by_index(0)
            # numdevices = info.get('deviceCount')
            # for i in range(0, numdevices):
            #     if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            #         pass
            #print('Input Device id', i, '-', p.get_device_info_by_host_api_device_index(0, i).get('name'))
            # get the default device info
            #print(p.get_default_input_device_info())

            # create a numpy array holding a single read of audio data

            #now = datetime.now()

            if flag == True:
                # if count ==1:
                #     sleep(.5)

                np.set_printoptions(threshold=sys.maxsize)

                data = np.fromstring(stream.read(CHUNK), dtype=np.short)
                #print(stream)
                time = np.arange(0, CHUNK)

                #peak=np.average(np.abs(data))*21
                #bars="#"*int(50*peak/2**16)
                #print("%04d %s"%(i,data))
                #print("%s %s" % (data/32768,now ))

                #print("Input data is ", type(data))

                # Test Matlab data 1
                #res = mlab.run_func('jk.m', {'arg1': data})
                #print("Output data is ", type(res['result']))
                #data1 = res['result']  # The data in matlab is float64 (e.g for 64bit window) https://stackoverflow.com/questions/8855574/convert-ndarray-from-float64-to-integer
                #M_data1 = data1[0] / 32768
                #print("jk.m is",res)

                # data1 = np.array(res['result'], dtype=np.float64).astype(np.int64)
                # print(type(data1))

                #Write data to text file before matlab
                # with open("SignalTest1.txt", "wt") as file:
                #     file.write("%s" % (str(M_data1).lstrip('[').rstrip(']')))
                #     file.flush()
                #     file.close()
                #     # file.writelines("%s %04d %s\n"%(now,i,data))
                #     # close the stream gracefully

                # max_val =np.amax(data)
                # print(max_val)
                # if max_val >30000:

                #data/32768
                #print(M_data1)

                if count == 1:
                    print("Write")
                    with open("SignalTest.txt", "wt") as out_file:
                        out_file.writelines(
                            str(data))  #it can only write string

                if plot == True and count == 2:

                    past = stream.get_time()
                    np.set_printoptions(threshold=sys.maxsize)
                    data = np.fromstring(stream.read(CHUNK), dtype=np.short)
                    present = stream.get_time()
                    delay = present - past
                    print("The delay is %f" % delay)

                    plt.title('AudioSample')
                    plt.plot(time, data)
                    plt.ylim(-40000, 40000)
                    plt.ylabel('Amplitude')
                    plt.xlabel('Sample Size')
                    #plt.pause(.0000000000000000000000000000000000000000000000000000000001)
                    #plt.clf()

                    #print(stream.get_time())

                    dataprocess = mlab.run_func(
                        'final_judge.m', {"arg1": data})  # ,{'arg1':data}
                    # print("The input data is ",M_data1)
                    print(np.amax(data))
                    print(dataprocess['result'])
                    d1 = dataprocess['result']

                    if d1 == 1:
                        ser.write(serSignal.encode(
                            "utf-8"))  # encode is used for string.encode()
                        # print(ser.write(serSignal.encode("utf-8")))
                        #print(ser.readline().decode("utf-8"))
                        #d1 = 2
                    plt.show()
                    flag = False

                    count = 0
            count += 1

        #ser.reset_output_buffer()
    mlab.stop()
    out_file.close()
    stream.stop_stream()
    stream.close()
    p.terminate()

    sys.exit(0)
Esempio 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')
Esempio n. 31
0
    # Store local results
    hdf5storage.savemat(local_dir + '/results.mat', {
        'bler_ae': bler_ae[run_idx],
        'ber_ae': ber_ae[run_idx],
        'bler_aeq': bler_aeq[run_idx],
        'ber_aeq': ber_aeq[run_idx],
        'bler_enc': bler_enc[run_idx],
        'ber_enc': ber_enc[run_idx],
        'bler_encq': bler_encq[run_idx],
        'ber_encq': ber_encq[run_idx],
        'val_loss': history.history['val_loss']
    },
                        truncate_existing=True)
    # Store global results incrementally
    hdf5storage.savemat(global_dir + '/results_global%d.mat' % global_seed, {
        'bler_ae': bler_ae,
        'ber_ae': ber_ae,
        'bler_aeq': bler_aeq,
        'ber_aeq': ber_aeq,
        'bler_enc': bler_enc,
        'ber_enc': ber_enc,
        'bler_encq': bler_encq,
        'ber_encq': ber_encq,
        'local_seed_collect': local_seed_collect
    },
                        truncate_existing=True)

# Close MATLAB engine
eng.stop()
Esempio n. 32
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()
Esempio n. 33
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) + '/')
Esempio n. 34
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()