Exemplo n.º 1
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()
 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.º 3
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)
Exemplo n.º 4
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.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
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
Exemplo n.º 12
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.º 13
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.º 14
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.º 15
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.º 16
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)
Exemplo n.º 17
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()
Exemplo n.º 18
0
    times which emulates a counter. Once it is done with
    counting it shuts down the matlab server.   
'''

from pymatbridge import Matlab
from distutils.spawn import find_executable


mcruns = 800
res = {'result': 0}

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

# Create matlab session
mlab = Matlab(executable=prog)
mlab.start()

# Call jk.m in a for loop to emulate a counter
for mcindex in range(mcruns):
    res = mlab.run_func('../matlab/adder.m', {'arg1': res['result'], 'arg2': 1})
    print(res['result'])

# Shut down the matlab server
mlab.stop()

Exemplo n.º 19
0
from pymatbridge import Matlab
import os
import scipy.io

if __name__ == "__main__":
    config = {}
    execfile("settings.conf", config)
    # python 3: exec(open("example.conf").read(), config)
    mlab = Matlab(matlab=config["MATLAB_LOCATION"])
    path = os.getcwd() + "/"
    mlab.start()
    filename = config["f1arg1"]
    start = int(config["f1arg2"])
    end = int(config["f1arg3"])
    res = mlab.run_func(path + config["f1"], {"arg1": filename, "arg2": start, "arg3": end})
    mlab.stop()
    print("done")
Exemplo n.º 20
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.º 21
0
import pymatbridge
from pymatbridge import Matlab

# Initialise MATLAB
mlab = Matlab()

# Start the server
mlab.start()
# Run a test function: just adds 1 to the argument a
res = []
for i in range(5):
    res.append(mlab.run_func('demo_func.m', {'a': i})['result'])
    print res[-1]

# test the JSON parsing.
# quotes, and \n
res.append(mlab.run_code('fprintf(1,char([34,104,105,34,10]));'))
print res[-1]
# \b, \n
res.append(mlab.run_code('fprintf(1,char([8,10]));'))
print res[-1]
# \f, \n
res.append(mlab.run_code('fprintf(1,char([12,10]));'))
print res[-1]
# \r, \n
res.append(mlab.run_code('fprintf(1,char([13,10]));'))
print res[-1]
# \t, \n
res.append(mlab.run_code('fprintf(1,char([9,10]));'))
print res[-1]
# \\, \n
Exemplo n.º 22
0
#  Parameters Setting
filename = "./Input_data/example_lena.png"
npixs = 512
Nstd = 0.4
NE = 20
seedNo = 1
numImf = 6
runCEEMD = 1
maxSift = 10
typeSpline = 2
toModifyBC = 1
randType = 2
checksignal = 1

##
mlab = Matlab()
mlab.start()
mlab.run_code("addpaths")

# Fast 2dEEMD
res = mlab.run_func(
    "meemd", filename, npixs, Nstd, NE, numImf, runCEEMD, maxSift, typeSpline, toModifyBC, randType, seedNo, checksignal
)
imfs = res["result"]

# Plot Results
HHTplots.example_lena(filename, imfs)

mlab.stop()
for i in range(0,len(second_level)):
        name = second_level[i].split("Database/",1)[1].rsplit(".",1)[0]
        address, name2 = os.path.split(name)
        if name2 != ".DS_Store" and name2 != '':
            if not os.path.exists('Database_5pt/'+address):
                os.makedirs('Database_5pt/'+address)
            pt = open("Database_5pt/"+address+'/'+name2+".5pt","w+")
            img=io.imread(second_level[i])
            dets = detector(img, 1)
            for k, d in enumerate(dets):
                shape = predictor(img, d)
                left_eye = shape.part(45)
                right_eye = shape.part(36)
                pt.write(str(left_eye.x)+' '+str(left_eye.y)+'\n')
                pt.write(str(right_eye.x)+' '+str(right_eye.y)+'\n')
                nose = shape.part(30)
                pt.write(str(nose.x)+' '+str(nose.y)+'\n')
                left_mouse = shape.part(54)
                right_mouse = shape.part(48)
                pt.write(str(left_mouse.x)+' '+str(left_mouse.y)+'\n')
                pt.write(str(right_mouse.x)+' '+str(right_mouse.y)+'\n')
                pt.close()
                ec_y = shape.part(36).y-shape.part(45).y
                ec_y= (ec_y,-ec_y)(ec_y<0)
                mc_y = shape.part(54).y-shape.part(48).y
                mc_y =(mc_y,-mc.y)[ec_y<0]
                ec_mc_y = mc_y-ec_y
                pt.close()
                #computations
                mlab.run_func('src/software/face_db_align.m', {'face_dir': 'Database', 'ffp_dir': 'Database_5pt', 'ec_mc_y': ec_mc_y, 'ec_y': ec_y, 'img_size': size, 'save_dir': 'Database_preprocessed'})
Exemplo n.º 24
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.º 25
0
mlab.start()
N = len(samples)
involved = [[] for _ in range(N)]
row = 0
for i in range(N):
    for j in range(i + 1, N):
        involved[i].append(row)
        involved[j].append(row)
        row += 1
rows = set(range(row))
notinvolved = [list(rows.difference(_)) for _ in involved]
dsts = np.zeros((len(samples), 2))
thetas = np.zeros((len(samples), 31))
i = 0
for train_row, test_row in zip(notinvolved, involved):
    res = mlab.run_func('optimize_weights.m', {'A': A[train_row, :].tolist()})
    x = np.array(res['result'])
    dsts[i, :] = [
        np.linalg.norm(np.dot(A[test_row, :], x)),
        np.linalg.norm(np.dot(A[test_row, :], theta))
    ]
    thetas[i, :] = x.ravel()
    i += 1

s = clock()
res = mlab.run_func('optimize_weights.m', {
    'A': A.tolist(),
    'involved': involved,
    'notinvolved': notinvolved
})
clock() - s
Exemplo n.º 26
0
import pymatbridge
from pymatbridge import Matlab

# Initialise MATLAB
mlab = Matlab()

# Start the server
mlab.start()
# Run a test function: just adds 1 to the argument a
res = []
for i in range(5):
     res.append(mlab.run_func('demo_func.m', {'a': i})['result'])
     print res[-1]

# test the JSON parsing.
# quotes, and \n
res.append(mlab.run_code('fprintf(1,char([34,104,105,34,10]));'))
print res[-1]
# \b, \n
res.append(mlab.run_code('fprintf(1,char([8,10]));'))
print res[-1]
# \f, \n
res.append(mlab.run_code('fprintf(1,char([12,10]));'))
print res[-1]
# \r, \n
res.append(mlab.run_code('fprintf(1,char([13,10]));'))
print res[-1]
# \t, \n
res.append(mlab.run_code('fprintf(1,char([9,10]));'))
print res[-1]
# \\, \n
Exemplo n.º 27
0
from pymatbridge import Matlab
import os

mlab = Matlab()
mlab.start()
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_list = ['/Input_data', '/HHT_MATLAB_package', '/HHT_MATLAB_package/EMD',
          '/HHT_MATLAB_package/checkIMFs', '/HHT_MATLAB_package/HT']
for d in dir_list:
    res = mlab.run_func('addpath', dir_path + d)

class EMD():
    def __init__(self, signal, Nstd, NE, num_imf=10, run_CEEMD=1, max_sift=10,
                 type_spline=2, modify_BC=1, rand_type=2, seed_no=1,
                 check_signal=1):
        res = mlab.run_func('feemd_post_pro', signal, Nstd, NE, num_imf,
                            run_CEEMD, max_sift, type_spline, modify_BC,
                            rand_type, seed_no, check_signal)
        self.imfs = res['result']

    def get_oi(self):
        oi = mlab.run_func('ratio1', self.imfs)
        oi_pair = mlab.run_func('ratioa', self.imfs)
        oi_dic = {'Non-orthogonal leakage of components': oi['result'],
                  'Non-orthogonal leakage for pair of adjoining components': oi_pair['result']}
        return oi_dic
 img = io.imread(second_level[i])
 dets = detector(img, 1)
 for k, d in enumerate(dets):
     shape = predictor(img, d)
     left_eye = shape.part(45)
     right_eye = shape.part(36)
     pt.write(str(left_eye.x) + ' ' + str(left_eye.y) + '\n')
     pt.write(str(right_eye.x) + ' ' + str(right_eye.y) + '\n')
     nose = shape.part(30)
     pt.write(str(nose.x) + ' ' + str(nose.y) + '\n')
     left_mouse = shape.part(54)
     right_mouse = shape.part(48)
     pt.write(str(left_mouse.x) + ' ' + str(left_mouse.y) + '\n')
     pt.write(str(right_mouse.x) + ' ' + str(right_mouse.y) + '\n')
     pt.close()
     ec_y = shape.part(36).y - shape.part(45).y
     ec_y = (ec_y, -ec_y)(ec_y < 0)
     mc_y = shape.part(54).y - shape.part(48).y
     mc_y = (mc_y, -mc.y)[ec_y < 0]
     ec_mc_y = mc_y - ec_y
     pt.close()
     #computations
     mlab.run_func(
         'src/software/face_db_align.m', {
             'face_dir': 'Database',
             'ffp_dir': 'Database_5pt',
             'ec_mc_y': ec_mc_y,
             'ec_y': ec_y,
             'img_size': size,
             'save_dir': 'Database_preprocessed'
         })
Exemplo n.º 29
0
randType = 2
checksignal = 1

##
data = np.loadtxt(filename)
time = data[:, 0]
amp = data[:, 1]
dt = time[1] - time[0]

##
mlab = Matlab()
mlab.start()
mlab.run_code('addpaths')

# Fast EEMD
res = mlab.run_func('feemd_post_pro', amp, Nstd, NE, numImf, runCEEMD, maxSift,
                    typeSpline, toModifyBC, randType, seedNo, checksignal)
imfs = res['result']


# Orthogonality Checking
oi = mlab.run_func('ratio1', imfs)
oi_pair = mlab.run_func('ratioa', imfs)
print('Non-orthogonal leakage of components:')
print(oi['result'])
print('Non-orthogonal leakage for pair of adjoining components:')
print(oi_pair['result'])

# Hilbert Transform
fa_res = mlab.run_func('fa', imfs[:, 3], dt, 'hilbtm', 'pchip', 0, nargout=2)
fa = fa_res['result']
ifreq = np.transpose(fa[0])
Exemplo n.º 30
0
mlab.start()
N = len(samples)
involved = [[] for _ in range(N)]
row = 0
for i in range(N):
    for j in range(i+1, N):
        involved[i].append(row)
        involved[j].append(row)
        row += 1
rows = set(range(row))
notinvolved = [list(rows.difference(_)) for _ in involved]
dsts = np.zeros((len(samples), 2))
thetas = np.zeros((len(samples), 31))
i = 0
for train_row, test_row in zip(notinvolved, involved):
    res = mlab.run_func('optimize_weights.m', {'A': A[train_row, :].tolist()})
    x = np.array(res['result'])
    dsts[i, :] = [np.linalg.norm(np.dot(A[test_row, :], x)),
                  np.linalg.norm(np.dot(A[test_row, :], theta))]
    thetas[i, :] = x.ravel()
    i += 1

s = clock()
res = mlab.run_func('optimize_weights.m',
                    {'A': A.tolist(), 'involved': involved,
                     'notinvolved': notinvolved})
clock() - s

mlab.stop()
dsts = np.array(res['result']['distances'])
thetas = np.array(res['result']['thetas'])
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.º 32
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) + '/')