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']
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
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
def start_matlab(): prog = find_executable('matlab') if not prog: sys.exit("Could not find MATLAB on the PATH. Exiting...") else: print("Found MATLAB in "+prog) mlab = Matlab(executable=prog) mlab.start() return mlab
def __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 __init__(self): super().__init__() self.matlab = Matlab() # As of July 2016, there seems to be a bug which wrecks the data # dimensionality when feeding it to MATLAB, causing a matrix dimension # mismatch to happen. raise ValueError("MATLAB interop via pymatbridge doesn't work.")
def random_sampling_matlab(self): # Perform test via MATLAB from pymatbridge import Matlab mlab = Matlab() mlab.start() mlab.run_code('cvx_solver mosek;') mlab.run_code("addpath '~/mosek/7/toolbox/r2012a';") self.mlab = mlab if self.block_sizes is not None and len(self.block_sizes) == \ self.A.shape[1]: self.output['error'] = "Trivial example: nblocks == nroutes" logging.error(self.output['error']) self.mlab.stop() self.mlab = None return duration_time = time.time() p = self.mlab.run_func('%s/scenario_to_output.m' % self.CS_PATH, { 'filename' : self.fname, 'type': self.test, 'algorithm' : self.method, 'outpath' : self.OUT_PATH }) duration_time = time.time() - duration_time self.mlab.stop() return array(p['result'])
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)
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)
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 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()
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)
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()
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 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()
def main(): #start matlab connection mlab = Matlab() mlab.start() vehicle = None data=None vehicle1=None vehicle2=None vehicle3=None vehicle4=None vehicle5=None actor_list = [] sensors = [] argparser = argparse.ArgumentParser( description=__doc__) argparser.add_argument( '--host', metavar='H', default='127.0.0.1', help='IP of the host server (default: 127.0.0.1)') argparser.add_argument( '-p', '--port', metavar='P', default=2000, type=int, help='TCP port to listen to (default: 2000)') args = argparser.parse_args() try: client = carla.Client(args.host, args.port) client.set_timeout(2.0) world = client.get_world() ourMap = world.get_map() debug = world.debug; #********* setting fixed time stamp for simulation ************** settings = world.get_settings(); settings.fixed_delta_seconds = 0.05; world.apply_settings(settings); spectator = world.get_spectator(); #*************** deffinition of sensors **************************** camera_blueprint = world.get_blueprint_library().find('sensor.camera.rgb') #*********** definition of blueprints for vehicles ******************* blueprint = world.get_blueprint_library().find('vehicle.audi.tt') #for main(ego) vehicle blueprint.set_attribute('role_name', 'hero') #*********** for non-player vehicles ********************************* non_playerBlueprint1 = random.choice(world.get_blueprint_library().filter('vehicle.bmw.grandtourer')) non_playerBlueprint2 = random.choice(world.get_blueprint_library().filter('vehicle.tesla.*')) non_playerBlueprint4 = random.choice(world.get_blueprint_library().filter('vehicle.mercedes-benz.coupe')) non_playerBlueprint3 = random.choice(world.get_blueprint_library().filter('vehicle.toyota.*')) #******************************* set weather ********************************** weather = carla.WeatherParameters( cloudyness=0.0, precipitation=0.0, sun_altitude_angle=70.0, sun_azimuth_angle=50.0) world.set_weather(weather) #************************ spawn non-player vehicles ****************************** class spawn_vehicle: def __init__(self, blueprint, x, y): self.vehicle=None; spawn_points = ourMap.get_spawn_points() spawn_point = spawn_points[30]; spawn_point.location.x += x; spawn_point.location.y += y; self.vehicle = world.spawn_actor(blueprint, spawn_point) #first non-player vehicle data=spawn_vehicle(non_playerBlueprint1, -20, 1.0); vehicle1=data.vehicle; actor_list.append(vehicle1) #second non-player vehicle data=spawn_vehicle(non_playerBlueprint2, -12, 1.0); vehicle2=data.vehicle; actor_list.append(vehicle2) # 3rd non-player vehicle data=spawn_vehicle(non_playerBlueprint3, 2.0, 1.0); vehicle3=data.vehicle; actor_list.append(vehicle3) #4th nonplayer vehicle data=spawn_vehicle(non_playerBlueprint4, 10.0, 1.0); vehicle4=data.vehicle; actor_list.append(vehicle4) #********************** spawn main vehicle ***************************** data=spawn_vehicle(blueprint, -23.0, -1.5); vehicle=data.vehicle; actor_list.append(vehicle) #set the first movement of ego car control = carla.VehicleControl(throttle=0.1) vehicle.apply_control(control) #************************ camera-sensor settings *********************** camera_location = carla.Transform(carla.Location(x=1.3, z=2.0)) camera_blueprint.set_attribute('sensor_tick', '0.4'); camera_sensor = world.spawn_actor(camera_blueprint, camera_location, attach_to=vehicle); #=======================================obstacle sensors for maneuver============================================================== class ObstacleSensor(object): def __init__(self, parent_actor,x,y,z,angle): self.sensor = None self._parent = parent_actor self.actor = 0.0 self.distance = 0.0 self.x=x self.y=y self.z=z self.angle=angle; world = self._parent.get_world() bp = world.get_blueprint_library().find('sensor.other.obstacle') bp.set_attribute('debug_linetrace', 'false'); self.sensor = world.spawn_actor(bp, carla.Transform(carla.Location(x=self.x, y=self.y, z=self.z), carla.Rotation(yaw=self.angle)), attach_to=self._parent); sensors.append(self.sensor); weak_self = weakref.ref(self) self.sensor.listen(lambda event: ObstacleSensor._on_event(weak_self, event)) @staticmethod def _on_event(weak_self, event): self = weak_self() if not self: return self.actorId = event.other_actor.id self.actorName = event.other_actor.type_id self.distance = event.distance forward_sensor = ObstacleSensor(vehicle, x=1.3, y=0.9, z=0.6, angle=90); rearSide_sensor = ObstacleSensor(vehicle, x=-1.3, y=0.9, z=0.6, angle=90); center_sensor = ObstacleSensor(vehicle, x=0.0, y=0.9, z=0.6, angle=90); back_sensor = ObstacleSensor(vehicle, x=-1.3, y=0.0, z=0.6, angle=180); #====================================step1:Parking Decision========================================================== global image; time.sleep(5.0); textLocation = vehicle.get_location(); textLocation.x +=5.0; textLocation.y -=0.8; textLocation.z +=1.8; startText = "Search for parking place" debug.draw_string(textLocation, startText, True, carla.Color(255,255,0), 3, True); textLocation.y +=1.0; debug.draw_box(carla.BoundingBox(textLocation,carla.Vector3D(0.2,1.5,0.5)),carla.Rotation(yaw=180), 0.1, carla.Color(255,0,0),3, True); camera_sensor.listen(lambda image: parking_decision(image)); def parking_decision(data): camera_sensor.stop(); cameraControl = carla.VehicleControl(throttle=0); vehicle.apply_control(cameraControl); output=interface.decision(data,mlab); overlapRatio = output['result']; print('rate of Overlap:', overlapRatio); if overlapRatio < 0.1: print('parking place is between the next 2 vehicles'); location = vehicle.get_location(); location.x +=15; location.y +=1.0; location.z +=1.8; text = 'Parking vacancy detected'; debug.draw_string(location, text, True, carla.Color(255,255,0), 4.0, True); location.y +=1.5; debug.draw_box(carla.BoundingBox(location,carla.Vector3D(0.2,2.0,0.8)),carla.Rotation(yaw=180), 0.1, carla.Color(255,0,0),4.0, True); time.sleep(3.0); mlab.end(); if hasattr(center_sensor, 'actorId'): print('center_sensor info in detection time:', center_sensor.actorName, center_sensor.actorId); if center_sensor.actorId != 0: number=2; else: number=1; print('matlab disconnected'); parking_preparation(number); else: print('no parking place has been detected right now'); cameraControl = carla.VehicleControl(throttle=0.2); vehicle.apply_control(cameraControl); print('car is moving'); camera_sensor.listen(lambda image: parking_decision(image)); #==================================step2:Parking Preparation(position phase)============================================= def parking_preparation(limit): print('limit',limit); x1=x2=0; vehicle.apply_control(carla.VehicleControl(throttle=0.3)); while config.count < limit: current_vhcl = center_sensor.actorId; if config.nonplayer != current_vhcl and current_vhcl != 0: config.nonplayer = current_vhcl; config.count += 1; continue; else: first_parked_car = forward_sensor.actorId; print('first_parked_car', first_parked_car); while config.count == limit: if forward_sensor.actorId == 0: print('first_parked_car passed'); config.parkingWidth = forward_sensor.distance; #lateral distance of the parking bay x1 = vehicle.get_location().x; #vehicle first place in parking bay x1 -= 1.3; #because this location is from center of the car print('parkingWidth:', config.parkingWidth, 'x1:', x1); break; while forward_sensor.actorId == 0: continue; else: if forward_sensor.actorId != 0 and forward_sensor.actorId != first_parked_car: print('we reached second static car:', forward_sensor.actorId); print('second_parked_car', forward_sensor.actorId); x2 = vehicle.get_location().x; #vehicle second place in parking bay x2 += 1.3; config.parkingLength = abs(x2 - x1); #length of the parking print('parkingLength:', config.parkingLength, 'x2:', x2); while rearSide_sensor.distance > 1.0: vehicle.apply_control(carla.VehicleControl(steer=0.0, throttle=0.2)); time.sleep(1.0); if rearSide_sensor.actorId != 0: #when the static vehicle detected vehicle.apply_control(carla.VehicleControl(steer=0.0, throttle=0.0, brake=1.0)); parking_maneuver(); break; else: vehicle.apply_control(carla.VehicleControl(steer=0.0, throttle=0.0, brake=1.0)); time.sleep(3.0); parking_maneuver(); #====================================step3:Parking Maneuver================================================================= def parking_maneuver(): time.sleep(3.0); maneuver.calculate_maneuverTime(vehicle); maneuver.calculate_max_steeringAng(vehicle); time.sleep(3.0); for t in numpy.arange(0,config.T,config.sampling_period): time.sleep(0.08); if (hasattr(back_sensor, 'actorId') and (back_sensor.actorId != 0) and (back_sensor.distance < 4)): #or (hasattr(rearSide_sensor, 'actorId') and (rearSide_sensor.actorId == 0) and (rearSide_sensor.distance < 5.0)): print('1****vehicle should be stopped!', back_sensor.distance); vehicle.apply_control(carla.VehicleControl(throttle=0, steer=0.0, brake=1.0)); control=vehicle.get_control(); print('throttle', control.throttle, 'brake', control.brake, 'steer', control.steer) else: print('2****move'); maneuver.parking(t,vehicle); #call parking function from maneuver.py control=vehicle.get_control(); print('throttle', control.throttle, 'brake', control.brake, 'steer', control.steer) #set simulator view to the location of the vehicle while True: time.sleep(1.0) viewLocation = vehicle.get_location(); viewLocation.z += 1.0; spectator.set_transform(get_transform(viewLocation, -180)) finally: print('\ndestroying %d actors' % len(actor_list)) for actor in actor_list: if actor is not None: actor.destroy() for sensor in sensors: if sensor is not None: sensor.destroy()
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()
""" This script was used to generate dwt_matlabR2012a_result.npz by storing the outputs from Matlab R2012a. """ from __future__ import division, print_function, absolute_import import numpy as np import pywt try: from pymatbridge import Matlab mlab = Matlab() _matlab_missing = False except ImportError: print("To run Matlab compatibility tests you need to have MathWorks " "MATLAB, MathWorks Wavelet Toolbox and the pymatbridge Python " "package installed.") _matlab_missing = True if _matlab_missing: raise EnvironmentError("Can't generate matlab data files without MATLAB") size_set = 'reduced' # list of mode names in pywt and matlab modes = [('zero', 'zpd'), ('constant', 'sp0'), ('symmetric', 'sym'), ('periodic', 'ppd'), ('smooth', 'sp1'), ('periodization', 'per')]
:Created on: 26/01/2015 9:55 """ __author__ = 'bejar' from pymatbridge import Matlab from config.experiments import experiments, lexperiments import time # lexperiments = ['e130716', 'e130827', 'e130903', 'e141113', 'e141029', 'e141016', 'e140911', 'e140311', 'e140225', 'e140220'] lexperiments = ['130827']#'e130827','e140225', 'e140220', 'e141016', 'e140911'] mlab = Matlab(executable='/home/bejar/bin/MATLAB/R2014b/bin/matlab') mlab.start() a = mlab.run_code('cd(\'/home/bejar/PycharmProjects/PeakDataAnalysis/Matlab/\')') print a datasufix = ''#'-RawResampled' wtime = '120e-3' # Window length in miliseconds for expname in lexperiments: datainfo = experiments[expname] sampling = datainfo.sampling #/ 6.0 for file in [datainfo.datafiles[0]]: print time.ctime()
same_label = [] row = 0 for i in range(len(samples)): for j in range(i + 1, len(samples)): pdist[row, :] = distance_vector(i, j) same_label.append(1 if samples[i].label == samples[j].label else -1) row += 1 theta = np.ones((31, )) / np.sqrt(31) class_size = Counter(same_label) sl = np.array(same_label).astype(float) sl[sl > 0] = 1.0 / class_size[1] sl[sl < 0] = -1.0 / class_size[1] A = sl.reshape(len(same_label), 1) * pdist np.linalg.norm(np.dot(A, theta)) from pymatbridge import Matlab mlab = Matlab(matlab='/m/fs/software/matlab/r2014a/bin/glnxa64/MATLAB', maxtime=10) 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):
'confidenceLine.m', 'dist_value.m', 'extrema.m'] for files in checkIMFs_mfile_list: shutil.move('./Matlab_runcode/' + files, checkIMFs_dir) HSA_mfile_list = os.listdir('./Matlab_runcode/') for files in HSA_mfile_list: shutil.move('./Matlab_runcode/' + files, HSA_dir) # Delete unnecessary directories/files os.remove('EEMD.zip') os.remove('Matlab_runcode.zip') os.rmdir('Matlab_runcode') print('...Done.') # Check the MATLAB version & replace the deprecated function with the new one. mlab = Matlab() print('* Checking your MATLAB version...') mlab.start() mlab.run_code('v = version;') version = mlab.get_variable('v') mlab.stop() print('Your MATLAB version is: ' + version) version = version.split('.') if int(version[0]) >= 8: print('The function "getDefaultStream" in eemd.m is no longer be used ' + 'in your MATLAB version.') print('* Replacing it with the function "getGlobalStream"...') with open(EEMD_dir + 'eemd.m', 'r', encoding='iso-8859-1') as infile: data = infile.read().replace('getDefaultStream', 'getGlobalStream') infile.close() with open(EEMD_dir + 'eemd2.m', 'w',encoding='iso-8859-1') as outfile:
def train_save_sp_tensor(self, pmi=True): gatherer = self.get_pmi_gatherer(3) if pmi: print('creating PPMI tensor...') else: print('creating sparse count tensor...') indices, values = gatherer.create_pmi_tensor(positive=True, debug=True, symmetric=False, pmi=pmi, shift=-np.log2(15.)) matfile_name = 'sp_tensor_{}_{}_log15.mat'.format( self.num_sents, self.min_count) scipy.io.savemat(matfile_name, {'indices': indices, 'values': values}) print('saved {}. exiting.'.format(matfile_name)) sys.exit() from pymatbridge import Matlab session = Matlab('/usr/local/bin/matlab') print('starting matlab session...') session.start() #session.set_variable('indices', indices+1) #session.set_variable('vals', values) print('setting up variables...') session.run_code("d = load('/home/eric/code/gensim/sp_tensor.mat');") session.run_code("indices = d.indices + 1;") session.run_code("vals = d.values';") #session.run_code('size_ = [{0} {0} {0}];'.format(len(self.model.vocab))) session.run_code('size_ = [{0} {0} {0}];'.format(8)) session.run_code('R = {};'.format(self.embedding_dim)) import pdb pdb.set_trace() res = session.run_code("T = sptensor(indices, vals, size_);") print('running ALS...') t = time.time() res = session.run_code('[P, U0, out] = cp_als(T, R)') print('ALS took {} secs'.format(time.time() - t)) session.run_code('lambda = P.lambda;') session.run_code('U = P{1,1};') session.run_code('V = P{2,1};') session.run_code('W = P{3,1};') lambda_ = session.get_variable('lambda') U = session.get_variable('U') import pdb pdb.set_trace() '''
def process(self): mlab = Matlab() mlab.start() return mlab
n_test = np.repeat(ref_n[..., None], ref_llr.shape[-1], axis=1) # Apply conjugate operator to features if enc_cfg['conj_inputs']: y_test = np.matmul(np.conj(np.swapaxes(h_test, -2, -1)), y_test[..., None]) h_test = np.matmul(np.conj(np.swapaxes(h_test, -2, -1)), h_test) y_test = np.reshape(y_test, (-1, num_rx)) h_test = np.reshape(h_test, h_test.shape[:-2] + (-1, )) h_test = np.reshape(h_test, (-1, num_rx * num_tx)) n_test = np.reshape(n_test, (-1, 1)) # Convert complex to reals y_test = y_test.view(np.float64) h_test = h_test.view(np.float64) # Start Matlab engine eng = Matlab() eng.start() # Move to right path eng.run_code( 'cd /home/yanni/marius/deep-llr-quantization-master/deep-llr-quantization-master/' ) # How many runs num_runs = 1 # Global metrics local_seed_collect = np.zeros((num_runs, )) bler_ae, ber_ae = np.zeros((num_runs, num_snr)), np.zeros((num_runs, num_snr)) bler_aeq, ber_aeq = np.zeros((num_runs, len(bits_per_dim), num_snr)), np.zeros( (num_runs, len(bits_per_dim), num_snr)) bler_enc, ber_enc = np.zeros((num_runs, num_snr)), np.zeros( (num_runs, num_snr))
ground_truth_folder=args.ground_truth_path), batch_size=batch_size, shuffle=True, drop_last=True) test_loader = data.DataLoader(dataset=CleansingDataset( root=os.path.join(args.dataset, 'test_dataset'), transform=test_transform, dataset=args.dataset, special_list=[], filter_type='not_in', feature_folder=feature_floder, ground_truth_folder='annotation_folder'), batch_size=batch_size, shuffle=True, drop_last=True) train(model, train_loader, test_loader, epochs, args.logdir, savepath) get_resnet_result(model, args.dataset, savepath) endtime = datetime.datetime.now() print('code ends at ', endtime - starttime) if args.use_pymatbridge: from pymatbridge import Matlab mlab = Matlab() mlab.start() results = mlab.run_code('run {}/eva/Main.m'.format(args.dataset)) meanJacc, stdJacc, meanAcc, stdAcc = mlab.get_variable( 'meanJacc'), mlab.get_variable('stdJacc'), mlab.get_variable( 'meanAcc'), mlab.get_variable('stdAcc')
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 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()
scaled_for_color = math.ceil(32 * pixel_normalized_val + 32) if scaled_for_color == 0: scaled_for_color = 1 group["intelligent_icon"][row][column] = scaled_for_color if add_struct_comma_to_expr: matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "," else: add_struct_comma_to_expr = True matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "struct('title', '" + group_name + "', 'patches', [" add_pixel_comma_to_expr = False #flatten the bitmap into a vector of pixels (see matlab/show_icons.m) for row in range(row_pixel_count): for column in range(row_pixel_count): if add_pixel_comma_to_expr: matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "," else: add_pixel_comma_to_expr = True matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + str(group["intelligent_icon"][row][column]) matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "])" matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "]" print("rendering intelligent icon grid") #initialise matlab bridge mlab = Matlab(port=4000, matlab=matlab_path) mlab.start() #i cant imagine more ghetto way to do this, but it seems to be the only way to pass this data through some bug in the data parsing part of the bridge (Which i didnt write, for the record!!!) icon_data_expr_encoded = base64.b64encode(matlab_intelligent_icon_visualization_data_expr) print(icon_data_expr_encoded) mlab.run(base_dir + "/matlab/show_icons.m", {"encoded": icon_data_expr_encoded}) print("done without errors")
def readSUN3D_singleData(list_dir, threshold_PCE, threshold_camDist, threshold_overlap, threshold_iter, choose_secondFrame, output): mlab = Matlab(executable='/usr/bin/matlab') mlab.start() dataDir = list_dir[random.randint(0, len(list_dir) - 1)] print(dataDir) minFrame = 1 maxFrame = len(glob(dataDir + 'image/*')) frameLength = maxFrame #print (minFrame,maxFrame) frameID = random.randint(minFrame, maxFrame) frameIDAnother = -1 count = 1 while (True): if (frameID == frameIDAnother): frameID = random.randint(1, frameLength) else: while (True): frameIDAnother = random.randint( max(minFrame, frameID - choose_secondFrame), min(maxFrame, frameID + choose_secondFrame)) if (frameID != frameIDAnother): break output_tmp = dataLoad_SUN3D(mlab, dataDir, frameID, frameIDAnother, '', './SUN3Dflow_py.m', False) #print(output_tmp['camDist'], output_tmp['pce'] ,output_tmp['overlapRatio']) if (output_tmp['camDist'] > threshold_camDist and output_tmp['pce'] < threshold_PCE and output_tmp['overlapRatio'] > threshold_overlap and output_tmp['overlapRatio'] < 1): """ egomotion_extended = np.zeros(7) egomotion_tmp = output_tmp['egomotion'] scale_tmp = np.linalg.norm(egomotion_tmp[0:3]) egomotion_tmp[0:3] = egomotion_tmp[0:3]/scale_tmp egomotion_extended[0:6] = egomotion_tmp egomotion_extended[6] = scale_tmp output_tmp['egomotion'] = egomotion_extended """ print('finished') output.put(output_tmp) break """ scale_tmp = np.linalg.norm(egomotion_tmp[0:3]) target_egomotion[iterBatch,0:3] = egomotion_tmp[0:3]/scale_tmp target_egomotion[iterBatch,3:3] = egomotion_tmp[3:3] target_egomotion[iterBatch,6] = scale_tmp """ else: # print(count) count = count + 1 if (output_tmp['overlapRatio'] <= threshold_overlap): minFrame = min(frameID, frameIDAnother) maxFrame = max(frameID, frameIDAnother) if (count == threshold_iter): count = 1 frameIDAnother = frameID minFrame = 1 maxFrame = frameLength
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()
y(n, s, :) = tempt.*(tempx*2 - 1); rt(n, s, :) = tempt; acc(n, s, :) = tempx; end end '''.format(nsims, nsubs, ntrials) # Write out code modelfile = 'simultrialparams.m' f = open(modelfile, 'w') f.write(matlabcode) f.close() # Simulate some data from simuldiff in MATLAB (find a way to do this in Python) mlab = Matlab(maxtime=240) # Increase max start time mlab.start() results2 = mlab.run_code('simultrialparams;') genparam = dict() genparam['tersub'] = mlab.get_variable('tersub') genparam['alphasub'] = mlab.get_variable('alphasub') genparam['deltasub'] = mlab.get_variable('deltasub') genparam['tertrialsd'] = mlab.get_variable('tertrialsd') genparam['deltatrialsd'] = mlab.get_variable('deltatrialsd') genparam['prob_mindwander'] = mlab.get_variable('prob_mindwander') genparam['rt'] = mlab.get_variable('rt') genparam['acc'] = mlab.get_variable('acc') genparam['y'] = mlab.get_variable('y') mlab.stop() sio.savemat('genparam_test.mat', genparam)
#_*_ coding: utf-8 _*_ from block import render_notext, path from experiment_global import features #from experiment_global import data as ed from dataset import DataSet import json from pymatbridge import Matlab mlab = Matlab(executable='matlab') mlab.start() #extract features from the website.png def get_feature(url): if url: sitename = render_notext(url) print sitename data = DataSet() data.load_sample(path, sitename) f2 = features.extract(data.data[0]) dstr = '[%s]' % (','.join(map(str, list(f2))), ) #dstr is a matrix of array return dstr #send features to matlab and return score def matlab(d): if not d: return 'err'
def print_matlab_script(path): with open(path, 'r') as f: code = f.read() print_matlab_code(code) def print_python_function(fun): code = inspect.getsource(fun) print_python_code(code) # ## Start Matlab matlab = Matlab() matlab.start() # ## Add `NoiseTools` to the Matlab's path matlab.run_code('addpath(\'{}\')'.format(noise_tools_dir)) # # Simulate data # Let's look at the example 1 code: print_matlab_script(example_1) # Let's create synthetic data in Matlab and transfer it here. example_1_code = open(example_1, 'r').readlines()
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) + '/')
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
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 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)
from IPython.display import clear_output import matplotlib.pyplot as plt import numpy as np from numpy import sin, cos from pymatbridge import Matlab get_ipython().magic(u'matplotlib inline') execfile('../../matplotlibrc.py') # Run Matlab code and fetch relevant data mlab = Matlab() mlab.start() results = mlab.run_code(open('fem1d.m').read()) K = mlab.get_variable('K') U = mlab.get_variable('U') nodeLocs = mlab.get_variable('nodeLocs') mlab.stop() clear_output() print('K') print(K) print('U') print(U) x = nodeLocs[:,0] xex = np.linspace(0, 1); w = np.sqrt(2); uex = (cos(w) - 1)*sin(w*xex)/(2.0*sin(w)) - 0.5*cos(w*xex) + 0.5 fig, ax = plt.subplots(figsize=(14, 10)) ax.plot(xex, uex, 'k-', label='Exact') ax.plot(x, U, 'b-o', label='FEM')
def __init__(self, parsed, source, mode, formatdict): from pymatbridge import Matlab self.matlab = Matlab() self.matlab.start() PwebProcessor.__init__(self, parsed, source, mode, formatdict)
for (dirpath, dirnames, filenames) in os.walk("Database"): first_level_in_database.extend(dirnames) break first_level_in_database = sorted(first_level_in_database) # Name of the file in each directory for i in range(0, len(first_level_in_database)): for root, dirs, files in os.walk( os.path.abspath("Database/" + first_level_in_database[i])): for file in sorted(files): second_level.append(os.path.join(root, file)) if not os.path.exists("Database_5pt"): os.makedirs("Database_5pt") size = 144 * 144 mlab = Matlab(matlab='/Applications/MATLAB_R2015a.app/bin/matlab') mlab.start() 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)
""" This script was used to generate dwt_matlabR2012a_result.npz by storing the outputs from Matlab R2012a. """ from __future__ import division, print_function, absolute_import import numpy as np import pywt try: from pymatbridge import Matlab mlab = Matlab() _matlab_missing = False except ImportError: print("To run Matlab compatibility tests you need to have MathWorks " "MATLAB, MathWorks Wavelet Toolbox and the pymatbridge Python " "package installed.") _matlab_missing = True if _matlab_missing: raise EnvironmentError("Can't generate matlab data files without MATLAB") size_set = 'reduced' # list of mode names in pywt and matlab modes = [('zero', 'zpd'), ('constant', 'sp0'), ('symmetric', 'sym'), ('periodic', 'ppd'), ('smooth', 'sp1'), ('periodization', 'per')] families = ('gaus', 'mexh', 'morl', 'cgau', 'shan', 'fbsp', 'cmor') wavelets = sum([pywt.wavelist(name) for name in families], []) rstate = np.random.RandomState(1234)
numImf = 10 runCEEMD = 1 maxSift = 10 typeSpline = 2 toModifyBC = 1 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:')
def startMatlab(): logging.info("startMatlab: was called") mlab = Matlab(executable=Constants.MATLAB_EXECUTABLE) mlab.start() return mlab
def __init__(self, *args, **kwargs): excecutable = kwargs.pop('excecutable', 'matlab') super(MatlabKernel, self).__init__(*args, **kwargs) self._matlab = Matlab(excecutable) self._matlab.start()
# Run a more comprehensive set of problem sizes. This could take more than # an hour to complete. size_set = 'full' use_precomputed = False else: size_set = 'reduced' use_precomputed = True if use_precomputed: data_dir = os.path.join(os.path.dirname(__file__), 'data') matlab_data_file = os.path.join(data_dir, 'dwt_matlabR2012a_result.npz') matlab_result_dict = np.load(matlab_data_file) else: try: from pymatbridge import Matlab mlab = Matlab() _matlab_missing = False except ImportError: print("To run Matlab compatibility tests you need to have MathWorks " "MATLAB, MathWorks Wavelet Toolbox and the pymatbridge Python " "package installed.") _matlab_missing = True # list of mode names in pywt and matlab modes = [('zero', 'zpd'), ('constant', 'sp0'), ('symmetric', 'sym'), ('reflect', 'symw'), ('periodic', 'ppd'), ('smooth', 'sp1'), ('periodization', 'per')] families = ('db', 'sym', 'coif', 'bior', 'rbio') wavelets = sum([pywt.wavelist(name) for name in families], [])
from IPython.display import clear_output import matplotlib.pyplot as plt import numpy as np from numpy import sin, cos from pymatbridge import Matlab get_ipython().magic(u'matplotlib inline') execfile('../../matplotlibrc.py') # Run Matlab code and fetch relevant data mlab = Matlab() mlab.start() results = mlab.run_code(open('fem1d.m').read()) K = mlab.get_variable('K') U = mlab.get_variable('U') nodeLocs = mlab.get_variable('nodeLocs') mlab.stop() clear_output() print('K') print(K) print('U') print(U) x = nodeLocs[:, 0] xex = np.linspace(0, 1) w = np.sqrt(2) uex = (cos(w) - 1) * sin(w * xex) / (2.0 * sin(w)) - 0.5 * cos(w * xex) + 0.5 fig, ax = plt.subplots(figsize=(14, 10)) ax.plot(xex, uex, 'k-', label='Exact') ax.plot(x, U, 'b-o', label='FEM')
im_gt = [] files_gt = glob.glob(IMAGE_GT_FILE) for f in files_gt: #print 'loading', f im = np.array(Image.open(f)) im = utils.modcrop(im, UP_SCALE).astype(np.float32) im_gt += [im] im_l = [] if len(IMAGE_FILE) > 0: assert (len(im_gt) == 1) im_l = [np.array(Image.open(IMAGE_FILE)).astype(np.float32)] else: #down scale from ground truth using Matlab try: from pymatbridge import Matlab mlab = Matlab() mlab.start() for im in im_gt: mlab.set_variable('a', im) mlab.set_variable('s', 1.0 / UP_SCALE) mlab.run_code('b=imresize(a, s);') im_l += [mlab.get_variable('b')] mlab.stop() except: print 'failed to load Matlab!' assert (0) #im_l = utils.imresize(im_gt, 1.0/UP_SCALE) #upscaling #sr = Bicubic() sr = SCN(MODEL_FILE)
def __init__(self, *args, **kwargs): super(MatlabKernel, self).__init__(*args, **kwargs) executable = os.environ.get('MATLAB_EXECUTABLE', 'matlab') self._matlab = Matlab(executable) self._matlab.start()
from pymatbridge import Matlab mlab = Matlab() mlab.start() res = mlab.run_code('a=10, b=a+3') mlab.stop()
im_gt = [] files_gt = glob.glob(IMAGE_GT_FILE) for f in files_gt: #print 'loading', f im = np.array(Image.open(f)) im = utils.modcrop(im, UP_SCALE).astype(np.float32) im_gt += [im] im_l = [] if len(IMAGE_FILE)>0: assert(len(im_gt)==1) im_l = [np.array(Image.open(IMAGE_FILE)).astype(np.float32)] else: #down scale from ground truth using Matlab try: from pymatbridge import Matlab mlab = Matlab() mlab.start() for im in im_gt: mlab.set_variable('a', im) mlab.set_variable('s', 1.0/UP_SCALE) mlab.run_code('b=imresize(a, s);') im_l += [mlab.get_variable('b')] mlab.stop() except: print 'failed to load Matlab!' assert(0) #im_l = utils.imresize(im_gt, 1.0/UP_SCALE) #upscaling #sr = Bicubic() sr = SCN(MODEL_FILE)
import os import sys from pymatbridge import Matlab sys.path.append('/Users/stephane/Documents/git/takumi/fapm/cine_local') matlab_path = '/Applications/MATLAB_R2019a.app/bin/matlab' faqm_dir = os.path.split(os.path.realpath(__file__))[0] matlabdir = os.path.join(faqm_dir, 'matlab_codes') # DATA ARCHITECTURE scriptdir, scriptname = os.path.split(os.path.realpath(__file__)) mlab = Matlab(executable=matlab_path) mlabstart = mlab.start() mlab.run_code('addpath %s; savepath;' % matlabdir) print('DONE') print('... MATLAB may crash by this process, but the setup was successful.') mlab.quit()
from pymatbridge import Matlab mlab = Matlab(matlab='/Applications/MATLAB_R2014a.app/bin/matlab') mlab.start() res = mlab.run('/Users/yanguango/Lab/SpamFilter/Project/src/PCA.m', {'arg1': 3, 'arg2': 5}) print res['result']
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Wed Jan 9 15:53:25 2019 @author: rdamseh """ import matlab.engine as eng from pymatbridge import Matlab mlab = Matlab() mlab.start() mlab.run_code('x=[1,2,3,4,5,6].^2') x=mlab.get_variable('x')
Created on Wed May 16 19:52:46 2018 Dynamic Flux Analysis & Constrained Flux Regulation @author: scott """ from __future__ import print_function import sys, os import cobra, cobra.test, json from escher import Builder from gurobipy import * from os.path import join from cobra.io.mat import model_to_pymatbridge from pymatbridge import Matlab # if you're going to be working with both Matlab and Python: mlab = Matlab() mlab.start() results = mlab.run_code('a=1;') model.solver = 'gurobi' # redirect towards the datafiles data_dir = r'C:\\Users\\scott\\Google Drive\\Work\\UM\\Research\\Sriram\\Projects\\Lyssiotis_Proteomics' # we need to use a recon1 model with an objective function: recon1_model = cobra.io.load_matlab_model(join(data_dir, "model_human_duarte.mat")) model_to_pymatbridge(recon1_model, variable_name="metabolicmodel") recon1_model.metabolicmodel.S #Palsson_core = cobra.io.load_matlab_model(join(data_dir, "Core_Model_Palsson.mat"), variable_name="core_genecomb") def flux_activity_coeff(model, timecourse_metabolomics, sheetname='Sheet1', kappa=None, kappa2=None, genedelflag=None, rxndelflag=None, normquantile=None):
def __init__(self, *args, **kwargs): super(MatlabProcessor, self).__init__(*args, **kwargs) from pymatbridge import Matlab self.matlab = Matlab() self.matlab.start()
# 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()