def _session_hook(self): from matlab.engine import start_matlab startup_ops = '-nodesktop -nodisplay' if self._num_threads != 1 else \ '-nodesktop -nodisplay -singleCompThread' try: return start_matlab(startup_ops) except: return start_matlab()
def _start_matlab(self, event: mp.Event, alive_check_period=60, startup_options=None, shared_name=None): pid = mp.current_process().pid logger.info('Starting Matlab engine "{}" helper process {}...', shared_name, pid) self._terminate_event.clear() event.clear() try: if startup_options is None: eng = me.start_matlab() else: eng = me.start_matlab(startup_options) if shared_name: eng.matlab.engine.shareEngine(shared_name, nargout=0) else: eng.matlab.engine.shareEngine(nargout=0) shared_name = eng.matlab.engine.engineName(nargout=1) logger.info('Matlab engine "{}" is started', shared_name) except (me.EngineError, me.MatlabExecutionError): logger.exception('Cannot start Matlab engine "{}"', shared_name) raise finally: event.set() while not self._terminate_event.is_set(): # Is matlab alive? try: eng.version(nargout=0) except (me.EngineError, me.CancelledError): break try: time.sleep(alive_check_period) except KeyboardInterrupt: break try: eng.exit() except SystemError: logger.exception('Cannot terminate Matlab process correctly') else: logger.info('Terminate Matlab engine "{}" helper process {}', shared_name, pid)
def get_matlab_engine(): if sys.platform == 'win32': matlab = get_preferred_matlab() if not matlab: warnings.warn( "MATLAB not found in registry. Using Python implementation.", RuntimeWarning) return None if matlab[0] == platform.architecture()[0]: MATLABROOT = matlab[2] engine = import_matlab_python_engine(MATLABROOT) return MatlabInProcessProxyReplacement(engine.start_matlab()) else: python_exe = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'dist_{}\\python.exe'.format(matlab[0])) if not os.path.isfile(python_exe): raise Exception( "'{}' does not exist. Run `setup.py py2exe` with Python {} in the matlab_proxy dir" .format(python_exe, matlab[0])) return get_engine_proxy(matlab[2], python_exe, matlab[0]) try: import matlab.engine return MatlabInProcessProxyReplacement(matlab.engine.start_matlab()) except ImportError as e: warnings.warn("Failed to import matlab.engine: %s" % e, RuntimeWarning) return None
def getFinalScore(A, B, H, Pa, Pb, node_A=None, node_B=None): eng = mateng.start_matlab() if node_A is None and node_B is None: node_A = np.ones((A.get_shape()[0], 1)) node_B = np.ones((B.get_shape()[0], 1)) else: node_A = node_A.reshape((node_A.shape[0], 1)) if np.array_equal(node_A, Pa.dot(node_A)): print "node_A == Pa.dot(node_A)" node_A = Pa.dot(node_A) node_B = node_B.reshape((node_B.shape[0], 1)) if np.array_equal(node_B, Pb.dot(node_B)): print "node_B == Pb.dot(node_B)" node_B = Pb.dot(node_B) io.savemat('temp_final.mat', dict(A=A, B=B, H=H, Pa=Pa, Pb=Pb, node_A=node_A, node_B=node_B)) accuracy, ma, mb = eng.runFinal(nargout=3) matching_matrix = getCSRMatrix(A.shape, ma, mb) print "final: " + str(accuracy) eng.quit() return accuracy, matching_matrix
def _runMatlabCode(matrix, matlab_session): #matrix = scipy.sparse.coo_matrix(matrix) dir = os.path.dirname(__file__) visuAddress = os.path.join(dir, "GenLouvain-master") #print("saving matrix for matlab ") scipy.io.savemat(visuAddress + '/file.mat', {"B": matrix}) result_file = visuAddress + '/result.mat' ###matFormat = matlab.double(matrix.tolist()) #print("starting matlab engine") eng = matlab_session if eng == None: eng = engine.start_matlab() eng.addpath(visuAddress, nargout=0) #print("matlab engine started successfully") start_time = time.time() out = io.StringIO() err = io.StringIO() #(S, Q) = eng.genlouvain('file.mat', nargout=2) eng.genlouvain('file.mat', result_file, stdout=out, stderr=err) duration = time.time() - start_time #print("matlab code ran successfully") #print(err.getvalue()) res = scipy.io.loadmat(result_file) S = res["S"] return (S, duration)
def parse_annotations(args): eng = mateng.start_matlab() [s, X] = eng.read_binary_blob(args.PATH_TO_DATA + '176.conv5b', nargout = 2) color = 'b' i = 0 for label in all_segments: print "Parsing label " + label segments = all_segments[label] if label == '2': color = 'r' elif label == '3': color = 'g' else: color = 'b' for seg in segments: j = seg[0] while j <= seg[1]: if j == 176: color_map[i] = color index_map[i] = j j += 16 i += 1 continue [s, data] = eng.read_binary_blob(args.PATH_TO_DATA + str(j) + '.conv5b', nargout = 2) data = np.array(data) X = np.concatenate((X, data), axis = 0) color_map[i] = color index_map[i] = j j += 16 i += 1 return X
def main(): """ Run the preprocess functionality on each of the participant directories. Some of the values for the current study are hard coded into this file for ease of use. """ parser = ArgumentParser() parser.add_argument("inputDir") parser.add_argument("outputDir") args = parser.parse_args() directory = args.inputDir outputDirectory = args.outputDir intervalSize = 5 trialLength = 600 # Start a matlab instance to speed up repeated processes eng = start_matlab() participantIds = [ '001', '002', '003', '004', '005', '006', '007'] for participantId in participantIds: participantDir = path.join( directory, participantId ) preprocess( participantDir, outputDirectory, intervalSize, trialLength, eng ) eng.quit()
def parse_annotations_pickle(annotations_file, PATH_TO_DATA): index_map = {} label_map = {} eng = mateng.start_matlab() map_index_data = pickle.load(open(annotations_file, "rb")) X = None i = 0 for index in map_index_data: print "Parsing label " + str(index) segments = map_index_data[index] color = np.random.rand(3,1) for seg in segments: j = seg[0] while j <= seg[1]: if X is None: [s, X] = eng.read_binary_blob(PATH_TO_DATA + str(j) + '.conv5b', nargout = 2) else: [s, data] = eng.read_binary_blob(PATH_TO_DATA + str(j) + '.conv5b', nargout = 2) data = np.array(data) X = np.concatenate((X, data), axis = 0) index_map[i] = j label_map[i] = index j += 16 i += 1 return X, label_map, index_map
def standard_methods_to_test(): eng = engine.start_matlab() def smoothed_louvain(x, elapsed_time=True): return tn.DCD.iterative_match(x, CDalgo="smoothedLouvain", elapsed_time=elapsed_time) # methods_to_test = {"iterative":DCD.iterative_match,"dynamo":dynamo,"dynmoga":dynmoga,"smoothed_louvain":smoothed_louvain} def mucha_opti(x, elapsed_time=True): return transversal_network_mucha_original(x, elapsed_time=elapsed_time, matlab_session=eng) def mucha_global(x, elapsed_time=True): return transversal_network_mucha_original(x, elapsed_time=elapsed_time, matlab_session=eng, form="global") print("pas de mucha") methods_to_test = { "iterative": tn.DCD.iterative_match, "dynamo": dynamo, "smoothed_louvain": smoothed_louvain, "mucha": mucha_opti, # "mucha_global":mucha_global, "survival_graph": tn.DCD.label_smoothing } # ,"dynmoga":dynmoga}# # methods_to_test = {"smoothed_louvain":smoothed_louvain}#,"dynmoga":dynmoga}# return methods_to_test
def main(scp): data = load_data(scp) params = [] bot = top = 0 for line in data: line = line.strip().split() fin = line[0] gender = line[1] fout = line[2] des = fout.split("/")[:-1] des = "/".join(des) if gender == "M": bot = 50.0 top = 300.0 #continue else: #continue bot = 75.0 top = 500.0 #param = [[fin], [bot], [top], [fout]] #param = [fin, des, fout, bot, top] param = [fin, des, fout, bot, top] params.append(param) eng = me.start_matlab() eng.f0_extraction_parallel(params, nargout=0) eng.quit() print("Done with f0 extraction...")
def main(): """ Run the preprocess functionality on each of the participant directories. Some of the values for the current study are hard coded into this file for ease of use. """ parser = ArgumentParser() parser.add_argument("inputDir") parser.add_argument("outputDir") args = parser.parse_args() directory = args.inputDir outputDirectory = args.outputDir intervalSize = 5 trialLength = 600 # Start a matlab instance to speed up repeated processes eng = start_matlab() participantIds = ['001', '002', '003', '004', '005', '006', '007'] for participantId in participantIds: participantDir = path.join(directory, participantId) preprocess(participantDir, outputDirectory, intervalSize, trialLength, eng) eng.quit()
def run1(GUI2): initSignal(GUI2) eng = engine.start_matlab() eng.cd(r'.\2') def send1(): GUI2.signal1.emit(eng) def send2(): GUI2.signal2.emit(eng) def send3(): GUI2.signal3.emit(eng) def send4(): GUI2.signal4.emit(eng) def send5(): GUI2.signal5.emit(eng) GUI2.firstButton1.clicked.connect(send1) GUI2.secondButton1.clicked.connect(send2) GUI2.secondButton2.clicked.connect(send3) GUI2.thirdButton1.clicked.connect(send4) GUI2.thirdButton2.clicked.connect(send5)
def warping(anno_dic, gaze_dic, ref_img_name, gaze_path=None, save=False): ''' Warps the gaze coordinates of the required image to the plane of a reference image using Thin Plate Spline transformation and saves the resultant coordinates in a new csv file. :param anno_dic: dictionary with image names and list of annotated coordinates as key value pair. :param gaze_dic: dictionary with image names and list of gaze coordinates as key value pair. :param ref_img_name: Reference image name as string. :param gaze_path: string that gives the path to the gaze files :param save: boolean to save the output warped gaze data to a csv file. :return: None ''' warped_dict = {} eng = me.start_matlab() ref_annos = m.double( np.transpose(np.array(anno_dic[ref_img_name])).tolist()) for img in anno_dic: annotations = m.double(np.transpose(np.array(anno_dic[img])).tolist()) if np.shape(np.array(annotations))[1] == np.shape( np.array(ref_annos))[1]: gaze = m.double(np.transpose(np.array(gaze_dic[img])).tolist()) tps = eng.tpaps(annotations, ref_annos) warp = eng.fnval(tps, gaze) warp_ = np.transpose(np.array(warp)).tolist() warp_ = [[int(point[0]), int(point[1])] for point in warp_] if save is True: with open(gaze_path + 'output_warped/' + img + '.csv', 'wb') as cs: writer = csv.writer(cs, delimiter=',') for point in warp_: writer.writerow(point) warped_dict[img] = warp_ # print('Done Warping!') return warped_dict
def _connect_matlab_engine(self, use_existing=True, persistent=True): """ Connect to a matlab engine, starting a new one if it hasn't been started yet :return: """ found_eng = False if use_existing: eng_names = engine.find_matlab() for eng_name in eng_names: try: self.eng = engine.connect_matlab(eng_name) self.eng_name = eng_name found_eng = True # unable to connect to an instance because it already has a connection except matlab.engine.EngineError: pass if found_eng: break if not found_eng: # if we're making a new persistent engine, create it if persistent: self.eng_name = self._start_persistent_shared_matlab() self.eng = engine.connect_matlab(self.eng_name) # otherwise, make a new engine just for the lifetime of self else: self.eng = engine.start_matlab() self.eng_name = None # to be explicit self.eng_deleted = False
def _runMatlabCode(dummy_coms_files, graphs_files, T, ouput_file): dir = os.path.dirname(__file__) visuAddress = os.path.join(dir, "DYNMOGA2015b-2") print("starting matlab engine") eng = engine.start_matlab() eng.addpath(visuAddress, nargout=0) print("matlab engine started successfully") start_time = time.time() out = io.StringIO() err = io.StringIO() #(S, Q) = eng.genlouvain('file.mat', nargout=2) try: eng.run_DYNMOGA(dummy_coms_files, graphs_files, T, ouput_file, stdout=out, stderr=err, nargout=0) except: print(err.getvalue()) print(out.getvalue()) print("matlab code ran successfully") #print(err.getvalue()) duration = time.time() - start_time return (duration)
def __init__(self, logger, beh): import matlab.engine as eng self.mat = eng.start_matlab() self.mat.run('c:/pipeline/pipeline/setPath.m', nargout=0) self.mat.run('c:/pipeline/pipeline/setDJ.m', nargout=0) self.trial = [] super(DaqPiezoStim, self).__init__(logger, beh)
def parseAndRemoveBaseline(inputFilename, outputDirectory, eng=None): """ Parse the specified input EDF file, remove baseline, and write the relevant channel data out to a tab-separated text file that can be more easily read. A matlab engine can be passed in to cut down on processing time. Returns the name of the file the data was written to. """ outputFilename = path.join(outputDirectory, "eeg_parsed.txt") existing = True # Create a matlab instance if one was not provided if eng == None: eng = start_matlab() existing = False # Add the current directory to the matlab path so that the function can be found eng.addpath(dirname(realpath(__file__))) eng.ParseEEG(inputFilename, outputFilename, nargout=0) eng.clc(nargout=0) eng.clear(nargout=0) # Shutdown matlab if it was created here if not existing: eng.quit() return outputFilename
def __init__(self, logger, beh): import matlab.engine as eng self.mat = eng.start_matlab() self.mat.run('/home/atlab/pipeline/setPath.m', nargout=0) self.mat.run('/home/atlab/pipeline/setDJ.m', nargout=0) self.trial = [] super(Psychtoolbox, self).__init__(logger, beh)
def parse_annotations(args): eng = mateng.start_matlab() [s, X] = eng.read_binary_blob(args.PATH_TO_DATA + '176.conv5b', nargout=2) color = 'b' i = 0 for label in all_segments: print "Parsing label " + label segments = all_segments[label] if label == '2': color = 'r' elif label == '3': color = 'g' else: color = 'b' for seg in segments: j = seg[0] while j <= seg[1]: if j == 176: color_map[i] = color index_map[i] = j j += 16 i += 1 continue [s, data] = eng.read_binary_blob(args.PATH_TO_DATA + str(j) + '.conv5b', nargout=2) data = np.array(data) X = np.concatenate((X, data), axis=0) color_map[i] = color index_map[i] = j j += 16 i += 1 return X
def parse_annotations_pickle(annotations_file, PATH_TO_DATA): index_map = {} label_map = {} eng = mateng.start_matlab() map_index_data = pickle.load(open(annotations_file, "rb")) X = None i = 0 for index in map_index_data: print "Parsing label " + str(index) segments = map_index_data[index] color = np.random.rand(3, 1) for seg in segments: j = seg[0] while j <= seg[1]: if X is None: [s, X] = eng.read_binary_blob(PATH_TO_DATA + str(j) + '.conv5b', nargout=2) else: [s, data] = eng.read_binary_blob(PATH_TO_DATA + str(j) + '.conv5b', nargout=2) data = np.array(data) X = np.concatenate((X, data), axis=0) index_map[i] = j label_map[i] = index j += 16 i += 1 return X, label_map, index_map
def parseAndRemoveBaseline( inputFilename, outputDirectory, eng = None ): """ Parse the specified input EDF file, remove baseline, and write the relevant channel data out to a tab-separated text file that can be more easily read. A matlab engine can be passed in to cut down on processing time. Returns the name of the file the data was written to. """ outputFilename = path.join( outputDirectory, "eeg_parsed.txt" ) existing = True # Create a matlab instance if one was not provided if eng == None: eng = start_matlab() existing = False # Add the current directory to the matlab path so that the function can be found eng.addpath( dirname(realpath(__file__)) ) eng.ParseEEG( inputFilename, outputFilename, nargout=0) eng.clc(nargout = 0) eng.clear(nargout = 0) # Shutdown matlab if it was created here if not existing: eng.quit() return outputFilename
def _start_matlab(self, event: mp.Event, alive_check_period=60, startup_options=None, shared_name=None): pid = mp.current_process().pid self._pid.value = pid logger.info('Starting matlab engine "{}" helper process {}', shared_name, pid) event.clear() if startup_options is None: eng = me.start_matlab() else: eng = me.start_matlab(startup_options) try: if shared_name: eng.matlab.engine.shareEngine(shared_name, nargout=0) else: eng.matlab.engine.shareEngine(nargout=0) shared_name = eng.matlab.engine.engineName(nargout=1) logger.info('Matlab shared engine "{}" is started', shared_name) except me.MatlabExecutionError: logger.exception('Cannot start Matlab shared engine "{}"', shared_name) raise finally: event.set() while self._pid.value != -1: # Is matlab alive? try: eng.version(nargout=0) except me.EngineError: break time.sleep(alive_check_period) if eng._check_matlab(): eng.exit() logger.info('Terminate matlab engine "{}" helper process {}', shared_name, pid)
def get_matlab_engine(): """ Returns matlab engine with path to functions for WSI appended """ matlab_dir = os.path.dirname(__file__) eng = engine.start_matlab() eng.cd(r'{}'.format(matlab_dir), nargout=0) return eng
def __init__(self, *args, **kwargs) -> None: # Init base class QuadrigaInterface.__init__(self, *args, **kwargs) # Start the matlab engine self.__engine = start_matlab()
def _start_matlab(self, event: mp.Event, alive_check_period=60, startup_options=None, shared_name=None): pid = mp.current_process().pid self._pid.value = pid print('Start matlab process %s with process ID {}'.format(pid) % shared_name) event.clear() if startup_options is None: eng = me.start_matlab() else: eng = me.start_matlab(startup_options) try: if shared_name: eng.matlab.engine.shareEngine(shared_name, nargout=0) else: eng.matlab.engine.shareEngine(nargout=0) shared_name = eng.matlab.engine.engineName(nargout=1) print('Matlab shared engine {} is started'.format(shared_name)) except me.MatlabExecutionError: raise finally: event.set() while self._pid.value != -1: # Is matlab alive? try: eng.version(nargout=0) except me.EngineError: break time.sleep(alive_check_period) if eng._check_matlab(): eng.exit() print( 'Terminate matlab helper process %s with process ID {}'.format(pid) % shared_name)
def __init__( self, m_file_path, benchmark_os_path, parallel, shared_engine=None #TAG:MSH ): self.parallel = parallel import matlab.engine as matlab_engine import matlab as matlab # Don't seem to nee matlab.engine other than to start the matlab # session. # self.matlab_engine = matlab_engine self.matlab = matlab #TAG:MSH if shared_engine is None: print 'starting matlab...' self.eng = matlab_engine.start_matlab() print 'done' else: print 'attempting to connect to an existing matlab session' self.eng = matlab_engine.connect_matlab(shared_engine) self.m_file = m_file_path m_file_name_split = self.m_file.split('.') if m_file_name_split[1].strip() != 'm': raise err.Fatal('internal error!') self.m_fun_str = m_file_name_split[0].strip() # self.sim_fun = self.eng.simulate_m_file(1) # add paths # comm.call_function([], 'addpath', [SQ + benchmark_os_path + SQ]) self.eng.addpath(benchmark_os_path) #TAG:CLSS # TODO: Remove this hack. Added for backwards compatibility # detect if the simulator file is a function or a class # source: http://blogs.mathworks.com/loren/2013/08/26/what-kind-of-matlab-file-is-this/ # classy = 8 if class else is 0 (function or a script) classy = self.eng.exist(self.m_fun_str, 'class') if classy == 0.0: print 'maltab file is a function' self.sim_is_class = False elif classy == 8.0: print 'maltab file is a class' self.sim_is_class = True self.sim_obj = self.eng.init_plant(self.m_fun_str) else: raise err.Fatal( '''Supplied matlab simulator is neither a class or a function: possible floating point error?. exist() returned: {}'''.format( classy))
def start_engine() -> Any: """ """ return global MATLAB_ENGINE if not MATLAB_ENGINE: from matlab import engine print("Starting Matlab") if True: MATLAB_ENGINE = engine.start_matlab() else: future = engine.start_matlab(background=True) MATLAB_ENGINE = future.result() print("Matlab started") else: print("Matlab already running") return MATLAB_ENGINE
def __init__(self, filepath): super().__init__() self.filepath = filepath start_time = time() print("Initializing IMU...") self.eng = m_engine.start_matlab() print("Initialization Complete. Time elapsed: {0}s".format(time() - start_time)) self.eng.addpath(os.path.join(config.ROOT_DIR, '3rd_party_scripts'))
def getNetalignScore(A, B, L, Pa, Pb): eng = mateng.start_matlab() # eng.saveMat(matlab.double(A.tolist()), matlab.double(B.tolist()), matlab.double(L.tolist()), s, nargout=0) io.savemat('temp.mat', dict(A=A, B=B, L=L, Pa=Pa, Pb=Pb)) accuracy = eng.runNetalign(nargout=1) print "netalign: " + str(accuracy) eng.quit() return accuracy
def __init__(self): self.mod_name = 'model_path.mat' self.max_pos = [850, 700] self.min_pos = [0, 0] self.engine = me.start_matlab() self.model = self.engine.ModelLoader(self.mod_name) self.mc = MC.Motorcontrol() self.mc.setup() self.grabPos = [0] self.currPos = [0, 0]
def __init__( self, m_file_path, benchmark_os_path, parallel, shared_engine=None #TAG:MSH ): self.parallel = parallel import matlab.engine as matlab_engine import matlab as matlab # Don't seem to nee matlab.engine other than to start the matlab # session. # self.matlab_engine = matlab_engine self.matlab = matlab #TAG:MSH if shared_engine is None: print 'starting matlab...' self.eng = matlab_engine.start_matlab() print 'done' else: print 'attempting to connect to an existing matlab session' self.eng = matlab_engine.connect_matlab(shared_engine) self.m_file = m_file_path m_file_name_split = self.m_file.split('.') if m_file_name_split[1].strip() != 'm': raise err.Fatal('internal error!') self.m_fun_str = m_file_name_split[0].strip() # self.sim_fun = self.eng.simulate_m_file(1) # add paths # comm.call_function([], 'addpath', [SQ + benchmark_os_path + SQ]) self.eng.addpath(benchmark_os_path) #TAG:CLSS # TODO: Remove this hack. Added for backwards compatibility # detect if the simulator file is a function or a class # source: http://blogs.mathworks.com/loren/2013/08/26/what-kind-of-matlab-file-is-this/ # classy = 8 if class else is 0 (function or a script) classy = self.eng.exist(self.m_fun_str, 'class') if classy == 0.0: print 'maltab file is a function' self.sim_is_class = False elif classy == 8.0: print 'maltab file is a class' self.sim_is_class = True self.sim_obj = self.eng.init_plant(self.m_fun_str) else: raise err.Fatal('''Supplied matlab simulator is neither a class or a function: possible floating point error?. exist() returned: {}'''.format(classy))
def parse(): eng = mateng.start_matlab() [s, X] = eng.read_binary_blob(PATH_TO_DATA + '1.conv5b', nargout = 2) i = 17; while i <= 1489: [s, data] = eng.read_binary_blob(PATH_TO_DATA + str(i) + '.conv5b', nargout = 2) data = np.array(data) X = np.concatenate((X, data), axis = 0) i += 16 return X
def __init__(self, input_image): print("Matlab engine initialization started") self._matlab_eng = start_matlab() # changing directory to the one with matlab functions self._matlab_eng.cd("..\\PSO\\MatlabFiles") matlab_img = matlab.double(input_image.tolist()) super().__init__(matlab_img) self._matlab_eng.initialize_mat(matlab_img, nargout=0) #super().__init__(matlab.double(input_image)) print("Matlab engine initialization finished")
def getIsoRankScore(A, B, L, Pa, Pb): eng = mateng.start_matlab() # eng.saveMat(matlab.double(A.tolist()), matlab.double(B.tolist()), matlab.double(L.tolist()), s, nargout=0) io.savemat('temp_iso.mat', dict(A=A, B=B, L=L, Pa=Pa, Pb=Pb)) accuracy = eng.runIsoRank(nargout=1) print "isorank: " + str(accuracy) eng.quit() return accuracy
def __init__(self, logger, beh): import matlab.engine as eng self.mat = eng.start_matlab() self.mat.run('c:/pipeline/pipeline/setPath.m', nargout=0) # get datajoint info from environment variables set in startup script self.mat.setenv('DJ_HOST',os.getenv('DJ_HOST'),nargout=0) self.mat.setenv('DJ_USER',os.getenv('DJ_USER'),nargout=0) self.mat.setenv('DJ_PASS',os.getenv('DJ_PASS'),nargout=0) self.trial = [] super(DaqStim, self).__init__(logger, beh)
def processChannelFile( filename, eng = None ): """ Process a single channel file, creating band information for each segment of data. A matlab engine can be passed in to reduce overhead. Returns a 2D list of band information in the order: [ delta, theta, alpha, beta, gamma ] for each time interval """ existing = True # Create a new matlab instance if one was not provided if eng == None: eng = start_matlab() existing = False # Add the current directory to the matlab path so that the function can be found eng.addpath( dirname(realpath(__file__)) ) channelData = [] with open( filename ) as fin: for line in fin: contents = line.strip().split('\t') # Remove the time stamp contents.pop(0) # Cast the elements to floats values = [ float(ele) for ele in contents ] # Make a matlab array from the data mArray = matlab.double(values) bands = eng.Bandify( mArray, nargout = 5 ) eng.clc(nargout = 0) eng.clear(nargout = 0) channelData.append( bands ) # Shutdown matlab if it was created here if not existing: eng.quit() return channelData
def cca(W, Z): """ Canonical Correlation Analysis (CCA): Returns rows of Z which are maximally correlated to W. """ print "CCA....." import matlab import matlab.engine as mateng eng = mateng.start_matlab() W_mat = matlab.double(W.tolist()) Z_mat = matlab.double(Z.tolist()) [A, B, r, U, V, stats] = eng.canoncorr(W_mat, Z_mat, nargout = 6) Z = np.array(V) eng.quit() return Z
def main(): print strftime("start time: %H:%M:%S") setGatewayIP() time.sleep(1) recordSound() # start matlab object eng = mat.start_matlab() ret = eng.soundtest(WAVE_OUTPUT_FILENAME) if int(ret) > 3: print "found %d" % ret getSnapShot() person = findPerson("detected", "image.jpg") person = person.split("\\")[1] print person, People[person] if person != None: sendSMS(ret, person) getVideo() sendEmail() print strftime("end time: %H:%M:%S")
def get_matlab_engine(): if sys.platform == 'win32': matlab = get_preferred_matlab() if not matlab: warnings.warn("MATLAB not found in registry. Using Python implementation.", RuntimeWarning) return None if matlab[0] == platform.architecture()[0]: MATLABROOT = matlab[2] engine = import_matlab_python_engine(MATLABROOT) return MatlabInProcessProxyReplacement(engine.start_matlab()) else: python_exe = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dist_{}\\python.exe'.format(matlab[0])) if not os.path.isfile(python_exe): raise Exception("'{}' does not exist. Run `setup.py py2exe` with Python {} in the matlab_proxy dir".format(python_exe, matlab[0])) return get_engine_proxy(matlab[2], python_exe) try: import matlab.engine return MatlabInProcessProxyReplacement(matlab.engine.start_matlab()) except ImportError as e: warnings.warn("Failed to import matlab.engine: %s" % e, RuntimeWarning) return None
def before__first_request_(): global eng global mappedX global cl_idx global Wtopk global voca global distanceMatrix global distanceMatrix_main tic = time.time() print "Starting matlab - ", eng = engine.start_matlab() eng.cd(os.path.dirname(os.getcwd())) print "%.4f" % (time.time() - tic) tic = time.time() # dataset # vis=0 # cnn=1 # nyt=2 # dataset=2 # if dataset ==0: # eng.main_topic(nargout=0) # distanceMatrix = io.loadmat('./../Visdata.mat')['DD'] # elif dataset ==1: # eng.main_cnn(nargout=0) # distanceMatrix = io.loadmat('./../result2.mat')['DD'] # else: eng.main_nyt(nargout=0) distanceMatrix = io.loadmat("./../nyt.mat")["DD"] mappedX = eng.workspace["mappedX"] cl_idx = eng.workspace["cl_idx"] Wtopk_idx = eng.workspace["Wtopk_idx"] voca = eng.workspace["dict"] print "%.4f" % (time.time() - tic) tic = time.time() print "Calculate data - ", Wtopk = [] for idxArray in Wtopk_idx: tempArray = [] for idx in idxArray: tempArray.append(voca[int(idx) - 1]) Wtopk.append(tempArray) cl_idx = cl_idx[0] # cl_idx = cl_idx # distanceMatrix = distanceMatrix # cl_idx = cl_idx # distanceMatrix = distanceMatrix sameTopicWeight = 0.8 differentTopicWeight = 1.2 distanceMatrix_main = supervisedTSNE( distanceMatrix, cl_idx, sameTopicWeight=sameTopicWeight, differentTopicWeight=differentTopicWeight ) print "%.4f" % (time.time() - tic) distanceMatrix_main = distanceMatrix_main.tolist()
def build_sample_graph(self): ####### Things that happen at system boot ######## #Users self.add_user_node('HR1') self.add_user_node('HR2') self.add_user_node('HR3') self.add_user_node('HR4') self.add_user_node('HR5') self.add_user_node('CEO') self.add_user_node('Dev1') self.add_user_node('Dev2') self.add_user_node('Dev3') self.add_user_node('Intern') self.add_user_node('External') #Machines self.add_machine_node('HR-laptop1.company.com') self.add_machine_node('HR-laptop2.company.com') self.add_machine_node('HR-laptop3.company.com') self.add_machine_node('HR-laptop4.company.com') self.add_machine_node('HR-laptop5.company.com') self.add_machine_node('HR-front-desk.company.com') self.add_machine_node('CEO-laptop.company.com') self.add_machine_node('CEO-desktop.company.com') self.add_machine_node('Dev-laptop1.company.com') self.add_machine_node('Dev-laptop2.company.com') self.add_machine_node('Dev-laptop3.company.com') self.add_machine_node('Dev-desktop1.company.com') self.add_machine_node('Dev-desktop2.company.com') self.add_machine_node('Dev-desktop3.company.com') self.add_machine_node('extra-computer1.company.com') self.add_machine_node('extra-computer2.company.com') self.add_machine_node('server.company.com') self.add_machine_node('database.company.com') self.add_machine_node('use-cluster.company.com') self.add_machine_node('sandbox-cluster.company.com') self.add_machine_node('R1') self.add_machine_node('R2') self.add_machine_node('R3') #Applications self.add_application_node('ETR') self.add_application_node('payroll-app') self.add_application_node('website-host') self.add_application_node('email-host') self.add_application_node('website-backup') self.add_application_node('employee-database') #Physical Topology self.add_physical_edge('R1', 'R2',0,20) self.add_physical_edge('R1', 'R3',0) self.add_physical_edge('HR-laptop1.company.com', 'R3',0) self.add_physical_edge('HR-laptop2.company.com', 'R3',0) self.add_physical_edge('HR-laptop3.company.com', 'R3',0) self.add_physical_edge('HR-laptop4.company.com', 'R3',0) self.add_physical_edge('HR-laptop5.company.com', 'R3',0) self.add_physical_edge('HR-front-desk.company.com', 'R3',0) self.add_physical_edge('CEO-laptop.company.com', 'R3',0) self.add_physical_edge('CEO-desktop.company.com', 'R3',0) self.add_physical_edge('Dev-laptop1.company.com', 'R3',0) self.add_physical_edge('Dev-laptop2.company.com', 'R3',0) self.add_physical_edge('Dev-laptop3.company.com', 'R3',0) self.add_physical_edge('Dev-desktop1.company.com', 'R3',0) self.add_physical_edge('Dev-desktop2.company.com', 'R3',0) self.add_physical_edge('Dev-desktop3.company.com', 'R3',0) self.add_physical_edge('extra-computer1.company.com', 'R3',0) self.add_physical_edge('extra-computer2.company.com', 'R3',0) self.add_physical_edge('server.company.com', 'R2',0) self.add_physical_edge('database.company.com', 'R2',0) self.add_physical_edge('use-cluster.company.com', 'R2',0) self.add_physical_edge('sandbox-cluster.company.com', 'R2',0) #Access Control self.add_access_edge('HR1', 'HR-laptop1.company.com', 'standard',0) self.add_access_edge('Dev1', 'HR-laptop1.company.com', 'sudo',0) self.add_access_edge('Dev2', 'HR-laptop1.company.com', 'sudo',0) self.add_access_edge('Dev3', 'HR-laptop1.company.com', 'sudo',0) self.add_access_edge('HR2', 'HR-laptop2.company.com', 'standard',0) self.add_access_edge('Dev1', 'HR-laptop2.company.com', 'sudo',0) self.add_access_edge('Dev2', 'HR-laptop2.company.com', 'sudo',0) self.add_access_edge('Dev3', 'HR-laptop2.company.com', 'sudo',0) self.add_access_edge('HR3', 'HR-laptop3.company.com', 'standard',0) self.add_access_edge('Dev1', 'HR-laptop3.company.com', 'sudo',0) self.add_access_edge('Dev2', 'HR-laptop3.company.com', 'sudo',0) self.add_access_edge('Dev3', 'HR-laptop3.company.com', 'sudo',0) self.add_access_edge('HR4', 'HR-laptop4.company.com', 'standard',0) self.add_access_edge('Dev1', 'HR-laptop4.company.com', 'sudo',0) self.add_access_edge('Dev2', 'HR-laptop4.company.com', 'sudo',0) self.add_access_edge('Dev3', 'HR-laptop4.company.com', 'sudo',0) self.add_access_edge('HR5', 'HR-laptop5.company.com', 'standard',0) self.add_access_edge('Dev1', 'HR-laptop5.company.com', 'sudo',0) self.add_access_edge('Dev2', 'HR-laptop5.company.com', 'sudo',0) self.add_access_edge('Dev3', 'HR-laptop5.company.com', 'sudo',0) self.add_access_edge('HR1', 'HR-front-desk.company.com', 'standard',0) self.add_access_edge('HR2', 'HR-front-desk.company.com', 'standard',0) self.add_access_edge('HR3', 'HR-front-desk.company.com', 'standard',0) self.add_access_edge('HR4', 'HR-front-desk.company.com', 'standard',0) self.add_access_edge('HR5', 'HR-front-desk.company.com', 'standard',0) self.add_access_edge('Dev1', 'HR-front-desk.company.com', 'sudo',0) self.add_access_edge('Dev2', 'HR-front-desk.company.com', 'sudo',0) self.add_access_edge('Dev3', 'HR-front-desk.company.com', 'sudo',0) self.add_access_edge('CEO', 'CEO-laptop.company.com', 'sudo',0) self.add_access_edge('Dev1', 'CEO-laptop.company.com', 'sudo',0) self.add_access_edge('CEO', 'CEO-desktop.company.com', 'sudo',0) self.add_access_edge('Dev1', 'CEO-desktop.company.com', 'sudo',0) self.add_access_edge('Dev1', 'Dev-laptop1.company.com', 'sudo',0) self.add_access_edge('Dev1', 'Dev-desktop1.company.com', 'sudo',0) self.add_access_edge('Dev2', 'Dev-laptop2.company.com', 'sudo',0) self.add_access_edge('Dev2', 'Dev-desktop2.company.com', 'sudo',0) self.add_access_edge('Dev3', 'Dev-laptop3.company.com', 'sudo',0) self.add_access_edge('Dev3', 'Dev-desktop3.company.com', 'sudo',0) self.add_access_edge('HR1', 'extra-computer1.company.com', 'standard',0) self.add_access_edge('HR2', 'extra-computer1.company.com', 'standard',0) self.add_access_edge('HR3', 'extra-computer1.company.com', 'standard',0) self.add_access_edge('HR4', 'extra-computer1.company.com', 'standard',0) self.add_access_edge('HR5', 'extra-computer1.company.com', 'standard',0) self.add_access_edge('CEO', 'extra-computer1.company.com', 'standard',0) self.add_access_edge('Intern', 'extra-computer1.company.com', 'standard',0) self.add_access_edge('Dev1', 'extra-computer1.company.com', 'sudo',0) self.add_access_edge('Dev2', 'extra-computer1.company.com', 'sudo',0) self.add_access_edge('Dev3', 'extra-computer1.company.com', 'sudo',0) self.add_access_edge('HR1', 'extra-computer2.company.com', 'standard',0) self.add_access_edge('HR2', 'extra-computer2.company.com', 'standard',0) self.add_access_edge('HR3', 'extra-computer2.company.com', 'standard',0) self.add_access_edge('HR4', 'extra-computer2.company.com', 'standard',0) self.add_access_edge('HR5', 'extra-computer2.company.com', 'standard',0) self.add_access_edge('CEO', 'extra-computer2.company.com', 'standard',0) self.add_access_edge('Intern', 'extra-computer2.company.com', 'standard',0) self.add_access_edge('Dev1', 'extra-computer2.company.com', 'sudo',0) self.add_access_edge('Dev2', 'extra-computer2.company.com', 'sudo',0) self.add_access_edge('Dev3', 'extra-computer2.company.com', 'sudo',0) self.add_access_edge('Dev1', 'server.company.com', 'sudo',0) self.add_access_edge('Dev2', 'server.company.com', 'standard',0) self.add_access_edge('Dev1', 'database.company.com', 'sudo',0) self.add_access_edge('Dev3', 'database.company.com', 'standard',0) self.add_access_edge('CEO', 'use-cluster.company.com', 'standard',0) self.add_access_edge('Intern', 'use-cluster.company.com', 'standard',0) self.add_access_edge('Dev1', 'use-cluster.company.com', 'sudo',0) self.add_access_edge('Dev2', 'use-cluster.company.com', 'sudo',0) self.add_access_edge('Dev3', 'use-cluster.company.com', 'sudo',0) self.add_access_edge('Dev1', 'sandbox-cluster.company.com', 'sudo',0) self.add_access_edge('Dev2', 'sandbox-cluster.company.com', 'standard',0) self.add_access_edge('Dev3', 'sandbox-cluster.company.com', 'standard',0) #Systems self.add_hosting_edge('ETR', 'use-cluster.company.com', 880,0) self.add_hosting_edge('payroll-app', 'use-cluster.company.com', 50,0) self.add_hosting_edge('website-host', 'server.company.com', 10,0) self.add_hosting_edge('email-host', 'server.company.com', 80,0) self.add_hosting_edge('website-backup', 'database.company.com', 880,0) self.add_hosting_edge('employee-database', 'database.company.com', 62,0) ####### Set times ############ random.seed(seed) flows = [] for i in range(200): flows.append((random.uniform(0,200),'HR1', 'payroll-app', 'HR1')) flows.append((random.uniform(0,200),'HR2', 'payroll-app', 'HR2')) flows.append((random.uniform(0,200),'HR3', 'payroll-app', 'HR3')) flows.append((random.uniform(0,200),'HR4', 'payroll-app', 'HR4')) flows.append((random.uniform(0,200),'HR5', 'payroll-app', 'HR5')) flows.append((random.uniform(0,200),'HR1', 'ETR', 'HR1')) flows.append((random.uniform(0,200),'HR2', 'ETR', 'HR2')) flows.append((random.uniform(0,200),'HR3', 'ETR', 'HR3')) flows.append((random.uniform(0,200),'HR5', 'ETR', 'HR5')) flows.append((random.uniform(0,200),'CEO', 'ETR', 'CEO')) flows.append((random.uniform(0,200),'Dev1', 'ETR', 'Dev1')) flows.append((random.uniform(0,200),'Dev2', 'ETR', 'Dev2')) flows.append((random.uniform(0,200),'Dev3', 'ETR', 'Dev3')) flows.append((random.uniform(0,200),'Intern', 'ETR', 'Intern')) flows.append((random.uniform(0,200),'HR1', 'email-host', 'HR1')) flows.append((random.uniform(0,200),'HR2', 'email-host', 'HR2')) flows.append((random.uniform(0,200),'HR3', 'email-host', 'HR3')) flows.append((random.uniform(0,200),'HR4', 'email-host', 'HR4')) flows.append((random.uniform(0,200),'HR5', 'email-host', 'HR5')) flows.append((random.uniform(0,200),'CEO', 'email-host', 'CEO')) flows.append((random.uniform(0,200),'Dev1', 'email-host', 'Dev1')) flows.append((random.uniform(0,200),'Dev2', 'email-host', 'Dev2')) flows.append((random.uniform(0,200),'Dev3', 'email-host', 'Dev3')) flows.append((random.uniform(0,200),'Intern', 'email-host', 'Intern')) flows.append((random.uniform(0,200),'Dev2', 'website-host', 'Dev2')) flows.append((random.uniform(0,200),'Dev3', 'employee-database', 'Dev3')) flows.append((random.uniform(0,200),'payroll-app', 'ETR', 'Dev1')) flows.append((random.uniform(0,200),'website-backup', 'website-host', 'Dev1')) ##External user is the weird one## for i in range(800): flows.append((random.uniform(0,200),'External', 'website-host', 'External')) for i in range(5000): flows.append((50+i/1000, 'External', 'website-host', 'External')) ####### Loop through the time windows and add flows/perform actions ############ i = 1 if take_action: engine = m.start_matlab() for flow in sorted(flows): if flow[0] >= i: #We have hit the next time window #print(i) if i > 10: self.print_steady_state_with_snapshot(i-1,i) ####Print out the throughput for this snapshot (Netflow case) times = [] with open('output/test/' + str(i-1) + '-' + str(i) + '_application_graph.csv') as f: reader = csv.reader(f) printzero = True for row in reader: if row[1] == 'website-host': num_bytes = float(row[2]) duration = float(row[3]) times.append(num_bytes/duration) printzero = False if printzero: times.append(0) print(statistics.mean(times)) #Decide to take action or not if take_action: engine.Main_demo(nargout=0,stdout=out,stderr=err) with open('output.csv') as f: reader = csv.reader(f) j = 0 for row in reader: if not j==0: if take_action and float(row[1]) > 0: print('Taking action against', row[0]) self.blocked_users['website-host'].append(row[0]) self.remove_pending_requests(i,row[0],'website-host') self.remove_pending_requests(i,row[0],'email-host') j += 1 #if i == 52: # self.blocked_users['website-host'].append('External') # self.remove_pending_requests(54,'External','website-host') # self.remove_pending_requests(54,'External','email-host') i += 1 self.create_modeled_dataflow_edge(flow[1], flow[2], flow[0], random.gauss(50,5), flow[3])
def featurize_cnn_features(list_of_demonstrations, kinematics, layer, folder, feature_index, net, sr = 3, config = [True, False, False], C = 100): # For config params [x,y,z] refers to perform PCA, CCA and GRP respectively data_X_PCA = {} data_X_CCA = {} data_X_GRP = {} big_Z = None # Initialization demonstration_size = {} init_demonstration = list_of_demonstrations[0] big_Z = utils.sample_matrix(load_cnn_features(init_demonstration, layer, folder, net), sampling_rate = sr) demonstration_size[init_demonstration] = big_Z.shape[0] kinematics_sampled = {} kinematics_sampled[init_demonstration] = utils.sample_matrix(kinematics[init_demonstration], sampling_rate = sr) dimensions_of_PCA = demonstration_size[init_demonstration] for demonstration in list_of_demonstrations[1:]: print "Loading Visual Features for ", demonstration Z = load_cnn_features(demonstration, layer, folder, net) Z_sampled = utils.sample_matrix(Z, sampling_rate = sr) print "Sampled visual", Z.shape, Z_sampled.shape dimensions_of_PCA += Z_sampled.shape[0] big_Z = np.concatenate((big_Z, Z_sampled), axis = 0) demonstration_size[demonstration] = Z_sampled.shape[0] kinematics_sampled[demonstration] = utils.sample_matrix(kinematics[demonstration], sampling_rate = sr) print "Sampled kinematics", kinematics_sampled[demonstration].shape print "PCA # of rows: ", dimensions_of_PCA PC = min(C, min(demonstration_size.values())) print "XXXX - PC:", str(PC) # Quick check to see if kinematics and visual features are aligned for demonstration in list_of_demonstrations: print demonstration_size[demonstration], kinematics_sampled[demonstration].shape[0] assert demonstration_size[demonstration] == kinematics_sampled[demonstration].shape[0] if config[0]: big_Z_pca = utils.pca_incremental(big_Z, PC = PC) if config[2]: big_Z_grp = utils.grp(big_Z) start = 0 end = 0 import matlab import matlab.engine as mateng eng = mateng.start_matlab() for demonstration in list_of_demonstrations: W = kinematics_sampled[demonstration] size = demonstration_size[demonstration] end = start + size # ------------- PCA ------------- if config[0]: Z = big_Z_pca[start:end] X = np.concatenate((W, Z), axis = 1) data_X_PCA[demonstration] = X # ------------- CCA ------------- if config[1]: Z = big_Z[start:end] Z = utils.cca(W, Z) X = np.concatenate((W, Z), axis = 1) data_X_CCA[demonstration] = X # ------------- GRP ------------- if config[2]: Z = big_Z_grp[start:end] X = np.concatenate((W, Z), axis = 1) data_X_GRP[demonstration] = X start += size if config[0]: pickle.dump(data_X_PCA, open(PATH_TO_FEATURES + str(feature_index) + "_PCA.p", "wb")) if config[1]: pickle.dump(data_X_CCA, open(PATH_TO_FEATURES + str(feature_index) + "_CCA.p", "wb")) if config[2]: pickle.dump(data_X_GRP, open(PATH_TO_FEATURES + str(feature_index) + "_GRP.p", "wb"))
import locale import json import tornado.ioloop import tornado.web from requestManager import RequestManager from matlab import engine ERROR_MESSAGE = "F**k. This wasn't supposed to happen." eng = engine.start_matlab() eng.cd('matlab') class MainHandler(tornado.web.RequestHandler): def get(self): data = None with open("hold.html", "r") as htmlfile: data = htmlfile.read().replace("\n", "") data = data if data is not None else ERROR_MESSAGE self.set_cookie("digitATM", "") self.write(data) class DebugHandler(tornado.web.RequestHandler): def get(self): data = None with open("debug.html", "r") as htmlfile: data = htmlfile.read().replace("\n", "") data = data if data is not None else ERROR_MESSAGE self.write(data)
def featurize_cnn_features_no_kinematics(list_of_demonstrations, layer, folder, feature_index, net, sr = 3, config = [True, False, True]): # For config params [x,y,z] refers to perform PCA, CCA and GRP respectively data_X_PCA = {} data_X_GRP = {} big_Z = None # Initialization demonstration_size = {} init_demonstration = list_of_demonstrations[0] big_Z = utils.sample_matrix(load_cnn_features(init_demonstration, layer, folder, net), sampling_rate = sr) demonstration_size[init_demonstration] = big_Z.shape[0] for demonstration in list_of_demonstrations[1:]: print "Loading Visual Features for ", demonstration Z = load_cnn_features(demonstration, layer, folder, net) Z_sampled = utils.sample_matrix(Z, sampling_rate = sr) big_Z = np.concatenate((big_Z, Z_sampled), axis = 0) demonstration_size[demonstration] = Z_sampled.shape[0] PC = min(100, min(demonstration_size.values())) if config[0]: big_Z_pca = utils.pca_incremental(big_Z, PC = PC) if config[2]: big_Z_grp = utils.grp(big_Z) start = 0 end = 0 import matlab import matlab.engine as mateng eng = mateng.start_matlab() for demonstration in list_of_demonstrations: size = demonstration_size[demonstration] end = start + size # ------------- PCA ------------- if config[0]: Z = big_Z_pca[start:end] data_X_PCA[demonstration] = Z # ------------- GRP ------------- if config[2]: Z = big_Z_grp[start:end] data_X_GRP[demonstration] = Z start += size if config[0]: pickle.dump(data_X_PCA, open(PATH_TO_FEATURES + str(feature_index) + "_PCA.p", "wb")) if config[2]: pickle.dump(data_X_GRP, open(PATH_TO_FEATURES + str(feature_index) + "_GRP.p", "wb"))
# decode print ("coder: %s" % strCoder) arrDecoded_bin_1 = decode(eng, arrEncoded_bin_1, n, k, strCoder) arrDecoded_bin_2 = decode(eng, arrEncoded_bin_2, n, k, strCoder) print "decoded1(%d): " % len(arrDecoded_bin_1), arrDecoded_bin_1 print "decoded2(%d): " % len(arrDecoded_bin_2), arrDecoded_bin_2 # BER nCount = 0 for i in xrange(len(arrDecoded_bin_1)): if arrDecoded_bin_1[i] != arrDecoded_bin_2[i]: nCount += 1 dBER = nCount * 1.0 / len(arrDecoded_bin_1) print "BER: ", dBER if __name__ == "__main__": eng = mtEngine.start_matlab() #%% print "** error correction test **" n = 7 k = 4 strCoder = "hamming/binary" arrEncoded = np.random.randint(0, 2, 140) test_error_correction(eng, arrEncoded, 5, n, k, strCoder) #%% eng.quit()