def replay(self, fig=[], ax=[], **kwargs): """ replay a trajectory Parameters ---------- fig ax speed : float speed ratio """ # plt.ion() if fig == []: fig = plt.gcf() if ax == []: ax = plt.gca() limkwargs = copy.copy(kwargs) if "c" in kwargs: limkwargs.pop("c") if "color" in kwargs: limkwargs.pop("c") limkwargs["marker"] = "*" limkwargs["s"] = 20 if ("m" or "marker") not in kwargs: kwargs["marker"] = "o" if ("c" or "color") not in kwargs: kwargs["color"] = "b" L = Layout(self.Lfilename) fig, ax = L.showG("s", fig=fig, ax=ax, **kwargs) time = self[0].time() line, = ax.plot([], [], "ob", lw=2) time_template = "time = %.1fs" time_text = ax.text(0.05, 0.9, "", transform=ax.transAxes) def init(): line.set_data([], []) time_text.set_text("") return line, time_text def animate(it): X = [] Y = [] for t in self: if t.typ == "ag": X.append(t["x"].values[it]) Y.append(t["y"].values[it]) line.set_data(X, Y) time_text.set_text(time_template % (time[it])) return line, time_text ani = animation.FuncAnimation(fig, animate, np.arange(1, len(time)), interval=25, blit=True, init_func=init) plt.show()
def create_layout(self): """ Create Layout in Simpy the_world thanks to Tk backend """ self.the_world = world(width = float(self.lay_opt['the_world_width']), height = float(self.lay_opt['the_world_height']), scale=float(self.lay_opt['the_world_scale'])) # tk = self.the_world.tk # canvas, x_, y_ = tk.canvas, tk.x_, tk.y_ # canvas.create_rectangle(x_(-1), y_(-1), x_(100), y_(100), fill='white') _filename = self.lay_opt['filename'] #sl=Slab.SlabDB(self.lay_opt['slab'],self.lay_opt['slabmat']) #G1 = Graph.Graph(sl=sl,filename=_filename) self.L = Layout(_filename) #if _filename.split('.')[1] == 'str': # self.L.loadstr(_filename) #elif _filename.split('.')[1] == 'str2': # self.L.loadstr2(_filename) #elif _filename.split('.')[1] == 'ini': # self.L.loadini(_filename) try: self.L.dumpr() print 'Layout graphs are loaded from ',basename,'/struc/ini' except: #self.L.sl = sl #self.L.loadGr(G1) print 'This is the first time the layout file is used\ Layout graphs are curently being built, it may take few minutes.' self.L.build() self.L.dumpw() x_offset = 0 # float(self.lay_opt['x_offset']) y_offset = 0 # float(self.lay_opt['y_offset']) for ks in self.L.Gs.pos.keys(): self.L.Gs.pos[ks] = (self.L.Gs.pos[ks][0] + x_offset, self.L.Gs.pos[ks][1] + y_offset) for ks in self.L.Gr.pos.keys(): self.L.Gr.pos[ks] = (self.L.Gr.pos[ks][0] + x_offset, self.L.Gr.pos[ks][1] + y_offset) for ks in self.L.Gw.pos.keys(): self.L.Gw.pos[ks] = (self.L.Gw.pos[ks][0] + x_offset, self.L.Gw.pos[ks][1] + y_offset) # # Create Layout # walls = self.L.thwall(0, 0) for wall in walls: points = [] # for point in wall: # points.append(x_(point[0])) # points.append(y_(point[1])) # canvas.create_polygon(points, fill='maroon', outline='black') for ii in range(0, len(wall) - 1): self.the_world.add_wall(wall[ii], wall[ii + 1])
def create_layout(self): """ create Layout in Simpy the_world thanks to Tk backend """ _filename = self.lay_opt['filename'] self.L = Layout(_filename) self.the_world = world() try: self.L.dumpr() print 'Layout graphs are loaded from ',basename,'/struc/ini' except: #self.L.sl = sl #self.L.loadGr(G1) print 'This is the first time the layout file is used\ Layout graphs are curently being built, it may take few minutes.' self.L.build() self.L.dumpw() # # Create Layout # walls = self.L.thwall(0, 0) for wall in walls: for ii in range(0, len(wall) - 1): self.the_world.add_wall(wall[ii], wall[ii + 1])
def __init__(self,_fileini='coverage.ini'): self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini,pstruc['DIRSIMUL'])) self.plm = dict(self.config.items('pl_model')) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.txopt = dict(self.config.items('tx')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) self.L = Layout(self.layoutopt['filename']) self.model = PLSmodel(f=eval(self.plm['fghz']), rssnp=eval(self.plm['rssnp']), d0=eval(self.plm['d0']), sigrss=eval(self.plm['sigrss'])) self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) self.mode = eval(self.gridopt['full']) self.boundary = eval(self.gridopt['boundary']) # transitter section self.fGHz = eval(self.txopt['fghz']) self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) self.ptdbm = eval(self.txopt['ptdbm']) self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section self.rxsens = eval(self.rxopt['sensitivity']) kBoltzmann = 1.3806503e-23 self.bandwidthmhz = eval(self.rxopt['bandwidthmhz']) self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) Pn = (10**(self.noisefactordb/10.)+1)*kBoltzmann*self.temperaturek*self.bandwidthmhz*1e3 self.pndbm = 10*np.log10(Pn)+60 self.show = str2bool(self.showopt['show']) try: self.L.Gt.nodes() except: pass try: self.L.dumpr('t') except: self.L.buildGt() self.L.dumpw('t') self.creategrid(full=self.mode,boundary=self.boundary)
def load_simul(self, source): """ load a simultraj configuration file Parameters ---------- source : string name of simulation file to be loaded """ self.filetraj = source if not os.path.isfile(source): raise AttributeError('Trajectory file'+source+'has not been found.\ Please make sure you have run a simulnet simulation before runining simultraj.') # get the trajectory traj = tr.Trajectories() traj.loadh5(self.filetraj) # get the layout self.L = Layout(traj.Lfilename) # resample trajectory for ut, t in enumerate(traj): if t.typ == 'ag': person = Body(t.name + '.ini') tt = t.time() self.dpersons.update({t.name: person}) self._tmin = tt[0] self._tmax = tt[-1] self.time = tt else: pos = np.array([t.x[0], t.y[0], t.z[0]]) self.dap.update({t.ID: {'pos': pos, 'ant': antenna.Antenna(), 'name': t.name } }) self.ctime = np.nan self.Nag = len(self.dpersons.keys()) self.Nap = len(self.dap.keys()) self.traj = traj
def load_simul(self, source): """ load a simultraj configuration file Parameters ---------- source : string name of simulation file to be loaded """ self.filetraj = source # get the trajectory traj = tr.Trajectories() traj.loadh5(self.filetraj) # get the layout self.L = Layout(traj.Lfilename) # resample trajectory for ut, t in enumerate(traj): if t.typ == 'ag': person = Body(t.name + '.ini') tt = t.time() self.dpersons.update({t.name: person}) self._tmin = tt[0] self._tmax = tt[-1] self.time = tt else: pos = np.array([t.x[0], t.y[0], t.z[0]]) self.dap.update({t.ID: {'pos': pos, 'ant': antenna.Antenna(), 'name': t.name } }) self.ctime = np.nan self.Nag = len(self.dpersons.keys()) self.Nap = len(self.dap.keys()) self.traj = traj
def layout(self, _filestruc): """ load a layout in the simulation oject Parameters ---------- _filestruc : string short file name of the Layout object Examples -------- >>> from pylayers.simul.simulem import * >>> S = Simul() >>> S.layout('defstr.ini') """ self.filestr = _filestruc self.L = Layout(_filestruc) # update config self.config.set("files", "struc", self.filestr) self.save()
from pylayers.gis.layout import Layout import networkx as nx import matplotlib.pyplot as plt import doctest plt.ion() #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout('WHERE1.ini') #L= Layout('11Dbibli.ini') #L.show() #L = Layout('PTIN.ini') #L = Layout('DLR.ini') #L.build() L.dumpr() L.showG('i', en=11) #Ga = L.buildGr() #L.showGs() #nx.draw(Ga,Ga.pos)
def __init__(self, **args): """ Mobile Agent Init Parameters ---------- 'ID': string agent ID 'name': string Agent name 'typ': string agent typ . 'ag' for moving agent, 'ap' for static acces point 'pos' : np.array([]) numpy array containing the initial position of the agent 'roomId': int Room number where the agent is initialized (Layout.Gr) 'meca_updt': float update time interval for the mechanical process 'loc': bool enable/disable localization process of the agent 'loc_updt': float update time interval for localization process 'L': pylayers.gis.Layout() 'net':pylayers.network.Network(), 'wstd': list of string list of used radio access techology of the agent 'world': transit.world() Soon deprecated 'save': list of string list of save method ( soon deprecated) 'sim':Simpy.SimulationRT.Simulation(), 'epwr': dictionnary dictionnary of emmited power of transsmitter{'wstd#':epwr value} 'sens': dictionnary dictionnary of sensitivity of reveicer {'wstd#':sens value} 'dcond': dictionnary Not used yet 'gcom':pylayers.communication.Gcom() Communication graph 'comm_mod': string Communication between nodes mode: 'autonomous': all TOAs are refreshed regulary 'synchro' : only visilbe TOAs are refreshed """ defaults = {'ID': '0', 'name': 'johndoe', 'typ': 'ag', 'color': 'k', 'pdshow': False, 'pos': np.array([]), 'roomId': -1, 'froom': [], 'wait': [], 'seed': 0, 'cdest': 'random', 'meca_updt': 0.1, 'loc': False, 'loc_updt': 0.5, 'loc_method': ['geo'], 'L': Layout(), 'network': True, 'net': Network(), 'wstd': ['rat1'], 'world': world(), 'save': [], 'sim': Simulation(), 'epwr': {}, 'sens': {}, 'dcond': {}, 'gcom': Gcom(), 'comm_mode': 'autonomous'} for key, value in defaults.items(): if key not in args: args[key] = value self.args = args self.ID = args['ID'] self.name = args['name'] self.typ = args['typ'] # Create Network self.net = args['net'] self.epwr = args['epwr'] self.gcom = args['gcom'] self.sim = args['sim'] self.wstd = args['wstd'] if args['epwr'] == {}: self.epwr = {x: 0 for x in self.wstd} else: self.epwr = args['epwr'] if args['sens'] == {}: self.sens = {x: -180 for x in self.wstd} else: self.sens = args['sens'] try: self.dcond = args['dcond'] except: pass # check if node id already given if self.ID in self.net.nodes(): raise NameError( 'another agent has the ID: ' + self.ID + ' .Please use an other ID') if self.typ == 'ag': # mechanical init self.meca = Person(ID=self.ID, color=args['color'], pdshow=args['pdshow'], roomId=args['roomId'], L=args['L'], net=self.net, interval=args['meca_updt'], wld=args['world'], sim=args['sim'], seed=args['seed'], moving=True, froom=args['froom'], wait=args['wait'], cdest=args['cdest'], save=args['save'] ) self.meca.behaviors = [Seek(), Containment(), Separation(), InterpenetrationConstraint()] self.meca.steering_mind = queue_steering_mind # Network init self.node = Node(ID=self.ID,name=self.name, p=conv_vecarr(self.meca.position), t=self.sim.now(), wstd=args['wstd'], epwr=self.epwr, sens=self.sens, typ=self.typ) self.net.add_nodes_from(self.node.nodes(data=True)) self.sim.activate(self.meca, self.meca.move(), 0.0) self.PN = self.net.node[self.ID]['PN'] # Communication init if args['comm_mode'] == 'synchro' and args['network']: # The TOA requests are made every refreshTOA time ( can be modified in agent.ini) # This Mode will be deprecated in future version self.rxr = RX(net=self.net, ID=self.ID, dcond=self.dcond, gcom=self.gcom, sim=self.sim) self.rxt = RX(net=self.net, ID=self.ID, dcond=self.dcond, gcom=self.gcom, sim=self.sim) self.sim.activate(self.rxr, self.rxr.refresh_RSS(), 0.0) self.sim.activate(self.rxt, self.rxt.refresh_TOA(), 0.0) elif args['comm_mode'] == 'autonomous' and args['network']: # The requests are made by node only when they are in # visibility of pairs. # self.rxr only manage a refresh RSS process self.rxr = RX(net=self.net, ID=self.ID, gcom=self.gcom, sim=self.sim) # self.tx manage all requests to other nodes self.tx = TX(net=self.net, ID=self.ID, gcom=self.gcom, sim=self.sim) # self.tx replies to requests from self.tx self.rx = RX(net=self.net, ID=self.ID, gcom=self.gcom, sim=self.sim) self.sim.activate(self.rxr, self.rxr.refresh_RSS(), 0.0) self.sim.activate(self.tx, self.tx.request(), 0.0) self.sim.activate(self.rx, self.rx.wait_request(), 0.0) elif self.typ == 'ap': if args['roomId'] == -1: self.node = Node(ID=self.ID, p=self.args['pos'], t=self.sim.now(), wstd=args['wstd'], epwr=self.epwr, sens=self.sens, typ=self.typ) else: pp = np.array(args['L'].Gr.pos[self.args['roomId']]) self.node = Node( ID=self.ID, p=pp, t=self.sim.now(), wstd=args['wstd'], epwr=self.epwr, sens=self.sens, typ=self.typ) self.net.add_nodes_from(self.node.nodes(data=True)) self.sim = args['sim'] self.PN = self.net.node[self.ID]['PN'] self.PN.node[self.ID]['pe'] = self.net.node[self.ID]['p'] if args['comm_mode'] == 'autonomous' and args['network']: self.rx = RX(net=self.net, ID=self.ID, gcom=self.gcom, sim=self.sim) self.sim.activate(self.rx, self.rx.wait_request(), 0.0) p = self.args['pos'] self.posdf = pd.DataFrame( {'t': pd.Timestamp(0), 'x': p[0], 'y': p[1], 'z': p[2], 'vx': np.array([0.0]), 'vy': np.array([0.0]), 'ax': np.array([0.0]), 'ay': np.array([0.0]), }, columns=['t', 'x', 'y', 'z', 'vx', 'vy', 'ax', 'ay'], index=np.array([0])) else: raise NameError( 'wrong agent typ, it must be either agent (ag) or acces point (ap) ') if self.typ == 'ap': self.MoA = 1 else: self.MoA = 0 if 'mysql' in args['save']: config = ConfigParser.ConfigParser() config.read(pyu.getlong('simulnet.ini', 'ini')) sql_opt = dict(config.items('Mysql')) db = Database(sql_opt['host'], sql_opt['user'], sql_opt['passwd'], sql_opt['dbname']) db.writenode(self.ID, self.name, self.MoA) if 'txt' in args['save']: pyu.writenode(self) if self.typ != 'ap' and args['loc']: self.loc = Localization(net=self.net, ID=self.ID, method=args['loc_method']) self.Ploc = PLocalization(loc=self.loc, loc_updt_time=args['loc_updt'], tx=self.tx, sim=args['sim']) self.sim.activate(self.Ploc, self.Ploc.run(), 1.5)
def replay(self, fig=[], ax=[], **kwargs): """ replay a trajectory Parameters ---------- fig ax speed : float speed ratio """ # plt.ion() if fig==[]: fig = plt.gcf() if ax == []: ax = plt.gca() limkwargs = copy.copy(kwargs) if 'c' in kwargs: limkwargs.pop('c') if 'color'in kwargs: limkwargs.pop('c') limkwargs['marker'] = '*' limkwargs['s'] = 20 if ('m' or 'marker') not in kwargs: kwargs['marker'] = 'o' if ('c' or 'color') not in kwargs: kwargs['color'] = 'b' L=Layout(self.Lfilename) fig, ax = L.showG('s',fig=fig, ax=ax, **kwargs) time=self[0].time() line, = ax.plot([], [], 'ob', lw=2) time_template = 'time = %.1fs' time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) def init(): line.set_data([],[]) time_text.set_text('') return line, time_text def animate(it): X=[] Y=[] for t in self: if t.typ == 'ag': X.append(t['x'].values[it]) Y.append(t['y'].values[it]) line.set_data(X,Y) time_text.set_text(time_template%(time[it])) return line, time_text ani = animation.FuncAnimation(fig, animate, np.arange(1, len(time)), interval=25, blit=True, init_func=init) plt.show()
def replay(self, fig=[], ax=[], **kwargs): """ replay a trajectory Parameters ---------- fig ax speed : float speed ratio """ # plt.ion() if fig == []: fig = plt.gcf() if ax == []: ax = plt.gca() limkwargs = copy.copy(kwargs) if 'c' in kwargs: limkwargs.pop('c') if 'color' in kwargs: limkwargs.pop('c') limkwargs['marker'] = '*' limkwargs['s'] = 20 if ('m' or 'marker') not in kwargs: kwargs['marker'] = 'o' if ('c' or 'color') not in kwargs: kwargs['color'] = 'b' L = Layout(self.Lfilename) fig, ax = L.showG('s', fig=fig, ax=ax, **kwargs) time = self[0].time() line, = ax.plot([], [], 'ob', lw=2) time_template = 'time = %.1fs' time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) def init(): line.set_data([], []) time_text.set_text('') return line, time_text def animate(it): X = [] Y = [] for t in self: if t.typ == 'ag': X.append(t['x'].values[it]) Y.append(t['y'].values[it]) line.set_data(X, Y) time_text.set_text(time_template % (time[it])) return line, time_text ani = animation.FuncAnimation(fig, animate, np.arange(1, len(time)), interval=25, blit=True, init_func=init) plt.show()
from pylayers.gis.layout import Layout import matplotlib.pyplot as plt import doctest #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout('DLR.ini') try: L.dumpr() except: L.build() L.dumpw() #L.editor() fig = plt.gcf() #ax1 = fig.add_subplot(221) ax1 = fig.add_subplot(321) L.display['thin'] = True fig, ax1 = L.showGs(fig=fig, ax=ax1) #L.display['edlabel']=True #L.display['edlblsize']=50 # display selected segments L.display['thin'] = True L.showG(fig=fig, ax=ax1, graph='t') fig = plt.gcf() ax1 = plt.gca() fig, ax1 = L.showGs(fig=fig, ax=ax1, edlist=[125], width=4) ax11 = fig.add_subplot(322) L.showG(fig=fig, ax=ax11, graph='') #plt.savefig('graphGs.png') #build topological graph
from pylayers.gis.layout import Layout import networkx as nx import matplotlib.pyplot as plt import doctest plt.ion() #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout('WHERE1.ini') #L= Layout('11Dbibli.ini') #L.show() #L = Layout('PTIN.ini') #L = Layout('DLR.ini') #L.build() L.dumpr() L.showG('i',en=11) #Ga = L.buildGr() #L.showGs() #nx.draw(Ga,Ga.pos)
def __init__(self, _fileini='coverage.ini'): """ object constructor Parameters ---------- _fileini : string name of the configuration file Notes ----- Coverage is described in an ini file. Default file is coverage.ini and is placed in the ini directory of the current project. """ self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini, pstruc['DIRSIMUL'])) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.apopt = dict(self.config.items('ap')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) # get the Layout filename = self.layoutopt['filename'] if filename.endswith('lay'): self.typ = 'indoor' self.L = Layout(filename) # get the receiving grid self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) self.mode = self.gridopt['mode'] assert self.mode in ['file', 'full', 'zone'], "Error reading grid mode " self.boundary = eval(self.gridopt['boundary']) self.filespa = self.gridopt['file'] # # create grid # self.creategrid(mode=self.mode, boundary=self.boundary, _fileini=self.filespa) self.dap = {} for k in self.apopt: kwargs = eval(self.apopt[k]) ap = std.AP(**kwargs) self.dap[eval(k)] = ap try: self.L.Gt.nodes() except: pass try: self.L.dumpr() except: self.L.build() self.L.dumpw() else: self.typ = 'outdoor' self.E = ez.Ezone(filename) self.E.loadh5() self.E.rebase() # The frequency is fixed from the AP nature self.fGHz = np.array([]) #self.fGHz = eval(self.txopt['fghz']) #self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) #self.ptdbm = eval(self.txopt['ptdbm']) #self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section #self.rxsens = eval(self.rxopt['sensitivity']) self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) # show section self.bshow = str2bool(self.showopt['show'])
from pylayers.gis.layout import Layout from pylayers.util.project import * import pylayers.util.pyutil as pyu import networkx as nx import matplotlib.pyplot as plt import warnings import shutil warnings.filterwarnings("error") #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout() lL = L.ls() for tL in lL: print 'Layout : ', tL print '--------------------------' L = Layout(tL, bbuild=0, bgraphs=0) if L.check(): L.save() filein = pyu.getlong(L._filename, pstruc['DIRLAY']) fileout = '/home/uguen/Documents/rch/devel/pylayers/data/struc/lay/' + L._filename print fileout shutil.copy2(filein, fileout) #figure(figsize=(20,10)) #plt.axis('off') #f,a = L.showG('s',nodes=False,fig=f) #plt.show() #f,a = L.showG('r',edge_color='b',linewidth=4,fig=f) #L= Layout('11Dbibli.ini') #L.show()
def Lname(self,Lname): # change layout and build/load self._L = Layout(Lname) self._Lname = Lname self.reset_config()
from pylayers.gis.layout import Layout import matplotlib.pyplot as plt import doctest #doctest.testmod(layout) L = Layout() L.load('TA-Office.str') #L.editor() fig = plt.gcf() #ax1 = fig.add_subplot(221) ax1 = fig.add_subplot(111) L.display['thin']=True fig,ax1 = L.showGs(fig=fig,ax=ax1) L.display['thin']=False L.display['edlabel']=True L.display['edlblsize']=50 fig,ax1 = L.showGs(fig=fig,ax=ax1,edlist=[10,7,54],width=4) plt.show() #plt.savefig('graphGs.png') # build topological graph #L.buildGt() #ax2 = fig.add_subplot(222) #L.showG(fig=fig,ax=ax2,graph='t') #plt.title('Topological graph') #plt.savefig('graphGt.png') # build graph of rooms #L.buildGr() #ax3 = fig.add_subplot(223) #L.showG(fig=fig,ax=ax3,graph='r')
def __init__(self, **kwargs): """ deterministic link evaluation Parameters ---------- L : Layout Layout to be used a : np.ndarray (3,) position of a device dev_a b : np.ndarray (3,) position of a device dev_b Aa : Antenna Antenna of device dev_a Ab : Antenna Antenna of device dev_b Ta : np.ndarray (3,3) Rotation matrice of Antenna of device dev_a relative to global Layout scene Tb : np.ndarray (3,3) Rotation matrice of Antenna of device dev_b relative to global Layout scene fGHz : np.ndarray (Nf,) frequency range of Nf points used for evaluation of channel wav : Waveform Waveform to be applied on the channel save_idx : int number to identify the h5 file generated Advanced (change only if you really know what you do !) save_opt : list (['sig','ray','Ct','H']) information to be saved in the Links h5 file. Should never be Modified ! force_create : Boolean (False) forcecreating the h5py file (if already exist, will be erased) Notes ----- All simulations are stored into a unique file in your <PyProject>/output directory using the following convention: Links_<save_idx>_<LayoutFilename>.h5 where <save_idx> is an integer number to distinguish different links simulations and <LayoutFilename> is the Layout used for the link simulation. Dataset organisation: Links_<idx>_<Layout_name>.h5 | |/sig/si_ID#0/ | /si_ID#1/ | ... | |/ray/ray_ID#0/ | /ray_ID#1/ | ... | |/Ct/Ct_ID#0/ | /Ct_ID#1/ | ... | |/H/H_ID#0/ | /H_ID#1/ | ... | | |p_map |c_map |f_map |A_map |T_map Roots Dataset : c_map : Cycles (Nc x 3) p_map : Positions (Np x 3) f_map : Frequency (Nf x 3) T_map : Rotation matrices (Nt x 3) A_map : Antenna name (Na x 3) Groups and subgroups: Signature identifier (si_ID#N): ca_cb_cutoff Ray identifier (ray_ID#N): cutoff_ua_ub Ctilde identifier (Ct_ID#N): ua_ub_uf H identifier (H_ID#N): ua_ub_uf_uTa_uTb_uAa_uAb with ca : cycle number of a cb : cycle number of b cutoff : signature.run cutoff ua : indice of a position in 'p_map' position dataset ub : indice of a position in 'p_map' position dataset uf : indice of freq position in 'f_map' frequency dataset uTa : indice of a position in 'T_map' Rotation dataset uTb : indice of a position in 'T_map' Rotation dataset uAa : indice of a position in 'A_map' Antenna name dataset uAb : indice of b position in 'A_map' Antenna name dataset Examples -------- >>> from pylayers.simul.link import * >>> L = DLink(verbose=False) >>> aktk = L.eval() """ Link.__init__(self) defaults={ 'L':Layout(), 'a':np.array(()), 'b':np.array(()), 'Aa':Antenna(typ='Omni'), 'Ab':Antenna(typ='Omni'), 'Ta':np.eye(3), 'Tb':np.eye(3), 'fGHz':[], 'wav':wvf.Waveform(), 'cutoff':3, 'save_opt':['sig','ray','Ct','H'], 'save_idx':0, 'force_create':False, 'verbose':True, 'graph':'tcvirw' } self._ca=-1 self._cb=-1 specset = ['a','b','Aa','Ab','Ta','Tb','L','fGHz','wav'] # set default attribute for key, value in defaults.items(): if key not in kwargs: if key in specset : setattr(self,'_'+key,value) else : setattr(self,key,value) else : if key in specset : setattr(self,'_'+key,kwargs[key]) else : setattr(self,key,kwargs[key]) force=self.force_create delattr(self,'force_create') if self.fGHz == []: self.initfreq() else : pass try: self._Lname = self._L.filename except: self._L=Layout(self._L) self._Lname = self._L.filename ########### # Transmitter and Receiver positions ########### self.tx = RadioNode(name = '', typ = 'tx', _fileini = 'radiotx.ini', ) self.rx = RadioNode(name = '', typ = 'rx', _fileini = 'radiorx.ini', ) self.filename = 'Links_' + str(self.save_idx) + '_' + self._Lname + '.h5' filenameh5 = pyu.getlong(self.filename,pstruc['DIRLNK']) # check if save file alreasdy exists if not os.path.exists(filenameh5) or force: print 'Links save file for ' + self.L.filename + ' does not exist.' print 'Creating file. You\'ll see this message only once per Layout' self.save_init(filenameh5) # dictionnary data exists self.dexist={'sig':{'exist':False,'grpname':''}, 'ray':{'exist':False,'grpname':''}, 'Ct':{'exist':False,'grpname':''}, 'H':{'exist':False,'grpname':''} } try: self.L.dumpr() except: print('This is the first time the Layout is used. Graphs have to be built. Please Wait') self.L.build(graph=self.graph) self.L.dumpw() #self.L.build() ########### # init pos & cycles # # If a and b are not specified # they are chosen as center of gravity of cycle 0 # ########### if len(self.a)==0: self.ca = 1 # self.a = self.L.cy2pt(self.ca) else: if len(kwargs['a']) ==2: a=np.r_[kwargs['a'],1.0] else: a=kwargs['a'] self.a = a # self.ca = self.L.pt2cy(self.a) if len(self.b)==0: if len(self.L.Gt.node)>2: self.cb = 2 else: self.cb = 1 # self.b = self.L.cy2pt(self.cb) else: if len(kwargs['b']) ==2: b=np.r_[kwargs['b'],1.0] else: b=kwargs['b'] self.b = b # self.cb = self.L.pt2cy(self.b) ########### # init freq # TODO Check where it is used redundant with fGHz ########### #self.fmin = self.fGHz[0] #self.fmax = self.fGHz[-1] #self.fstep = self.fGHz[1]-self.fGHz[0] self.Si = Signatures(self.L,self.ca,self.cb,cutoff=self.cutoff) self.R = Rays(self.a,self.b) self.C = Ctilde() self.H = Tchannel()
from pylayers.gis.layout import Layout import networkx as nx import matplotlib.pyplot as plt #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout() lL = L.ls() for tL in lL: print 'Layout :', tL try: L = Layout(tL) L.save() except: print 'Layout :', tL, ' problem' #f = plt.figure(figsize=(20,10)) #plt.axis('off') #f,a = L.showG('s',nodes=False,fig=f) #plt.show() #f,a = L.showG('r',edge_color='b',linewidth=4,fig=f) #L= Layout('11Dbibli.ini') #L.show() #L = Layout('PTIN.ini') #L = Layout('DLR.ini') #L.buildGt() #Ga = L.buildGr() #L.showGs() #nx.draw(Ga,Ga.pos)
class Coverage(PyLayers): """ Handle Layout Coverage Methods ------- creategrid() create a uniform grid for evaluating losses cover() run the coverage calculation showPower() display the map of received power showLoss() display the map of losses Attributes ---------- All attributes are read from fileini ino the ini directory of the current project _fileini default coverage.ini L : a Layout nx : number of point on x ny : number of point on y tx : transmitter position txpe : transmitter power emmission level show : boolean for automatic display power map na : number of access point """ def __init__(self, _fileini='coverage.ini'): """ object constructor Parameters ---------- _fileini : string name of the configuration file Notes ----- Coverage is described in an ini file. Default file is coverage.ini and is placed in the ini directory of the current project. """ self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini, pstruc['DIRSIMUL'])) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.apopt = dict(self.config.items('ap')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) # get the Layout filename = self.layoutopt['filename'] if filename.endswith('lay'): self.typ = 'indoor' self.L = Layout(filename) # get the receiving grid self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) if 'zgrid' in self.gridopt: self.zgrid = eval(self.gridopt['zgrid']) else: self.zgrid = 1.0 self.mode = self.gridopt['mode'] assert self.mode in ['file', 'full', 'zone'], "Error reading grid mode " self.boundary = eval(self.gridopt['boundary']) self.filespa = self.gridopt['file'] # # create grid # self.creategrid(mode=self.mode, boundary=self.boundary, _fileini=self.filespa) self.dap = {} for k in self.apopt: kwargs = eval(self.apopt[k]) ap = std.AP(**kwargs) self.dap[eval(k)] = ap try: self.L.Gt.nodes() except: pass try: self.L.dumpr() except: self.L.build() self.L.dumpw() else: self.typ = 'outdoor' self.E = ez.Ezone(filename) self.E.loadh5() self.E.rebase() # The frequency is fixed from the AP nature self.fGHz = np.array([]) #self.fGHz = eval(self.txopt['fghz']) #self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) #self.ptdbm = eval(self.txopt['ptdbm']) #self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section #self.rxsens = eval(self.rxopt['sensitivity']) self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) self.PtDB = eval(self.rxopt['ptdb']) self.Gt = eval(self.rxopt['gt']) self.Gr = eval(self.rxopt['gr']) self.do = eval(self.rxopt['do']) self.Prdo = eval(self.rxopt['prdo']) self.n = eval(self.rxopt['n']) self.desviopadrao = eval(self.rxopt['desviopadrao']) self.x0 = eval(self.rxopt['x0']) self.y0 = eval(self.rxopt['y0']) self.xt = eval(self.rxopt['xt']) self.yt = eval(self.rxopt['yt']) # show section self.bshow = str2bool(self.showopt['show']) def __repr__(self): st = '' if self.typ == 'indoor': st = st + 'Layout file : ' + self.L._filename + '\n\n' st = st + '-----list of Access Points ------' + '\n' for k in self.dap: st = st + self.dap[k].__repr__() + '\n' st = st + '-----Rx------' + '\n' st = st + 'temperature (K) : ' + str(self.temperaturek) + '\n' st = st + 'noisefactor (dB) : ' + str(self.noisefactordb) + '\n\n' st = st + '--- Grid ----' + '\n' st = st + 'mode : ' + str(self.mode) + '\n' if self.mode <> 'file': st = st + 'nx : ' + str(self.nx) + '\n' st = st + 'ny : ' + str(self.ny) + '\n' if self.mode == 'zone': st = st + 'boundary (xmin,ymin,xmax,ymax) : ' + str( self.boundary) + '\n\n' if self.mode == 'file': st = st + ' filename : ' + self.filespa + '\n' return (st) def creategrid(self, mode='full', boundary=[], _fileini=''): """ create a grid Parameters ---------- full : boolean default (True) use all the layout area boundary : (xmin,ymin,xmax,ymax) if full is False the boundary argument is used """ if mode == "file": self.RN = RadioNode(name='', typ='rx', _fileini=_fileini, _fileant='def.vsh3') self.grid = self.RN.position[0:2, :].T else: if mode == "full": mi = np.min(self.L.Gs.pos.values(), axis=0) + 0.01 ma = np.max(self.L.Gs.pos.values(), axis=0) - 0.01 if mode == "zone": assert boundary <> [] mi = np.array([boundary[0], boundary[1]]) ma = np.array([boundary[2], boundary[3]]) x = np.linspace(mi[0], ma[0], self.nx) y = np.linspace(mi[1], ma[1], self.ny) self.grid = np.array((list(np.broadcast(*np.ix_(x, y))))) self.ng = self.grid.shape[0] def where1(self): """ Unfinished : Not sure this is the right place (too specific) """ M1 = UWBMeasure(1) self.dap = {} self.dap[1] = {} self.dap[2] = {} self.dap[3] = {} self.dap[4] = {} self.dap[1]['p'] = M1.rx[1, 0:2] self.dap[2]['p'] = M1.rx[1, 0:2] self.dap[3]['p'] = M1.rx[1, 0:2] self.dap[4]['p'] = M1.rx[1, 0:2] for k in range(300): try: M = UWBMeasure(k) tx = M.tx self.grid = np.vstack((self.grid, tx[0:2])) D = M.rx - tx[np.newaxis, :] D2 = D * D dist = np.sqrt(np.sum(D2, axis=1))[1:] Emax = M.Emax() Etot = M.Etot()[0] try: td1 = np.hstack((td1, dist[0])) td2 = np.hstack((td2, dist[1])) td3 = np.hstack((td3, dist[2])) td4 = np.hstack((td4, dist[3])) te1 = np.hstack((te1, Emax[0])) te2 = np.hstack((te2, Emax[1])) te3 = np.hstack((te3, Emax[2])) te4 = np.hstack((te4, Emax[3])) tt1 = np.hstack((tt1, Etot[0])) tt2 = np.hstack((tt2, Etot[1])) tt3 = np.hstack((tt3, Etot[2])) tt4 = np.hstack((tt4, Etot[3])) #tdist = np.hstack((tdist,dist)) #te = np.hstack((te,Emax)) except: td1 = np.array(dist[0]) td2 = np.array(dist[1]) td3 = np.array(dist[2]) td4 = np.array(dist[3]) te1 = np.array(Emax[0]) te2 = np.array(Emax[1]) te3 = np.array(Emax[2]) te4 = np.array(Emax[3]) tt1 = np.array(Etot[0]) tt2 = np.array(Etot[1]) tt3 = np.array(Etot[2]) tt4 = np.array(Etot[3]) except: pass def cover(self, sinr=True, snr=True, best=True): """ run the coverage calculation Parameters ---------- sinr : boolean snr : boolean best : boolean Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> f,a=C.show(typ='sinr',figsize=(10,8)) >>> plt.show() Notes ----- self.fGHz is an array, it means that Coverage is calculated at once for a whole set of frequencies. In practice, it would be the center frequency of a given standard channel. This function is calling `loss.Losst` which calculates Losses along a straight path. In a future implementation we will abstract the EM solver in order to make use of other calculation approaches as a full or partial Ray Tracing. The following members variables are evaluated : + freespace Loss @ fGHz PL() PathLoss (shoud be rename FS as free space) $ + prdbmo : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{odB}` + prdbmp : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{pdB}` + snro : SNR polar o (H) + snrp : SNR polar p (H) See Also -------- pylayers.antprop.loss.Losst pylayers.antprop.loss.PL """ # # select active AP # lactiveAP = [] try: del self.aap del self.ptdbm except: pass self.kB = 1.3806503e-23 # Boltzmann constant # # Loop opver access points # for iap in self.dap: if self.dap[iap]['on']: lactiveAP.append(iap) fGHz = self.dap[iap].s.fcghz # The frequency band is set here self.fGHz = np.unique(np.hstack((self.fGHz, fGHz))) apchan = self.dap[iap]['chan'] try: self.aap = np.vstack((self.aap, self.dap[iap]['p'])) self.ptdbm = np.vstack( (self.ptdbm, self.dap[iap]['PtdBm'])) self.bmhz = np.vstack( (self.bmhz, self.dap[iap].s.chan[apchan[0]]['BMHz'])) except: self.aap = self.dap[iap]['p'] self.ptdbm = np.array(self.dap[iap]['PtdBm']) self.bmhz = np.array( self.dap[iap].s.chan[apchan[0]]['BMHz']) PnW = np.array((10**(self.noisefactordb / 10.)) * self.kB * self.temperaturek * self.bmhz * 1e6) self.pnw = PnW # Evaluate Noise Power (in dBm) self.pndbm = np.array(10 * np.log10(PnW) + 30) #lchan = map(lambda x: self.dap[x]['chan'],lap) #apchan = zip(self.dap.keys(),lchan) #self.bmhz = np.array(map(lambda x: self.dap[x[0]].s.chan[x[1][0]]['BMHz']*len(x[1]),apchan)) self.ptdbm = self.ptdbm.T self.pndbm = self.pndbm.T # creating all links # all grid to all ap # if len(self.pndbm.shape) == 0: self.ptdbm = self.ptdbm.reshape(1, 1) self.pndbm = self.pndbm.reshape(1, 1) p = product(range(self.ng), lactiveAP) # # pa : access point # pg : grid point # # 1 x na for k in p: pg = self.grid[k[0], :] pa = np.array(self.dap[k[1]]['p']) # exemple with 3 AP # 321 0 # 321 1 # 321 2 # 322 0 try: self.pa = np.vstack((self.pa, pa)) except: self.pa = pa try: self.pg = np.vstack((self.pg, pg)) except: self.pg = pg self.pa = self.pa.T shpa = self.pa.shape shpg = self.pg.shape if shpa[0] != 3: self.pa = np.vstack((self.pa, np.ones(shpa[1]))) self.pg = self.pg.T self.pg = np.vstack((self.pg, self.zgrid * np.ones(shpg[0]))) self.nf = len(self.fGHz) # retrieving dimensions along the 3 axis na = len(lactiveAP) self.na = na ng = self.ng nf = self.nf for k, iap in enumerate(self.dap): # select only one access point u = na * np.arange(0, ng, 1).astype('int') + k if self.dap[iap]['on']: pt = self.pa[:, u] pr = self.pg[:, u] azoffset = self.dap[iap]['phideg'] * np.pi / 180. self.dap[iap].A.eval(fGHz=self.fGHz, pt=pt, pr=pr, azoffset=azoffset) gain = (self.dap[iap].A.G).T #pdb.set_trace() # to handle omnidirectional antenna (nf,1,1) if gain.shape[1] == 1: gain = np.repeat(gain, ng, axis=1) try: tgain = np.dstack((tgain, gain[:, :, None])) except: tgain = gain[:, :, None] #Lwo,Lwp,Edo,Edp = loss.Losst(self.L,self.fGHz,self.pa,self.pg,dB=False) Lwo, Lwp, Edo, Edp = loss.Losst(self.L, self.fGHz, self.pa, self.pg, dB=False) self.Lwo = Lwo.reshape(nf, ng, na) self.Edo = Edo.reshape(nf, ng, na) self.Lwp = Lwp.reshape(nf, ng, na) self.Edp = Edp.reshape(nf, ng, na) freespace = loss.PL(self.fGHz, self.pa, self.pg, dB=False) self.freespace = freespace.reshape(nf, ng, na) # transmitting power # f x g x a # CmW : Received Power coverage in mW self.CmWo = 10**(self.ptdbm[np.newaxis, ...] / 10.) * self.Lwo * self.freespace * tgain self.CmWp = 10**(self.ptdbm[np.newaxis, ...] / 10.) * self.Lwp * self.freespace * tgain if self.typ == 'intensity': self.cmWo = 10 * np.log10(self.cmWo) self.cmWo = 10 * np.log10(self.cmWp) if snr: self.evsnr() if sinr: self.evsinr() if best: self.evbestsv() def evsnr(self): """ calculates signal to noise ratio """ NmW = 10**(self.pndbm / 10.)[np.newaxis, :] self.snro = self.CmWo / NmW self.snrp = self.CmWp / NmW def evsinr(self): """ calculates sinr """ # na : number of access point na = self.na # U : 1 x 1 x na x na U = (np.ones((na, na)) - np.eye(na))[np.newaxis, np.newaxis, :, :] # CmWo : received power in mW orthogonal polarization # CmWp : received power in mW parallel polarization ImWo = np.einsum('ijkl,ijl->ijk', U, self.CmWo) ImWp = np.einsum('ijkl,ijl->ijk', U, self.CmWp) NmW = 10**(self.pndbm / 10.)[np.newaxis, :] self.sinro = self.CmWo / (ImWo + NmW) self.sinrp = self.CmWp / (ImWp + NmW) def evbestsv(self): """ determine the best server map Notes ----- C.bestsv """ na = self.na ng = self.ng nf = self.nf # find best server regions Vo = self.CmWo Vp = self.CmWp self.bestsvo = np.empty(nf * ng * na).reshape(nf, ng, na) self.bestsvp = np.empty(nf * ng * na).reshape(nf, ng, na) for kf in range(nf): MaxVo = np.max(Vo[kf, :, :], axis=1) MaxVp = np.max(Vp[kf, :, :], axis=1) for ka in range(na): uo = np.where(Vo[kf, :, ka] == MaxVo) up = np.where(Vp[kf, :, ka] == MaxVp) self.bestsvo[kf, uo, ka] = ka + 1 self.bestsvp[kf, up, ka] = ka + 1 def plot(self, **kwargs): """ """ defaults = { 'typ': 'pr', 'grid': False, 'f': 0, 'a': 0, 'db': True, 'label': '', 'pol': 'p', 'col': 'b' } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] if 'fig' in kwargs: fig = kwargs['fig'] else: fig = plt.figure() if 'ax' in kwargs: ax = kwargs['ax'] else: ax = fig.add_subplot(111) if kwargs['typ'] == 'pr': if kwargs['a'] <> -1: if kwargs['pol'] == 'p': U = self.CmWp[kwargs['f'], :, kwargs['a']] if kwargs['pol'] == 'o': U = self.CmWo[kwargs['f'], :, kwargs['a']] else: if kwargs['pol'] == 'p': U = self.CmWp[kwargs['f'], :, :].reshape(self.na * self.ng) else: U = self.CmWo[kwargs['f'], :, :].reshape(self.na * self.ng) if kwargs['db']: U = 10 * np.log10(U) D = np.sqrt(np.sum((self.pa - self.pg) * (self.pa - self.pg), axis=0)) if kwargs['a'] <> -1: D = D.reshape(self.ng, self.na) ax.semilogx(D[:, kwargs['a']], U, '.', color=kwargs['col'], label=kwargs['label']) else: ax.semilogx(D, U, '.', color=kwargs['col'], label=kwargs['label']) return fig, ax def show(self, **kwargs): """ show coverage Parameters ---------- typ : string 'pr' | 'sinr' | 'capacity' | 'loss' | 'best' | 'egd' grid : boolean polar : string 'o' | 'p' best : boolean draw best server contour if True f : int frequency index a : int access point index (-1 all access point) Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> f,a = C.show(typ='pr',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='best',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='loss',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='sinr',figsize=(10,8)) >>> plt.show() See Also -------- pylayers.gis.layout.Layout.showG """ defaults = { 'typ': 'pr', 'grid': False, 'polar': 'p', 'f': 0, 'a': -1, 'db': True, 'cmap': cm.jet, 'best': True } title = self.dap[self.dap.keys()[0]].s.name + ' : ' for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] polar = kwargs['polar'] assert polar in ['p', 'o'], "polar wrongly defined in show coverage" if 'fig' in kwargs: if 'ax' in kwargs: fig, ax = self.L.showG('s', fig=kwargs['fig'], ax=kwargs['ax']) else: fig, ax = self.L.showG('s', fig=kwargs['fig']) else: if 'figsize' in kwargs: fig, ax = self.L.showG('s', figsize=kwargs['figsize']) else: fig, ax = self.L.showG('s') # plot the grid self.typ = kwargs['typ'] typ = kwargs['typ'] if kwargs['grid']: for k in self.dap: p = self.dap[k].p ax.plot(p[0], p[1], 'or') f = kwargs['f'] a = kwargs['a'] assert typ in [ 'best', 'egd', 'sinr', 'snr', 'capacity', 'pr', 'loss', 'intensity', 'losssombreamento', 'prsombreamento', 'snrsombreamento' ], "typ unknown in show coverage" best = kwargs['best'] dB = kwargs['db'] # setting the grid l = self.grid[0, 0] r = self.grid[-1, 0] b = self.grid[0, 1] t = self.grid[-1, -1] arq = open('cobertura.txt', 'w') texto = '''dBm e V/m \n''' arq.write(texto) arq.close() if typ == 'best': title = title + 'Best server' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar for ka in range(self.na): if polar == 'p': bestsv = self.bestsvp[f, :, ka] if polar == 'o': bestsv = self.bestsvo[f, :, ka] m = np.ma.masked_where(bestsv == 0, bestsv) if self.mode <> 'file': W = m.reshape(self.nx, self.ny).T ax.imshow(W, extent=(l, r, b, t), origin='lower', vmin=1, vmax=self.na + 1) else: ax.scatter(self.grid[:, 0], self.grid[:, 1], c=m, s=20, linewidth=0) ax.set_title(title) else: if typ == 'egd': title = title + 'excess group delay : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar V = self.Ed dB = False legcb = 'Delay (ns)' if typ == 'sinr': title = title + 'SINR : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar == 'o': V = self.sinro if polar == 'p': V = self.sinrp if typ == 'snr': title = title + 'SNR : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar == 'o': V = self.snro if polar == 'p': V = self.snrp if typ == 'capacity': title = title + 'Capacity : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar legcb = 'Mbit/s' if polar == 'o': V = self.bmhz.T[np.newaxis, :] * np.log( 1 + self.sinro) / np.log(2) if polar == 'p': V = self.bmhz.T[np.newaxis, :] * np.log( 1 + self.sinrp) / np.log(2) if typ == 'pr': title = title + 'Pr : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dBm' else: lgdcb = 'mW' if polar == 'o': V = self.CmWo if polar == 'p': V = self.CmWp if typ == 'loss': title = title + 'Loss : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar == 'o': V = self.Lwo * self.freespace if polar == 'p': V = self.Lwp * self.freespace if typ == 'losssombreamento': #relaxa que esse so muda no final title = title + 'Loss Log : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dB' else: lgdcb = 'Linear scale' if polar == 'o': V = self.CmWo if polar == 'p': V = self.CmWp if typ == 'prsombreamento': #relaxa que esse so muda no final title = title + 'Pr Log : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dBm' else: lgdcb = 'Linear scale' if polar == 'o': V = self.CmWo if polar == 'p': V = self.CmWp if typ == 'snrsombreamento': #relaxa que esse so muda no final title = title + 'SNR Log : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dB' else: lgdcb = 'Linear scale' if polar == 'o': V = self.CmWo if polar == 'p': V = self.CmWp if typ == 'intensity': title = title + 'Intensity : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar legcb = 'V/m' if polar == 'o': V = np.power(10, ( (self.CmWo + 20 * np.log10(self.fGHz[f] * 1000) + 77.2) / 20)) * 0.000001 if polar == 'p': V = np.power(10, ( (self.CmWp + 20 * np.log10(self.fGHz[f] * 1000) + 77.2) / 20)) * 0.000001 if a == -1: V = np.max(V[f, :, :], axis=1) else: V = V[f, :, a] # reshaping the data on the grid if self.mode != 'file': U = V.reshape((self.nx, self.ny)).T else: U = V if dB and typ != 'intensity': if typ == 'losssombreamento' or typ == 'prsombreamento' or typ == 'snrsombreamento': f = self.fGHz[f] * 1000 Lf = self.PtDB - self.Prdo + self.Gt + self.Gr medidas1 = [] dx = np.linspace(0, self.xt, self.nx) dy = np.linspace(0, self.yt, self.ny) for i in range(self.ny): medidas1.append([]) for i in range(self.ny): for j in range(self.nx): medidas1[i].append( np.sqrt( np.power(dx[j] - self.x0, 2) + np.power(dy[i] - self.y0, 2)) / 1000) desvio = 0 X = np.random.normal(0, self.desviopadrao, len(medidas1)) PL2 = [] maior, menor = 0, 100 for i in range(self.ny): PL2.append([]) for i in range(self.ny): for j in range(self.nx): oi = Lf + 10 * self.n * np.log10( medidas1[i][j] / self.do) + X[i] PL2[i].append(oi) if (maior < oi): maior = oi if (menor > oi): menor = oi menor = np.floor(menor / 10) * 10 maior = np.ceil(maior / 10) * 10 if typ == 'prsombreamento': maior, menor = -100, 0 pr = [] for i in range(self.ny): pr.append([]) for i in range(self.ny): for j in range(self.nx): oi = self.PtDB - PL2[i][j] pr[i].append(oi) if (maior < oi): maior = oi if (menor > oi): menor = oi menor = np.floor(menor / 10) * 10 maior = np.ceil(maior / 10) * 10 U = pr elif typ == 'snrsombreamento': maior, menor = -100, 100 pr = [] for i in range(self.ny): pr.append([]) for i in range(self.ny): for j in range(self.nx): oi = PL2[i][j] / self.pnw[0][0] pr[i].append(oi) if (maior < oi): maior = oi if (menor > oi): menor = oi menor = np.floor(menor / 10) * 10 maior = np.ceil(maior / 10) * 10 U = pr else: U = PL2 else: U = 10 * np.log10(U) #print U arq = open('cobertura.txt', 'w') texto = [] for linha in U: texto.append(str(linha) + '\n') arq.writelines(texto) arq.close() if 'vmin' in kwargs: vmin = kwargs['vmin'] else: if typ == 'losssombreamento' or typ == 'prsombreamento' or typ == 'snrsombreamento': vmin = menor else: vmin = U.min() if 'vmax' in kwargs: vmax = kwargs['vmax'] else: if typ == 'losssombreamento' or typ == 'prsombreamento' or typ == 'snrsombreamento': vmax = maior else: vmax = U.max() if self.mode != 'file': img = ax.imshow(U, extent=(l, r, b, t), origin='lower', vmin=vmin, vmax=vmax, cmap=kwargs['cmap']) else: img = ax.scatter(self.grid[:, 0], self.grid[:, 1], c=U, s=20, linewidth=0, cmap=kwargs['cmap'], vmin=vmin, vmax=vmax) for k in range(self.na): ax.annotate(str(k), xy=(self.pa[0, k], self.pa[1, k])) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) clb = fig.colorbar(img, cax) clb.set_label(legcb) if typ == 'losssombreamento' or typ == 'prsombreamento' or typ == 'snrsombreamento': print 'Modelo do Sombreamento Log Normal' else: if best: if self.mode <> 'file': if polar == 'o': ax.contour(np.sum(self.bestsvo, axis=2)[f, :].reshape( self.nx, self.ny).T, extent=(l, r, b, t), linestyles='dotted') if polar == 'p': ax.contour(np.sum(self.bestsvp, axis=2)[f, :].reshape( self.nx, self.ny).T, extent=(l, r, b, t), linestyles='dotted') # display access points if a == -1: ax.scatter(self.pa[0, :], self.pa[1, :], s=30, c='r', linewidth=0) else: ax.scatter(self.pa[0, a], self.pa[1, a], s=30, c='r', linewidth=0) plt.tight_layout() return (fig, ax)
class Coverage(PyLayers): """ Handle Layout Coverage Methods ------- creategrid() create a uniform grid for evaluating losses cover() run the coverage calculation showPower() display the map of received power showLoss() display the map of losses Attributes ---------- All attributes are read from fileini ino the ini directory of the current project _fileini default coverage.ini L : a Layout nx : number of point on x ny : number of point on y tx : transmitter position txpe : transmitter power emmission level show : boolean for automatic display power map na : number of access point """ def __init__(self,_fileini='coverage.ini'): """ object constructor Parameters ---------- _fileini : string name of the configuration file Notes ----- Coverage is described in an ini file. Default file is coverage.ini and is placed in the ini directory of the current project. """ self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini,pstruc['DIRSIMUL'])) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.apopt = dict(self.config.items('ap')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) # get the Layout filename = self.layoutopt['filename'] if filename.endswith('ini'): self.typ = 'indoor' self.L = Layout(filename) # get the receiving grid self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) self.mode = self.gridopt['mode'] self.boundary = eval(self.gridopt['boundary']) self.filespa = self.gridopt['file'] # # create grid # self.creategrid(mode=self.mode,boundary=self.boundary,_fileini=self.filespa) self.dap = {} for k in self.apopt: kwargs = eval(self.apopt[k]) ap = std.AP(**kwargs) self.dap[eval(k)] = ap try: self.L.Gt.nodes() except: pass try: self.L.dumpr() except: self.L.build() self.L.dumpw() else: self.typ='outdoor' self.E = ez.Ezone(filename) self.E.loadh5() self.E.rebase() self.fGHz = np.array([]) #self.fGHz = eval(self.txopt['fghz']) #self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) #self.ptdbm = eval(self.txopt['ptdbm']) #self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section #self.rxsens = eval(self.rxopt['sensitivity']) self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) # show section self.bshow = str2bool(self.showopt['show']) def __repr__(self): st='' if self.typ=='indoor': st = st+ 'Layout file : '+self.L.filename + '\n\n' st = st + '-----list of Access Points ------'+'\n' for k in self.dap: st = st + self.dap[k].__repr__()+'\n' st = st + '-----Rx------'+'\n' st= st+ 'temperature (K) : '+ str(self.temperaturek) + '\n' st= st+ 'noisefactor (dB) : '+ str(self.noisefactordb) + '\n\n' st = st + '--- Grid ----'+'\n' st= st+ 'mode : ' + str(self.mode) + '\n' if self.mode<>'file': st= st+ 'nx : ' + str(self.nx) + '\n' st= st+ 'ny : ' + str(self.ny) + '\n' if self.mode=='zone': st= st+ 'boundary (xmin,ymin,xmax,ymax) : ' + str(self.boundary) + '\n\n' if self.mode=='file': st = st+' filename : '+self.filespa+'\n' return(st) def creategrid(self,mode='full',boundary=[],_fileini=''): """ create a grid Parameters ---------- full : boolean default (True) use all the layout area boundary : (xmin,ymin,xmax,ymax) if full is False the boundary argument is used """ if mode=="file": self.RN = RadioNode(name='', typ='rx', _fileini = _fileini, _fileant = 'def.vsh3') self.grid =self.RN.position[0:2,:].T else: if mode=="full": mi=np.min(self.L.Gs.pos.values(),axis=0)+0.01 ma=np.max(self.L.Gs.pos.values(),axis=0)-0.01 if mode=="zone": assert boundary<>[] mi = np.array([boundary[0],boundary[1]]) ma = np.array([boundary[2],boundary[3]]) x = np.linspace(mi[0],ma[0],self.nx) y = np.linspace(mi[1],ma[1],self.ny) self.grid=np.array((list(np.broadcast(*np.ix_(x, y))))) self.ng = self.grid.shape[0] def where1(self): """ Unfinished : Not sure this is the right place (too specific) """ M1 = UWBMeasure(1) self.dap={} self.dap[1]={} self.dap[2]={} self.dap[3]={} self.dap[4]={} self.dap[1]['p']=M1.rx[1,0:2] self.dap[2]['p']=M1.rx[1,0:2] self.dap[3]['p']=M1.rx[1,0:2] self.dap[4]['p']=M1.rx[1,0:2] for k in range(300): try: M = UWBMeasure(k) tx = M.tx self.grid=np.vstack((self.grid,tx[0:2])) D = M.rx-tx[np.newaxis,:] D2 = D*D dist = np.sqrt(np.sum(D2,axis=1))[1:] Emax = M.Emax() Etot = M.Etot()[0] try: td1 = np.hstack((td1,dist[0])) td2 = np.hstack((td2,dist[1])) td3 = np.hstack((td3,dist[2])) td4 = np.hstack((td4,dist[3])) te1 = np.hstack((te1,Emax[0])) te2 = np.hstack((te2,Emax[1])) te3 = np.hstack((te3,Emax[2])) te4 = np.hstack((te4,Emax[3])) tt1 = np.hstack((tt1,Etot[0])) tt2 = np.hstack((tt2,Etot[1])) tt3 = np.hstack((tt3,Etot[2])) tt4 = np.hstack((tt4,Etot[3])) #tdist = np.hstack((tdist,dist)) #te = np.hstack((te,Emax)) except: td1=np.array(dist[0]) td2=np.array(dist[1]) td3=np.array(dist[2]) td4=np.array(dist[3]) te1 =np.array(Emax[0]) te2 =np.array(Emax[1]) te3 =np.array(Emax[2]) te4 =np.array(Emax[3]) tt1 =np.array(Etot[0]) tt2 =np.array(Etot[1]) tt3 =np.array(Etot[2]) tt4 =np.array(Etot[3]) except: pass def cover(self,sinr=True,snr=True,best=True): """ run the coverage calculation Parameters ---------- sinr : boolean snr : boolean best : boolean Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> f,a=C.show(typ='sinr',figsize=(10,8)) >>> plt.show() Notes ----- self.fGHz is an array, it means that Coverage is calculated at once for a whole set of frequencies. In practice, it would be the center frequency of a given standard channel. This function is calling `loss.Losst` which calculates Losses along a straight path. In a future implementation we will abstract the EM solver in order to make use of other calculation approaches as a full or partial Ray Tracing. The following members variables are evaluated : + freespace Loss @ fGHz PL() PathLoss (shoud be rename FS as free space) $ + prdbmo : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{odB}` + prdbmp : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{pdB}` + snro : SNR polar o (H) + snrp : SNR polar p (H) See Also -------- pylayers.antprop.loss.Losst pylayers.antprop.loss.PL """ # # select active AP # lactiveAP = [] try: del self.aap del self.ptdbm except: pass self.kB = 1.3806503e-23 # Boltzmann constant for iap in self.dap: if self.dap[iap]['on']: lactiveAP.append(iap) fGHz = self.dap[iap].s.fcghz self.fGHz=np.unique(np.hstack((self.fGHz,fGHz))) apchan = self.dap[iap]['chan'] try: self.aap = np.vstack((self.aap,self.dap[iap]['p'][0:2])) self.ptdbm = np.vstack((self.ptdbm,self.dap[iap]['PtdBm'])) self.bmhz = np.vstack((self.bmhz, self.dap[iap].s.chan[apchan[0]]['BMHz'])) except: self.aap = self.dap[iap]['p'][0:2] self.ptdbm = np.array(self.dap[iap]['PtdBm']) self.bmhz = np.array(self.dap[iap].s.chan[apchan[0]]['BMHz']) PnW = np.array((10**(self.noisefactordb/10.))*self.kB*self.temperaturek*self.bmhz*1e6) # Evaluate Noise Power (in dBm) self.pndbm = np.array(10*np.log10(PnW)+30) #lchan = map(lambda x: self.dap[x]['chan'],lap) #apchan = zip(self.dap.keys(),lchan) #self.bmhz = np.array(map(lambda x: self.dap[x[0]].s.chan[x[1][0]]['BMHz']*len(x[1]),apchan)) self.ptdbm = self.ptdbm.T self.pndbm = self.pndbm.T # creating all links p = product(range(self.ng),lactiveAP) # # pa : access point # pg : grid point # # 1 x na for k in p: pg = self.grid[k[0],:] pa = self.dap[k[1]]['p'][0:2] try: self.pa = np.vstack((self.pa,pa)) except: self.pa = pa try: self.pg = np.vstack((self.pg,pg)) except: self.pg = pg self.pa = self.pa.T self.pg = self.pg.T self.nf = len(self.fGHz) # retrieving dimensions along the 3 axis na = len(lactiveAP) self.na = na ng = self.ng nf = self.nf Lwo,Lwp,Edo,Edp = loss.Losst(self.L,self.fGHz,self.pa,self.pg,dB=False) self.Lwo = Lwo.reshape(nf,ng,na) self.Edo = Edo.reshape(nf,ng,na) self.Lwp = Lwp.reshape(nf,ng,na) self.Edp = Edp.reshape(nf,ng,na) freespace = loss.PL(self.fGHz,self.pa,self.pg,dB=False) self.freespace = freespace.reshape(nf,ng,na) # transmitting power # f x g x a # CmW : Received Power coverage in mW self.CmWo = 10**(self.ptdbm[np.newaxis,...]/10.)*self.Lwo*self.freespace self.CmWp = 10**(self.ptdbm[np.newaxis,...]/10.)*self.Lwp*self.freespace if snr: self.evsnr() if sinr: self.evsinr() if best: self.evbestsv() def evsnr(self): """ calculates snr """ NmW = 10**(self.pndbm/10.)[np.newaxis,:] self.snro = self.CmWo/NmW self.snrp = self.CmWp/NmW def evsinr(self): """ calculates sinr """ # na : number of access point na = self.na # U : 1 x 1 x na x na U = (np.ones((na,na))-np.eye(na))[np.newaxis,np.newaxis,:,:] # CmWo : received power in mW orthogonal polarization # CmWp : received power in mW parallel polarization ImWo = np.einsum('ijkl,ijl->ijk',U,self.CmWo) ImWp = np.einsum('ijkl,ijl->ijk',U,self.CmWp) NmW = 10**(self.pndbm/10.)[np.newaxis,:] self.sinro = self.CmWo/(ImWo+NmW) self.sinrp = self.CmWp/(ImWp+NmW) def evbestsv(self): """ determine the best server map Notes ----- C.bestsv """ na = self.na ng = self.ng nf = self.nf # find best server regions Vo = self.CmWo Vp = self.CmWp self.bestsvo = np.empty(nf*ng*na).reshape(nf,ng,na) self.bestsvp = np.empty(nf*ng*na).reshape(nf,ng,na) for kf in range(nf): MaxVo = np.max(Vo[kf,:,:],axis=1) MaxVp = np.max(Vp[kf,:,:],axis=1) for ka in range(na): uo = np.where(Vo[kf,:,ka]==MaxVo) up = np.where(Vp[kf,:,ka]==MaxVp) self.bestsvo[kf,uo,ka]=ka+1 self.bestsvp[kf,up,ka]=ka+1 # def showEd(self,polar='o',**kwargs): # """ shows a map of direct path excess delay # # Parameters # ---------- # # polar : string # 'o' | 'p' # # Examples # -------- # # .. plot:: # :include-source: # # >> from pylayers.antprop.coverage import * # >> C = Coverage() # >> C.cover() # >> C.showEd(polar='o') # # """ # # if not kwargs.has_key('alphacy'): # kwargs['alphacy']=0.0 # if not kwargs.has_key('colorcy'): # kwargs['colorcy']='w' # if not kwargs.has_key('nodes'): # kwargs['nodes']=False # # fig,ax = self.L.showG('s',**kwargs) # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # cdict = { # 'red' : ((0., 0.5, 0.5), (1., 1., 1.)), # 'green': ((0., 0.5, 0.5), (1., 1., 1.)), # 'blue' : ((0., 0.5, 0.5), (1., 1., 1.)) # } # #generate the colormap with 1024 interpolated values # my_cmap = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024) # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # # # # if polar=='o': # mcEdof = np.ma.masked_where(prdbm < self.rxsens,self.Edo) # # cov=ax.imshow(mcEdof.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'jet', # origin='lower') # # # # # cov=ax.imshow(self.Edo.reshape((self.nx,self.ny)).T, # # extent=(l,r,b,t), # # origin='lower') # titre = "Map of LOS excess delay, polar orthogonal" # # if polar=='p': # mcEdpf = np.ma.masked_where(prdbm < self.rxsens,self.Edp) # # cov=ax.imshow(mcEdpf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'jet', # origin='lower') # # # cov=ax.imshow(self.Edp.reshape((self.nx,self.ny)).T, # # extent=(l,r,b,t), # # origin='lower') # titre = "Map of LOS excess delay, polar parallel" # # ax.scatter(self.tx[0],self.tx[1],linewidth=0) # ax.set_title(titre) # # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # clb = fig.colorbar(cov,cax) # clb.set_label('excess delay (ns)') # # if self.show: # plt.show() # return fig,ax # # def showPower(self,rxsens=True,nfl=True,polar='o',**kwargs): # """ show the map of received power # # Parameters # ---------- # # rxsens : bool # clip the map with rx sensitivity set in self.rxsens # nfl : bool # clip the map with noise floor set in self.pndbm # polar : string # 'o'|'p' # # Examples # -------- # # .. plot:: # :include-source: # # > from pylayers.antprop.coverage import * # > C = Coverage() # > C.cover() # > C.showPower() # # """ # # if not kwargs.has_key('alphacy'): # kwargs['alphacy']=0.0 # if not kwargs.has_key('colorcy'): # kwargs['colorcy']='w' # if not kwargs.has_key('nodes'): # kwargs['nodes']=False # fig,ax = self.L.showG('s',**kwargs) # # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # ## tCM = plt.cm.get_cmap('jet') ## tCM._init() ## alphas = np.abs(np.linspace(.0,1.0, tCM.N)) ## tCM._lut[:-3,-1] = alphas # # title='Map of received power - Pt = ' + str(self.ptdbm) + ' dBm'+str(' fGHz =') + str(self.fGHz) + ' polar = '+polar # # cdict = { # 'red' : ((0., 0.5, 0.5), (1., 1., 1.)), # 'green': ((0., 0.5, 0.5), (1., 1., 1.)), # 'blue' : ((0., 0.5, 0.5), (1., 1., 1.)) # } # # if not kwargs.has_key('cmap'): # # generate the colormap with 1024 interpolated values # cmap = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024) # else: # cmap = kwargs['cmap'] # #my_cmap = cm.copper # # # if rxsens : # # ## values between the rx sensitivity and noise floor # mcPrf = np.ma.masked_where((prdbm > self.rxsens) # & (prdbm < self.pndbm),prdbm) # # mcPrf = np.ma.masked_where((prdbm > self.rxsens) ,prdbm) # # cov1 = ax.imshow(mcPrf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = cm.copper, # vmin=self.rxsens,origin='lower') # # ### values above the sensitivity # mcPrs = np.ma.masked_where(prdbm < self.rxsens,prdbm) # cov = ax.imshow(mcPrs.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t), # cmap = cmap, # vmin=self.rxsens,origin='lower') # title=title + '\n black : Pr (dBm) < %.2f' % self.rxsens + ' dBm' # # else : # cov=ax.imshow(prdbm.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t), # cmap = cmap, # vmin=self.pndbm,origin='lower') # # if nfl: # ### values under the noise floor # ### we first clip the value below the noise floor # cl = np.nonzero(prdbm<=self.pndbm) # cPr = prdbm # cPr[cl] = self.pndbm # mcPruf = np.ma.masked_where(cPr > self.pndbm,cPr) # cov2 = ax.imshow(mcPruf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'binary', # vmax=self.pndbm,origin='lower') # title=title + '\n white : Pr (dBm) < %.2f' % self.pndbm + ' dBm' # # # ax.scatter(self.tx[0],self.tx[1],s=10,c='k',linewidth=0) # # ax.set_title(title) # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # clb = fig.colorbar(cov,cax) # clb.set_label('Power (dBm)') # # if self.show: # plt.show() # # return fig,ax # # # def showTransistionRegion(self,polar='o'): # """ # # Notes # ----- # # See : "Analyzing the Transitional Region in Low Power Wireless Links" # Marco Zuniga and Bhaskar Krishnamachari # # Examples # -------- # # .. plot:: # :include-source: # # > from pylayers.antprop.coverage import * # > C = Coverage() # > C.cover() # > C.showTransitionRegion() # # """ # # frameLength = self.framelengthbytes # # PndBm = self.pndbm # gammaU = 10*np.log10(-1.28*np.log(2*(1-0.9**(1./(8*frameLength))))) # gammaL = 10*np.log10(-1.28*np.log(2*(1-0.1**(1./(8*frameLength))))) # # PrU = PndBm + gammaU # PrL = PndBm + gammaL # # fig,ax = self.L.showGs() # # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # # zones = np.zeros(np.shape(prdbm)) # #pdb.set_trace() # # uconnected = np.nonzero(prdbm>PrU) # utransition = np.nonzero((prdbm < PrU)&(prdbm > PrL)) # udisconnected = np.nonzero(prdbm < PrL) # # zones[uconnected] = 1 # zones[utransition] = (prdbm[utransition]-PrL)/(PrU-PrL) # cov = ax.imshow(zones.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'BuGn',origin='lower') # # title='PDR region' # ax.scatter(self.tx[0],self.tx[1],linewidth=0) # # ax.set_title(title) # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # fig.colorbar(cov,cax) # if self.show: # plt.show() # def plot(self,**kwargs): """ """ defaults = { 'typ': 'pr', 'grid': False, 'f' : 0, 'a' : 0, 'db':True, 'col':'b' } for k in defaults: if k not in kwargs: kwargs[k]=defaults[k] if 'fig' in kwargs: fig=kwargs['fig'] else: fig=plt.figure() if 'ax' in kwargs: ax = kwargs['ax'] else: ax = fig.add_subplot(111) if kwargs['typ']=='pr': if kwargs['a']<>-1: U = self.CmW[kwargs['f'],:,kwargs['a']] else: U = self.CmW[kwargs['f'],:,:].reshape(self.na*self.ng) if kwargs['db']: U = 10*np.log10(U) D = np.sqrt(np.sum((self.pa-self.pg)*(self.pa-self.pg),axis=0)) if kwargs['a']<>-1: D = D.reshape(self.ng,self.na) ax.semilogx(D[:,kwargs['a']],U,'.',color=kwargs['col']) else: ax.semilogx(D,U,'.',color=kwargs['col']) return fig,ax def show(self,**kwargs): """ show coverage Parameters ---------- typ : string 'pr' | 'sinr' | 'capacity' | 'loss' | 'best' | 'egd' grid : boolean polar : string 'o' | 'p' best : boolean draw best server contour if True f : int frequency index a : int access point index (-1 all access point) Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> f,a = C.show(typ='pr',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='best',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='loss',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='sinr',figsize=(10,8)) >>> plt.show() See Also -------- pylayers.gis.layout.Layout.showG """ defaults = { 'typ': 'pr', 'grid': False, 'polar':'p', 'f' : 0, 'a' :-1, 'db':True, 'cmap' :cm.jet, 'best':True } title = self.dap[1].s.name+ ' : ' for k in defaults: if k not in kwargs: kwargs[k]=defaults[k] polar = kwargs['polar'] if 'fig' in kwargs: if 'ax' in kwargs: fig,ax=self.L.showG('s',fig=kwargs['fig'],ax=kwargs['ax']) else: fig,ax=self.L.showG('s',fig=kwargs['fig']) else: if 'figsize' in kwargs: fig,ax=self.L.showG('s',figsize=kwargs['figsize']) else: fig,ax=self.L.showG('s') # plot the grid if kwargs['grid']: for k in self.dap: p = self.dap[k].p ax.plot(p[0],p[1],'or') f = kwargs['f'] a = kwargs['a'] typ = kwargs['typ'] best = kwargs['best'] dB = kwargs['db'] # setting the grid l = self.grid[0,0] r = self.grid[-1,0] b = self.grid[0,1] t = self.grid[-1,-1] if typ=='best': title = title + 'Best server'+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+polar for ka in range(self.na): if polar=='p': bestsv = self.bestsvp[f,:,ka] if polar=='o': bestsv = self.bestsvo[f,:,ka] m = np.ma.masked_where(bestsv == 0,bestsv) if self.mode<>'file': W = m.reshape(self.nx,self.ny).T ax.imshow(W, extent=(l,r,b,t), origin='lower', vmin=1, vmax=self.na+1) else: ax.scatter(self.grid[:,0],self.grid[:,1],c=m,s=20,linewidth=0) ax.set_title(title) else: if typ=='egd': title = title + 'excess group delay : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+polar V = self.Ed dB = False legcb = 'Delay (ns)' if typ=='sinr': title = title + 'SINR : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar=='o': V = self.sinro if polar=='p': V = self.sinrp if typ=='snr': title = title + 'SNR : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar=='o': V = self.snro if polar=='p': V = self.snrp if typ=='capacity': title = title + 'Capacity : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+polar legcb = 'Mbit/s' if polar=='o': V = self.bmhz.T[np.newaxis,:]*np.log(1+self.sinro)/np.log(2) if polar=='p': V = self.bmhz.T[np.newaxis,:]*np.log(1+self.sinrp)/np.log(2) if typ=='pr': title = title + 'Pr : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+polar if dB: legcb = 'dBm' else: lgdcb = 'mW' if polar=='o': V = self.CmWo if polar=='p': V = self.CmWp if typ=='loss': title = title + 'Loss : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar=='o': V = self.Lwo*self.freespace if polar=='p': V = self.Lwp*self.freespace if a == -1: V = np.max(V[f,:,:],axis=1) else: V = V[f,:,a] # reshaping the data on the grid if self.mode!='file': U = V.reshape((self.nx,self.ny)).T else: U = V if dB: U = 10*np.log10(U) if 'vmin' in kwargs: vmin = kwargs['vmin'] else: vmin = U.min() if 'vmax' in kwargs: vmax = kwargs['vmax'] else: vmax = U.max() if self.mode!='file': img = ax.imshow(U, extent=(l,r,b,t), origin='lower', vmin = vmin, vmax = vmax, cmap = kwargs['cmap']) else: img=ax.scatter(self.grid[:,0],self.grid[:,1],c=U,s=20,linewidth=0,cmap=kwargs['cmap']) for k in range(self.na): ax.annotate(str(k),xy=(self.pa[0,k],self.pa[1,k])) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) clb = fig.colorbar(img,cax) clb.set_label(legcb) if best: if self.mode<>'file': if polar=='o': ax.contour(np.sum(self.bestsvo,axis=2)[f,:].reshape(self.nx,self.ny).T,extent=(l,r,b,t),linestyles='dotted') if polar=='p': ax.contour(np.sum(self.bestsvp,axis=2)[f,:].reshape(self.nx,self.ny).T,extent=(l,r,b,t),linestyles='dotted') # display access points if a==-1: ax.scatter(self.pa[0,:],self.pa[1,:],s=30,c='r',linewidth=0) else: ax.scatter(self.pa[0,a],self.pa[1,a],s=30,c='r',linewidth=0) plt.tight_layout() return(fig,ax)
class Simul(PyLayers): """ Link oriented simulation A simulation requires : + A Layout + A Person + A Trajectory or a CorSer instance Members ------- dpersons : dictionnary of persons (agent) dap : dictionnary of access points Methods ------- load_simul : load configuration file load_Corser : load a Corser file _gen_net : generate network and asociated links show : show layout and network evaldeter : run simulation over time """ def __init__(self, source='simulnet_TA-Office.h5', verbose=False): """ object constructor Parameters ---------- source : string h5 trajectory file default simulnet_TA-Office.h5 verbose : boolean Notes ----- The simultraj has a dataframe """ # self.progress = -1 # simulation not loaded self.verbose = verbose self.cfield = [] self.dpersons = {} self.dap = {} self.Nag = 0 self.Nap = 0 # source analysis if isinstance(source, str): self.filetraj = source self.load_simul(source) self.source = 'simul' elif 'pylayers' in source.__module__: self.filetraj = source._filename self.load_CorSer(source) cutoff = 2 self.source = 'CorSer' # generate the Network # The wireless standard and frequency is fixed in this function # self._gen_net() # initialize Stochastic Link self.SL = SLink() # initialize Deterministic Link self.DL = DLink(L=self.L, verbose=self.verbose) self.DL.cutoff = cutoff self.filename = 'simultraj_' + self.filetraj + '.h5' # data is a panda container which is initialized # # We do not save all the simulation in a DataFRame anymore # #self.data = pd.DataFrame(columns=['id_a', 'id_b', # 'x_a', 'y_a', 'z_a', # 'x_b', 'y_b', 'z_b', # 'd', 'eng', 'typ', # 'wstd', 'fcghz', # 'fbminghz', 'fbmaxghz', 'fstep', 'aktk_id', # 'sig_id', 'ray_id', 'Ct_id', 'H_id' # ]) #self.data.index.name='t' self._filecsv = self.filename.split('.')[0] + '.csv' self.todo = {'OB': True, 'B2B': True, 'B2I': True, 'I2I': False} filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) if os.path.exists(filenameh5): self.loadpd() self.settime(0.) # self._saveh5_init() def __repr__(self): s = 'Simul trajectories class\n' s = s + '------------------------\n' s = s + '\n' s = s + 'Used layout: ' + self.L.filename + '\n' s = s + 'Number of Agents: ' + str(self.Nag) + '\n' s = s + 'Number of Access Points: ' + str(self.Nap) + '\n' s = s + 'Link to be evaluated: ' + str(self.todo) + '\n' s = s + 'tmin: ' + str(self._tmin) + '\n' s = s + 'tmax: ' + str(self._tmax) + '\n' s = s + '\n' # network info s = s + 'self.N :\n' s = s + self.N.__repr__() + '\n' s = s + 'CURRENT TIME: ' + str(self.ctime) + '\n' return s def load_simul(self, source): """ load a simultraj configuration file Parameters ---------- source : string name of simulation file to be loaded """ self.filetraj = source if not os.path.isfile(source): raise AttributeError('Trajectory file' + source + 'has not been found.\ Please make sure you have run a simulnet simulation before runining simultraj.' ) # get the trajectory traj = tr.Trajectories() traj.loadh5(self.filetraj) # get the layout self.L = Layout(traj.Lfilename) # resample trajectory for ut, t in enumerate(traj): if t.typ == 'ag': person = Body(t.name + '.ini') tt = t.time() self.dpersons.update({t.name: person}) self._tmin = tt[0] self._tmax = tt[-1] self.time = tt else: pos = np.array([t.x[0], t.y[0], t.z[0]]) self.dap.update({ t.ID: { 'pos': pos, 'ant': antenna.Antenna(), 'name': t.name } }) self.ctime = np.nan self.Nag = len(self.dpersons.keys()) self.Nap = len(self.dap.keys()) self.traj = traj def load_CorSer(self, source): """ load CorSer file for simulation Parameters ---------- source : name of simulation file to be loaded """ if isinstance(source.B, Body): B = [source.B] elif isinstance(source.B, list): B = source.B elif isinstance(source.B, dict): B = source.B.values() else: raise AttributeError('CorSer.B must be a list or a Body') self.L = source.L self.traj = tr.Trajectories() self.traj.Lfilename = self.L._filename for b in B: self.dpersons.update({b.name: b}) self._tmin = b.time[0] self._tmax = b.time[-1] self.time = b.time self.traj.append(b.traj) for ap in source.din: techno, ID = ap.split(':') if techno == 'HKB': techno = 'hikob' if techno == 'TCR': techno = 'tcr' if techno == 'BS': techno = 'bespoon' self.dap.update({ ap: { 'pos': source.din[ap]['p'], 'ant': source.din[ap]['ant'], 'T': source.din[ap]['T'], 'name': techno } }) self.ctime = np.nan self.Nag = len(B) self.Nap = len(source.din) self.corser = source def _gen_net(self): """ generate Network and associated links Notes ----- Create self.N : Network object See Also -------- pylayers.network.network """ # # Create Network # N = Network() # # get devices on bodies # # forall person # forall device for p in self.dpersons: D = [] for dev in self.dpersons[p].dev: aDev = Device(self.dpersons[p].dev[dev]['name'], ID=dev) D.append(aDev) D[-1].ant['A1']['name'] = self.dpersons[p].dev[dev]['file'] D[-1].ant['antenna'] = self.dpersons[p].dev[dev]['ant'] N.add_devices(D, grp=p) # # get access point devices # for ap in self.dap: D = Device(self.dap[ap]['name'], ID=ap) D.ant['antenna'] = self.dap[ap]['ant'] N.add_devices(D, grp='ap', p=self.dap[ap]['pos']) N.update_orient(ap, self.dap[ap]['T'], now=0.) # create Network # # _get_wstd # _get_grp # _connect # _init_PN # N.create() self.N = N def show(self): """ show actual simlulation configuration """ fig, ax = self.L.showGs() fig, ax = self.N.show(fig=fig, ax=ax) return fig, ax def evaldeter(self, na, nb, wstd, fmod='force', nf=10, fGHz=[], **kwargs): """ deterministic evaluation of a link Parameters ---------- na : string: node a id in self.N (Network) nb : string: node b id in self.N (Network) wstd : string: wireless standard used for commmunication between na and nb fmode : string ('center'|'band'|'force') mode of frequency evaluation center : single frequency (center frequency of a channel) band : nf points on the whole band force : takes directly fGHz nf : int: number of frequency points (if fmode = 'band') **kwargs : argument of DLink Returns ------- (a, t ) a : ndarray alpha_k t : ndarray tau_k See Also -------- pylayers.simul.link.DLink """ # todo in network : # take into consideration the postion and rotation of antenna and not device self.DL.Aa = self.N.node[na]['ant']['antenna'] self.DL.a = self.N.node[na]['p'] self.DL.Ta = self.N.node[na]['T'] self.DL.Ab = self.N.node[nb]['ant']['antenna'] self.DL.b = self.N.node[nb]['p'] self.DL.Tb = self.N.node[nb]['T'] # # The frequency band is chosen from the selected standard # if fmode == 'center' # only center frequency is calculated # #' if fmod == 'center': self.DL.fGHz = self.N.node[na]['wstd'][wstd]['fcghz'] if fmod == 'band': fminGHz = self.N.node[na]['wstd'][wstd]['fbminghz'] fmaxGHz = self.N.node[na]['wstd'][wstd]['fbmaxghz'] self.DL.fGHz = np.linspace(fminGHz, fmaxGHz, nf) if fmod == 'force': assert len(fGHz) > 0, "fGHz has not been defined" self.DL.fGHz = fGHz a, t = self.DL.eval(**kwargs) return a, t def evalstat(self, na, nb): """ statistical evaluation of a link Parameters ---------- na : string: node a id in self.N (Netwrok) nb : string: node b id in self.N (Netwrok) Returns ------- (a, t, eng) a : ndarray alpha_k t : ndarray tau_k eng : float engagement """ pa = self.N.node[na]['p'] pb = self.N.node[nb]['p'] if self.source == 'simul': dida, name = na.split('_') didb, name = nb.split('_') elif self.source == 'CorSer': bpa, absolutedida, dida, name, technoa = self.corser.devmapper(na) bpb, absolutedidb, didb, name, technob = self.corser.devmapper(nb) ak, tk, eng = self.SL.onbody(self.dpersons[name], dida, didb, pa, pb) return ak, tk, eng def settime(self, t): """ set current time """ self.ctime = t self._traj = copy.copy(self.traj) self.update_pos(t) def run(self, **kwargs): """ run the link evaluation along a trajectory Parameters ---------- OB: boolean perform on body statistical link evaluation B2B: boolean perform body to body deterministic link evaluation B2I: boolean perform body to infrastructure deterministic link evaluation I2I: boolean perform infrastructure to infrastructure deterministic link eval. links: dict dictionnary of link to be evaluated (key is wtsd and value is a list of links) (if [], all link are considered) wstd: list list of wstd to be evaluated (if [], all wstd are considered) t: np.array list of timestamp to be evaluated (if [], all timestamps are considered) tbr : boolean time in bit reverse order (tmin,tmax,N) Npoints=2**N replace_data: boolean (True) if True , reference id of all already simulated link will be erased and replace by new simulation id fGHz : np.array frequency in GHz Examples -------- >>> from pylayers.simul.simultraj import * >>> from pylayers.measures.cormoran import * >>> C=CorSer(layout=True) >>> S=Simul(C,verbose=True) >>> link={'ieee802154':[]} >>> link['ieee802154'].append(S.N.links['ieee802154'][0]) >>> lt = [0,0.2,0.3,0.4,0.5] >>> S.run(links=link,t=lt) """ defaults = { 'OB': True, 'B2B': True, 'B2I': True, 'I2I': False, 'links': {}, 'wstd': [], 't': np.array([]), 'btr': True, 'DLkwargs': {}, 'replace_data': True, 'fmod': 'force', 'fGHz': np.array([2.45]) } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] DLkwargs = kwargs.pop('DLkwargs') links = kwargs.pop('links') wstd = kwargs.pop('wstd') OB = kwargs.pop('OB') B2B = kwargs.pop('B2B') B2I = kwargs.pop('B2I') I2I = kwargs.pop('I2I') fmod = kwargs.pop('fmod') self.fGHz = kwargs.pop('fGHz') self.todo.update({'OB': OB, 'B2B': B2B, 'B2I': B2I, 'I2I': I2I}) # Check link attribute if links == {}: links = self.N.links elif not isinstance(links, dict): raise AttributeError( 'links is {wstd:[list of links]}, see self.N.links') for k in links.keys(): checkl = [l in self.N.links[k] for l in links[k]] if len(np.where(checkl == False)[0]) > 0: # if sum(checkl) != len(self.N.links): uwrong = np.where(np.array(checkl) is False)[0] raise AttributeError( str(np.array(links)[uwrong]) + ' links does not exist in Network') wstd = links.keys() # # Check wstd attribute # if wstd == []: # wstd = self.N.wstd.keys() # elif not isinstance(wstd, list): # wstd = [wstd] checkw = [w in self.N.wstd.keys() for w in wstd] if sum(checkw) != len(wstd): uwrong = np.where(np.array(checkw) is False)[0] raise AttributeError( str(np.array(wstd)[uwrong]) + ' wstd are not in Network') # force time attribute compliant if not isinstance(kwargs['t'], np.ndarray): if isinstance(kwargs['t'], list): lt = np.array(kwargs['t']) elif (isinstance(kwargs['t'], int) or isinstance(kwargs['t'], float)): lt = np.array([kwargs['t']]) else: lt = kwargs['t'] #if len(lt) == 0: # lt = self.time # check time attribute if kwargs['btr']: if (lt[0] < self._tmin) or\ (lt[1] > self._tmax) : raise AttributeError('Requested time range not available') # self._traj is a copy of self.traj, which is affected by resampling. # it is only a temporary attribute for a given run # if len(lt) > 1: # sf = 1/(1.*lt[1]-lt[0]) # self._traj = self.traj.resample(sf=sf, tstart=lt[0]) # else: # self._traj = self.traj.resample(sf=1.0, tstart=lt[0]) # self._traj.time() # self.time = self._traj.t # self._time = pd.to_datetime(self.time,unit='s') # # Nested Loops # # time # standard # links # evaldeter &| evalstat # #lt = self.get_sim_time(lt) #self._time=self.get_sim_time(lt) init = True if kwargs['btr']: tmin = lt[0] tmax = lt[1] Nt = int(2**lt[2]) ta = np.linspace(tmin, tmax, Nt) it = np.hstack((np.r_[0], np.r_[pyu.bitreverse(Nt, int(lt[2]))])) #trev = t[it] else: ta = kwargs['t'] it = range(len(ta)) ## Start to loop over time ## ut : counter ## t : time value (s) #for ut, t in enumerate(lt): for ks, ut in enumerate(it): t = ta[ut] self.ctime = t # update spatial configuration of the scene for time t self.update_pos(t) # print self.N.__repr__() ## Start to loop over available Wireless standard ## for w in wstd: ## Start to loop over the chosen links stored in links ## for na, nb, typ in links[w]: # If type of link is valid (Body 2 Body,...) # if self.todo[typ]: if self.verbose: print '-' * 30 print 'time:', t, '/', lt[-1], ' time idx:', ut, '/', len(ta), '/', ks print 'processing: ', na, ' <-> ', nb, 'wstd: ', w print '-' * 30 eng = 0 # # Invoque link deterministic simulation # # node : na # node : nb # wstd : w # self.evaldeter(na, nb, w, applywav=False, fmod=fmod, fGHz=self.fGHz, **DLkwargs) # if typ == 'OB': # self.evalstat(na, nb) # eng = self.SL.eng # L = self.DL + self.SL # self._ak = L.H.ak # self._tk = L.H.tk # else : # Get alphak an tauk self._ak = self.DL.H.ak self._tk = self.DL.H.tk aktk_id = str(ut) + '_' + na + '_' + nb + '_' + w # this is a dangerous way to proceed ! # the id as a finite number of characters while len(aktk_id) < 40: aktk_id = aktk_id + ' ' df = pd.DataFrame( { 'id_a': na, 'id_b': nb, 'x_a': self.N.node[na]['p'][0], 'y_a': self.N.node[na]['p'][1], 'z_a': self.N.node[na]['p'][2], 'x_b': self.N.node[nb]['p'][0], 'y_b': self.N.node[nb]['p'][1], 'z_b': self.N.node[nb]['p'][2], 'd': self.N.edge[na][nb]['d'], 'eng': eng, 'typ': typ, 'wstd': w, 'fcghz': self.N.node[na]['wstd'][w]['fcghz'], 'fbminghz': self.fGHz[0], 'fbmaxghz': self.fGHz[-1], 'nf': len(self.fGHz), 'aktk_id': aktk_id, 'sig_id': self.DL.dexist['sig']['grpname'], 'ray_id': self.DL.dexist['ray']['grpname'], 'Ct_id': self.DL.dexist['Ct']['grpname'], 'H_id': self.DL.dexist['H']['grpname'], }, columns=[ 'id_a', 'id_b', 'x_a', 'y_a', 'z_a', 'x_b', 'y_b', 'z_b', 'd', 'eng', 'typ', 'wstd', 'fcghz', 'fbminghz', 'fbmaxghz', 'fstep', 'aktk_id', 'sig_id', 'ray_id', 'Ct_id', 'H_id' ], index=[t]) #self._time[ut]]) self.savepd(df) def replace_data(self, df): """check if a dataframe df already exists in self.data Parameters ---------- df : pd.DataFrame Returns ------- boolean True if already exists False otherwise """ self.data[(self.data.index == df.index) & (self.data['id_a'] == df['id_a'].values[0]) & (self.data['id_b'] == df['id_b'].values[0]) & (self.data['wstd'] == df['wstd'].values[0])] = df.values def check_exist(self, df): """check if a dataframe df already exists in self.data Parameters ---------- df : pd.DataFrame Returns ------- boolean True if already exists False otherwise """ # check init case if not len(self.data.index) == 0: ud = self.data[(self.data.index == df.index) & (self.data['id_a'] == df['id_a'].values[0]) & (self.data['id_b'] == df['id_b'].values[0]) & (self.data['wstd'] == df['wstd'].values[0])] if len(ud) == 0: return False else: return True else: return False def savepd(self, df): """ save data information of a simulation Parameters ---------- df : one index data Notes ----- """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) store = pd.HDFStore(filenameh5) #self.data=self.data.sort() store.append('df', df) store.close() def loadpd(self): """ load data from previous simulations """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) store = pd.HDFStore(filenameh5) #self.data = pd.read_hdf(filenameh5,'df') self.data = store.get('df') self.data.index.name = 't' self.data = self.data.sort() def get_sim_time(self, t): """ retrieve closest time value in regard of passed t value in parameter """ if not isinstance(t, list) and not isinstance(t, np.ndarray): return np.array([self.time[np.where(self.time <= t)[0][-1]]]) else: return np.array([self.get_sim_time(tt) for tt in t])[:, 0] def get_df_from_link(self, id_a, id_b, wstd=''): """ Return a restricted data frame for a specific link Parameters ---------- id_a : str node id a id_b: str node id b wstd: str optionnal :wireslees standard """ if wstd == '': return self.data[(self.data['id_a'] == id_a) & (self.data['id_b'] == id_b)] else: return self.data[(self.data['id_a'] == id_a) & (self.data['id_b'] == id_b) & self.data['wstd'] == wstd] def update_pos(self, t): """ update positions of devices and bodies for a given time index Parameters ---------- t : int time value """ # if a bodies are involved in simulation if ((self.todo['OB']) or (self.todo['B2B']) or (self.todo['B2I'])): nodeid = [] pos = [] devlist = [] orient = [] for up, person in enumerate(self.dpersons.values()): person.settopos(self._traj[up], t=t, cs=True) name = person.name dev = person.dev.keys() devlist.extend(dev) #nodeid.extend([n + '_' + name for n in dev]) pos.extend([person.dcs[d][:, 0] for d in dev]) orient.extend([person.acs[d] for d in dev]) # TODO !!!!!!!!!!!!!!!!!!!! # in a future version , the network update must also update # antenna position in the device coordinate system self.N.update_pos(devlist, pos, now=t) self.N.update_orient(devlist, orient, now=t) self.N.update_dis() def get_value(self, **kwargs): """ retrieve output parameter at a specific time Parameters ---------- typ : list list of parameters to be retrieved (R | C |H | ak | tk | rss ) links: list dictionnary of link to be evaluated (key is wtsd and value is a list of links) (if [], all link are considered) t: int or np.array list of timestamp to be evaluated | singlr time instant Returns ------- output: dict [link_key]['t'] ['ak'] ... """ # get time defaults = { 't': 0, 'typ': ['ak'], 'links': {}, 'wstd': [], 'angles': False } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] # allocate an empty dictionnary for wanted selected output output = {} # manage time t can be a list or a float t = kwargs['t'] t = self.get_sim_time(t) dt = self.time[1] - self.time[0] # manage links plinks = kwargs['links'] links = [] if isinstance(plinks, dict): for l in plinks.keys(): links.extend(plinks[l]) if len(links) == 0: raise AttributeError('Please give valid links to get values') # output['t']=[] # output['time_to_simul']=[] # for each requested time step for tt in t: # for each requested links for link in links: linkname = link[0] + '-' + link[1] if not output.has_key(linkname): output[linkname] = {} if not output[linkname].has_key('t'): output[linkname]['t'] = [] # restrict global dataframe self.data to the specific link df = self.get_df_from_link(link[0], link[1]) # restrict global dataframe self.data to the specific z df = df[(df.index > tt - dt) & (df.index <= tt + dt)] if len(df) != 0: output[linkname]['t'].append(tt) if len(df) > 1: print 'Warning possible issue in self.get_value' line = df.iloc[-1] # # get info of the corresponding timestamp # line = df[(df['id_a'] == link[0]) & (df['id_b'] == link[1])].iloc[-1] # if len(line) == 0: # line = df[(df['id_b'] == link[0]) & (df['id_a'] == link[1])] # if len(line) == 0: # raise AttributeError('invalid link') #retrieve correct position and orientation given the time #self.update_pos(t=tt) # antennas positions #self.DL.a = self.N.node[link[0]]['p'] #self.DL.b = self.N.node[link[1]]['p'] # antennas orientation #self.DL.Ta = self.N.node[link[0]]['T'] #self.DL.Tb = self.N.node[link[1]]['T'] # antennas object #self.DL.Aa = self.N.node[link[0]]['ant']['antenna'] #self.DL.Ab = self.N.node[link[1]]['ant']['antenna'] # get the antenna index #uAa_opt, uAa = self.DL.get_idx('A_map',self.DL.Aa._filename) #uAb_opt, uAb = self.DL.get_idx('A_map',self.DL.Ab._filename) if 'ak' in kwargs['typ'] or 'tk' in kwargs[ 'typ'] or 'rss' in kwargs['typ']: H_id = line['H_id'].decode('utf8') # load the proper link # parse index lid = H_id.split('_') #if (lid[5]==str(uAa))&(lid[6]==str(uAb)): self.DL.load(self.DL.H, H_id) if 'ak' in kwargs['typ']: if not output[linkname].has_key('ak'): output[linkname]['ak'] = [] output[linkname]['ak'].append( copy.deepcopy(self.DL.H.ak)) if 'tk' in kwargs['typ']: if not output[linkname].has_key('tk'): output[linkname]['tk'] = [] output[linkname]['tk'].append( copy.deepcopy(self.DL.H.tk)) if 'rss' in kwargs['typ']: if not output[linkname].has_key('rss'): output[linkname]['rss'] = [] output[linkname]['rss'].append( copy.deepcopy(self.DL.H.rssi())) if 'R' in kwargs['typ']: if not output[linkname].has_key('R'): output[linkname]['R'] = [] ray_id = line['ray_id'] self.DL.load(self.DL.R, ray_id) output[linkname]['R'].append(copy.deepcopy(self.DL.R)) if 'C' in kwargs['typ']: if not output[linkname].has_key('C'): output[linkname]['C'] = [] Ct_id = line['Ct_id'] self.DL.load(self.DL.C, Ct_id) if kwargs['angles']: self.DL.C.islocal = False self.DL.C.locbas(Tt=self.DL.Ta, Tr=self.DL.Tb) #T channel output[linkname]['C'].append(copy.deepcopy(self.DL.C)) if 'H' in kwargs['typ']: if not output[linkname].has_key('H'): output[linkname]['H'] = [] H_id = line['H_id'] lid = H_id.split('_') #if (lid[5]==str(uAa))&(lid[6]==str(uAb)): self.DL.load(self.DL.H, H_id) output[linkname]['H'].append(copy.deepcopy(self.DL.H)) # if time value not found in dataframe else: if not output[linkname].has_key('time_to_simul'): output[linkname]['time_to_simul'] = [] output[linkname]['time_to_simul'].append(tt) for l in output.keys(): if output[l].has_key('time_to_simul'): print 'link', l, 'require simulation for timestamps', output[ l]['time_to_simul'] return (output) def get_link(self, **kwargs): """ retrieve a Link specific time from a simultraj Parameters ---------- typ : list list of parameters to be retrieved (ak | tk | R |C) links: list dictionnary of link to be evaluated (key is wtsd and value is a list of links) (if [], all link are considered) t: int or np.array list of timestamp to be evaluated | singlr time instant Returns ------- DL : DLink Examples -------- >>> from pylayers.simul.simultraj import * >>> from pylayers.measures.cormoran import * >>> C=CorSer(serie=6,day=11,layout=True) >>> S = Simul(C,verbose=False) >>> DL = S.get_link(typ=['R','C','H']) """ # get time defaults = { 't': 0, 'typ': ['ak'], 'links': {}, 'wstd': [], 'angles': False } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] output = {} # manage time t = kwargs['t'] t = self.get_sim_time(t) dt = self.time[1] - self.time[0] # manage links plinks = kwargs['links'] links = [] if isinstance(plinks, dict): for l in plinks.keys(): links.extend(plinks[l]) if len(links) == 0: raise AttributeError('Please give valid links to get values') # output['t']=[] # output['time_to_simul']=[] # for each requested time step for tt in t: # for each requested links for link in links: linkname = link[0] + '-' + link[1] if not output.has_key(linkname): output[linkname] = {} if not output[linkname].has_key('t'): output[linkname]['t'] = [] # restrict global dataframe self.data to the specific link df = self.get_df_from_link(link[0], link[1]) # restrict global dataframe self.data to the specific z df = df[(df.index > tt - dt) & (df.index <= tt + dt)] if len(df) != 0: output[linkname]['t'].append(tt) if len(df) > 1: print 'Warning possible issue in self.get_link' line = df.iloc[-1] # # get info of the corresponding timestamp # line = df[(df['id_a'] == link[0]) & (df['id_b'] == link[1])].iloc[-1] # if len(line) == 0: # line = df[(df['id_b'] == link[0]) & (df['id_a'] == link[1])] # if len(line) == 0: # raise AttributeError('invalid link') #retrieve correct position and orientation given the time self.update_pos(t=tt) self.DL.a = self.N.node[link[0]]['p'] self.DL.b = self.N.node[link[1]]['p'] self.DL.Ta = self.N.node[link[0]]['T'] self.DL.Tb = self.N.node[link[1]]['T'] #self.DL.Aa = self.N.node[link[0]]['ant']['antenna'] #self.DL.Ab = self.N.node[link[1]]['ant']['antenna'] #H_id = line['H_id'].decode('utf8') #self.DL.load(self.DL.H,H_id) if 'R' in kwargs['typ']: ray_id = line['ray_id'] self.DL.load(self.DL.R, ray_id) if 'C' in kwargs['typ']: Ct_id = line['Ct_id'] self.DL.load(self.DL.C, Ct_id) if kwargs['angles']: self.DL.C.islocal = False self.DL.C.locbas(Tt=self.DL.Ta, Tr=self.DL.Tb) if 'H' in kwargs['typ']: H_id = line['H_id'] self.DL.load(self.DL.H, H_id) return (self.DL) def _show3(self, **kwargs): """ 3D show using Mayavi Parameters ---------- t: float time index link: list [id_a, id_b] id_a : node id a id_b : node id b 'lay': bool show layout 'net': bool show net 'body': bool show bodies 'rays': bool show rays """ defaults = { 't': 0, 'link': [], 'wstd': [], 'lay': True, 'net': True, 'body': True, 'rays': True, 'ant': False } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] link = kwargs['link'] self.update_pos(kwargs['t']) if len(self.data) != 0: df = self.data[self.data.index == pd.to_datetime(kwargs['t'])] if len(df) != 0: raise AttributeError('invalid time') # default if link == []: line = df[df.index <= pd.to_datetime(0)] link = [line['id_a'].values[0], line['id_b'].values[0]] else: # get info of the corresponding timestamp line = df[(df['id_a'] == link[0]) & (df['id_b'] == link[1])] if len(line) == 0: line = df[(df['id_b'] == link[0]) & (df['id_a'] == link[1])] if len(line) == 0: raise AttributeError('invalid link') rayid = line['ray_id'].values[0] self.DL.a = self.N.node[link[0]]['p'] self.DL.b = self.N.node[link[1]]['p'] self.DL.Ta = self.N.node[link[0]]['T'] self.DL.Tb = self.N.node[link[1]]['T'] self.DL.load(self.DL.R, rayid) self.DL._show3(newfig=False, lay=kwargs['lay'], rays=kwargs['rays'], ant=False) else: self.DL._show3(newfig=False, lay=True, rays=False, ant=False) if kwargs['net']: self.N._show3(newfig=False) if kwargs['body']: for p in self.dpersons: self.dpersons[p]._show3(newfig=False, topos=True, pattern=kwargs['ant']) # def _saveh5_init(self): # """ initialization of the h5py file # """ # filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) # import ipdb # try: # f5 = h5py.File(filenameh5, 'w') # f5.create_dataset('time', shape=self.time.shape, data=self.time) # f5.close() # except: # f5.close() # raise NameError('simultra.saveinit: \ # issue when writting h5py file') def _saveh5(self, ut, ida, idb, wstd): """ Save in h5py format Parameters ---------- ut : int time index in self.time ida : string node a index idb : string node b index wstd : string wireless standard of used link Notes ----- Dataset organisation: simultraj_<trajectory_filename.h5>.h5 | |time | ... | |/<tidx_ida_idb_wstd>/ |attrs | |a_k | |t_k Root dataset : time : array range of simulation time Group identifier : tidx : index in time dataset ida : node a index in Network idb : node b index in Network wstd : wireless standar of link interest Inside group: a_k : alpha_k values t_k : tau_k values See Also -------- pylayers.simul.links """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) grpname = str(ut) + '_' + ida + '_' + idb + '_' + wstd # try/except to avoid loosing the h5 file if # read/write error try: fh5 = h5py.File(filenameh5, 'a') if not grpname in fh5.keys(): fh5.create_group(grpname) f = fh5[grpname] # for k in kwargs: # f.attrs[k] = kwargs[k] f.create_dataset('alphak', shape=self._ak.shape, maxshape=(None), data=self._ak) f.create_dataset('tauk', shape=self._tk.shape, maxshape=(None), data=self._tk) else: pass #print grpname + ' already exists in ' + filenameh5 fh5.close() except: fh5.close() raise NameError('Simultraj._saveh5: issue when writting h5py file') def _loadh5(self, grpname): """ Load in h5py format Parameters ---------- grpname : string group name which can be found sin self.data aktk_idx column Returns ------- (ak, tk, conf) ak : ndarray: alpha_k tk : ndarray: alpha_k """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) # try/except to avoid loosing the h5 file if # read/write error try: fh5 = h5py.File(filenameh5, 'r') if not grpname in fh5.keys(): fh5.close() raise NameError(grpname + ' cannot be reached in ' + self.filename) f = fh5[grpname] # for k in f.attrs.keys(): # conf[k]=f.attrs[k] ak = f['alphak'][:] tk = f['tauk'][:] fh5.close() return ak, tk except: fh5.close() raise NameError('Simultraj._loadh5: issue when reading h5py file') def tocsv(self, ut, ida, idb, wstd, init=False): filecsv = pyu.getlong(self._filecsv, pstruc['DIRLNK']) with open(filecsv, 'a') as csvfile: fil = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL) if init: keys = self.data.iloc[-1].keys() data = [k for k in keys] data.append('ak') data.append('tk') fil.writerow(data) values = self.data.iloc[-1].values data = [v for v in values] sak = str(self._ak.tolist()) stk = str(self._tk.tolist()) data.append(sak) data.append(stk) fil.writerow(data)
class Coverage(PyLayers): """ Handle Layout Coverage Methods ------- creategrid() create a uniform grid for evaluating losses cover() run the coverage calculation showPower() display the map of received power showLoss() display the map of losses Attributes ---------- All attributes are read from fileini ino the ini directory of the current project _fileini default coverage.ini L : a Layout nx : number of point on x ny : number of point on y tx : transmitter position txpe : transmitter power emmission level show : boolean for automatic display power map na : number of access point """ def __init__(self,_fileini='coverage.ini'): """ object constructor Parameters ---------- _fileini : string name of the configuration file Notes ----- Coverage is described in an ini file. Default file is coverage.ini and is placed in the ini directory of the current project. """ self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini,pstruc['DIRSIMUL'])) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.apopt = dict(self.config.items('ap')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) # get the Layout self.L = Layout(self.layoutopt['filename']) # get the receiving grid self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) self.ng = self.nx*self.ny self.mode = eval(self.gridopt['full']) self.boundary = eval(self.gridopt['boundary']) # create grid # we could here construct a grid locally around the access point # to be done later for code acceleration # self.creategrid(full=self.mode,boundary=self.boundary) self.dap = {} for k in self.apopt: kwargs = eval(self.apopt[k]) ap = std.AP(**kwargs) self.dap[eval(k)] = ap try: self.aap = np.vstack((self.aap,ap['p'][0:2])) self.ptdbm = np.vstack((self.ptdbm,ap['PtdBm'])) except: self.aap = ap['p'][0:2] self.ptdbm = ap['PtdBm'] # 1 x na self.ptdbm = self.ptdbm.T # number of access points self.na = len(self.dap) # creating all links p = product(range(self.ng),range(self.na)) # # a : access point # g : grid # for k in p: pg = self.grid[k[0],:] pa = self.aap[k[1]] try: self.pa = np.vstack((self.pa,pa)) except: self.pa = pa try: self.pg = np.vstack((self.pg,pg)) except: self.pg = pg self.pa = self.pa.T self.pg = self.pg.T # frequency is chosen as all the center frequencies of the standard # warning assuming the same standard self.fGHz = self.dap[0].s.fcghz self.nf = len(self.fGHz) # AP section #self.fGHz = eval(self.txopt['fghz']) #self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) #self.ptdbm = eval(self.txopt['ptdbm']) #self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section self.rxsens = eval(self.rxopt['sensitivity']) kBoltzmann = 1.3806503e-23 self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) # list of access points lap = self.dap.keys() # list of channels lchan = map(lambda x: self.dap[x]['chan'],lap) apchan = zip(self.dap.keys(),lchan) self.bmhz = np.array(map(lambda x: self.dap[x[0]].s.chan[x[1][0]]['BMHz']*len(x[1]),apchan)) # Evaluate Noise Power (in dBm) Pn = (10**(self.noisefactordb/10.)+1)*kBoltzmann*self.temperaturek*self.bmhz*1e3 self.pndbm = 10*np.log10(Pn)+60 # show section self.bshow = str2bool(self.showopt['show']) try: self.L.Gt.nodes() except: pass try: self.L.dumpr() except: self.L.build() self.L.dumpw() def __repr__(self): st='' st = st+ 'Layout file : '+self.L.filename + '\n\n' st = st + '-----list of Access Points ------'+'\n' for k in self.dap: st = st + self.dap[k].__repr__()+'\n' st = st + '-----Rx------'+'\n' st= st+ 'rxsens (dBm) : '+ str(self.rxsens) + '\n' st= st+ 'bandwith (Mhz) : '+ str(self.bmhz) + '\n' st= st+ 'temperature (K) : '+ str(self.temperaturek) + '\n' st= st+ 'noisefactor (dB) : '+ str(self.noisefactordb) + '\n\n' st = st + '--- Grid ----'+'\n' st= st+ 'nx : ' + str(self.nx) + '\n' st= st+ 'ny : ' + str(self.ny) + '\n' st= st+ 'nlink : ' + str(self.ng*self.na) + '\n' st= st+ 'full grid : ' + str(self.mode) + '\n' st= st+ 'boundary (xmin,ymin,xmax,ymax) : ' + str(self.boundary) + '\n\n' return(st) def creategrid(self,full=True,boundary=[]): """ create a grid Parameters ---------- full : boolean default (True) use all the layout area boundary : (xmin,ymin,xmax,ymax) if full is False the boundary argument is used """ if full: mi=np.min(self.L.Gs.pos.values(),axis=0)+0.01 ma=np.max(self.L.Gs.pos.values(),axis=0)-0.01 else: assert boundary<>[] mi = np.array([boundary[0],boundary[1]]) ma = np.array([boundary[2],boundary[3]]) x = np.linspace(mi[0],ma[0],self.nx) y = np.linspace(mi[1],ma[1],self.ny) self.grid=np.array((list(np.broadcast(*np.ix_(x, y))))) # def coverold(self): # """ run the coverage calculation (Deprecated) # # Parameters # ---------- # # lay_bound : bool # If True, the coverage is performed only inside the Layout # and clip the values of the grid chosen in coverage.ini # # Examples # -------- # # .. plot:: # :include-source: # # >> from pylayers.antprop.coverage import * # >> C = Coverage() # >> C.cover() # >> C.showPower() # # Notes # ----- # # self.fGHz is an array it means that coverage is calculated at once # for a whole set of frequencies. In practice the center frequency of a # given standard channel. # # This function is calling `Losst` which calculates Losses along a # straight path. In a future implementation we will # abstract the EM solver in order to make use of other calculation # approaches as full or partial Ray Tracing. # # The following members variables are evaluated : # # + freespace Loss @ fGHz PL() PathLoss (shoud be rename FS as free space) $ # + prdbmo : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{odB}` # + prdbmp : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{pdB}` # + snro : SNR polar o (H) # + snrp : SNR polar p (H) # # See Also # -------- # # pylayers.antprop.loss.Losst # pylayers.antprop.loss.PL # # """ # # self.Lwo,self.Lwp,self.Edo,self.Edp = loss.Losst(self.L,self.fGHz,self.grid.T,self.tx) # self.freespace = loss.PL(self.fGHz,self.grid,self.tx) # # self.prdbmo = self.ptdbm - self.freespace - self.Lwo # self.prdbmp = self.ptdbm - self.freespace - self.Lwp # self.snro = self.prdbmo - self.pndbm # self.snrp = self.prdbmp - self.pndbm def cover(self,polar='o',sinr=True,snr=True,best=True): """ run the coverage calculation Parameters ---------- polar : string 'o' | 'p' sinr : boolean snr : boolean best : boolean Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> f,a=C.show(typ='sinr') >>> plt.show() Notes ----- self.fGHz is an array it means that coverage is calculated at once for a whole set of frequencies. In practice the center frequency of a given standard channel. This function is calling `Losst` which calculates Losses along a straight path. In a future implementation we will abstract the EM solver in order to make use of other calculation approaches as full or partial Ray Tracing. The following members variables are evaluated : + freespace Loss @ fGHz PL() PathLoss (shoud be rename FS as free space) $ + prdbmo : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{odB}` + prdbmp : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{pdB}` + snro : SNR polar o (H) + snrp : SNR polar p (H) See Also -------- pylayers.antprop.loss.Losst pylayers.antprop.loss.PL """ # retrieving dimensions along the 3 axis na = self.na ng = self.ng nf = self.nf Lwo,Lwp,Edo,Edp = loss.Losst(self.L,self.fGHz,self.pa,self.pg,dB=False) if polar=='o': self.polar='o' self.Lw = Lwo.reshape(nf,ng,na) self.Ed = Edo.reshape(nf,ng,na) if polar=='p': self.polar='p' self.Lw = Lwp.reshape(nf,ng,na) self.Ed = Edp.reshape(nf,ng,na) freespace = loss.PL(self.fGHz,self.pa,self.pg,dB=False) self.freespace = freespace.reshape(nf,ng,na) # Warning we are assuming here all transmitters have the same # transmitting power (to be modified) # f x g x a # CmW : Received Power coverage in mW self.CmW = 10**(self.ptdbm[np.newaxis,...]/10.)*self.Lw*self.freespace if snr: self.snr() if sinr: self.sinr() if best: self.best() def snr(self): """ calculate snr """ NmW = 10**(self.pndbm/10.)[np.newaxis,np.newaxis,:] self.snr = self.CmW/NmW def sinr(self): """ calculate sinr """ na = self.na U = (np.ones((na,na))-np.eye(na))[np.newaxis,np.newaxis,:,:] ImW = np.einsum('ijkl,ijl->ijk',U,self.CmW) NmW = 10**(self.pndbm/10.)[np.newaxis,np.newaxis,:] self.sinr = self.CmW/(ImW+NmW) def best(self): """ determine best server map Notes ----- C.bestsv """ na = self.na ng = self.ng nf = self.nf # find best server regions V = self.CmW self.bestsv = np.zeros(nf*ng*na).reshape(nf,ng,na) for kf in range(nf): MaxV = np.max(V[kf,:,:],axis=1) for ka in range(na): u = np.where(V[kf,:,ka]==MaxV) self.bestsv[kf,u,ka]=ka+1 # def showEd(self,polar='o',**kwargs): # """ shows a map of direct path excess delay # # Parameters # ---------- # # polar : string # 'o' | 'p' # # Examples # -------- # # .. plot:: # :include-source: # # >> from pylayers.antprop.coverage import * # >> C = Coverage() # >> C.cover() # >> C.showEd(polar='o') # # """ # # if not kwargs.has_key('alphacy'): # kwargs['alphacy']=0.0 # if not kwargs.has_key('colorcy'): # kwargs['colorcy']='w' # if not kwargs.has_key('nodes'): # kwargs['nodes']=False # # fig,ax = self.L.showG('s',**kwargs) # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # cdict = { # 'red' : ((0., 0.5, 0.5), (1., 1., 1.)), # 'green': ((0., 0.5, 0.5), (1., 1., 1.)), # 'blue' : ((0., 0.5, 0.5), (1., 1., 1.)) # } # #generate the colormap with 1024 interpolated values # my_cmap = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024) # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # # # # if polar=='o': # mcEdof = np.ma.masked_where(prdbm < self.rxsens,self.Edo) # # cov=ax.imshow(mcEdof.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'jet', # origin='lower') # # # # # cov=ax.imshow(self.Edo.reshape((self.nx,self.ny)).T, # # extent=(l,r,b,t), # # origin='lower') # titre = "Map of LOS excess delay, polar orthogonal" # # if polar=='p': # mcEdpf = np.ma.masked_where(prdbm < self.rxsens,self.Edp) # # cov=ax.imshow(mcEdpf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'jet', # origin='lower') # # # cov=ax.imshow(self.Edp.reshape((self.nx,self.ny)).T, # # extent=(l,r,b,t), # # origin='lower') # titre = "Map of LOS excess delay, polar parallel" # # ax.scatter(self.tx[0],self.tx[1],linewidth=0) # ax.set_title(titre) # # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # clb = fig.colorbar(cov,cax) # clb.set_label('excess delay (ns)') # # if self.show: # plt.show() # return fig,ax # # def showPower(self,rxsens=True,nfl=True,polar='o',**kwargs): # """ show the map of received power # # Parameters # ---------- # # rxsens : bool # clip the map with rx sensitivity set in self.rxsens # nfl : bool # clip the map with noise floor set in self.pndbm # polar : string # 'o'|'p' # # Examples # -------- # # .. plot:: # :include-source: # # > from pylayers.antprop.coverage import * # > C = Coverage() # > C.cover() # > C.showPower() # # """ # # if not kwargs.has_key('alphacy'): # kwargs['alphacy']=0.0 # if not kwargs.has_key('colorcy'): # kwargs['colorcy']='w' # if not kwargs.has_key('nodes'): # kwargs['nodes']=False # fig,ax = self.L.showG('s',**kwargs) # # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # ## tCM = plt.cm.get_cmap('jet') ## tCM._init() ## alphas = np.abs(np.linspace(.0,1.0, tCM.N)) ## tCM._lut[:-3,-1] = alphas # # title='Map of received power - Pt = ' + str(self.ptdbm) + ' dBm'+str(' fGHz =') + str(self.fGHz) + ' polar = '+polar # # cdict = { # 'red' : ((0., 0.5, 0.5), (1., 1., 1.)), # 'green': ((0., 0.5, 0.5), (1., 1., 1.)), # 'blue' : ((0., 0.5, 0.5), (1., 1., 1.)) # } # # if not kwargs.has_key('cmap'): # # generate the colormap with 1024 interpolated values # cmap = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024) # else: # cmap = kwargs['cmap'] # #my_cmap = cm.copper # # # if rxsens : # # ## values between the rx sensitivity and noise floor # mcPrf = np.ma.masked_where((prdbm > self.rxsens) # & (prdbm < self.pndbm),prdbm) # # mcPrf = np.ma.masked_where((prdbm > self.rxsens) ,prdbm) # # cov1 = ax.imshow(mcPrf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = cm.copper, # vmin=self.rxsens,origin='lower') # # ### values above the sensitivity # mcPrs = np.ma.masked_where(prdbm < self.rxsens,prdbm) # cov = ax.imshow(mcPrs.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t), # cmap = cmap, # vmin=self.rxsens,origin='lower') # title=title + '\n black : Pr (dBm) < %.2f' % self.rxsens + ' dBm' # # else : # cov=ax.imshow(prdbm.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t), # cmap = cmap, # vmin=self.pndbm,origin='lower') # # if nfl: # ### values under the noise floor # ### we first clip the value below the noise floor # cl = np.nonzero(prdbm<=self.pndbm) # cPr = prdbm # cPr[cl] = self.pndbm # mcPruf = np.ma.masked_where(cPr > self.pndbm,cPr) # cov2 = ax.imshow(mcPruf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'binary', # vmax=self.pndbm,origin='lower') # title=title + '\n white : Pr (dBm) < %.2f' % self.pndbm + ' dBm' # # # ax.scatter(self.tx[0],self.tx[1],s=10,c='k',linewidth=0) # # ax.set_title(title) # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # clb = fig.colorbar(cov,cax) # clb.set_label('Power (dBm)') # # if self.show: # plt.show() # # return fig,ax # # # def showTransistionRegion(self,polar='o'): # """ # # Notes # ----- # # See : "Analyzing the Transitional Region in Low Power Wireless Links" # Marco Zuniga and Bhaskar Krishnamachari # # Examples # -------- # # .. plot:: # :include-source: # # > from pylayers.antprop.coverage import * # > C = Coverage() # > C.cover() # > C.showTransitionRegion() # # """ # # frameLength = self.framelengthbytes # # PndBm = self.pndbm # gammaU = 10*np.log10(-1.28*np.log(2*(1-0.9**(1./(8*frameLength))))) # gammaL = 10*np.log10(-1.28*np.log(2*(1-0.1**(1./(8*frameLength))))) # # PrU = PndBm + gammaU # PrL = PndBm + gammaL # # fig,ax = self.L.showGs() # # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # # zones = np.zeros(np.shape(prdbm)) # #pdb.set_trace() # # uconnected = np.nonzero(prdbm>PrU) # utransition = np.nonzero((prdbm < PrU)&(prdbm > PrL)) # udisconnected = np.nonzero(prdbm < PrL) # # zones[uconnected] = 1 # zones[utransition] = (prdbm[utransition]-PrL)/(PrU-PrL) # cov = ax.imshow(zones.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'BuGn',origin='lower') # # title='PDR region' # ax.scatter(self.tx[0],self.tx[1],linewidth=0) # # ax.set_title(title) # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # fig.colorbar(cov,cax) # if self.show: # plt.show() # def show(self,**kwargs): """ show coverage Parameters ---------- typ : string 'pr' | 'sinr' | 'capacity' | 'loss' | 'best' grid : boolean best : boolean draw best server contour if True f : int frequency index a : int access point index (-1 all access point) Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover(polar='o') >>> f,a = C.show(typ='pr') >>> plt.show() >>> f,a = C.show(typ='best') >>> plt.show() >>> f,a = C.show(typ='loss') >>> plt.show() >>> f,a = C.show(typ='sinr') >>> plt.show() """ defaults = { 'typ': 'pr', 'grid': False, 'f' : 0, 'a' :-1, 'db':True, 'cmap' :cm.jet, 'best':True } title = self.dap[0].s.name+ ' : ' for k in defaults: if k not in kwargs: kwargs[k]=defaults[k] if 'fig' in kwargs: fig,ax=self.L.showG('s',fig=kwargs['fig']) else: fig,ax=self.L.showG('s') # plot the grid if kwargs['grid']: for k in self.dap: p = self.dap[k].p ax.plot(p[0],p[1],'or') f = kwargs['f'] a = kwargs['a'] typ = kwargs['typ'] best = kwargs['best'] dB = kwargs['db'] # setting the grid l = self.grid[0,0] r = self.grid[-1,0] b = self.grid[0,1] t = self.grid[-1,-1] if typ=='best': title = title + 'Best server'+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+self.polar for ka in range(self.na): bestsv = self.bestsv[f,:,ka] m = np.ma.masked_where(bestsv == 0,bestsv) W = m.reshape(self.nx,self.ny).T ax.imshow(W, extent=(l,r,b,t), origin='lower', vmin=1, vmax=self.na+1) ax.set_title(title) else: if typ=='sinr': title = title + 'SINR : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+self.polar if dB: legcb = 'dB' else: legcb = 'Linear scale' V = self.sinr if typ=='snr': title = title + 'SNR : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+self.polar if dB: legcb = 'dB' else: legcb = 'Linear scale' V = self.snr if typ=='capacity': title = title + 'Capacity : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+self.polar legcb = 'Mbit/s' V = self.bmhz[np.newaxis,np.newaxis,:]*np.log(1+self.sinr)/np.log(2) if typ=='pr': title = title + 'Pr : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+self.polar if dB: legcb = 'dBm' else: lgdcb = 'mW' V = self.CmW if typ=='loss': title = title + 'Loss : '+' fc = '+str(self.fGHz[f])+' GHz'+ ' polar : '+self.polar if dB: legcb = 'dB' else: legcb = 'Linear scale' V = self.Lw*self.freespace if a == -1: V = np.max(V[f,:,:],axis=1) else: V = V[f,:,a] # reshaping the data on the grid U = V.reshape((self.nx,self.ny)).T if dB: U = 10*np.log10(U) if 'vmin' in kwargs: vmin = kwargs['vmin'] else: vmin = U.min() if 'vmax' in kwargs: vmax = kwargs['vmax'] else: vmax = U.max() img = ax.imshow(U, extent=(l,r,b,t), origin='lower', vmin = vmin, vmax = vmax, cmap = kwargs['cmap']) for k in range(self.na): ax.annotate(str(k),xy=(self.pa[0,k],self.pa[1,k])) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) clb = fig.colorbar(img,cax) clb.set_label(legcb) if best: ax.contour(np.sum(self.bestsv,axis=2)[f,:].reshape(self.nx,self.ny).T,extent=(l,r,b,t),linestyles='dotted') # display access points ax.scatter(self.pa[0,:],self.pa[1,:],s=10,c='k',linewidth=0) return(fig,ax)
from pylayers.gis.layout import Layout from pylayers.util.project import * import pylayers.util.pyutil as pyu import networkx as nx import matplotlib.pyplot as plt import warnings import shutil warnings.filterwarnings("error") #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout() lL = L.ls() for tL in lL: print 'Layout : ',tL print '--------------------------' if 'Munich' not in tL: L = Layout(tL,bbuild=0,bgraphs=0) f,a = L.showG('s') plt.title(tL,fontsize=32) plt.show() plt.close('all') #if L.check(): # L.save() #filein = pyu.getlong(L._filename, pstruc['DIRLAY']) #fileout = '/home/uguen/Documents/rch/devel/pylayers/data/struc/lay/'+L._filename #print fileout #shutil.copy2(filein,fileout) #figure(figsize=(20,10)) #plt.axis('off')
def load(self, _filesimul): """ load a simulation configuration file each transmiter simulation results in the creation of an .ini file with the following sections related to the results obtained for different receivers Parameters ---------- _filesimul : file in the simul directory of the Project """ self.filesimul = _filesimul filesimul = pyu.getlong(self.filesimul, "ini") self.config.read(filesimul) sections = self.config.sections() try: _filetx = self.config.get("files", "tx") except: raise NameError('Error in section tx from ' + _filesimul) try: _filerx = self.config.get("files", "rx") except: raise NameError('Error in section rx from ' + _filesimul) try: _fileini = self.config.get("files", "struc") except: raise NameError('Error in section struc from ' + _fileini) try: _fileanttx = self.config.get("files", "txant") except: raise NameError('Error in section txant from ' + _filesimul) try: _fileantrx = self.config.get("files", "rxant") except: raise NameError('Error in section rxant from ' + _filesimul) try: self.tx = RadioNode(name='', typ='tx', _fileini=_filetx, _fileant=_fileanttx) self.rx = RadioNode(name='', typ='rx', _fileini=_filerx, _fileant=_fileantrx) except: raise NameError('Error during Radionode load') # # Load Layout # try: self.L = Layout(_fileini) except: raise NameError('Layout load error') # # Frequency base # if "frequency" in sections: try: self.fGHz = np.linspace( float(self.config.getfloat("frequency", "fghzmin")), float(self.config.getfloat("frequency", "fghzmax")), int(self.config.getint("frequency", "nf")), endpoint=True) except: raise NameError('Error in section frequency from ' + _filesimul) # update .freq file in tud directory filefreq = pyu.getlong(self.filefreq, pstruc['DIRTUD']) fd = open(filefreq, "w") chaine = self.config.get("frequency", "fghzmin") + ' ' + \ self.config.get("frequency", "fghzmax") + ' ' + \ self.config.get("frequency", "nf") fd.write(chaine) fd.close # # Simulation Progress # # self.output = {} # if "output" in sections: # for itx in self.config.options("output"): # _filename = self.config.get("output", itx) # self.dout[int(itx)] = _filename # filename = pyu.getlong(_filename, "output") # output = ConfigParser.ConfigParser() # output.read(filename) # secout = output.sections() # self.dtra[int(itx)] = {} # self.dtud[int(itx)] = {} # self.dtang[int(itx)] = {} # self.drang[int(itx)] = {} # self.dtauk[int(itx)] = {} # self.dfield[int(itx)] = {} # self.dcir[int(itx)] = {} # if "launch" in secout: # self.progress = 1 # keys_launch = output.options("launch") # for kl in keys_launch: # self.dlch[int(kl)] = output.get("launch", kl) # if "trace" in secout: # self.progress = 2 # keys_tra = output.options("trace") # for kt in keys_tra: # self.dtra[int(itx)][int(kt)] = output.get("trace", kt) # # if "tang" in secout: # self.progress = 3 # keys_tang = output.options("tang") # for kt in keys_tang: # self.dtang[int(itx)][int(kt)] = output.get("tang", kt) # self.drang[int(itx)][int(kt)] = output.get("rang", kt) # self.dtud[int(itx)][int(kt)] = output.get("tud", kt) # if "field" in secout: # self.progress = 4 # keys_field = output.options("field") # for kt in keys_field: # self.dfield[int(itx)][int(kt)] = output.get( # "field", kt) # self.dtauk[int(itx)][int(kt)] = output.get("tauk", kt) # if "cir" in secout: # self.progress = 5 # keys_cir = output.options("cir") # for kt in keys_cir: # self.dcir[int(itx)][int(kt)] = output.get("cir", kt) # # self.output[int(itx)] = output # # Waveform section # self.wav = wvf.Waveform() self.wav.read(self.config)
class Coverage(object): """ Handle Layout Coverage Methods ------- create grid() create a uniform grid for evaluating losses cover() run the coverage calculation showPower() display the map of received power showLoss() display the map of losses Attributes ---------- All attributes are read from fileini ino the ini directory of the current project _fileini default coverage.ini L : a Layout model : a pylayers.network.model object. xstep : x step for grid ystep : y step for grid tx : transmitter position txpe : transmitter power emmission level show : boolean for automatic display power map """ def __init__(self,_fileini='coverage.ini'): self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini,pstruc['DIRSIMUL'])) self.plm = dict(self.config.items('pl_model')) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.txopt = dict(self.config.items('tx')) self.rxopt = dict(self.config.items('rx')) self.showopt=dict(self.config.items('show')) self.L=Layout(self.layoutopt['filename']) self.model=Model(f=eval(self.plm['f']), rssnp=eval(self.plm['rssnp']), d0=eval(self.plm['d0']), sigrss=eval(self.plm['sigrss'])) self.xstep = eval(self.gridopt['xstep']) self.ystep = eval(self.gridopt['ystep']) # transitter section self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) self.ptdbm = eval(self.txopt['ptdbm']) self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section self.rxsens = eval(self.rxopt['sensitivity']) kBoltzmann = 1.3806503e-23 self.bandwidthmhz = eval(self.rxopt['bandwidthmhz']) self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) Pn = (10**(self.noisefactordb/10.)+1)*kBoltzmann*self.temperaturek*self.bandwidthmhz*1e3 self.pndbm = 10*np.log10(Pn)+60 self.show = str2bool(self.showopt['show']) try: self.L.Gt.nodes() except: pass try: self.L.dumpr('t') except: self.L.buildGt() self.L.dumpw('t') self.creategrid() def creategrid(self): """create a grid create a grid for various evaluation """ mi=np.min(self.L.Gs.pos.values(),axis=0)+0.01 ma=np.max(self.L.Gs.pos.values(),axis=0)-0.01 x=np.linspace(mi[0],ma[0],self.xstep) y=np.linspace(mi[1],ma[1],self.ystep) self.grid=np.array((list(np.broadcast(*np.ix_(x, y))))) def cover(self): """ start the coverage calculation Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showPr() """ self.Lwo,self.Lwp,self.Edo,self.Edp = Loss0_v2(self.L,self.grid,self.model.f,self.tx) self.freespace = PL(self.grid,self.model.f,self.tx) self.prdbmo = self.ptdbm - self.freespace - self.Lwo self.prdbmp = self.ptdbm - self.freespace - self.Lwp def showEd(self,polarization='o'): """ show direct path excess of delay map Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showEdo() """ fig=plt.figure() fig,ax=self.L.showGs(fig=fig) l=self.grid[0,0] r=self.grid[-1,0] b=self.grid[0,1] t=self.grid[-1,-1] if polarization=='o': cov=ax.imshow(self.Edo.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t), origin='lower') titre = "Map of LOS excess delay, polar orthogonal" if polarization=='p': cov=ax.imshow(self.Edp.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t), origin='lower') titre = "Map of LOS excess delay, polar parallel" ax.scatter(self.tx[0],self.tx[1],linewidth=0) ax.set_title(titre) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) clb = fig.colorbar(cov,cax) clb.set_label('excess delay (ns)') if self.show: plt.show() def showPower(self,rxsens=True,nfl=True,polarization='o'): """ show the map of received power Parameters ---------- rxsens : bool clip the map with rx sensitivity set in self.rxsens nfl : bool clip the map with noise floor set in self.pndbm polarization : string 'o'|'p' Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showPower() """ fig=plt.figure() fig,ax=self.L.showGs(fig=fig) l=self.grid[0,0] r=self.grid[-1,0] b=self.grid[0,1] t=self.grid[-1,-1] if polarization=='o': prdbm=self.prdbmo if polarization=='p': prdbm=self.prdbmp # tCM = plt.cm.get_cmap('jet') # tCM._init() # alphas = np.abs(np.linspace(.0,1.0, tCM.N)) # tCM._lut[:-3,-1] = alphas title='Map of received power - Pt = '+str(self.ptdbm)+' dBm' cdict = { 'red' : ((0., 0.5, 0.5), (1., 1., 1.)), 'green': ((0., 0.5, 0.5), (1., 1., 1.)), 'blue' : ((0., 0.5, 0.5), (1., 1., 1.)) } #generate the colormap with 1024 interpolated values my_cmap = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024) if rxsens : ### values between the rx sensitivity and noise floor mcPrf = np.ma.masked_where((prdbm > self.rxsens) & (prdbm < self.pndbm),prdbm) cov1 = ax.imshow(mcPrf.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t),cmap = my_cmap, vmin=self.rxsens,origin='lower') ### values above the sensitivity mcPrs = np.ma.masked_where(prdbm < self.rxsens,prdbm) cov = ax.imshow(mcPrs.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t), cmap = 'jet', vmin=self.rxsens,origin='lower') title=title + '\n gray : Pr (dBm) < %.2f' % self.rxsens + ' dBm' else : cov=ax.imshow(prdbm.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t), cmap = 'jet', vmin=self.PndBm,origin='lower') if nfl: ### values under the noise floor ### we first clip the value below he noise floor cl = np.nonzero(prdbm<=self.pndbm) cPr = prdbm cPr[cl] = self.pndbm mcPruf = np.ma.masked_where(cPr > self.pndbm,cPr) cov2 = ax.imshow(mcPruf.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t),cmap = 'binary', vmax=self.pndbm,origin='lower') title=title + '\n white : Pr (dBm) < %.2f' % self.pndbm + ' dBm' ax.scatter(self.tx[0],self.tx[1],s=10,linewidth=0) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) clb = fig.colorbar(cov,cax) clb.set_label('Power (dBm)') if self.show: plt.show() def showTransistionRegion(self,polarization='o'): """ Notes ----- See : Analyzing the Transitional Region in Low Power Wireless Links Marco Zuniga and Bhaskar Krishnamachari Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showTransitionRegion() """ frameLength = self.framelengthbytes PndBm = self.pndbm gammaU = 10*np.log10(-1.28*np.log(2*(1-0.9**(1./(8*frameLength))))) gammaL = 10*np.log10(-1.28*np.log(2*(1-0.1**(1./(8*frameLength))))) PrU = PndBm + gammaU PrL = PndBm + gammaL fig=plt.figure() fig,ax = self.L.showGs(fig=fig) l = self.grid[0,0] r = self.grid[-1,0] b = self.grid[0,1] t = self.grid[-1,-1] if polarization=='o': prdbm=self.prdbmo if polarization=='p': prdbm=self.prdbmp zones = np.zeros(len(prdbm)) uconnected = np.nonzero(prdbm>PrU)[0] utransition = np.nonzero((prdbm < PrU)&(prdbm > PrL))[0] udisconnected = np.nonzero(prdbm < PrL)[0] zones[uconnected] = 1 zones[utransition] = (prdbm[utransition]-PrL)/(PrU-PrL) cov = ax.imshow(zones.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t),cmap = 'BuGn',origin='lower') title='PDR region' ax.scatter(self.tx[0],self.tx[1],linewidth=0) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(cov,cax) if self.show: plt.show() def showLoss(self,polarization='o'): """ show losses map Parameters ---------- polarization : string 'o'|'p'|'both' Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showLoss(polarization='o') >>> C.showLoss(polarization='p') """ fig = plt.figure() fig,ax=self.L.showGs(fig=fig) l=self.grid[0,0] r=self.grid[-1,0] b=self.grid[0,1] t=self.grid[-1,-1] if polarization=='o': cov = ax.imshow(self.Lwo.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t), origin='lower') title = ('Map of losses, orthogonal (V) polarization') if polarization=='p': cov = ax.imshow(self.Lwp.reshape((self.xstep,self.ystep)).T, extent=(l,r,b,t), origin='lower') title = ('Map of losses, parallel (H) polarization') ax.scatter(self.tx[0],self.tx[1],linewidth=0) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(cov,cax) if self.show: plt.show()
def __init__(self, **args): """ Mobile Agent Init Parameters ---------- 'ID': string agent ID 'name': string Agent name 'type': string agent type . 'ag' for moving agent, 'ap' for static acces point 'pos' : np.array([]) numpy array containing the initial position of the agent 'roomId': int Room number where the agent is initialized (Layout.Gr) 'meca_updt': float update time interval for the mechanical process 'loc': bool enable/disable localization process of the agent 'loc_updt': float update time interval for localization process 'Layout': pylayers.gis.Layout() 'net':pylayers.network.Network(), 'RAT': list of string list of used radio access techology of the agent 'world': transit.world() Soon deprecated 'save': list of string list of save method ( soon deprecated) 'sim':Simpy.SimulationRT.Simulation(), 'epwr': dictionnary dictionnary of emmited power of transsmitter{'rat#':epwr value} 'sens': dictionnary dictionnary of sensitivity of reveicer {'rat#':sens value} 'dcond': dictionnary Not used yet 'gcom':pylayers.communication.Gcom() Communication graph 'comm_mod': string Communication between nodes mode: 'autonomous': all TOAs are refreshed regulary 'synchro' : only visilbe TOAs are refreshed """ defaults = { 'ID': 0, 'name': 'johndoe', 'type': 'ag', 'pos': np.array([]), 'roomId': 0, 'froom': [], 'wait': [], 'cdest': 'random', 'meca_updt': 0.1, 'loc': False, 'loc_updt': 0.5, 'loc_method': ['geo'], 'Layout': Layout(), 'net': Network(), 'RAT': ['wifi'], 'world': world(), 'save': [], 'sim': Simulation(), 'epwr': {}, 'sens': {}, 'dcond': {}, 'gcom': Gcom(), 'comm_mode': 'autonomous' } for key, value in defaults.items(): if key not in args: args[key] = value self.args = args self.ID = args['ID'] self.name = args['name'] self.type = args['type'] # Create Network self.net = args['net'] self.epwr = args['epwr'] self.gcom = args['gcom'] try: self.dcond = args['dcond'] except: pass if self.type == 'ag': # mechanical init self.meca = Person(ID=self.ID, roomId=args['roomId'], L=args['Layout'], net=self.net, interval=args['meca_updt'], wld=args['world'], sim=args['sim'], moving=True, froom=args['froom'], wait=args['wait'], cdest=args['cdest'], save=args['save']) self.meca.behaviors = [Queuing(),Seek(), Containment(),\ Separation(), InterpenetrationConstraint()] self.meca.steering_mind = queue_steering_mind # self.meca.steering_mind = queue_steering_mind # filll in network ## Network init self.node = Node(ID=self.ID, p=conv_vecarr(self.meca.position), t=time.time(), RAT=args['RAT'], epwr=args['epwr'], sens=args['sens'], type=self.type) self.net.add_nodes_from(self.node.nodes(data=True)) self.sim = args['sim'] self.sim.activate(self.meca, self.meca.move(), 0.0) self.PN = self.net.node[self.ID]['PN'] ## Communication init if args['comm_mode'] == 'synchro': ## The TOA requests are made every refreshTOA time ( can be modified in agent.ini) self.rxr = RX(net=self.net, ID=self.ID, dcond=self.dcond, gcom=self.gcom, sim=self.sim) self.rxt = RX(net=self.net, ID=self.ID, dcond=self.dcond, gcom=self.gcom, sim=self.sim) self.sim.activate(self.rxr, self.rxr.refresh_RSS(), 0.0) self.sim.activate(self.rxt, self.rxt.refresh_TOA(), 0.0) elif args['comm_mode'] == 'autonomous': ## The TOA requests are made by node only when they are in visibility of pairs. self.rxr = RX(net=self.net, ID=self.ID, dcond=self.dcond, gcom=self.gcom, sim=self.sim) self.rxt = RX(net=self.net, ID=self.ID, dcond=self.dcond, gcom=self.gcom, sim=self.sim) self.txt = TX(net=self.net, ID=self.ID, dcond=self.dcond, gcom=self.gcom, sim=self.sim) self.sim.activate(self.rxr, self.rxr.refresh_RSS(), 0.0) self.sim.activate(self.rxt, self.rxt.wait_TOArq(), 0.0) self.sim.activate(self.txt, self.txt.request_TOA(), 0.0) elif self.type == 'ap': if args['roomId'] == -1: self.node = Node(ID=self.ID, p=self.args['pos'], t=time.time(), RAT=args['RAT'], epwr=args['epwr'], sens=args['sens'], type=self.type) else: pp = np.array(args['Layout'].Gr.pos[self.args['roomId']]) self.node = Node(ID=self.ID, p=pp, t=time.time(), RAT=args['RAT'], epwr=args['epwr'], sens=args['sens'], type=self.type) self.net.add_nodes_from(self.node.nodes(data=True)) self.sim = args['sim'] # self.sim.activate(self.meca, self.meca.move(),0.0) self.PN = self.net.node[self.ID]['PN'] self.PN.node[self.ID]['pe'] = self.net.node[self.ID]['p'] else: raise NameError( 'wrong agent type, it must be either agent (ag) or acces point (ap) ' ) if self.type == 'ap': self.MoA = 1 else: self.MoA = 0 if 'mysql' in args['save']: config = ConfigParser.ConfigParser() config.read(pyu.getlong('simulnet.ini', 'ini')) sql_opt = dict(config.items('Mysql')) db = Database(sql_opt['host'], sql_opt['user'], sql_opt['passwd'], sql_opt['dbname']) db.writenode(self.ID, self.name, self.MoA) if 'txt' in args['save']: pyu.writenode(self) if args['loc'] and self.type != 'ap': self.loc = Localization(net=self.net, ID=self.ID, method=args['loc_method']) self.Ploc = PLocalization(loc=self.loc, loc_updt_time=args['loc_updt'], sim=args['sim']) self.sim.activate(self.Ploc, self.Ploc.run(), 1.5)
import time from pylayers.util.project import * import pylayers.util.pyutil as pyu from pylayers.util.utilnet import str2bool from pylayers.gis.layout import Layout from pylayers.antprop.multiwall import * from pylayers.antprop.coverage import * from pylayers.network.model import * L = Layout('TA-Office.ini') L.dumpr() A=np.array((4,1)) # defining transmitter position B=np.array((30,12)) # defining receiver position fGHz = 2.4 r = np.array((B,B))
from pylayers.gis.layout import Layout import networkx as nx import matplotlib.pyplot as plt import doctest plt.ion() #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout('WHERE1.ini') #L= Layout('11Dbibli.ini') #L.show() #L = Layout('PTIN.ini') #L = Layout('DLR.ini') #L.build() L.dumpr() L.showGi(en=11) #Ga = L.buildGr() #L.showGs() #nx.draw(Ga,Ga.pos)
from pylayers.gis.layout import Layout import networkx as nx import matplotlib.pyplot as plt #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout() lL = L.ls() for tL in lL: print tL #f = plt.figure(figsize=(20,10)) #plt.axis('off') #f,a = L.showG('s',nodes=False,fig=f) #plt.show() #f,a = L.showG('r',edge_color='b',linewidth=4,fig=f) #L= Layout('11Dbibli.ini') #L.show() #L = Layout('PTIN.ini') #L = Layout('DLR.ini') #L.buildGt() #Ga = L.buildGr() #L.showGs() #nx.draw(Ga,Ga.pos)
def ishow(self): """ interactive show of trajectories Examples -------- .. plot:: :include-source: >>> from pylayers.mobility.trajectory import * >>> T=Trajectories() >>> T.loadh5() >>> T.ishow() """ fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2, left=0.3) t = np.arange(0, len(self[0].index), self[0].ts) L = Layout(self.Lfilename) fig, ax = L.showG('s', fig=fig, ax=ax) valinit = 0 lines = [] labels = [] colors = "bgrcmykw" for iT, T in enumerate(self): if T.typ == 'ag': lines.extend(ax.plot(T['x'][0:valinit],T['y'][0:valinit], 'o', color=colors[iT], visible=True)) labels.append(T.name + ':' + T.ID) else: lines.extend(ax.plot(T['x'][0], T['y'][0], '^', ms=12, color=colors[iT], visible=True)) labels.append(T.name + ':' + T.ID) t = self[0].time() # init boolean value for visible in checkbutton blabels = [True]*len(labels) ######## # slider ######## slider_ax = plt.axes([0.1, 0.1, 0.8, 0.02]) slider = Slider(slider_ax, "time", self[0].tmin, self[0].tmax, valinit=valinit, color='#AAAAAA') def update(val): if val >= 1: pval=np.where(val>t)[0] ax.set_title(str(self[0].index[pval[-1]].time())[:11].ljust(12), loc='left') for iT, T in enumerate(self): if T.typ == 'ag': lines[iT].set_xdata(T['x'][pval]) lines[iT].set_ydata(T['y'][pval]) fig.canvas.draw() slider.on_changed(update) ######## # choose ######## rax = plt.axes([0.02, 0.5, 0.3, 0.2], aspect='equal') # check (ax.object, name of the object , bool value for the obsject) check = CheckButtons(rax, labels, tuple(blabels)) def func(label): i = labels.index(label) lines[i].set_visible(not lines[i].get_visible()) fig.canvas.draw() check.on_clicked(func) fig.canvas.draw() plt.show(fig)
class Simul(PyLayers): """ Simulation Class Methods ------- gui() graphical user interface choose() choose a simulation file in simuldir info() info about the simulation showray(itx,irx,iray) show a single ray help() help on using Simulation object freq() return the frequency base save() save Simulation file layout load a Layout file load() load Simulation show3() geomview vizualization show3l(itx,irx) geomview vizualization of link itx irx show() 2D visualization of simulation with furniture run(itx,irx) run simulation for links (itx,irx) cir(itx,irx,store_level=0,alpha=1.0) Channel Impulse Response calculation Attributes ---------- fileconf filetauk filetang filerang tx rx progress indoor mat sl Notes ------ This class group together all the parametrization files of a simulation Directory list file : fileconf Constitutive parameters file : fileslab filemat Ray Tracing parameters files : filepalch filetra Frequency list file : filefreq """ def __init__(self, _filesimul='default.ini'): self.filesimul = _filesimul self.config = ConfigParser.ConfigParser() self.config.add_section("files") self.config.add_section("frequency") self.config.add_section("waveform") self.config.add_section("output") self.dtang = {} self.drang = {} self.dtauk = {} self.dfield = {} self.dcir = {} self.output = {} # # Here was a nasty bug : Rule for the future # "Always precise the key value of the passed argument" # # Mal nommer les choses, c'est ajouter au malheur du monde ( Albert Camus ) # self.tx = RadioNode(name = '', typ = 'tx', _fileini = 'radiotx.ini', _fileant = 'defant.vsh3', ) self.rx = RadioNode(name = '', typ = 'rx', _fileini = 'radiorx.ini', _fileant = 'defant.vsh3', ) self.filefreq = "def.freq" self.progress = -1 # simulation not loaded self.filetang = [] self.filerang = [] self.filetauk = [] self.filefield = [] self.fileconf = "project.conf" self.cfield = [] self.fGHz = np.linspace(2, 11, 181, endpoint=True) self.wav = wvf.Waveform() try: self.load(_filesimul) except: pass def gui(self): """ gui to modify the simulation file """ simulgui = multenterbox('', 'Simulation file', ('filesimul', 'filestr', 'filefreq', 'filespaTx', 'filespaRx', 'fileantTx' 'fileantRx'), (self.filesimul, self.filestr, self.filefreq, self.filespaTx, self.filespaRx, self.fileantTx, self.fileantRx)) if simulgui is not None: self.filesimul = simulgui[0] self.filestr = simulgui[1] self.filefreq = simulgui[6] self.filespaTx = simulgui[7] self.filespaRx = simulgui[8] self.fileantTx = simulgui[9] self.fileantRx = simulgui[10] def updcfg(self): """ update simulation .ini config file with values currently in use. """ self.config.set("files", "struc", self.filestr) self.config.set("files", "conf", self.fileconf) self.config.set("files", "txant", self.tx.fileant) self.config.set("files", "rxant", self.rx.fileant) self.config.set("files", "tx", self.tx.fileini) self.config.set("files", "rx", self.rx.fileini) self.config.set("files", "mat", self.filematini) self.config.set("files", "slab", self.fileslabini) self.config.set("tud", "purc", str(self.patud.purc)) self.config.set("tud", "nrmax", str(self.patud.nrmax)) self.config.set("tud", "num", str(self.patud.num)) # # frequency section # self.config.set("frequency", "fghzmin", self.fGHz[0]) self.config.set("frequency", "fghzmax", self.fGHz[-1]) self.config.set("frequency", "nf", str(len(self.fGHz))) # # waveform section # self.config.set("waveform", "tw", str(self.wav['twns'])) self.config.set("waveform", "band", str(self.wav['bandGHz'])) self.config.set("waveform", "fc", str(self.wav['fcGHz'])) self.config.set("waveform", "thresh", str(self.wav['threshdB'])) self.config.set("waveform", "type", str(self.wav['typ'])) self.config.set("waveform", "fe", str(self.wav['feGHz'])) # # output section # for k in self.output.keys(): self.config.set("output",str(k),self.dout[k]) # Initialize waveform self.wav = wvf.Waveform() # Update waveform self.wav.read(self.config) self.save() def clean(self, level=1): """ clean Notes ----- obsolete Parameters ---------- level = 1 """ if level > 0: for itx in range(self.tx.N): filename = pyu.getlong(self.filelch[itx], pstruc['DIRLCH']) print filename if level > 1: for itx in range(self.tx.N): for irx in range(self.rx.N): filename = pyu.getlong(self.filetra[itx][irx], pstruc(['DIRTRA'])) print filename if level > 2: for itx in range(self.tx.N): for irx in range(self.rx.N): filename = pyu.getlong(self.filetud[itx][irx], pstruc['DIRTUD']) print filename filename = pyu.getlong(self.filetauk[itx][irx],pstruc['DIRTUD']) print filename if level > 3: for itx in range(self.tx.N): for irx in range(self.rx.N): filename = pyu.getlong(self.filetang[itx][irx], pstruc['DIRTUD']) print filename filename = pyu.getlong(self.filerang[itx][irx], pstruc['DIRTUD']) print filename filename = pyu.getlong(self.filefield[itx][irx], pstruc['DIRTUD']) print filename def clean_project(self,verbose= True): """ Clean Pyrpoject directory remove .lch, .tra, .field .tud, .tauk, .tang, .rang, <pyproject>/output remove [output] entries into .ini of self.filesimul Parameters ---------- verbose : boolean Verbose mode on/off Returns ------- Boolean: True if the project has been cleaned, False otherwise """ if verbose: print "-----------------------------------" print "-----------------------------------" print " WARNING " print "-----------------------------------" print "-----------------------------------" print "You are about to remove ALL previous computed raytracing files." print "If you decide to remove it, you will need to restart the entire \ raytracing simulation to exploit simulation results" print "\n Do you want to remove these simulation files ? y/n" r=raw_input() else : r =='y' if r == 'y': inifile=self.filesimul try: path=os.getenv('BASENAME') except: print('Error : there is no project directory in $BASENAME') dirlist=['output'] extension=['.lch','.field','.tra','.tud','.tang','.rang','.tauk'] rindic=False # remove file for d in dirlist: for ex in extension: files = os.listdir(path +'/' +d ) for f in files: if not os.path.isdir(path+'/'+d+'/'+f) and ex in f: rindic=True if verbose: print f os.remove(path+'/'+d+'/'+f) if rindic: if verbose: print 'removed *' + ex +' from ' +d +'\n' rindic=False # remove output into the self.filesimul ini file simcfg = ConfigParser.ConfigParser() simcfg.read(pyu.getlong(inifile,pstruc['DIRSIMUL'])) simcfg.remove_section('output') f=open(pyu.getlong(inifile,pstruc['DIRSIMUL']),'wb') simcfg.write(f) f.close() self.dout = {} self.dlch = {} self.dtra = {} self.dtud = {} self.dtang = {} self.drang = {} self.dtauk = {} self.dfield = {} self.dcir = {} self.output = {} if verbose: print 'removed [output] entries into ' +inifile +'\n' print 'Project CLEANED' return True else : if verbose: print "clean project process ABORTED" return False def save(self): """ save simulation file Simulation files are .ini files which are saved in a dedicated directory basename/ini in the Project tree """ filesimul = pyu.getlong(self.filesimul, "ini") fd = open(filesimul, "w") # getting current spa file if any try: self.config.set("files", "tx", self.tx.fileini) except: pass try: self.config.set("files", "rx", self.rx.fileini) except: pass self.config.write(fd) fd.close() # save tx self.tx.save() # save rx self.rx.save() # save slab and mat file # -- # self.slab.save(self.fileslabini) # self.slab.mat.save(self.filematini) # -- # fix bug #189 # slab is a member of S.L not of S anymore # mat is a member of S.L.sl not of S.slab try: self.L.sl.save(self.fileslabini) except: pass try: self.L.sl.mat.save(self.filematini) except: pass # def saveold(self): # """ save simulation file # """ # filesimul = pyu.getlong(self.filesimul, "ini") # fd = open(filesimul, "w") # config = ConfigParser.ConfigParser() # # # # files section # # # #config.add_section("files") # self.config.set("files", "conf", self.fileconf) # self.config.set("files", "struc", self.filestr) # self.config.set("files", "slab", self.fileslab) # self.config.set("files", "mat", self.filemat) # try: # self.config.set("files", "tx", self.tx.filespa) # except: # pass # try: # self.config.set("files", "rx", self.rx.filespa) # except: # pass # try: # self.config.set("files", "txant", self.tx.fileant) # except: # pass # try: # self.config.set("files", "rxant", self.rx.fileant) # except: # pass # self.config.set("files", "patra", self.filepatra) # self.config.set("files", "palch", self.filepalch) # self.palch.save() # self.patra.save() # # # # tud section # # # self.config.set("tud", "purc", self.patud.purc) # self.config.set("tud", "nrmax", self.patud.nrmax) # self.config.set("tud", "num", self.patud.num) # # # # frequency section # # # self.config.set("frequency", "fghzmin", self.freq[0]) # self.config.set("frequency", "fghzmax", self.freq[-1]) # self.config.set("frequency", "Nf", len(self.freq)) # # # # output section # # # #filelch exists # if self.progress > 0: # #config.add_section("output") # for k in range(len(self.filelch)): # _fileout = "out" + "???" # filename = self.filelch[k] # self.config.set("launch", str(k + 1), filename) # # filetra exists # for k in range(len(self.filelch)): # if self.progress > 1: # #self.config.add_section("trace") # for l in arange(len(self.filetra[k])): # filename = self.filetra[k][l] # self.config.set("trace", "rx" + str(l + 1), filename) # # .tang exists # # .rang exists # # .tud exists # if self.progress > 2: # #config.add_section("tud") # #config.add_section("tang") # #config.add_section("rang") # for l in arange(len(self.filetud[k])): # ftud = self.filetud[k][l] # self.config.set("tud", "rx" + str(l + 1), ftud) # for l in arange(len(self.filetang[k])): # ftang = self.filetang[k][l] # self.config.set("tang", "rx" + str(l + 1), ftang) # for l in arange(len(self.filerang[k])): # frang = self.filerang[k][l] # self.config.set("rang", "rx" + str(l + 1), frang) # # .field exist # # .tauk exist # if self.progress > 3: # #config.add_section("tauk") # #config.add_section("field") # for l in arange(len(self.filetud[k])): # ftauk = self.filetud[k][l] # self.config.set("tauk", "rx" + str(l + 1), ftauk) # for l in arange(len(self.filefield[k])): # ffield = self.filefield[k][l] # self.config.set("field", "rx" + str(l + 1), ffield) # self.config.write(fd) # fd.close() def save_project(self): """ save Simulation files in a zipfile Simulation files are .ini files which are saved in a dedicated directory basename/ini in the Project tree """ root = Tkinter.Tk() zipfileName = tkFileDialog.asksaveasfilename(parent=root, filetypes = [("zipped file","zip")] , title="Save Project", ) pyu.zipd(basename,zipfileName) root.withdraw() print "Current project saved in", zipfileName def load_project(self): """ load Simulation files from a zipfile Simulation files are .ini files which are saved in a dedicated directory basename/ini in the Project tree """ root = Tkinter.Tk() zipfileName= tkFileDialog.askopenfile(parent=root, mode='rb', title='Choose a project') dirname = tkFileDialog.askdirectory(parent=root, initialdir=basename, title='Please select a directory', mustexist=0) pyu.unzipd(dirname,zipfileName) root.withdraw() print "Current project loaded in", dirname def choose(self): """ Choose a simulation file in simuldir """ import tkFileDialog as FD fichsimul = FD.askopenfilename(filetypes=[("Fichiers simul ", "*.simul"), ("All", "*")], title="Please choose a simulation file", initialdir=simuldir) self.filesimul = pyu.getshort(fichsimul) self.load() def load(self, _filesimul): """ load a simulation configuration file each transmiter simulation results in the creation of an .ini file with the following sections related to the results obtained for different receivers Parameters ---------- _filesimul : file in the simul directory of the Project """ self.filesimul = _filesimul filesimul = pyu.getlong(self.filesimul, "ini") self.config.read(filesimul) sections = self.config.sections() try: _filetx = self.config.get("files", "tx") except: raise NameError('Error in section tx from '+ _filesimul) try: _filerx = self.config.get("files", "rx") except: raise NameError('Error in section rx from '+ _filesimul) try: _fileanttx = self.config.get("files", "txant") except: raise NameError('Error in section txant from '+ _filesimul) try: _fileantrx = self.config.get("files", "rxant") except: raise NameError('Error in section rxant from '+ _filesimul) try: self.tx = RadioNode(name = '', typ = 'tx', _fileini = _filetx, _fileant = _fileanttx, _filestr = self.filestr) self.rx = RadioNode(name = '', typ = 'rx', _fileini = _filerx, _fileant = _fileantrx, _filestr = self.filestr) except: raise NameError('Error during Radionode load') # # Load Layout # try: self.L = Layout(self.filestr) except: raise NameError('Layout load error') # # Frequency base # if "frequency" in sections: try: self.fGHz = np.linspace(float(self.config.getfloat("frequency", "fghzmin")), float(self.config.getfloat("frequency", "fghzmax")), int(self.config.getint("frequency", "nf")), endpoint=True) except: raise NameError('Error in section frequency from '+ _filesimul) # update .freq file in tud directory filefreq = pyu.getlong(self.filefreq, pstruc['DIRTUD']) fd = open(filefreq, "w") chaine = self.config.get("frequency", "fghzmin") + ' ' + \ self.config.get("frequency", "fghzmax") + ' ' + \ self.config.get("frequency", "nf") fd.write(chaine) fd.close # # Simulation Progress # self.output = {} if "output" in sections: for itx in self.config.options("output"): _filename = self.config.get("output", itx) self.dout[int(itx)] = _filename filename = pyu.getlong(_filename, "output") output = ConfigParser.ConfigParser() output.read(filename) secout = output.sections() self.dtra[int(itx)] = {} self.dtud[int(itx)] = {} self.dtang[int(itx)] = {} self.drang[int(itx)] = {} self.dtauk[int(itx)] = {} self.dfield[int(itx)] = {} self.dcir[int(itx)] = {} if "launch" in secout: self.progress = 1 keys_launch = output.options("launch") for kl in keys_launch: self.dlch[int(kl)] = output.get("launch", kl) if "trace" in secout: self.progress = 2 keys_tra = output.options("trace") for kt in keys_tra: self.dtra[int(itx)][int(kt)] = output.get("trace", kt) if "tang" in secout: self.progress = 3 keys_tang = output.options("tang") for kt in keys_tang: self.dtang[int(itx)][int(kt)] = output.get("tang", kt) self.drang[int(itx)][int(kt)] = output.get("rang", kt) self.dtud[int(itx)][int(kt)] = output.get("tud", kt) if "field" in secout: self.progress = 4 keys_field = output.options("field") for kt in keys_field: self.dfield[int(itx)][int(kt)] = output.get( "field", kt) self.dtauk[int(itx)][int(kt)] = output.get("tauk", kt) if "cir" in secout: self.progress = 5 keys_cir = output.options("cir") for kt in keys_cir: self.dcir[int(itx)][int(kt)] = output.get("cir", kt) self.output[int(itx)] = output # # Waveform section # self.wav = wvf.Waveform() self.wav.read(self.config) def layout(self, _filestruc): """ load a layout in the simulation oject Parameters ---------- _filestruc : string short file name of the Layout object Examples -------- >>> from pylayers.simul.simulem import * >>> S = Simul() >>> S.layout('defstr.ini') """ self.filestr = _filestruc self.L = Layout(_filestruc) # update config self.config.set("files", "struc", self.filestr) self.save() def show(self, itx=[-1], irx=[-1], furniture=True, s=8, c='b', traj=False, num=False,fig=[],ax=[]): """ show simulation Parameters ----------- itx : list of tx indices irx : list of rx indices furniture : boolean for METALIC furniture display s : scale fir scatter plot (default 8) c : color for scatter plot (default 'b') traj : boolean (def False) num : boolean (def False) display a number Examples -------- >>> import matplotlib.pyplot as plt >>> from pylayers.simul.simulem import * >>> S = Simul() >>> S.load('w1.ini') >>> S.L.loadfur('FurW1.ini') >>> S.show() >>> plt.show() """ if type(itx) == int: itx = [itx] if type(irx) == int: irx = [irx] if fig ==[]: fig = plt.gcf() if ax==[]: ax = fig.gca() #self.L.display['scaled']=False fig,ax=self.L.showG('s',fig=fig,ax=ax, aw=True) # if furniture: if 'lfur' in self.L.__dict__: for fur in self.L.lfur: if fur.Matname == 'METAL': fur.show(fig, ax) else: print "Warning : no furniture file loaded" if irx[0] == -1: ax.scatter(self.rx.position[0,:], self.rx.position[1,:], c='b', s=s, alpha=0.5) #ax.scatter(self.rx.position[0,0],self.rx.position[0,1],c='k',s=s,linewidth=0) #ax.scatter(self.rx.position[1,0],self.rx.position[1,1],c='b',s=s,linewidth=0) #ax.scatter(self.rx.position[2,0],self.rx.position[2,1],c='g',s=s,linewidth=0) #ax.scatter(self.rx.position[3,0],self.rx.position[3,1],c='c',s=s,linewidth=0) else: for k in irx: ax.scatter(self.rx.position[0,k - 1], self.rx.position[1,k - 1], c='b', s=s, alpha=0.5) if num: ax.text(self.rx.position[0,k - 1], self.rx.position[1,k - 1], str(k), color='blue') if itx[0] == -1: ax.scatter(self.tx.position[0,:], self.tx.position[1,:], c='r', s=s) else: if traj: cpt = 1 for k in itx: ax.scatter(self.tx.position[0,k - 1], self.tx.position[1,k - 1], c=c, s=s, linewidth=0) if num: if traj: ax.text(self.tx.position[0,k - 1], self.tx.position[1,k - 1], str(cpt), color='black') cpt = cpt + 1 else: ax.text(self.tx.position[0,k - 1], self.tx.position[1,k - 1], str(k), color='black') return (fig,ax) #for k in range(self.tx.N): # ax.text(self.tx.position[0,k],self.tx.position[1,k],str(k+1),color='black') # ax.scatter(self.tx.position[0,:],self.tx.position[0,:], #for k in range(self.rx.N): # ax.text(self.rx.position[0,k],self.rx.position[1,k],str(k),color='black') def PL(self, itx): """ plot Path Loss itx """ td = [] tEa = [] tEo = [] for irx in self.dcir[itx].keys(): d = self.delay(itx, irx) * 0.3 cira, ciro = self.loadcir(itx, irx) td.append(d) tEa.append(Ea) tEo.append(Eo) plt.semilogx(td, 10 * np.log10(tEa), 'xr') plt.semilogx(td, 10 * np.log10(tEo), 'xb') plt.show() return td, tEa, tEo def evalcir(self,cutoff=4,algo='new'): """ Parameters ---------- S tx rx wav cutoff """ crxp =-1 ctxp =-1 tcir = {} tx = self.tx.position Ntx = len(tx[0]) rx = self.rx.position Nrx = len(rx[0]) #for kt in range(1,Ntx-1): #print kt+1 kt=0 tcir[kt] = {} t = np.array([self.tx.position[0,kt],self.tx.position[1,kt],self.tx.position[2,kt]]) for kr in range(Nrx): if (np.mod(kr,10)==0): print kr+1 r = np.array([self.rx.position[0,kr],self.rx.position[1,kr],self.rx.position[2,kr]]) ctx = self.L.pt2cy(t) crx = self.L.pt2cy(r) if (ctx<>ctxp)|(crx<>crxp): Si = signature.Signatures(self.L,ctx,crx) ctxp = ctx crxp = crx Si.run4(cutoff=cutoff,algo=algo) r2d = Si.rays(t,r) #r2d.show(S.L) r3d = r2d.to3D(self.L) r3d.locbas(self.L) r3d.fillinter(self.L) Ct = r3d.eval(self.fGHz) sca = Ct.prop2tran(self.tx.A,self.rx.A) cir = sca.applywavB(self.wav.sfg) tcir[kt][kr] = cir return(tcir) def loadcir(self, itx, irx): """ Parameters ---------- itx : Tx index irx : Rx index Returns ------- cir(itx,irx) """ _filecir = self.dcir[itx][irx] + '.mat' ext = str(itx) if len(ext) == 1: ext = '00' + ext if len(ext) == 2: ext = '0' + ext filecir = pyu.getlong(_filecir, pstruc['DIRCIR']+'/Tx' + ext) D = spio.loadmat(filecir) kxa = 'ta' + str(irx) kya = 'cira' + str(irx) kxo = 'to' + str(irx) kyo = 'ciro' + str(irx) cira = bs.TUsignal(D[kxa], D[kya][:, 0]) ciro = bs.TUsignal(D[kxo], D[kyo][:, 0]) return(cira, ciro) def pltcir(self, itx=1, irx=1, mode='linear', noise=False, color='b',format='a',fig=[],ax=[]): """ plot Channel Impulse Response Parameters ---------- itx : Tx index irx : Rx index mode : str {'linear','dB'} noise : boolean color : string default 'b' >>> from pylayers.simul.simulem import * >>> S = Simul() >>> S.load('where2.ini') >>> S.run(1,1) >>> S.pltcir(1,1,mode='linear',noise=False,color='k') """ if fig ==[]: fig = plt.gcf() #if ax==[]: # ax = fig.gca() _filecir = self.dcir[itx][irx] + '.mat' filecir = pyu.getlong(_filecir, pstruc['DIRCIR']+'/Tx' + str('%0.3d' % itx)) D = spio.loadmat(filecir) ax = fig.add_subplot('211') fig,ax=self.show(itx, irx,fig=fig,ax=ax) ax=fig.add_subplot('212') if 'a' in format : kxa = 't' kya = 'cir' ta = D[kxa] Tobs = ta[-1] - ta[0] te = ta[1] - ta[0] if noise: na = bs.Noise(Tobs + te, 1. / te) naf = na.gating(4.493, 0.5) cira = bs.TUsignal(ta, D[kya][:, 0]) if 'o' in format: kxo = 'to' + str(irx) kyo = 'ciro' + str(irx) to = D[kxo] Tobs = to[-1] - to[0] te = to[1] - to[0] if noise: no = bs.Noise(Tobs + te, 1. / te) nof = no.gating(4.493, 0.5) ciro = bs.TUsignal(to, D[kyo][:, 0]) if mode == 'linear': #plt.plot(ta,naf.y,color='k',label='Noise') plt.plot(ta, D[kya], label='Rx ' + str(irx), color=color) plt.xlabel('Time (ns)') '''if noise: naf.plot(col='k') cira.plot(col=color)''' else: '''if noise: naf.plotdB(col='k') cira.plotdB()''' plt.plot(ta, 20 * np.log10(abs(D[kya])), label='Rx ' + str(irx), color=color) plt.xlabel('Time (ns)') # plt.legend() plt.show() #plt.savefig('Tx'+str(itx),format=pdf,dpi=300) def scatter(self, itx, irx, values, cmap=plt.cm.gray, s=30, spl=221, title='', vaxis=((-30, 10, 2, 18)), vmin=0, vmax=1, colbool=False, cblabel='dB'): """ Parameters ---------- itx irx values cmap s spl title vaxis vmin vmax colbool clabel """ fig = plt.gcf() ax = fig.add_subplot(spl) xtx = self.tx.position[itx, 0] ytx = self.tx.position[itx, 1] xrx = self.rx.position[irx, 0] yrx = self.rx.position[irx, 1] self.L.display['title'] = title self.L.showGs(ax) for furk in siradel.siradel_furniture.keys(): fur = siradel.siradel_furniture[furk] if fur.Matname == 'METAL': fur.show(fig, ax) #self.show(furniture=True) plt.axis(vaxis) b1 = ax.scatter(xtx, ytx, s=s, c=values, cmap=cmap, linewidths=0, vmin=vmin, vmax=vmax) ax.scatter(xrx, yrx, s=30, c='b', linewidths=0) if colbool: cb = colorbar(b1) cb.set_label(cblabel, fontsize=14) return(b1) def info(self, itx=[], irx=[]): """ display simulation information Parameters ---------- itx : Tx index irx : Rx index """ print self.filesimul print '------------------------------------------' try: print "Layout Info : \n", self.L.info() except: print "provide a Layout in the simulation : S.L " print ">>> S.layout(filename.str) " print "or " print ">>> S.layout(filename.str2 " print "or " print ">>> S.layout(filename.str,filematini,filematini) " print "default files exists for filematini and fileslabini " return try: print "Tx Info :\n", self.tx.info() except: print "provide a tx in the simulation : S.tx " return try: print "Rx Info :\n", self.rx.info() except: print "provide a rx in the simulation : S.rx " return # print "Tx : ",self.tx.points[itx] print "Rx : ", self.rx.points[itx] print "Delay (ns) :", self.delay(itx, irx) print "Distance (m) :", 0.3 / self.delay(itx, irx) print "" if itx in self.dlch.keys(): print "-----" print "Launching " print "-----" print " ", self.dlch[itx] if irx in self.dtra[itx].keys(): print "-----" print "Tracing " print "-----" print " ", self.dtra[itx][irx] gr = GrRay3D() gr.load(self.dtra[itx][irx], self.L) gr.info() if irx in self.dtud[itx].keys(): print "-----" print "Tud parameters " print "-----" print " ", self.dtud[itx][irx] print " ", self.dtang[itx][irx] print " ", self.drang[itx][irx] gt = GrRay3D.GrRayTud() gt.load(self.dtud[itx][irx], self.dtang[itx][irx], self.drang[itx][irx], self.sl) if irx in self.dtauk[itx].keys(): print self.dtauk[itx][irx] print self.dfield[itx][irx] VC = self.VC(itx, irx) if irx in self.dcir[itx].keys(): print self.dcir[itx][irx] def info2(self): for i, j in enumerate(self.__dict__.keys()): print j, ':', self.__dict__.values()[i] def filtray(self, itx, irx, tau0, tau1, col='b'): """ filter rays Parameters ---------- itx : irx : tau0 : tau1 : col : Display ray and nstr """ gr = GrRay3D() gr.load(self.dtra[itx][irx], self.L) self.L.display['Thin'] = True self.L.display['Node'] = False self.L.display['NodeNum'] = False self.L.display['EdgeNum'] = False plt.axis('scaled') #self.L.show(fig,ax,nodelist,seglist) self.L.showGs() delays = gr.delay() rayset = np.nonzero((delays >= tau0) & (delays <= tau1))[0] fig = plt.gcf() ax = fig.get_axes()[0] gr.show(ax, rayset, col=col, node=False) plt.title('Tx' + str(itx) + '-Rx' + str(irx) + ' : ' + str( tau0) + ' < tau < ' + str(tau1)) def showray(self, itx, irx, iray=np.array([]), fig=[], ax=[]): """ show layout and rays for a radio link Parameters ---------- itx : tx index irx : rx index iray : list of rays to be displayed ndarray """ #if ax==[]: # fig = plt.figure() # ax = fig.add_subplot('111') gr = GrRay3D() gr.load(self.dtra[itx][irx], self.L) if len(iray == 1): ray = gr.ray3d[iray[0]] nstr = ray.nstr[1:-1] uneg = np.nonzero(nstr < 0) upos = np.nonzero((nstr > 0) & (nstr <= self.L.Ne)) uceil = np.nonzero(nstr == self.L.Ne + 1) ufloor = np.nonzero(nstr == self.L.Ne + 2) #seglist = nstr[upos[0]]-1 #nodelist = -nstr[uneg[0]]-1 #seglist2 = S.L.segpt(nodelist) self.L.display['Thin'] = True self.L.display['Node'] = False self.L.display['NodeNum'] = False self.L.display['EdgeNum'] = False #self.L.show(fig,ax) #print ray.nn #print len(ray.nstr) #print ray.nstr #print nodelist #print seglist #print seglist2 #seglist = hstack((seglist,seglist2)) self.L.display['Node'] = False self.L.display['Thin'] = False self.L.display['NodeNum'] = True self.L.display['EdgeNum'] = True plt.axis('scaled') #self.L.show(fig,ax,nodelist,seglist) fig, ax = self.L.showGs(show=False) gr.show(ax, iray, col='b', node=False) if len(iray) == 1: plt.title(str(nstr)) else: plt.title('Tx' + str(itx) + '-Rx' + str(irx) + ' ' + str(min(iray)) + ' ' + str(max(iray))) def show3l(self, itx, irx): """ geomview display of a specific link g = S.show3l(itx,irx) Parameters ---------- itx transmitter index irx receiver index """ filetra = self.dtra[itx][irx] gr = GrRay3D() gr.load(filetra, self.L) gr.show3() return(gr) def _show3(self,rays=[],newfig = False,**kwargs): """ display of the simulation configuration using Mayavi Parameters ---------- rays: Ray3d object : display the rays of the simulation newfig : boolean (default : False) kwargs of Rays.show3() see also -------- pylayers.gis.layout pylayers.antprop.antenna pylayers.antprop.rays """ Atx = self.tx.A Arx = self.rx.A Ttx = self.tx.orientation Trx = self.rx.orientation ptx = self.tx.position prx = self.rx.position self.L._show3(newfig=False,opacity=0.7) Atx._show3(T=Ttx.reshape(3,3),po=ptx, title=False,colorbar=False,newfig=False) Arx._show3(T=Trx.reshape(3,3),po=prx, title=False,colorbar=False,newfig=False) if rays != []: rays._show3(**kwargs) def show3(self,rays=[],**kwargs): """ geomview display of the simulation configuration Parameters ---------- centered : boolean center the scene if True bdis : boolean display local basis """ try: self.tx.save() except: print('tx set is no defined') try: self.rx.save() except: print('rx set is no defined') _filename = self.filesimul.replace('.ini', '.off') filename = pyu.getlong(_filename, pstruc['DIRGEOM']) fo = open(filename, "w") fo.write("LIST\n") try: sttx = "{<" + self.tx.filegeom + "}\n" except: sttx = "\n" try: strx = "{<" + self.rx.filegeom + "}\n" except: strx = "\n" try: stst = "{<" + self.L.filegeom + "}\n" except: stst = "\n" fo.write(sttx) fo.write(strx) fo.write(stst) fo.write("{</usr/share/geomview/geom/xyz.vect}\n") if rays !=[]: kwargs['bdis']=False kwargs['L']=self.L kwargs['centered']=False fo.write("{<" + rays.show3(**kwargs) + "}") fo.close() command = "geomview -nopanel -b 1 1 1 " + filename + " 2>/dev/null &" os.system(command) def run(self, link, cirforce=True,verbose=False,cutoff=4): """ run the simulation for 1 tx and a set of rx Parameters ---------- itx : tx index srx : list of rx index cirforce : boolean Warnings -------- index point start with 1 Example ------- >>> from pylayers.simul.simulem import * >>> itx = 1 >>> srx = [1,2,3] >>> S = Simul() >>> S.load('where2.ini') >>> out = S.run(itx,srx) """ # get file prefix link = DLink(force=True,L=self.L,fGHz=self.fGHz, verbose=False) prefix = self.filesimul.replace('.ini', '') lsig = [] for k,il in enumerate(link): tx = self.tx.points[il[0]] rx = self.rx.points[il[1]] ctx = S.L.pt2cy(tx) crx = S.L.pt2cy(rx) _filecir = prefix +'-cir-'+str(k)+'-'+str(link)+'-'+str((ctx,crx)) D = {} D['Tx'] = tx D['Rx'] = rx link.a = tx link.b = rx ak,tauk = link.eval(verbose=False,diffrction=True) if (ctx,crx) not in lsig: Si = signature.Signatures(S.L,ctx,crx) # # Change the run number depending on # the algorithm used for signature determination # Si.run4(cutoff=cutoff) # keep track and save signature _filesir = prefix + '-sig-'+str((ctx,crx)) fd = open(filesig,'w') pickle.dump(Si,filesig) fd.close() lsig.appeng((ctx,crx)) Si.dump(S.L,(ctx,crx)) r2d = Si.rays(tx,rx) r2d.show(S.L) r3d = r2d.to3D() r3d.locbas(S.L) r3d.fillinter(S.L) Ct = r3d.eval(S.freq) sco = Ct.prop2tran(a='theta',b='phi') sca = Ct.prop2tran(a=S.tx.A,b=S.rx.A) ciro = sco.applywavB(self.wav.sfg) cira = sca.applywavB(self.wav.sfg) D['to'] = ciro.x D['ciro'] = ciro.y D['t'] = cira.x D['cir'] = cira.y filename = pyu.getlong(_filename, cirdir) spio.savemat(filename, D) def delay(self, itx, irx): """ calculate LOS link delay Parameters ---------- itx irx Returns ------- delay : float delay in ns """ tx = self.tx.points[itx] rx = self.rx.points[irx] df = tx - rx dist = np.sqrt(np.dot(df, df)) return(dist / 0.3)
def ishow(self): """ interactive show of trajectories Examples -------- .. plot:: :include-source: >>> from pylayers.mobility.trajectory import * >>> T=Trajectories() >>> T.loadh5() >>> T.ishow() """ fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2, left=0.3) t = np.arange(0, len(self[0].index), self[0].ts) L = Layout(self.Lfilename) fig, ax = L.showG('s', fig=fig, ax=ax) valinit = 0 lines = [] labels = [] colors = "bgrcmykw" for iT, T in enumerate(self): if T.typ == 'ag': lines.extend( ax.plot(T['x'][0:valinit], T['y'][0:valinit], 'o', color=colors[iT], visible=True)) labels.append(T.name + ':' + T.ID) else: lines.extend( ax.plot(T['x'][0], T['y'][0], '^', ms=12, color=colors[iT], visible=True)) labels.append(T.name + ':' + T.ID) t = self[0].time() # init boolean value for visible in checkbutton blabels = [True] * len(labels) ######## # slider ######## slider_ax = plt.axes([0.1, 0.1, 0.8, 0.02]) slider = Slider(slider_ax, "time", self[0].tmin, self[0].tmax, valinit=valinit, color='#AAAAAA') def update(val): if val >= 1: pval = np.where(val > t)[0] ax.set_title(str( self[0].index[pval[-1]].time())[:11].ljust(12), loc='left') for iT, T in enumerate(self): if T.typ == 'ag': lines[iT].set_xdata(T['x'][pval]) lines[iT].set_ydata(T['y'][pval]) fig.canvas.draw() slider.on_changed(update) ######## # choose ######## rax = plt.axes([0.02, 0.5, 0.3, 0.2], aspect='equal') # check (ax.object, name of the object , bool value for the obsject) check = CheckButtons(rax, labels, tuple(blabels)) def func(label): i = labels.index(label) lines[i].set_visible(not lines[i].get_visible()) fig.canvas.draw() check.on_clicked(func) fig.canvas.draw() plt.show(fig)
def load(self, _filesimul): """ load a simulation configuration file each transmiter simulation results in the creation of an .ini file with the following sections related to the results obtained for different receivers Parameters ---------- _filesimul : file in the simul directory of the Project """ self.filesimul = _filesimul filesimul = pyu.getlong(self.filesimul, "ini") self.config.read(filesimul) sections = self.config.sections() try: _filetx = self.config.get("files", "tx") except: raise NameError('Error in section tx from '+ _filesimul) try: _filerx = self.config.get("files", "rx") except: raise NameError('Error in section rx from '+ _filesimul) try: _fileanttx = self.config.get("files", "txant") except: raise NameError('Error in section txant from '+ _filesimul) try: _fileantrx = self.config.get("files", "rxant") except: raise NameError('Error in section rxant from '+ _filesimul) try: self.tx = RadioNode(name = '', typ = 'tx', _fileini = _filetx, _fileant = _fileanttx, _filestr = self.filestr) self.rx = RadioNode(name = '', typ = 'rx', _fileini = _filerx, _fileant = _fileantrx, _filestr = self.filestr) except: raise NameError('Error during Radionode load') # # Load Layout # try: self.L = Layout(self.filestr) except: raise NameError('Layout load error') # # Frequency base # if "frequency" in sections: try: self.fGHz = np.linspace(float(self.config.getfloat("frequency", "fghzmin")), float(self.config.getfloat("frequency", "fghzmax")), int(self.config.getint("frequency", "nf")), endpoint=True) except: raise NameError('Error in section frequency from '+ _filesimul) # update .freq file in tud directory filefreq = pyu.getlong(self.filefreq, pstruc['DIRTUD']) fd = open(filefreq, "w") chaine = self.config.get("frequency", "fghzmin") + ' ' + \ self.config.get("frequency", "fghzmax") + ' ' + \ self.config.get("frequency", "nf") fd.write(chaine) fd.close # # Simulation Progress # self.output = {} if "output" in sections: for itx in self.config.options("output"): _filename = self.config.get("output", itx) self.dout[int(itx)] = _filename filename = pyu.getlong(_filename, "output") output = ConfigParser.ConfigParser() output.read(filename) secout = output.sections() self.dtra[int(itx)] = {} self.dtud[int(itx)] = {} self.dtang[int(itx)] = {} self.drang[int(itx)] = {} self.dtauk[int(itx)] = {} self.dfield[int(itx)] = {} self.dcir[int(itx)] = {} if "launch" in secout: self.progress = 1 keys_launch = output.options("launch") for kl in keys_launch: self.dlch[int(kl)] = output.get("launch", kl) if "trace" in secout: self.progress = 2 keys_tra = output.options("trace") for kt in keys_tra: self.dtra[int(itx)][int(kt)] = output.get("trace", kt) if "tang" in secout: self.progress = 3 keys_tang = output.options("tang") for kt in keys_tang: self.dtang[int(itx)][int(kt)] = output.get("tang", kt) self.drang[int(itx)][int(kt)] = output.get("rang", kt) self.dtud[int(itx)][int(kt)] = output.get("tud", kt) if "field" in secout: self.progress = 4 keys_field = output.options("field") for kt in keys_field: self.dfield[int(itx)][int(kt)] = output.get( "field", kt) self.dtauk[int(itx)][int(kt)] = output.get("tauk", kt) if "cir" in secout: self.progress = 5 keys_cir = output.options("cir") for kt in keys_cir: self.dcir[int(itx)][int(kt)] = output.get("cir", kt) self.output[int(itx)] = output # # Waveform section # self.wav = wvf.Waveform() self.wav.read(self.config)
class Simul(SimulationRT): # Sympy 2 #class Simul(sympy.RealtimeEnvironment): """ Attributes ---------- config : config parser instance sim_opt : dictionary of configuration option for simulation ag_opt : dictionary of configuration option for agent lay_opt : dictionary of configuration option for layout meca_opt : dictionary of configuration option for mecanic net_opt : dictionary of configuration option for network loc_opt : dictionary of configuration option for localization save_opt : dictionary of configuration option for save sql_opt : dictionary of configuration option for sql Notes ------ All the prvious dictionnary are obtained from the chosen simulnet.ini file in the project directory """ def __init__(self): SimulationRT.__init__(self) #Sympy 2 #sympy.RealtimeEnvironment.__init__(self) #simpy 3 self.initialize() self.config = ConfigParser.ConfigParser() filename = pyu.getlong('simulnet.ini','ini') self.config.read(filename) self.sim_opt = dict(self.config.items('Simulation')) self.lay_opt = dict(self.config.items('Layout')) self.meca_opt = dict(self.config.items('Mechanics')) self.net_opt = dict(self.config.items('Network')) self.loc_opt = dict(self.config.items('Localization')) self.save_opt = dict(self.config.items('Save')) self.sql_opt = dict(self.config.items('Mysql')) self.verbose = str2bool(self.sim_opt['verbose']) if str2bool(self.net_opt['ipython_nb_show']): self.verbose = False self.roomlist=[] self.create() def create_layout(self): """ Create Layout in Simpy the_world thanks to Tk backend """ self.the_world = world(width = float(self.lay_opt['the_world_width']), height = float(self.lay_opt['the_world_height']), scale=float(self.lay_opt['the_world_scale'])) # tk = self.the_world.tk # canvas, x_, y_ = tk.canvas, tk.x_, tk.y_ # canvas.create_rectangle(x_(-1), y_(-1), x_(100), y_(100), fill='white') _filename = self.lay_opt['filename'] #sl=Slab.SlabDB(self.lay_opt['slab'],self.lay_opt['slabmat']) #G1 = Graph.Graph(sl=sl,filename=_filename) self.L = Layout(_filename) #if _filename.split('.')[1] == 'str': # self.L.loadstr(_filename) #elif _filename.split('.')[1] == 'str2': # self.L.loadstr2(_filename) #elif _filename.split('.')[1] == 'ini': # self.L.loadini(_filename) try: self.L.dumpr() print 'Layout graphs are loaded from ',basename,'/struc/ini' except: #self.L.sl = sl #self.L.loadGr(G1) print 'This is the first time the layout file is used\ Layout graphs are curently being built, it may take few minutes.' self.L.build() self.L.dumpw() x_offset = 0 # float(self.lay_opt['x_offset']) y_offset = 0 # float(self.lay_opt['y_offset']) for ks in self.L.Gs.pos.keys(): self.L.Gs.pos[ks] = (self.L.Gs.pos[ks][0] + x_offset, self.L.Gs.pos[ks][1] + y_offset) for ks in self.L.Gr.pos.keys(): self.L.Gr.pos[ks] = (self.L.Gr.pos[ks][0] + x_offset, self.L.Gr.pos[ks][1] + y_offset) for ks in self.L.Gw.pos.keys(): self.L.Gw.pos[ks] = (self.L.Gw.pos[ks][0] + x_offset, self.L.Gw.pos[ks][1] + y_offset) # # Create Layout # walls = self.L.thwall(0, 0) for wall in walls: points = [] # for point in wall: # points.append(x_(point[0])) # points.append(y_(point[1])) # canvas.create_polygon(points, fill='maroon', outline='black') for ii in range(0, len(wall) - 1): self.the_world.add_wall(wall[ii], wall[ii + 1]) def create_agent(self): """ create simulation's Agents ..todo:: change lAg list to a dictionnary ( modification in show.py too) """ self.lAg = [] agents=[] Cf = ConfigParser.ConfigParser() Cf.read(pyu.getlong('agent.ini','ini')) agents=eval(dict(Cf.items('used_agent'))['list']) for i, ag in enumerate(agents): ag_opt = dict(Cf.items(ag)) self.lAg.append(Agent( ID=ag_opt['id'], name=ag_opt['name'], type=ag_opt['type'], pos=np.array(eval(ag_opt['pos'])), roomId=int(ag_opt['roomid']), froom=eval(ag_opt['froom']), meca_updt=float(self.meca_opt['mecanic_update_time']), wait=float(ag_opt['wait']), cdest=eval(self.meca_opt['choose_destination']), loc=str2bool(self.loc_opt['localization']), loc_updt=float(self.loc_opt['localization_update_time']), loc_method=eval(self.loc_opt['method']), Layout=self.L, net=self.net, epwr=dict([(eval((ag_opt['rat']))[ep],eval((ag_opt['epwr']))[ep]) for ep in range(len(eval((ag_opt['rat']))))]), sens=dict([(eval((ag_opt['rat']))[ep],eval((ag_opt['sensitivity']))[ep]) for ep in range(len(eval((ag_opt['rat']))))]), world=self.the_world, RAT=eval(ag_opt['rat']), save=eval(self.save_opt['save']), gcom=self.gcom, comm_mode=eval(self.net_opt['communication_mode']), sim=self)) # if self.lAg[i].type == 'ag': self.activate(self.lAg[i].meca, self.lAg[i].meca.move(), 0.0) def create_EMS(self): """ Electromagnetic Solver object """ self.EMS = EMSolver(L=self.L) def create_network(self): """ create the whole network """ self.net = Network(EMS=self.EMS) self.gcom=Gcom(net=self.net,sim=self) self.create_agent() # create network self.net.create() # create All Personnal networks for n in self.net.nodes(): self.net.node[n]['PN'].get_RAT() self.net.node[n]['PN'].get_SubNet() self.gcom.create() # create Process Network self.Pnet = PNetwork(net=self.net, net_updt_time=float(self.net_opt['network_update_time']), L=self.L, sim=self, show_sg=str2bool(self.net_opt['show_sg']), disp_inf=str2bool(self.net_opt['dispinfo']), save=eval(self.save_opt['save'])) self.activate(self.Pnet, self.Pnet.run(), 0.0) def create_visual(self): """ Create visual Tk process """ self.visu = Updater( interval=float(self.sim_opt['show_interval']), sim=self) self.activate(self.visu, self.visu.execute(), 0.0) def create(self): """ Create the simulation This method is called at the end of __init__ """ # this is just to redump the database at each simulation if 'mysql' in self.save_opt['save']: if str2bool(self.sql_opt['dumpdb']): os.popen('mysql -u ' + self.sql_opt['user'] + ' -p ' + self.sql_opt['dbname'] +\ '< /private/staff/t/ot/niamiot/svn2/devel/simulator/pyray/SimWHERE2.sql' ) ## TODO supprimer la ref en dur if 'txt' in self.save_opt['save']: pyu.writeDetails(self) if os.path.isfile(basename+'/output/Nodes.txt'): print 'would you like to erase previous txt files ?' A=raw_input() if A == 'y': for f in os.listdir(basename+'/output/'): try: fi,ext=f.split('.') if ext == 'txt': os.remove(basename+'/output/'+f) except: pass self.create_layout() self.create_EMS() self.create_network() if str2bool(self.sim_opt['showtk']): self.create_visual() self.create_show() if str2bool(self.save_opt['savep']): self.save=Save(L=self.L,net=self.net,sim=self) self.activate(self.save,self.save.run(),0.0) # if str2bool(self.save_opt['savep']): # self.save=Save(net=self.net, # L= self.L, # sim = self) # self.activate(self.save, self.save.run(), 0.0) def create_show(self): """ """ plt.ion() fig_net = 'network' fig_table = 'table' if str2bool(self.net_opt['show']): if str2bool(self.net_opt['ipython_nb_show']): notebook=True else: notebook =False self.sh = ShowNet(net=self.net, L=self.L,sim=self,fname=fig_net,notebook=notebook) self.activate(self.sh,self.sh.run(),1.0) if str2bool(self.net_opt['show_table']): self.sht = ShowTable(net=self.net,lAg=self.lAg,sim=self,fname=fig_table) self.activate(self.sht,self.sht.run(),1.0) def runsimul(self): """ Run simulation """ self.simulate(until=float(self.sim_opt['duration']), real_time=True, rel_speed=float(self.sim_opt['speedratio'])) # self.simulate(until=float(self.sim_opt['duration'])) if self.save_opt['savep']: print 'Processing save results, please wait' self.save.mat_export()
from pylayers.gis.layout import Layout import matplotlib.pyplot as plt import doctest #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout('DLR.ini') try: L.dumpr() except: L.build() L.dumpw() #L.editor() fig = plt.gcf() #ax1 = fig.add_subplot(221) ax1 = fig.add_subplot(321) L.display['thin']=True fig,ax1 = L.showGs(fig=fig,ax=ax1) #L.display['edlabel']=True #L.display['edlblsize']=50 # display selected segments L.display['thin']=True L.showG(fig=fig,ax=ax1,graph='t') fig = plt.gcf() ax1 = plt.gca() fig,ax1 = L.showGs(fig=fig,ax=ax1,edlist=[125],width=4) ax11 = fig.add_subplot(322) L.showG(fig=fig,ax=ax11,graph='') #plt.savefig('graphGs.png')
class Coverage(PyLayers): """ Handle Layout Coverage Methods ------- creategrid() create a uniform grid for evaluating losses cover() run the coverage calculation showPower() display the map of received power showLoss() display the map of losses Attributes ---------- All attributes are read from fileini ino the ini directory of the current project _fileini default coverage.ini L : a Layout nx : number of point on x ny : number of point on y tx : transmitter position txpe : transmitter power emmission level show : boolean for automatic display power map na : number of access point """ def __init__(self, _fileini='coverage.ini'): """ object constructor Parameters ---------- _fileini : string name of the configuration file Notes ----- Coverage is described in an ini file. Default file is coverage.ini and is placed in the ini directory of the current project. """ self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini, pstruc['DIRSIMUL'])) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.apopt = dict(self.config.items('ap')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) # get the Layout filename = self.layoutopt['filename'] if filename.endswith('ini'): self.typ = 'indoor' self.L = Layout(filename) # get the receiving grid self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) self.mode = self.gridopt['mode'] assert self.mode in ['file', 'full', 'zone'], "Error reading grid mode " self.boundary = eval(self.gridopt['boundary']) self.filespa = self.gridopt['file'] # # create grid # self.creategrid(mode=self.mode, boundary=self.boundary, _fileini=self.filespa) self.dap = {} for k in self.apopt: kwargs = eval(self.apopt[k]) ap = std.AP(**kwargs) self.dap[eval(k)] = ap try: self.L.Gt.nodes() except: pass try: self.L.dumpr() except: self.L.build() self.L.dumpw() else: self.typ = 'outdoor' self.E = ez.Ezone(filename) self.E.loadh5() self.E.rebase() # The frequency is fixed from the AP nature self.fGHz = np.array([]) #self.fGHz = eval(self.txopt['fghz']) #self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) #self.ptdbm = eval(self.txopt['ptdbm']) #self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section #self.rxsens = eval(self.rxopt['sensitivity']) self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) # show section self.bshow = str2bool(self.showopt['show']) def __repr__(self): st = '' if self.typ == 'indoor': st = st + 'Layout file : ' + self.L._filename + '\n\n' st = st + '-----list of Access Points ------' + '\n' for k in self.dap: st = st + self.dap[k].__repr__() + '\n' st = st + '-----Rx------' + '\n' st = st + 'temperature (K) : ' + str(self.temperaturek) + '\n' st = st + 'noisefactor (dB) : ' + str(self.noisefactordb) + '\n\n' st = st + '--- Grid ----' + '\n' st = st + 'mode : ' + str(self.mode) + '\n' if self.mode <> 'file': st = st + 'nx : ' + str(self.nx) + '\n' st = st + 'ny : ' + str(self.ny) + '\n' if self.mode == 'zone': st = st + 'boundary (xmin,ymin,xmax,ymax) : ' + str( self.boundary) + '\n\n' if self.mode == 'file': st = st + ' filename : ' + self.filespa + '\n' return (st) def creategrid(self, mode='full', boundary=[], _fileini=''): """ create a grid Parameters ---------- full : boolean default (True) use all the layout area boundary : (xmin,ymin,xmax,ymax) if full is False the boundary argument is used """ if mode == "file": self.RN = RadioNode(name='', typ='rx', _fileini=_fileini, _fileant='def.vsh3') self.grid = self.RN.position[0:2, :].T else: if mode == "full": mi = np.min(self.L.Gs.pos.values(), axis=0) + 0.01 ma = np.max(self.L.Gs.pos.values(), axis=0) - 0.01 if mode == "zone": assert boundary <> [] mi = np.array([boundary[0], boundary[1]]) ma = np.array([boundary[2], boundary[3]]) x = np.linspace(mi[0], ma[0], self.nx) y = np.linspace(mi[1], ma[1], self.ny) self.grid = np.array((list(np.broadcast(*np.ix_(x, y))))) self.ng = self.grid.shape[0] def where1(self): """ Unfinished : Not sure this is the right place (too specific) """ M1 = UWBMeasure(1) self.dap = {} self.dap[1] = {} self.dap[2] = {} self.dap[3] = {} self.dap[4] = {} self.dap[1]['p'] = M1.rx[1, 0:2] self.dap[2]['p'] = M1.rx[1, 0:2] self.dap[3]['p'] = M1.rx[1, 0:2] self.dap[4]['p'] = M1.rx[1, 0:2] for k in range(300): try: M = UWBMeasure(k) tx = M.tx self.grid = np.vstack((self.grid, tx[0:2])) D = M.rx - tx[np.newaxis, :] D2 = D * D dist = np.sqrt(np.sum(D2, axis=1))[1:] Emax = M.Emax() Etot = M.Etot()[0] try: td1 = np.hstack((td1, dist[0])) td2 = np.hstack((td2, dist[1])) td3 = np.hstack((td3, dist[2])) td4 = np.hstack((td4, dist[3])) te1 = np.hstack((te1, Emax[0])) te2 = np.hstack((te2, Emax[1])) te3 = np.hstack((te3, Emax[2])) te4 = np.hstack((te4, Emax[3])) tt1 = np.hstack((tt1, Etot[0])) tt2 = np.hstack((tt2, Etot[1])) tt3 = np.hstack((tt3, Etot[2])) tt4 = np.hstack((tt4, Etot[3])) #tdist = np.hstack((tdist,dist)) #te = np.hstack((te,Emax)) except: td1 = np.array(dist[0]) td2 = np.array(dist[1]) td3 = np.array(dist[2]) td4 = np.array(dist[3]) te1 = np.array(Emax[0]) te2 = np.array(Emax[1]) te3 = np.array(Emax[2]) te4 = np.array(Emax[3]) tt1 = np.array(Etot[0]) tt2 = np.array(Etot[1]) tt3 = np.array(Etot[2]) tt4 = np.array(Etot[3]) except: pass def cover(self, sinr=True, snr=True, best=True): """ run the coverage calculation Parameters ---------- sinr : boolean snr : boolean best : boolean Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> f,a=C.show(typ='sinr',figsize=(10,8)) >>> plt.show() Notes ----- self.fGHz is an array, it means that Coverage is calculated at once for a whole set of frequencies. In practice, it would be the center frequency of a given standard channel. This function is calling `loss.Losst` which calculates Losses along a straight path. In a future implementation we will abstract the EM solver in order to make use of other calculation approaches as a full or partial Ray Tracing. The following members variables are evaluated : + freespace Loss @ fGHz PL() PathLoss (shoud be rename FS as free space) $ + prdbmo : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{odB}` + prdbmp : Received power in dBm .. math:`P_{rdBm} =P_{tdBm} - L_{pdB}` + snro : SNR polar o (H) + snrp : SNR polar p (H) See Also -------- pylayers.antprop.loss.Losst pylayers.antprop.loss.PL """ # # select active AP # lactiveAP = [] try: del self.aap del self.ptdbm except: pass self.kB = 1.3806503e-23 # Boltzmann constant for iap in self.dap: if self.dap[iap]['on']: lactiveAP.append(iap) fGHz = self.dap[iap].s.fcghz # The frequency band is set here self.fGHz = np.unique(np.hstack((self.fGHz, fGHz))) apchan = self.dap[iap]['chan'] try: self.aap = np.vstack((self.aap, self.dap[iap]['p'][0:2])) self.ptdbm = np.vstack( (self.ptdbm, self.dap[iap]['PtdBm'])) self.bmhz = np.vstack( (self.bmhz, self.dap[iap].s.chan[apchan[0]]['BMHz'])) except: self.aap = self.dap[iap]['p'][0:2] self.ptdbm = np.array(self.dap[iap]['PtdBm']) self.bmhz = np.array( self.dap[iap].s.chan[apchan[0]]['BMHz']) PnW = np.array((10**(self.noisefactordb / 10.)) * self.kB * self.temperaturek * self.bmhz * 1e6) # Evaluate Noise Power (in dBm) self.pndbm = np.array(10 * np.log10(PnW) + 30) #lchan = map(lambda x: self.dap[x]['chan'],lap) #apchan = zip(self.dap.keys(),lchan) #self.bmhz = np.array(map(lambda x: self.dap[x[0]].s.chan[x[1][0]]['BMHz']*len(x[1]),apchan)) self.ptdbm = self.ptdbm.T self.pndbm = self.pndbm.T # creating all links p = product(range(self.ng), lactiveAP) # # pa : access point # pg : grid point # # 1 x na for k in p: pg = self.grid[k[0], :] pa = np.array(self.dap[k[1]]['p'][0:2]) try: self.pa = np.vstack((self.pa, pa)) except: self.pa = pa try: self.pg = np.vstack((self.pg, pg)) except: self.pg = pg self.pa = self.pa.T shpa = self.pa.shape shpg = self.pg.shape self.pa = np.vstack((self.pa, np.ones(shpa[1]))) self.pg = self.pg.T self.pg = np.vstack((self.pg, np.ones(shpg[0]))) self.nf = len(self.fGHz) # retrieving dimensions along the 3 axis na = len(lactiveAP) self.na = na ng = self.ng nf = self.nf #Lwo,Lwp,Edo,Edp = loss.Losst(self.L,self.fGHz,self.pa,self.pg,dB=False) Lwo, Lwp, Edo, Edp = loss.Losst(self.L, self.fGHz, self.pa, self.pg, dB=False) self.Lwo = Lwo.reshape(nf, ng, na) self.Edo = Edo.reshape(nf, ng, na) self.Lwp = Lwp.reshape(nf, ng, na) self.Edp = Edp.reshape(nf, ng, na) freespace = loss.PL(self.fGHz, self.pa, self.pg, dB=False) self.freespace = freespace.reshape(nf, ng, na) # transmitting power # f x g x a # CmW : Received Power coverage in mW self.CmWo = 10**(self.ptdbm[np.newaxis, ...] / 10.) * self.Lwo * self.freespace self.CmWp = 10**(self.ptdbm[np.newaxis, ...] / 10.) * self.Lwp * self.freespace if snr: self.evsnr() if sinr: self.evsinr() if best: self.evbestsv() def evsnr(self): """ calculates signal to noise ratio """ NmW = 10**(self.pndbm / 10.)[np.newaxis, :] self.snro = self.CmWo / NmW self.snrp = self.CmWp / NmW def evsinr(self): """ calculates sinr """ # na : number of access point na = self.na # U : 1 x 1 x na x na U = (np.ones((na, na)) - np.eye(na))[np.newaxis, np.newaxis, :, :] # CmWo : received power in mW orthogonal polarization # CmWp : received power in mW parallel polarization ImWo = np.einsum('ijkl,ijl->ijk', U, self.CmWo) ImWp = np.einsum('ijkl,ijl->ijk', U, self.CmWp) NmW = 10**(self.pndbm / 10.)[np.newaxis, :] self.sinro = self.CmWo / (ImWo + NmW) self.sinrp = self.CmWp / (ImWp + NmW) def evbestsv(self): """ determine the best server map Notes ----- C.bestsv """ na = self.na ng = self.ng nf = self.nf # find best server regions Vo = self.CmWo Vp = self.CmWp self.bestsvo = np.empty(nf * ng * na).reshape(nf, ng, na) self.bestsvp = np.empty(nf * ng * na).reshape(nf, ng, na) for kf in range(nf): MaxVo = np.max(Vo[kf, :, :], axis=1) MaxVp = np.max(Vp[kf, :, :], axis=1) for ka in range(na): uo = np.where(Vo[kf, :, ka] == MaxVo) up = np.where(Vp[kf, :, ka] == MaxVp) self.bestsvo[kf, uo, ka] = ka + 1 self.bestsvp[kf, up, ka] = ka + 1 # def showEd(self,polar='o',**kwargs): # """ shows a map of direct path excess delay # # Parameters # ---------- # # polar : string # 'o' | 'p' # # Examples # -------- # # .. plot:: # :include-source: # # >> from pylayers.antprop.coverage import * # >> C = Coverage() # >> C.cover() # >> C.showEd(polar='o') # # """ # # if not kwargs.has_key('alphacy'): # kwargs['alphacy']=0.0 # if not kwargs.has_key('colorcy'): # kwargs['colorcy']='w' # if not kwargs.has_key('nodes'): # kwargs['nodes']=False # # fig,ax = self.L.showG('s',**kwargs) # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # cdict = { # 'red' : ((0., 0.5, 0.5), (1., 1., 1.)), # 'green': ((0., 0.5, 0.5), (1., 1., 1.)), # 'blue' : ((0., 0.5, 0.5), (1., 1., 1.)) # } # #generate the colormap with 1024 interpolated values # my_cmap = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024) # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # # # # if polar=='o': # mcEdof = np.ma.masked_where(prdbm < self.rxsens,self.Edo) # # cov=ax.imshow(mcEdof.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'jet', # origin='lower') # # # # # cov=ax.imshow(self.Edo.reshape((self.nx,self.ny)).T, # # extent=(l,r,b,t), # # origin='lower') # titre = "Map of LOS excess delay, polar orthogonal" # # if polar=='p': # mcEdpf = np.ma.masked_where(prdbm < self.rxsens,self.Edp) # # cov=ax.imshow(mcEdpf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'jet', # origin='lower') # # # cov=ax.imshow(self.Edp.reshape((self.nx,self.ny)).T, # # extent=(l,r,b,t), # # origin='lower') # titre = "Map of LOS excess delay, polar parallel" # # ax.scatter(self.tx[0],self.tx[1],linewidth=0) # ax.set_title(titre) # # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # clb = fig.colorbar(cov,cax) # clb.set_label('excess delay (ns)') # # if self.show: # plt.show() # return fig,ax # # def showPower(self,rxsens=True,nfl=True,polar='o',**kwargs): # """ show the map of received power # # Parameters # ---------- # # rxsens : bool # clip the map with rx sensitivity set in self.rxsens # nfl : bool # clip the map with noise floor set in self.pndbm # polar : string # 'o'|'p' # # Examples # -------- # # .. plot:: # :include-source: # # > from pylayers.antprop.coverage import * # > C = Coverage() # > C.cover() # > C.showPower() # # """ # # if not kwargs.has_key('alphacy'): # kwargs['alphacy']=0.0 # if not kwargs.has_key('colorcy'): # kwargs['colorcy']='w' # if not kwargs.has_key('nodes'): # kwargs['nodes']=False # fig,ax = self.L.showG('s',**kwargs) # # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # ## tCM = plt.cm.get_cmap('jet') ## tCM._init() ## alphas = np.abs(np.linspace(.0,1.0, tCM.N)) ## tCM._lut[:-3,-1] = alphas # # title='Map of received power - Pt = ' + str(self.ptdbm) + ' dBm'+str(' fGHz =') + str(self.fGHz) + ' polar = '+polar # # cdict = { # 'red' : ((0., 0.5, 0.5), (1., 1., 1.)), # 'green': ((0., 0.5, 0.5), (1., 1., 1.)), # 'blue' : ((0., 0.5, 0.5), (1., 1., 1.)) # } # # if not kwargs.has_key('cmap'): # # generate the colormap with 1024 interpolated values # cmap = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024) # else: # cmap = kwargs['cmap'] # #my_cmap = cm.copper # # # if rxsens : # # ## values between the rx sensitivity and noise floor # mcPrf = np.ma.masked_where((prdbm > self.rxsens) # & (prdbm < self.pndbm),prdbm) # # mcPrf = np.ma.masked_where((prdbm > self.rxsens) ,prdbm) # # cov1 = ax.imshow(mcPrf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = cm.copper, # vmin=self.rxsens,origin='lower') # # ### values above the sensitivity # mcPrs = np.ma.masked_where(prdbm < self.rxsens,prdbm) # cov = ax.imshow(mcPrs.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t), # cmap = cmap, # vmin=self.rxsens,origin='lower') # title=title + '\n black : Pr (dBm) < %.2f' % self.rxsens + ' dBm' # # else : # cov=ax.imshow(prdbm.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t), # cmap = cmap, # vmin=self.pndbm,origin='lower') # # if nfl: # ### values under the noise floor # ### we first clip the value below the noise floor # cl = np.nonzero(prdbm<=self.pndbm) # cPr = prdbm # cPr[cl] = self.pndbm # mcPruf = np.ma.masked_where(cPr > self.pndbm,cPr) # cov2 = ax.imshow(mcPruf.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'binary', # vmax=self.pndbm,origin='lower') # title=title + '\n white : Pr (dBm) < %.2f' % self.pndbm + ' dBm' # # # ax.scatter(self.tx[0],self.tx[1],s=10,c='k',linewidth=0) # # ax.set_title(title) # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # clb = fig.colorbar(cov,cax) # clb.set_label('Power (dBm)') # # if self.show: # plt.show() # # return fig,ax # # # def showTransistionRegion(self,polar='o'): # """ # # Notes # ----- # # See : "Analyzing the Transitional Region in Low Power Wireless Links" # Marco Zuniga and Bhaskar Krishnamachari # # Examples # -------- # # .. plot:: # :include-source: # # > from pylayers.antprop.coverage import * # > C = Coverage() # > C.cover() # > C.showTransitionRegion() # # """ # # frameLength = self.framelengthbytes # # PndBm = self.pndbm # gammaU = 10*np.log10(-1.28*np.log(2*(1-0.9**(1./(8*frameLength))))) # gammaL = 10*np.log10(-1.28*np.log(2*(1-0.1**(1./(8*frameLength))))) # # PrU = PndBm + gammaU # PrL = PndBm + gammaL # # fig,ax = self.L.showGs() # # l = self.grid[0,0] # r = self.grid[-1,0] # b = self.grid[0,1] # t = self.grid[-1,-1] # # if polar=='o': # prdbm=self.prdbmo # if polar=='p': # prdbm=self.prdbmp # # zones = np.zeros(np.shape(prdbm)) # #pdb.set_trace() # # uconnected = np.nonzero(prdbm>PrU) # utransition = np.nonzero((prdbm < PrU)&(prdbm > PrL)) # udisconnected = np.nonzero(prdbm < PrL) # # zones[uconnected] = 1 # zones[utransition] = (prdbm[utransition]-PrL)/(PrU-PrL) # cov = ax.imshow(zones.reshape((self.nx,self.ny)).T, # extent=(l,r,b,t),cmap = 'BuGn',origin='lower') # # title='PDR region' # ax.scatter(self.tx[0],self.tx[1],linewidth=0) # # ax.set_title(title) # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # fig.colorbar(cov,cax) # if self.show: # plt.show() # def plot(self, **kwargs): """ """ defaults = { 'typ': 'pr', 'grid': False, 'f': 0, 'a': 0, 'db': True, 'label': '', 'pol': 'p', 'col': 'b' } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] if 'fig' in kwargs: fig = kwargs['fig'] else: fig = plt.figure() if 'ax' in kwargs: ax = kwargs['ax'] else: ax = fig.add_subplot(111) if kwargs['typ'] == 'pr': if kwargs['a'] <> -1: if kwargs['pol'] == 'p': U = self.CmWp[kwargs['f'], :, kwargs['a']] if kwargs['pol'] == 'o': U = self.CmWo[kwargs['f'], :, kwargs['a']] else: if kwargs['pol'] == 'p': U = self.CmWp[kwargs['f'], :, :].reshape(self.na * self.ng) else: U = self.CmWo[kwargs['f'], :, :].reshape(self.na * self.ng) if kwargs['db']: U = 10 * np.log10(U) D = np.sqrt(np.sum((self.pa - self.pg) * (self.pa - self.pg), axis=0)) if kwargs['a'] <> -1: D = D.reshape(self.ng, self.na) ax.semilogx(D[:, kwargs['a']], U, '.', color=kwargs['col'], label=kwargs['label']) else: ax.semilogx(D, U, '.', color=kwargs['col'], label=kwargs['label']) return fig, ax def show(self, **kwargs): """ show coverage Parameters ---------- typ : string 'pr' | 'sinr' | 'capacity' | 'loss' | 'best' | 'egd' grid : boolean polar : string 'o' | 'p' best : boolean draw best server contour if True f : int frequency index a : int access point index (-1 all access point) Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> f,a = C.show(typ='pr',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='best',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='loss',figsize=(10,8)) >>> plt.show() >>> f,a = C.show(typ='sinr',figsize=(10,8)) >>> plt.show() See Also -------- pylayers.gis.layout.Layout.showG """ defaults = { 'typ': 'pr', 'grid': False, 'polar': 'p', 'f': 0, 'a': -1, 'db': True, 'cmap': cm.jet, 'best': True } title = self.dap[1].s.name + ' : ' for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] polar = kwargs['polar'] assert polar in ['p', 'o'], "polar wrongly defined in show coverage" if 'fig' in kwargs: if 'ax' in kwargs: fig, ax = self.L.showG('s', fig=kwargs['fig'], ax=kwargs['ax']) else: fig, ax = self.L.showG('s', fig=kwargs['fig']) else: if 'figsize' in kwargs: fig, ax = self.L.showG('s', figsize=kwargs['figsize']) else: fig, ax = self.L.showG('s') # plot the grid if kwargs['grid']: for k in self.dap: p = self.dap[k].p ax.plot(p[0], p[1], 'or') f = kwargs['f'] a = kwargs['a'] typ = kwargs['typ'] assert typ in ['best', 'egd', 'sinr', 'snr', 'capacity', 'pr', 'loss'], "typ unknown in show coverage" best = kwargs['best'] dB = kwargs['db'] # setting the grid l = self.grid[0, 0] r = self.grid[-1, 0] b = self.grid[0, 1] t = self.grid[-1, -1] if typ == 'best': title = title + 'Best server' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar for ka in range(self.na): if polar == 'p': bestsv = self.bestsvp[f, :, ka] if polar == 'o': bestsv = self.bestsvo[f, :, ka] m = np.ma.masked_where(bestsv == 0, bestsv) if self.mode <> 'file': W = m.reshape(self.nx, self.ny).T ax.imshow(W, extent=(l, r, b, t), origin='lower', vmin=1, vmax=self.na + 1) else: ax.scatter(self.grid[:, 0], self.grid[:, 1], c=m, s=20, linewidth=0) ax.set_title(title) else: if typ == 'egd': title = title + 'excess group delay : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar V = self.Ed dB = False legcb = 'Delay (ns)' if typ == 'sinr': title = title + 'SINR : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar == 'o': V = self.sinro if polar == 'p': V = self.sinrp if typ == 'snr': title = title + 'SNR : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar == 'o': V = self.snro if polar == 'p': V = self.snrp if typ == 'capacity': title = title + 'Capacity : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar legcb = 'Mbit/s' if polar == 'o': V = self.bmhz.T[np.newaxis, :] * np.log( 1 + self.sinro) / np.log(2) if polar == 'p': V = self.bmhz.T[np.newaxis, :] * np.log( 1 + self.sinrp) / np.log(2) if typ == 'pr': title = title + 'Pr : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dBm' else: lgdcb = 'mW' if polar == 'o': V = self.CmWo if polar == 'p': V = self.CmWp if typ == 'loss': title = title + 'Loss : ' + ' fc = ' + str( self.fGHz[f]) + ' GHz' + ' polar : ' + polar if dB: legcb = 'dB' else: legcb = 'Linear scale' if polar == 'o': V = self.Lwo * self.freespace if polar == 'p': V = self.Lwp * self.freespace if a == -1: V = np.max(V[f, :, :], axis=1) else: V = V[f, :, a] # reshaping the data on the grid if self.mode != 'file': U = V.reshape((self.nx, self.ny)).T else: U = V if dB: U = 10 * np.log10(U) if 'vmin' in kwargs: vmin = kwargs['vmin'] else: vmin = U.min() if 'vmax' in kwargs: vmax = kwargs['vmax'] else: vmax = U.max() if self.mode != 'file': img = ax.imshow(U, extent=(l, r, b, t), origin='lower', vmin=vmin, vmax=vmax, cmap=kwargs['cmap']) else: img = ax.scatter(self.grid[:, 0], self.grid[:, 1], c=U, s=20, linewidth=0, cmap=kwargs['cmap'], vmin=vmin, vmax=vmax) for k in range(self.na): ax.annotate(str(k), xy=(self.pa[0, k], self.pa[1, k])) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) clb = fig.colorbar(img, cax) clb.set_label(legcb) if best: if self.mode <> 'file': if polar == 'o': ax.contour(np.sum(self.bestsvo, axis=2)[f, :].reshape( self.nx, self.ny).T, extent=(l, r, b, t), linestyles='dotted') if polar == 'p': ax.contour(np.sum(self.bestsvp, axis=2)[f, :].reshape( self.nx, self.ny).T, extent=(l, r, b, t), linestyles='dotted') # display access points if a == -1: ax.scatter(self.pa[0, :], self.pa[1, :], s=30, c='r', linewidth=0) else: ax.scatter(self.pa[0, a], self.pa[1, a], s=30, c='r', linewidth=0) plt.tight_layout() return (fig, ax)
class Simul(PyLayers): """ Link oriented simulation A simulation requires : A Layout A Person A Trajectory """ def __init__(self, _filetraj='simulnet_TA-Office.h5',verbose=False): """ object constructor Parameters ---------- _filetraj : string h5 trajectory verbose : boolean """ self.filetraj = _filetraj # self.progress = -1 # simulation not loaded self.verbose = verbose self.cfield = [] self.dpersons = {} self.dap = {} self.Nag = 0 self.Nap = 0 self.load_config(_filetraj) self.gen_net() self.SL = SLink() self.DL = DLink(L=self.L,verbose=self.verbose) self.filename = 'simultraj_' + self.filetraj self.data = pd.DataFrame(columns=['id_a', 'id_b', 'x_a', 'y_a', 'z_a', 'x_b', 'y_b', 'z_b', 'd', 'eng', 'typ', 'wstd', 'fcghz', 'fbminghz', 'fbmaxghz', 'fstep', 'aktk_id', 'sig_id', 'ray_id', 'Ct_id', 'H_id' ]) self.data.index.name='t' self._filecsv = self.filename.split('.')[0] + '.csv' self.todo = {'OB': True, 'B2B': True, 'B2I': True, 'I2I': False} filenameh5 = pyu.getlong(self.filename,pstruc['DIRLNK']) if os.path.exists(filenameh5) : self.loadpd() # self._saveh5_init() def __repr__(self): s = 'Simul trajectories class\n' s = s + '------------------------\n' s = s +'\n' s = s + 'Used layout: ' + self.L.filename + '\n' s = s + 'Number of Agents: ' + str(self.Nag) + '\n' s = s + 'Number of Access Points: ' + str(self.Nap) + '\n' s = s + 'Link to be evaluated: ' + str(self.todo) + '\n' s = s + 'tmin: ' + str(self._tmin) + '\n' s = s + 'tmax: ' + str(self._tmax) + '\n' s = s +'\n' # network info s = s + 'self.N :\n' s = s + self.N.__repr__() + '\n' s = s + 'CURRENT TIME: ' + str(self.ctime) + '\n' return s def load_config(self, _filetraj): """ load a simultraj configuration file Parameters ---------- _filetraj : string name of simulation file to be loaded """ self.filetraj = _filetraj # get the trajectory traj = tr.Trajectories() traj.loadh5(self.filetraj) # get the layout self.L = Layout(traj.Lfilename) # resample trajectory for ut, t in enumerate(traj): if t.typ == 'ag': person = Body(t.name + '.ini') tt = t.time() self.dpersons.update({t.name: person}) self._tmin = tt[0] self._tmax = tt[-1] self.time = tt else: pos = np.array([t.x[0], t.y[0], t.z[0]]) self.dap.update({t.ID: {'pos': pos, 'ant': antenna.Antenna(), 'name': t.name } }) self.ctime = np.nan self.Nag = len(self.dpersons.keys()) self.Nap = len(self.dap.keys()) self.traj = traj def gen_net(self): """ generate Network and associated links Notes ----- Create self.N : Network object See Also -------- pylayers.network.network """ N = Network() # get devices on bodies for p in self.dpersons: D = [] for dev in self.dpersons[p].dev: D.append( Device(self.dpersons[p].dev[dev]['name'], ID=dev + '_' + p)) N.add_devices(D, grp=p) # get access point devices for ap in self.dap: D = Device(self.dap[ap]['name'], ID=ap) N.add_devices(D, grp='ap', p=self.dap[ap]['pos']) # create Network N.create() self.N = N def show(self): """ show actual simlulation configuration """ fig, ax = self.L.showGs() fig, ax = self.N.show(fig=fig, ax=ax) return fig, ax def evaldeter(self, na, nb, wstd, fmode='band', nf=10): """ deterministic evaluation of a link Parameters ---------- na : string: node a id in self.N (Network) nb : string: node b id in self.N (Network) wstd : string: wireless standard used for commmunication between na and nb fmode : string ('center'|'band') mode of frequency evaluation center : only on the centered frequency band : on the whole band nf : int: number of frequency points (if fmode = 'band') Returns ------- (a, t ) a : ndarray alpha_k t : ndarray tau_k """ # todo in network : # take into consideration the postion and rotation of antenna and not device self.DL.a = self.N.node[na]['p'] self.DL.Ta = self.N.node[na]['T'] self.DL.b = self.N.node[nb]['p'] self.DL.Tb = self.N.node[nb]['T'] if fmode == 'center': self.DL.fGHz = self.N.node[na]['wstd'][wstd]['fcghz'] else: minb = self.N.node[na]['wstd'][wstd]['fbminghz'] maxb = self.N.node[na]['wstd'][wstd]['fbmaxghz'] self.DL.fGHz = np.linspace(minb, maxb, nf) a, t = self.DL.eval() return a, t def evalstat(self, na, nb): """ statistical evaluation of a link Parameters ---------- na : string: node a id in self.N (Netwrok) nb : string: node b id in self.N (Netwrok) Returns ------- (a, t, eng) a : ndarray alpha_k t : ndarray tau_k eng : float engagement """ pa = self.N.node[na]['p'] pb = self.N.node[nb]['p'] dida, name = na.split('_') didb, name = nb.split('_') ak, tk, eng = self.SL.onbody(self.dpersons[name], dida, didb, pa, pb) return ak, tk, eng def run(self, **kwargs): """ run the link evaluation along a trajectory Parameters ---------- 'OB': boolean perform on body statistical link evaluation 'B2B': boolean perform body to body deterministic link evaluation 'B2I': boolean perform body to infrastructure deterministic link evaluation 'I2I': boolean perform infrastructure to infrastructure deterministic link eval. 'llink': list list of link to be evaluated (if [], all link are considered) 'wstd': list list of wstd to be evaluated (if [], all wstd are considered) 't': np.array list of timestamp to be evaluated (if [], all timestamps are considered) """ defaults = {'OB': True, 'B2B': True, 'B2I': True, 'I2I': False, 'llink': [], 'wstd': [], 't': np.array([]), } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] llink = kwargs.pop('llink') wstd = kwargs.pop('wstd') OB = kwargs.pop('OB') B2B = kwargs.pop('B2B') B2I = kwargs.pop('B2I') I2I = kwargs.pop('I2I') self.todo.update({'OB':OB,'B2B':B2B,'B2I':B2I,'I2I':I2I}) # Check link attribute if llink == []: llink = self.N.links elif not isinstance(llink, list): llink = [llink] checkl = [l in self.N.links for l in llink] if sum(checkl) != len(self.N.links): uwrong = np.where(np.array(checkl) is False)[0] raise AttributeError(str(np.array(llink)[uwrong]) + ' links does not exist in Network') # Check wstd attribute if wstd == []: wstd = self.N.wstd.keys() elif not isinstance(wstd, list): wstd = [wstd] checkw = [w in self.N.wstd.keys() for w in wstd] if sum(checkw) != len(self.N.wstd.keys()): uwrong = np.where(np.array(checkw) is False)[0] raise AttributeError(str(np.array(wstd)[uwrong]) + ' wstd are not in Network') # force time attribute compliant if not isinstance(kwargs['t'],np.ndarray): if isinstance(kwargs['t'],list): lt = np.array(kwargs['t']) elif (isinstance(kwargs['t'], int) or isinstance(kwargs['t'],float)): lt = np.array([kwargs['t']]) else : lt = kwargs['t'] if len(lt) == 0: lt = self.time # check time attribute if not lt[0] >= self._tmin and\ lt[-1] <= self._tmax: raise AttributeError('Requested time range not available') # self._traj is a copy of self.traj, which is affected by resampling. # it is only a temporary attribute for a given run if len(lt) > 1: sf = 1/(1.*lt[1]-lt[0]) self._traj = self.traj.resample(sf=sf, tstart=lt[0]) else: self._traj = self.traj.resample(sf=1.0, tstart=lt[0]) self._traj.time() self.time = self._traj.t self._time = pd.to_datetime(self.time,unit='s') # # Code # init = True for ut, t in enumerate(lt): self.ctime = t self.update_pos(t) print self.N.__repr__() for w in wstd: for na, nb, typ in llink[w]: if self.todo[typ]: if self.verbose: print '-'*30 print 'time:', t, '/', lt[-1] ,' time idx:', ut, '/',len(lt) print 'processing: ',na, ' <-> ', nb, 'wstd: ', w print '-'*30 eng = 0 self.evaldeter(na, nb, w) if typ == 'OB': self.evalstat(na, nb) eng = self.SL.eng L = self.DL + self.SL self._ak = L.H.ak self._tk = L.H.tk else : self._ak = self.DL.H.ak self._tk = self.DL.H.tk df = pd.DataFrame({\ 'id_a': na, 'id_b': nb, 'x_a': self.N.node[na]['p'][0], 'y_a': self.N.node[na]['p'][1], 'z_a': self.N.node[na]['p'][2], 'x_b': self.N.node[nb]['p'][0], 'y_b': self.N.node[nb]['p'][1], 'z_b': self.N.node[nb]['p'][2], 'd': self.N.edge[na][nb]['d'], 'eng': eng, 'typ': typ, 'wstd': w, 'fcghz': self.N.node[na]['wstd'][w]['fcghz'], 'fbminghz': self.DL.fmin, 'fbmaxghz': self.DL.fmax, 'fstep': self.DL.fstep, 'aktk_id': str(ut) + '_' + na + '_' + nb + '_' + w, 'sig_id': self.DL.dexist['sig']['grpname'], 'ray_id': self.DL.dexist['ray']['grpname'], 'Ct_id': self.DL.dexist['Ct']['grpname'], 'H_id': self.DL.dexist['H']['grpname'], },columns=['id_a', 'id_b', 'x_a', 'y_a', 'z_a', 'x_b', 'y_b', 'z_b', 'd', 'eng', 'typ', 'wstd', 'fcghz', 'fbminghz', 'fbmaxghz', 'fstep', 'aktk_id', 'sig_id', 'ray_id', 'Ct_id', 'H_id' ],index=[self._time[ut]]) if not self.check_exist(df): self.data = self.data.append(df) # self._index = self._index + 1 # save csv self.tocsv(ut, na, nb, w,init=init) init=False # save pandas self.data self.savepd() # save ak tauk self._saveh5(ut, na, nb, w) def check_exist(self, df): """check if a dataframe df already exists in self.data Parameters ---------- df : pd.DataFrame Returns ------- boolean True if already exists False otherwise """ ud = self.data[(self.data.index == df.index) & (self.data['id_a'] == df['id_a'].values[0]) & (self.data['id_b'] == df['id_b'].values[0]) & (self.data['wstd'] == df['wstd'].values[0]) ] if len(ud) == 0: return False else : return True def savepd(self): """ save data information of a simulation """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) store = pd.HDFStore(filenameh5,'a') self.data=self.data.sort() store['df'] = self.data store.close() def loadpd(self): """ load data from previous simulations """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) self.data = pd.read_hdf(filenameh5,'df') self.data.index.name='t' def update_pos(self, t): """ update positions of devices and bodies for a given time index Parameters ---------- t : int time value """ # if a bodies are involved in simulation if ((self.todo['OB']) or (self.todo['B2B']) or (self.todo['B2I'])): nodeid = [] pos = [] orient = [] for up, person in enumerate(self.dpersons.values()): person.settopos(self._traj[up], t=t, cs=True) name = person.name dev = person.dev.keys() nodeid.extend([n + '_' + name for n in dev]) pos.extend([person.dcs[d][:, 0] for d in dev]) orient.extend([person.acs[d] for d in dev]) # TODO !!!!!!!!!!!!!!!!!!!! # in a future version , the network update must also update # antenna positon in the device coordinate system self.N.update_pos(nodeid, pos, now=t) self.N.update_orient(nodeid, orient, now=t) self.N.update_dis() def _show3(self, **kwargs): """ 3D show using Mayavi Parameters ---------- t: float time index link: list [id_a, id_b] id_a : node id a id_b : node id b 'lay': bool show layout 'net': bool show net 'body': bool show bodies 'rays': bool show rays """ defaults = {'t': 0, 'link': [], 'wstd':[], 'lay': True, 'net': True, 'body': True, 'rays': True, 'ant': False } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] link = kwargs['link'] df = self.data[self.data['t'] == kwargs['t']] if len(df) == 0: raise AttributeError('invalid time') # default if link ==[]: line = df[df.index==1] link = [line['id_a'].values[0],line['id_b'].values[0]] else : # get info of the corresponding timestamp line = df[(df['id_a'] == link[0]) & (df['id_b'] == link[1])] if len(line) == 0: line = df[(df['id_b'] == link[0]) & (df['id_a'] == link[1])] if len(line) == 0: raise AttributeError('invalid link') rayid = line['ray_id'].values[0] self.update_pos(kwargs['t']) self.DL.a = self.N.node[link[0]]['p'] self.DL.b = self.N.node[link[1]]['p'] self.DL.Ta = self.N.node[link[0]]['T'] self.DL.Tb = self.N.node[link[1]]['T'] self.DL.load(self.DL.R,rayid) self.DL._show3(newfig= False, lay= kwargs['lay'], rays= kwargs['rays'], ant=False) if kwargs['net']: self.N._show3(newfig=False) if kwargs['body']: for p in self.dpersons: self.dpersons[p]._show3(newfig=False, topos=True, pattern=kwargs['ant']) # def _saveh5_init(self): # """ initialization of the h5py file # """ # filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) # import ipdb # try: # f5 = h5py.File(filenameh5, 'w') # f5.create_dataset('time', shape=self.time.shape, data=self.time) # f5.close() # except: # f5.close() # raise NameError('simultra.saveinit: \ # issue when writting h5py file') def _saveh5(self, ut, ida, idb, wstd): """ Save in h5py format Parameters ---------- ut : int time index in self.time ida : string node a index idb : string node b index wstd : string wireless standard of used link Notes ----- Dataset organisation: simultraj_<trajectory_filename.h5>.h5 | |time | ... | |/<tidx_ida_idb_wstd>/ |attrs | |a_k | |t_k Root dataset : time : array range of simulation time Group identifier : tidx : index in time dataset ida : node a index in Network idb : node b index in Network wstd : wireless standar of link interest Inside group: a_k : alpha_k values t_k : tau_k values See Also -------- pylayers.simul.links """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) grpname = str(ut) + '_' + ida + '_' + idb + '_' + wstd # try/except to avoid loosing the h5 file if # read/write error try: fh5 = h5py.File(filenameh5, 'a') if not grpname in fh5.keys(): fh5.create_group(grpname) f = fh5[grpname] # for k in kwargs: # f.attrs[k] = kwargs[k] f.create_dataset('alphak', shape=self._ak.shape, maxshape=(None), data=self._ak) f.create_dataset('tauk', shape=self._tk.shape, maxshape=(None), data=self._tk) else: pass#print grpname + ' already exists in ' + filenameh5 fh5.close() except: fh5.close() raise NameError('Simultraj._saveh5: issue when writting h5py file') def _loadh5(self, grpname): """ Load in h5py format Parameters ---------- grpname : string group name which can be found sin self.data aktk_idx column Returns ------- (ak, tk, conf) ak : ndarray: alpha_k tk : ndarray: alpha_k """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) # try/except to avoid loosing the h5 file if # read/write error try: fh5 = h5py.File(filenameh5, 'r') if not grpname in fh5.keys(): fh5.close() raise NameError(grpname + ' cannot be reached in ' + self.filename) f = fh5[grpname] # for k in f.attrs.keys(): # conf[k]=f.attrs[k] ak = f['alphak'][:] tk = f['tauk'][:] fh5.close() return ak, tk except: fh5.close() raise NameError('Simultraj._loadh5: issue when reading h5py file') def tocsv(self, ut, ida, idb, wstd,init=False): filecsv = pyu.getlong(self._filecsv,pstruc['DIRLNK']) with open(filecsv, 'a') as csvfile: fil = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL) if init: keys = self.data.iloc[-1].keys() data = [k for k in keys] data .append('ak') data .append('tk') fil.writerow(data) values = self.data.iloc[-1].values data = [v for v in values] sak = str(self._ak.tolist()) stk = str(self._tk.tolist()) data.append(sak) data.append(stk) fil.writerow(data)
def __init__(self,_fileini='coverage.ini'): """ object constructor Parameters ---------- _fileini : string name of the configuration file Notes ----- Coverage is described in an ini file. Default file is coverage.ini and is placed in the ini directory of the current project. """ self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini,pstruc['DIRSIMUL'])) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.apopt = dict(self.config.items('ap')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) # get the Layout filename = self.layoutopt['filename'] if filename.endswith('ini'): self.typ = 'indoor' self.L = Layout(filename) # get the receiving grid self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) self.mode = self.gridopt['mode'] self.boundary = eval(self.gridopt['boundary']) self.filespa = self.gridopt['file'] # # create grid # self.creategrid(mode=self.mode,boundary=self.boundary,_fileini=self.filespa) self.dap = {} for k in self.apopt: kwargs = eval(self.apopt[k]) ap = std.AP(**kwargs) self.dap[eval(k)] = ap try: self.L.Gt.nodes() except: pass try: self.L.dumpr() except: self.L.build() self.L.dumpw() else: self.typ='outdoor' self.E = ez.Ezone(filename) self.E.loadh5() self.E.rebase() self.fGHz = np.array([]) #self.fGHz = eval(self.txopt['fghz']) #self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) #self.ptdbm = eval(self.txopt['ptdbm']) #self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section #self.rxsens = eval(self.rxopt['sensitivity']) self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) # show section self.bshow = str2bool(self.showopt['show'])
class Simul(SimulationRT): # Sympy 2 #class Simul(sympy.RealtimeEnvironment): """ Attributes ---------- config : config parser instance sim_opt : dictionary of configuration option for simulation ag_opt : dictionary of configuration option for agent lay_opt : dictionary of configuration option for layout meca_opt : dictionary of configuration option for mecanic net_opt : dictionary of configuration option for network loc_opt : dictionary of configuration option for localization save_opt : dictionary of configuration option for save sql_opt : dictionary of configuration option for sql Parameters ---------- self.lAg : list of Agent(Object) list of agents involved in simulation self.L : Layout Layout used in simulation Notes ----- All the previous dictionnary are obtained from the chosen simulnet.ini file in the project directory """ def __init__(self): SimulationRT.__init__(self) #Sympy 2 #sympy.RealtimeEnvironment.__init__(self) #simpy 3 self.initialize() self.config = ConfigParser.ConfigParser() filename = pyu.getlong('simulnet.ini',pstruc['DIRSIMUL']) self.config.read(filename) self.sim_opt = dict(self.config.items('Simulation')) self.lay_opt = dict(self.config.items('Layout')) self.meca_opt = dict(self.config.items('Mechanics')) self.net_opt = dict(self.config.items('Network')) self.loc_opt = dict(self.config.items('Localization')) self.save_opt = dict(self.config.items('Save')) self.sql_opt = dict(self.config.items('Mysql')) self.seed = eval(self.sim_opt['seed']) self.traj=Trajectories() self.verbose = str2bool(self.sim_opt['verbose']) if str2bool(self.net_opt['ipython_nb_show']): self.verbose = False self.roomlist=[] self.finish = False self.create() def __repr__(self): s = 'Simulation information' + '\n----------------------' s = s + '\nLayout: ' + self.lay_opt['filename'] s = s + '\nSimulation duration: ' + self.sim_opt['duration'] s = s + '\nRandom seed: ' + self.sim_opt['seed'] s = s + '\nSave simulation: ' + self.save_opt['savep'] s = s + '\n\nUpdate times' + '\n-------------' s = s + '\nMechanical update: ' + self.meca_opt['mecanic_update_time'] s = s + '\nNetwork update: ' + self.net_opt['network_update_time'] s = s + '\nLocalization update: ' + self.net_opt['communication_mode'] s = s + '\n\nAgents => self.lAg[i]' + '\n------' s = s + '\nNumber of agents :' + str(len(self.lAg)) s = s + '\nAgents IDs: ' + str([self.lAg[i].ID for i in range(len(self.lAg))]) s = s + '\nAgents names: ' + str([self.lAg[i].name for i in range(len(self.lAg))]) s = s + '\nDestination of chosen agents: ' + self.meca_opt['choose_destination'] s = s + '\n\nNetwork' + '\n-------' s = s + '\nNodes per wstd: ' + str(self.net.wstd) s = s + '\n\nLocalization' + '------------' s = s + '\nLocalization enable: ' + self.loc_opt['localization'] s = s + '\nPostion estimation methods: ' + self.loc_opt['method'] return s def create_layout(self): """ create Layout in Simpy the_world thanks to Tk backend """ _filename = self.lay_opt['filename'] self.L = Layout(_filename) self.the_world = world() try: self.L.dumpr() print 'Layout graphs are loaded from ',basename,'/struc/ini' except: #self.L.sl = sl #self.L.loadGr(G1) print 'This is the first time the layout file is used\ Layout graphs are curently being built, it may take few minutes.' self.L.build() self.L.dumpw() # # Create Layout # walls = self.L.thwall(0, 0) for wall in walls: for ii in range(0, len(wall) - 1): self.the_world.add_wall(wall[ii], wall[ii + 1]) def create_agent(self): """ create simulation's Agents ..todo:: change lAg list to a dictionnary ( modification in show.py too) """ self.lAg = [] agents=[] Cf = ConfigParser.ConfigParser() Cf.read(pyu.getlong('agent.ini','ini')) agents=eval(dict(Cf.items('used_agent'))['list']) for i, ag in enumerate(agents): ag_opt = dict(Cf.items(ag)) self.lAg.append(Agent( ID=ag_opt['id'], name=ag_opt['name'], typ=ag_opt['typ'], color=eval(ag_opt['color']), pdshow=str2bool(self.meca_opt['pdshow']), pos=np.array(eval(ag_opt['pos'])), roomId=int(ag_opt['roomid']), froom=eval(ag_opt['froom']), meca_updt=float(self.meca_opt['mecanic_update_time']), wait=float(ag_opt['wait']), cdest=eval(self.meca_opt['choose_destination']), loc=str2bool(self.loc_opt['localization']), loc_updt=float(self.loc_opt['localization_update_time']), loc_method=eval(self.loc_opt['method']), L=self.L, network=str2bool(self.net_opt['network']), net=self.net, epwr=dict([(eval((ag_opt['wstd']))[ep],eval((ag_opt['epwr']))[ep]) for ep in range(len(eval((ag_opt['wstd']))))]), sens=dict([(eval((ag_opt['wstd']))[ep],eval((ag_opt['sensitivity']))[ep]) for ep in range(len(eval((ag_opt['wstd']))))]), world=self.the_world, wstd=eval(ag_opt['wstd']), save=eval(self.save_opt['save']), gcom=self.gcom, comm_mode=eval(self.net_opt['communication_mode']), sim=self, seed=self.seed)) def create_EMS(self): """ electromagnetic Solver object """ self.EMS = EMSolver(L=self.L) def create_network(self): """ create the whole network """ self.net = Network(EMS=self.EMS) self.gcom=Gcom(net=self.net,sim=self) self.create_agent() # create network if str2bool(self.net_opt['network']): self.net.create() # create All Personnal networks for n in self.net.nodes(): self.net.node[n]['PN']._get_wstd() self.net.node[n]['PN']._get_SubNet() self.gcom.create() # create Process Network self.Pnet = PNetwork(net=self.net, net_updt_time=float(self.net_opt['network_update_time']), L=self.L, sim=self, show_sg=str2bool(self.net_opt['show_sg']), disp_inf=str2bool(self.net_opt['dispinfo']), save=eval(self.save_opt['save'])) self.activate(self.Pnet, self.Pnet.run(), 0.0) def create_visual(self): """ create visual Tk process """ self.visu = Updater( interval=float(self.sim_opt['show_interval']), sim=self) self.activate(self.visu, self.visu.execute(), 0.0) def create(self): """ create the simulation This method is called at the end of __init__ """ # this is just to redump the database at each simulation if 'mysql' in self.save_opt['save']: if str2bool(self.sql_opt['dumpdb']): os.popen('mysql -u ' + self.sql_opt['user'] + ' -p ' + self.sql_opt['dbname'] +\ '< /private/staff/t/ot/niamiot/svn2/devel/simulator/pyray/SimWHERE2.sql' ) ## TODO supprimer la ref en dur if 'txt' in self.save_opt['save']: pyu.writeDetails(self) if os.path.isfile(basename+'/output/Nodes.txt'): print 'would you like to erase previous txt files ?' A=raw_input() if A == 'y': for f in os.listdir(basename+'/output/'): try: fi,ext=f.split('.') if ext == 'txt': os.remove(basename+'/output/'+f) except: pass self.create_layout() self.create_EMS() self.create_network() if str2bool(self.sim_opt['showtk']): self.create_visual() self.create_show() if str2bool(self.save_opt['savep']): self.save=Save(L=self.L,net=self.net,sim=self) self.activate(self.save,self.save.run(),0.0) def create_show(self): """ create a vizualization """ plt.ion() fig_net = 'network' fig_table = 'table' if str2bool(self.net_opt['show']): if str2bool(self.net_opt['ipython_nb_show']): notebook=True else: notebook =False self.sh = ShowNet(net=self.net, L=self.L,sim=self,fname=fig_net,notebook=notebook) self.activate(self.sh,self.sh.run(),1.0) if str2bool(self.net_opt['show_table']): self.sht = ShowTable(net=self.net,lAg=self.lAg,sim=self,fname=fig_table) self.activate(self.sht,self.sht.run(),1.0) def savepandas(self): """ save mechanics in pandas hdf5 format """ filename=pyu.getlong(eval(self.sim_opt["filename"]),pstruc['DIRNETSAVE']) layfile = self.L.filename.split('.')[0] store = pd.HDFStore(filename+'_'+layfile+'.h5','w') for a in self.lAg : if a.typ != 'ap': store.put( a.ID,a.meca.df.convert_objects() ) else : # if agent acces point, its position is saved store.put( a.ID,a.posdf ) store.get_storer(a.ID).attrs.typ = a.typ store.get_storer(a.ID).attrs.name = a.name store.get_storer(a.ID).attrs.ID = a.ID store.get_storer(a.ID).attrs.layout = self.L.filename #saving metadata store.close() self.traj.loadh5(eval(self.sim_opt["filename"])+'_'+layfile+'.h5') def runsimul(self): """ run simulation """ if not self.finish : seed(self.seed) self.simulate(until=float(self.sim_opt['duration']), real_time=True, rel_speed=float(self.sim_opt['speedratio'])) self.the_world._boids={} if str2bool(self.save_opt['savep']): print 'Processing save results, please wait' self.save.mat_export() if str2bool(self.save_opt['savepd']): self.savepandas() self.finish = True else : raise NameError('Reinstantiate a new simul object to run again')
def __init__(self,_fileini='coverage.ini'): """ object constructor Parameters ---------- _fileini : string name of the configuration file Notes ----- Coverage is described in an ini file. Default file is coverage.ini and is placed in the ini directory of the current project. """ self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini,pstruc['DIRSIMUL'])) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.apopt = dict(self.config.items('ap')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) # get the Layout self.L = Layout(self.layoutopt['filename']) # get the receiving grid self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) self.ng = self.nx*self.ny self.mode = eval(self.gridopt['full']) self.boundary = eval(self.gridopt['boundary']) # create grid # we could here construct a grid locally around the access point # to be done later for code acceleration # self.creategrid(full=self.mode,boundary=self.boundary) self.dap = {} for k in self.apopt: kwargs = eval(self.apopt[k]) ap = std.AP(**kwargs) self.dap[eval(k)] = ap try: self.aap = np.vstack((self.aap,ap['p'][0:2])) self.ptdbm = np.vstack((self.ptdbm,ap['PtdBm'])) except: self.aap = ap['p'][0:2] self.ptdbm = ap['PtdBm'] # 1 x na self.ptdbm = self.ptdbm.T # number of access points self.na = len(self.dap) # creating all links p = product(range(self.ng),range(self.na)) # # a : access point # g : grid # for k in p: pg = self.grid[k[0],:] pa = self.aap[k[1]] try: self.pa = np.vstack((self.pa,pa)) except: self.pa = pa try: self.pg = np.vstack((self.pg,pg)) except: self.pg = pg self.pa = self.pa.T self.pg = self.pg.T # frequency is chosen as all the center frequencies of the standard # warning assuming the same standard self.fGHz = self.dap[0].s.fcghz self.nf = len(self.fGHz) # AP section #self.fGHz = eval(self.txopt['fghz']) #self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) #self.ptdbm = eval(self.txopt['ptdbm']) #self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section self.rxsens = eval(self.rxopt['sensitivity']) kBoltzmann = 1.3806503e-23 self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) # list of access points lap = self.dap.keys() # list of channels lchan = map(lambda x: self.dap[x]['chan'],lap) apchan = zip(self.dap.keys(),lchan) self.bmhz = np.array(map(lambda x: self.dap[x[0]].s.chan[x[1][0]]['BMHz']*len(x[1]),apchan)) # Evaluate Noise Power (in dBm) Pn = (10**(self.noisefactordb/10.)+1)*kBoltzmann*self.temperaturek*self.bmhz*1e3 self.pndbm = 10*np.log10(Pn)+60 # show section self.bshow = str2bool(self.showopt['show']) try: self.L.Gt.nodes() except: pass try: self.L.dumpr() except: self.L.build() self.L.dumpw()
class Simul(PyLayers): """ Link oriented simulation A simulation requires : + A Layout + A Person + A Trajectory or a CorSer instance Members ------- dpersons : dictionnary of persons (agent) dap : dictionnary of access points Methods ------- load_simul : load configuration file load_Corser : load a Corser file _gen_net : generate network and asociated links show : show layout and network evaldeter : run simulation over time """ def __init__(self, source ='simulnet_TA-Office.h5',verbose=False): """ object constructor Parameters ---------- source : string h5 trajectory file default simulnet_TA-Office.h5 verbose : boolean Notes ----- The simultraj has a dataframe """ # self.progress = -1 # simulation not loaded self.verbose = verbose self.cfield = [] self.dpersons = {} self.dap = {} self.Nag = 0 self.Nap = 0 # source analysis if isinstance(source,str): self.filetraj = source self.load_simul(source) self.source = 'simul' elif 'pylayers' in source.__module__: self.filetraj = source._filename self.load_CorSer(source) cutoff=2 self.source = 'CorSer' # generate the Network # The wireless standard and frequency is fixed in this function # self._gen_net() # initialize Stochastic Link self.SL = SLink() # initialize Deterministic Link self.DL = DLink(L=self.L,verbose=self.verbose) self.DL.cutoff=cutoff self.filename = 'simultraj_' + self.filetraj + '.h5' # data is a panda container which is initialized # # We do not save all the simulation in a DataFRame anymore # #self.data = pd.DataFrame(columns=['id_a', 'id_b', # 'x_a', 'y_a', 'z_a', # 'x_b', 'y_b', 'z_b', # 'd', 'eng', 'typ', # 'wstd', 'fcghz', # 'fbminghz', 'fbmaxghz', 'fstep', 'aktk_id', # 'sig_id', 'ray_id', 'Ct_id', 'H_id' # ]) #self.data.index.name='t' self._filecsv = self.filename.split('.')[0] + '.csv' self.todo = {'OB': True, 'B2B': True, 'B2I': True, 'I2I': False} filenameh5 = pyu.getlong(self.filename,pstruc['DIRLNK']) if os.path.exists(filenameh5) : self.loadpd() self.settime(0.) # self._saveh5_init() def __repr__(self): s = 'Simul trajectories class\n' s = s + '------------------------\n' s = s +'\n' s = s + 'Used layout: ' + self.L.filename + '\n' s = s + 'Number of Agents: ' + str(self.Nag) + '\n' s = s + 'Number of Access Points: ' + str(self.Nap) + '\n' s = s + 'Link to be evaluated: ' + str(self.todo) + '\n' s = s + 'tmin: ' + str(self._tmin) + '\n' s = s + 'tmax: ' + str(self._tmax) + '\n' s = s +'\n' # network info s = s + 'self.N :\n' s = s + self.N.__repr__() + '\n' s = s + 'CURRENT TIME: ' + str(self.ctime) + '\n' return s def load_simul(self, source): """ load a simultraj configuration file Parameters ---------- source : string name of simulation file to be loaded """ self.filetraj = source if not os.path.isfile(source): raise AttributeError('Trajectory file'+source+'has not been found.\ Please make sure you have run a simulnet simulation before runining simultraj.') # get the trajectory traj = tr.Trajectories() traj.loadh5(self.filetraj) # get the layout self.L = Layout(traj.Lfilename) # resample trajectory for ut, t in enumerate(traj): if t.typ == 'ag': person = Body(t.name + '.ini') tt = t.time() self.dpersons.update({t.name: person}) self._tmin = tt[0] self._tmax = tt[-1] self.time = tt else: pos = np.array([t.x[0], t.y[0], t.z[0]]) self.dap.update({t.ID: {'pos': pos, 'ant': antenna.Antenna(), 'name': t.name } }) self.ctime = np.nan self.Nag = len(self.dpersons.keys()) self.Nap = len(self.dap.keys()) self.traj = traj def load_CorSer(self,source): """ load CorSer file for simulation Parameters ---------- source : name of simulation file to be loaded """ if isinstance(source.B,Body): B=[source.B] elif isinstance(source.B,list): B=source.B elif isinstance(source.B,dict): B=source.B.values() else: raise AttributeError('CorSer.B must be a list or a Body') self.L=source.L self.traj = tr.Trajectories() self.traj.Lfilename=self.L.filename for b in B: self.dpersons.update({b.name: b}) self._tmin = b.time[0] self._tmax = b.time[-1] self.time = b.time self.traj.append(b.traj) for ap in source.din: techno,ID=ap.split(':') if techno == 'HKB': techno = 'hikob' if techno == 'TCR': techno = 'tcr' if techno == 'BS': techno = 'bespoon' self.dap.update({ap: {'pos': source.din[ap]['p'], 'ant': source.din[ap]['ant'], 'T': source.din[ap]['T'], 'name': techno } }) self.ctime = np.nan self.Nag = len(B) self.Nap = len(source.din) self.corser = source def _gen_net(self): """ generate Network and associated links Notes ----- Create self.N : Network object See Also -------- pylayers.network.network """ # # Create Network # N = Network() # # get devices on bodies # # forall person # forall device for p in self.dpersons: D = [] for dev in self.dpersons[p].dev: aDev = Device(self.dpersons[p].dev[dev]['name'], ID = dev) D.append(aDev) D[-1].ant['A1']['name'] = self.dpersons[p].dev[dev]['file'] D[-1].ant['antenna'] = self.dpersons[p].dev[dev]['ant'] N.add_devices(D, grp=p) # # get access point devices # for ap in self.dap: D = Device(self.dap[ap]['name'], ID = ap) D.ant['antenna'] = self.dap[ap]['ant'] N.add_devices(D, grp = 'ap', p = self.dap[ap]['pos']) N.update_orient(ap, self.dap[ap]['T'], now = 0.) # create Network # # _get_wstd # _get_grp # _connect # _init_PN # N.create() self.N = N def show(self): """ show actual simlulation configuration """ fig, ax = self.L.showGs() fig, ax = self.N.show(fig=fig, ax=ax) return fig, ax def evaldeter(self, na, nb, wstd, fmod='force',nf=10,fGHz=[], **kwargs): """ deterministic evaluation of a link Parameters ---------- na : string: node a id in self.N (Network) nb : string: node b id in self.N (Network) wstd : string: wireless standard used for commmunication between na and nb fmode : string ('center'|'band'|'force') mode of frequency evaluation center : single frequency (center frequency of a channel) band : nf points on the whole band force : takes directly fGHz nf : int: number of frequency points (if fmode = 'band') **kwargs : argument of DLink Returns ------- (a, t ) a : ndarray alpha_k t : ndarray tau_k See Also -------- pylayers.simul.link.DLink """ # todo in network : # take into consideration the postion and rotation of antenna and not device self.DL.Aa = self.N.node[na]['ant']['antenna'] self.DL.a = self.N.node[na]['p'] self.DL.Ta = self.N.node[na]['T'] self.DL.Ab = self.N.node[nb]['ant']['antenna'] self.DL.b = self.N.node[nb]['p'] self.DL.Tb = self.N.node[nb]['T'] # # The frequency band is chosen from the selected standard # if fmode == 'center' # only center frequency is calculated # #' if fmod == 'center': self.DL.fGHz = self.N.node[na]['wstd'][wstd]['fcghz'] if fmod == 'band': fminGHz = self.N.node[na]['wstd'][wstd]['fbminghz'] fmaxGHz = self.N.node[na]['wstd'][wstd]['fbmaxghz'] self.DL.fGHz = np.linspace(fminGHz, fmaxGHz, nf) if fmod == 'force': assert len(fGHz)>0,"fGHz has not been defined" self.DL.fGHz = fGHz a, t = self.DL.eval(**kwargs) return a, t def evalstat(self, na, nb): """ statistical evaluation of a link Parameters ---------- na : string: node a id in self.N (Netwrok) nb : string: node b id in self.N (Netwrok) Returns ------- (a, t, eng) a : ndarray alpha_k t : ndarray tau_k eng : float engagement """ pa = self.N.node[na]['p'] pb = self.N.node[nb]['p'] if self.source == 'simul': dida, name = na.split('_') didb, name = nb.split('_') elif self.source =='CorSer': bpa,absolutedida,dida,name,technoa = self.corser.devmapper(na) bpb,absolutedidb,didb,name,technob = self.corser.devmapper(nb) ak, tk, eng = self.SL.onbody(self.dpersons[name], dida, didb, pa, pb) return ak, tk, eng def settime(self,t): """ set current time """ self.ctime = t self._traj=copy.copy(self.traj) self.update_pos(t) def run(self, **kwargs): """ run the link evaluation along a trajectory Parameters ---------- OB: boolean perform on body statistical link evaluation B2B: boolean perform body to body deterministic link evaluation B2I: boolean perform body to infrastructure deterministic link evaluation I2I: boolean perform infrastructure to infrastructure deterministic link eval. links: dict dictionnary of link to be evaluated (key is wtsd and value is a list of links) (if [], all link are considered) wstd: list list of wstd to be evaluated (if [], all wstd are considered) t: np.array list of timestamp to be evaluated (if [], all timestamps are considered) tbr : boolean time in bit reverse order (tmin,tmax,N) Npoints=2**N replace_data: boolean (True) if True , reference id of all already simulated link will be erased and replace by new simulation id fGHz : np.array frequency in GHz Examples -------- >>> from pylayers.simul.simultraj import * >>> from pylayers.measures.cormoran import * >>> C=CorSer() >>> S=Simul(C,verbose=True) >>> link={'ieee802154':[]} >>> link['ieee802154'].append(S.N.links['ieee802154'][0]) >>> lt = [0,0.2,0.3,0.4,0.5] >>> S.run(links=link,t=lt) """ defaults = {'OB': True, 'B2B': True, 'B2I': True, 'I2I': False, 'links': {}, 'wstd': [], 't': np.array([]), 'btr':True, 'DLkwargs':{}, 'replace_data':True, 'fmod':'force', 'fGHz':np.array([2.45]) } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] DLkwargs = kwargs.pop('DLkwargs') links = kwargs.pop('links') wstd = kwargs.pop('wstd') OB = kwargs.pop('OB') B2B = kwargs.pop('B2B') B2I = kwargs.pop('B2I') I2I = kwargs.pop('I2I') fmod = kwargs.pop('fmod') self.fGHz = kwargs.pop('fGHz') self.todo.update({'OB':OB,'B2B':B2B,'B2I':B2I,'I2I':I2I}) # Check link attribute if links == {}: links = self.N.links elif not isinstance(links, dict): raise AttributeError('links is {wstd:[list of links]}, see self.N.links') for k in links.keys(): checkl = [l in self.N.links[k] for l in links[k]] if len(np.where(checkl==False)[0])>0: # if sum(checkl) != len(self.N.links): uwrong = np.where(np.array(checkl) is False)[0] raise AttributeError(str(np.array(links)[uwrong]) + ' links does not exist in Network') wstd = links.keys() # # Check wstd attribute # if wstd == []: # wstd = self.N.wstd.keys() # elif not isinstance(wstd, list): # wstd = [wstd] checkw = [w in self.N.wstd.keys() for w in wstd] if sum(checkw) != len(wstd): uwrong = np.where(np.array(checkw) is False)[0] raise AttributeError(str(np.array(wstd)[uwrong]) + ' wstd are not in Network') # force time attribute compliant if not isinstance(kwargs['t'],np.ndarray): if isinstance(kwargs['t'],list): lt = np.array(kwargs['t']) elif (isinstance(kwargs['t'], int) or isinstance(kwargs['t'],float)): lt = np.array([kwargs['t']]) else : lt = kwargs['t'] #if len(lt) == 0: # lt = self.time # check time attribute if kwargs['btr']: if (lt[0] < self._tmin) or\ (lt[1] > self._tmax) : raise AttributeError('Requested time range not available') # self._traj is a copy of self.traj, which is affected by resampling. # it is only a temporary attribute for a given run # if len(lt) > 1: # sf = 1/(1.*lt[1]-lt[0]) # self._traj = self.traj.resample(sf=sf, tstart=lt[0]) # else: # self._traj = self.traj.resample(sf=1.0, tstart=lt[0]) # self._traj.time() # self.time = self._traj.t # self._time = pd.to_datetime(self.time,unit='s') # # Nested Loops # # time # standard # links # evaldeter &| evalstat # #lt = self.get_sim_time(lt) #self._time=self.get_sim_time(lt) init = True if kwargs['btr']: tmin = lt[0] tmax = lt[1] Nt = int(2**lt[2]) ta = np.linspace(tmin,tmax,Nt) it = np.hstack((np.r_[0],np.r_[pyu.bitreverse(Nt,int(lt[2]))])) #trev = t[it] else: ta = kwargs['t'] it = range(len(ta)) ## Start to loop over time ## ut : counter ## t : time value (s) #for ut, t in enumerate(lt): for ks,ut in enumerate(it): t = ta[ut] self.ctime = t # update spatial configuration of the scene for time t self.update_pos(t) # print self.N.__repr__() ## Start to loop over available Wireless standard ## for w in wstd: ## Start to loop over the chosen links stored in links ## for na, nb, typ in links[w]: # If type of link is valid (Body 2 Body,...) # if self.todo[typ]: if self.verbose: print '-'*30 print 'time:', t, '/', lt[-1] ,' time idx:', ut, '/',len(ta),'/',ks print 'processing: ',na, ' <-> ', nb, 'wstd: ', w print '-'*30 eng = 0 # # Invoque link deterministic simulation # # node : na # node : nb # wstd : w # self.evaldeter(na, nb, w, applywav=False, fmod = fmod, fGHz = self.fGHz, **DLkwargs) # if typ == 'OB': # self.evalstat(na, nb) # eng = self.SL.eng # L = self.DL + self.SL # self._ak = L.H.ak # self._tk = L.H.tk # else : # Get alphak an tauk self._ak = self.DL.H.ak self._tk = self.DL.H.tk aktk_id = str(ut) + '_' + na + '_' + nb + '_' + w # this is a dangerous way to proceed ! # the id as a finite number of characters while len(aktk_id)<40: aktk_id = aktk_id + ' ' df = pd.DataFrame({ 'id_a': na, 'id_b': nb, 'x_a': self.N.node[na]['p'][0], 'y_a': self.N.node[na]['p'][1], 'z_a': self.N.node[na]['p'][2], 'x_b': self.N.node[nb]['p'][0], 'y_b': self.N.node[nb]['p'][1], 'z_b': self.N.node[nb]['p'][2], 'd': self.N.edge[na][nb]['d'], 'eng': eng, 'typ': typ, 'wstd': w, 'fcghz': self.N.node[na]['wstd'][w]['fcghz'], 'fbminghz': self.fGHz[0], 'fbmaxghz': self.fGHz[-1], 'nf': len(self.fGHz), 'aktk_id':aktk_id, 'sig_id': self.DL.dexist['sig']['grpname'], 'ray_id': self.DL.dexist['ray']['grpname'], 'Ct_id': self.DL.dexist['Ct']['grpname'], 'H_id': self.DL.dexist['H']['grpname'], },columns=['id_a', 'id_b', 'x_a', 'y_a', 'z_a', 'x_b', 'y_b', 'z_b', 'd', 'eng', 'typ', 'wstd', 'fcghz', 'fbminghz', 'fbmaxghz', 'fstep', 'aktk_id', 'sig_id', 'ray_id', 'Ct_id', 'H_id' ],index= [t]) #self._time[ut]]) self.savepd(df) def replace_data(self, df): """check if a dataframe df already exists in self.data Parameters ---------- df : pd.DataFrame Returns ------- boolean True if already exists False otherwise """ self.data[(self.data.index == df.index) & (self.data['id_a'] == df['id_a'].values[0]) & (self.data['id_b'] == df['id_b'].values[0]) & (self.data['wstd'] == df['wstd'].values[0])]=df.values def check_exist(self, df): """check if a dataframe df already exists in self.data Parameters ---------- df : pd.DataFrame Returns ------- boolean True if already exists False otherwise """ # check init case if not len(self.data.index) == 0: ud = self.data[(self.data.index == df.index) & (self.data['id_a'] == df['id_a'].values[0]) & (self.data['id_b'] == df['id_b'].values[0]) & (self.data['wstd'] == df['wstd'].values[0])] if len(ud) == 0: return False else : return True else : return False def savepd(self,df): """ save data information of a simulation Parameters ---------- df : one index data Notes ----- """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) store = pd.HDFStore(filenameh5) #self.data=self.data.sort() store.append('df',df) store.close() def loadpd(self): """ load data from previous simulations """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) store = pd.HDFStore(filenameh5) #self.data = pd.read_hdf(filenameh5,'df') self.data = store.get('df') self.data.index.name='t' self.data = self.data.sort() def get_sim_time(self,t): """ retrieve closest time value in regard of passed t value in parameter """ if not isinstance(t,list) and not isinstance(t,np.ndarray): return np.array([self.time[np.where(self.time <=t)[0][-1]]]) else : return np.array([self.get_sim_time(tt) for tt in t])[:,0] def get_df_from_link(self,id_a,id_b,wstd=''): """ Return a restricted data frame for a specific link Parameters ---------- id_a : str node id a id_b: str node id b wstd: str optionnal :wireslees standard """ if wstd == '': return self.data[(self.data['id_a']==id_a) & (self.data['id_b']==id_b)] else : return self.data[(self.data['id_a']==id_a) & (self.data['id_b']==id_b) & self.data['wstd']==wstd] def update_pos(self, t): """ update positions of devices and bodies for a given time index Parameters ---------- t : int time value """ # if a bodies are involved in simulation if ((self.todo['OB']) or (self.todo['B2B']) or (self.todo['B2I'])): nodeid = [] pos = [] devlist = [] orient = [] for up, person in enumerate(self.dpersons.values()): person.settopos(self._traj[up], t=t, cs=True) name = person.name dev = person.dev.keys() devlist.extend(dev) #nodeid.extend([n + '_' + name for n in dev]) pos.extend([person.dcs[d][:, 0] for d in dev]) orient.extend([person.acs[d] for d in dev]) # TODO !!!!!!!!!!!!!!!!!!!! # in a future version , the network update must also update # antenna position in the device coordinate system self.N.update_pos(devlist, pos, now=t) self.N.update_orient(devlist, orient, now=t) self.N.update_dis() def get_value(self,**kwargs): """ retrieve output parameter at a specific time Parameters ---------- typ : list list of parameters to be retrieved (R | C |H | ak | tk | rss ) links: list dictionnary of link to be evaluated (key is wtsd and value is a list of links) (if [], all link are considered) t: int or np.array list of timestamp to be evaluated | singlr time instant Returns ------- output: dict [link_key]['t'] ['ak'] ... """ # get time defaults = {'t': 0, 'typ':['ak'], 'links': {}, 'wstd':[], 'angles':False } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] # allocate an empty dictionnary for wanted selected output output={} # manage time t can be a list or a float t = kwargs['t'] t = self.get_sim_time(t) dt = self.time[1]-self.time[0] # manage links plinks = kwargs['links'] links=[] if isinstance(plinks,dict): for l in plinks.keys(): links.extend(plinks[l]) if len(links) == 0: raise AttributeError('Please give valid links to get values') # output['t']=[] # output['time_to_simul']=[] # for each requested time step for tt in t : # for each requested links for link in links: linkname=link[0]+'-'+link[1] if not output.has_key(linkname): output[linkname] = {} if not output[linkname].has_key('t'): output[linkname]['t'] = [] # restrict global dataframe self.data to the specific link df = self.get_df_from_link(link[0],link[1]) # restrict global dataframe self.data to the specific z df = df[(df.index > tt-dt) & (df.index <= tt+dt)] if len(df) != 0: output[linkname]['t'].append(tt) if len(df)>1: print 'Warning possible issue in self.get_value' line = df.iloc[-1] # # get info of the corresponding timestamp # line = df[(df['id_a'] == link[0]) & (df['id_b'] == link[1])].iloc[-1] # if len(line) == 0: # line = df[(df['id_b'] == link[0]) & (df['id_a'] == link[1])] # if len(line) == 0: # raise AttributeError('invalid link') #retrieve correct position and orientation given the time #self.update_pos(t=tt) # antennas positions #self.DL.a = self.N.node[link[0]]['p'] #self.DL.b = self.N.node[link[1]]['p'] # antennas orientation #self.DL.Ta = self.N.node[link[0]]['T'] #self.DL.Tb = self.N.node[link[1]]['T'] # antennas object #self.DL.Aa = self.N.node[link[0]]['ant']['antenna'] #self.DL.Ab = self.N.node[link[1]]['ant']['antenna'] # get the antenna index #uAa_opt, uAa = self.DL.get_idx('A_map',self.DL.Aa._filename) #uAb_opt, uAb = self.DL.get_idx('A_map',self.DL.Ab._filename) if 'ak' in kwargs['typ'] or 'tk' in kwargs['typ'] or 'rss' in kwargs['typ']: H_id = line['H_id'].decode('utf8') # load the proper link # parse index lid = H_id.split('_') #if (lid[5]==str(uAa))&(lid[6]==str(uAb)): self.DL.load(self.DL.H,H_id) if 'ak' in kwargs['typ']: if not output[linkname].has_key('ak'): output[linkname]['ak']=[] output[linkname]['ak'].append(copy.deepcopy(self.DL.H.ak)) if 'tk' in kwargs['typ']: if not output[linkname].has_key('tk'): output[linkname]['tk']=[] output[linkname]['tk'].append(copy.deepcopy(self.DL.H.tk)) if 'rss' in kwargs['typ']: if not output[linkname].has_key('rss'): output[linkname]['rss']=[] output[linkname]['rss'].append(copy.deepcopy(self.DL.H.rssi())) if 'R' in kwargs['typ']: if not output[linkname].has_key('R'): output[linkname]['R']=[] ray_id = line['ray_id'] self.DL.load(self.DL.R,ray_id) output[linkname]['R'].append(copy.deepcopy(self.DL.R)) if 'C' in kwargs['typ']: if not output[linkname].has_key('C'): output[linkname]['C']=[] Ct_id = line['Ct_id'] self.DL.load(self.DL.C,Ct_id) if kwargs['angles']: self.DL.C.islocal=False self.DL.C.locbas(Tt=self.DL.Ta, Tr=self.DL.Tb) #T channel output[linkname]['C'].append(copy.deepcopy(self.DL.C)) if 'H' in kwargs['typ']: if not output[linkname].has_key('H'): output[linkname]['H']=[] H_id = line['H_id'] lid = H_id.split('_') #if (lid[5]==str(uAa))&(lid[6]==str(uAb)): self.DL.load(self.DL.H,H_id) output[linkname]['H'].append(copy.deepcopy(self.DL.H)) # if time value not found in dataframe else: if not output[linkname].has_key('time_to_simul'): output[linkname]['time_to_simul'] = [] output[linkname]['time_to_simul'].append(tt) for l in output.keys(): if output[l].has_key('time_to_simul'): print 'link', l , 'require simulation for timestamps', output[l]['time_to_simul'] return(output) def get_link(self,**kwargs): """ retrieve a Link specific time from a simultraj Parameters ---------- typ : list list of parameters to be retrieved (ak | tk | R |C) links: list dictionnary of link to be evaluated (key is wtsd and value is a list of links) (if [], all link are considered) t: int or np.array list of timestamp to be evaluated | singlr time instant Returns ------- DL : DLink Examples -------- >>> from pylayers.simul.simultraj import * >>> from pylayers.measures.cormoran import * >>> C=CorSer(serie=6i,day=11) >>> S = Simul(C,verb ose=False) >>> DL = S.get_link(typ=['R','C','H']) ... """ # get time defaults = {'t': 0, 'typ':['ak'], 'links': {}, 'wstd':[], 'angles':False } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] output={} # manage time t = kwargs['t'] t = self.get_sim_time(t) dt = self.time[1]-self.time[0] # manage links plinks = kwargs['links'] links=[] if isinstance(plinks,dict): for l in plinks.keys(): links.extend(plinks[l]) if len(links) == 0: raise AttributeError('Please give valid links to get values') # output['t']=[] # output['time_to_simul']=[] # for each requested time step for tt in t : # for each requested links for link in links: linkname=link[0]+'-'+link[1] if not output.has_key(linkname): output[linkname] = {} if not output[linkname].has_key('t'): output[linkname]['t'] = [] # restrict global dataframe self.data to the specific link df = self.get_df_from_link(link[0],link[1]) # restrict global dataframe self.data to the specific z df = df[(df.index > tt-dt) & (df.index <= tt+dt)] if len(df) != 0: output[linkname]['t'].append(tt) if len(df)>1: print 'Warning possible issue in self.get_link' line = df.iloc[-1] # # get info of the corresponding timestamp # line = df[(df['id_a'] == link[0]) & (df['id_b'] == link[1])].iloc[-1] # if len(line) == 0: # line = df[(df['id_b'] == link[0]) & (df['id_a'] == link[1])] # if len(line) == 0: # raise AttributeError('invalid link') #retrieve correct position and orientation given the time self.update_pos(t=tt) self.DL.a = self.N.node[link[0]]['p'] self.DL.b = self.N.node[link[1]]['p'] self.DL.Ta = self.N.node[link[0]]['T'] self.DL.Tb = self.N.node[link[1]]['T'] #self.DL.Aa = self.N.node[link[0]]['ant']['antenna'] #self.DL.Ab = self.N.node[link[1]]['ant']['antenna'] #H_id = line['H_id'].decode('utf8') #self.DL.load(self.DL.H,H_id) if 'R' in kwargs['typ']: ray_id = line['ray_id'] self.DL.load(self.DL.R,ray_id) if 'C' in kwargs['typ']: Ct_id = line['Ct_id'] self.DL.load(self.DL.C,Ct_id) if kwargs['angles']: self.DL.C.islocal=False self.DL.C.locbas(Tt=self.DL.Ta, Tr=self.DL.Tb) if 'H' in kwargs['typ']: H_id = line['H_id'] self.DL.load(self.DL.H,H_id) return(self.DL) def _show3(self, **kwargs): """ 3D show using Mayavi Parameters ---------- t: float time index link: list [id_a, id_b] id_a : node id a id_b : node id b 'lay': bool show layout 'net': bool show net 'body': bool show bodies 'rays': bool show rays """ defaults = {'t': 0, 'link': [], 'wstd':[], 'lay': True, 'net': True, 'body': True, 'rays': True, 'ant': False } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] link = kwargs['link'] self.update_pos(kwargs['t']) if len(self.data) != 0: df = self.data[self.data.index == pd.to_datetime(kwargs['t'])] if len(df) != 0: raise AttributeError('invalid time') # default if link ==[]: line = df[df.index<=pd.to_datetime(0)] link = [line['id_a'].values[0],line['id_b'].values[0]] else : # get info of the corresponding timestamp line = df[(df['id_a'] == link[0]) & (df['id_b'] == link[1])] if len(line) == 0: line = df[(df['id_b'] == link[0]) & (df['id_a'] == link[1])] if len(line) == 0: raise AttributeError('invalid link') rayid = line['ray_id'].values[0] self.DL.a = self.N.node[link[0]]['p'] self.DL.b = self.N.node[link[1]]['p'] self.DL.Ta = self.N.node[link[0]]['T'] self.DL.Tb = self.N.node[link[1]]['T'] self.DL.load(self.DL.R,rayid) self.DL._show3(newfig= False, lay= kwargs['lay'], rays= kwargs['rays'], ant=False) else : self.DL._show3(newfig= False, lay= True, rays= False, ant=False) if kwargs['net']: self.N._show3(newfig=False) if kwargs['body']: for p in self.dpersons: self.dpersons[p]._show3(newfig=False, topos=True, pattern=kwargs['ant']) # def _saveh5_init(self): # """ initialization of the h5py file # """ # filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) # import ipdb # try: # f5 = h5py.File(filenameh5, 'w') # f5.create_dataset('time', shape=self.time.shape, data=self.time) # f5.close() # except: # f5.close() # raise NameError('simultra.saveinit: \ # issue when writting h5py file') def _saveh5(self, ut, ida, idb, wstd): """ Save in h5py format Parameters ---------- ut : int time index in self.time ida : string node a index idb : string node b index wstd : string wireless standard of used link Notes ----- Dataset organisation: simultraj_<trajectory_filename.h5>.h5 | |time | ... | |/<tidx_ida_idb_wstd>/ |attrs | |a_k | |t_k Root dataset : time : array range of simulation time Group identifier : tidx : index in time dataset ida : node a index in Network idb : node b index in Network wstd : wireless standar of link interest Inside group: a_k : alpha_k values t_k : tau_k values See Also -------- pylayers.simul.links """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) grpname = str(ut) + '_' + ida + '_' + idb + '_' + wstd # try/except to avoid loosing the h5 file if # read/write error try: fh5 = h5py.File(filenameh5, 'a') if not grpname in fh5.keys(): fh5.create_group(grpname) f = fh5[grpname] # for k in kwargs: # f.attrs[k] = kwargs[k] f.create_dataset('alphak', shape=self._ak.shape, maxshape=(None), data=self._ak) f.create_dataset('tauk', shape=self._tk.shape, maxshape=(None), data=self._tk) else: pass#print grpname + ' already exists in ' + filenameh5 fh5.close() except: fh5.close() raise NameError('Simultraj._saveh5: issue when writting h5py file') def _loadh5(self, grpname): """ Load in h5py format Parameters ---------- grpname : string group name which can be found sin self.data aktk_idx column Returns ------- (ak, tk, conf) ak : ndarray: alpha_k tk : ndarray: alpha_k """ filenameh5 = pyu.getlong(self.filename, pstruc['DIRLNK']) # try/except to avoid loosing the h5 file if # read/write error try: fh5 = h5py.File(filenameh5, 'r') if not grpname in fh5.keys(): fh5.close() raise NameError(grpname + ' cannot be reached in ' + self.filename) f = fh5[grpname] # for k in f.attrs.keys(): # conf[k]=f.attrs[k] ak = f['alphak'][:] tk = f['tauk'][:] fh5.close() return ak, tk except: fh5.close() raise NameError('Simultraj._loadh5: issue when reading h5py file') def tocsv(self, ut, ida, idb, wstd,init=False): filecsv = pyu.getlong(self._filecsv,pstruc['DIRLNK']) with open(filecsv, 'a') as csvfile: fil = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL) if init: keys = self.data.iloc[-1].keys() data = [k for k in keys] data .append('ak') data .append('tk') fil.writerow(data) values = self.data.iloc[-1].values data = [v for v in values] sak = str(self._ak.tolist()) stk = str(self._tk.tolist()) data.append(sak) data.append(stk) fil.writerow(data)
class Simul(PyLayers): """ Simulation Class Methods ------- gui() graphical user interface choose() choose a simulation file in simuldir info() info about the simulation showray(itx,irx,iray) show a single ray help() help on using Simulation object freq() return the frequency base save() save Simulation file layout load a Layout file load() load Simulation show3() geomview vizualization show3l(itx,irx) geomview vizualization of link itx irx show() 2D visualization of simulation with furniture run(itx,irx) run simulation for links (itx,irx) cir(itx,irx,store_level=0,alpha=1.0) Channel Impulse Response calculation Attributes ---------- fileconf filetauk filetang filerang tx rx progress indoor mat sl Notes ------ This class group together all the parametrization files of a simulation """ def __init__(self, _filesimul='default.ini'): self.filesimul = _filesimul self.config = ConfigParser.ConfigParser() self.config.add_section("files") self.config.add_section("frequency") self.config.add_section("waveform") self.config.add_section("output") self.dtang = {} self.drang = {} self.dtauk = {} self.dfield = {} self.dcir = {} self.output = {} # # Here was a nasty bug : Rule for the future # "Always precise the key value of the passed argument" # # Mal nommer les choses, c'est ajouter au malheur du monde ( Albert Camus ) # self.tx = RadioNode( name='', typ='tx', _fileini='radiotx.ini', _fileant='defant.vsh3', ) self.rx = RadioNode( name='', typ='rx', _fileini='radiorx.ini', _fileant='defant.vsh3', ) self.filefreq = "def.freq" self.progress = -1 # simulation not loaded self.filetang = [] self.filerang = [] self.filetauk = [] self.filefield = [] self.fileconf = "project.conf" self.cfield = [] self.fGHz = np.linspace(2, 11, 181, endpoint=True) self.wav = wvf.Waveform() self.load(_filesimul) def gui(self): """ gui to modify the simulation file """ simulgui = multenterbox( '', 'Simulation file', ('filesimul', 'filestr', 'filefreq', 'filespaTx', 'filespaRx', 'fileantTx' 'fileantRx'), (self.filesimul, self.filestr, self.filefreq, self.filespaTx, self.filespaRx, self.fileantTx, self.fileantRx)) if simulgui is not None: self.filesimul = simulgui[0] self.filestr = simulgui[1] self.filefreq = simulgui[6] self.filespaTx = simulgui[7] self.filespaRx = simulgui[8] self.fileantTx = simulgui[9] self.fileantRx = simulgui[10] def updcfg(self): """ update simulation .ini config file with values currently in use. """ self.config.set("files", "struc", self.filestr) self.config.set("files", "conf", self.fileconf) self.config.set("files", "txant", self.tx.fileant) self.config.set("files", "rxant", self.rx.fileant) self.config.set("files", "tx", self.tx.fileini) self.config.set("files", "rx", self.rx.fileini) self.config.set("files", "mat", self.filematini) self.config.set("files", "slab", self.fileslabini) self.config.set("tud", "purc", str(self.patud.purc)) self.config.set("tud", "nrmax", str(self.patud.nrmax)) self.config.set("tud", "num", str(self.patud.num)) # # frequency section # self.config.set("frequency", "fghzmin", self.fGHz[0]) self.config.set("frequency", "fghzmax", self.fGHz[-1]) self.config.set("frequency", "nf", str(len(self.fGHz))) # # waveform section # self.config.set("waveform", "tw", str(self.wav['twns'])) self.config.set("waveform", "band", str(self.wav['bandGHz'])) self.config.set("waveform", "fc", str(self.wav['fcGHz'])) self.config.set("waveform", "thresh", str(self.wav['threshdB'])) self.config.set("waveform", "type", str(self.wav['typ'])) self.config.set("waveform", "fe", str(self.wav['feGHz'])) # # output section # for k in self.output.keys(): self.config.set("output", str(k), self.dout[k]) # Initialize waveform self.wav = wvf.Waveform() # Update waveform self.wav.read(self.config) self.save() def clean_project(self, verbose=True): """ Clean Pyrpoject directory remove .lch, .tra, .field .tud, .tauk, .tang, .rang, <pyproject>/output remove [output] entries into .ini of self.filesimul Parameters ---------- verbose : boolean Verbose mode on/off Returns ------- Boolean: True if the project has been cleaned, False otherwise """ if verbose: print "-----------------------------------" print "-----------------------------------" print " WARNING " print "-----------------------------------" print "-----------------------------------" print "You are about to remove ALL previous computed raytracing files." print "If you decide to remove it, you will need to restart the entire \ raytracing simulation to exploit simulation results" print "\n Do you want to remove these simulation files ? y/n" r = raw_input() else: r == 'y' if r == 'y': inifile = self.filesimul try: path = os.getenv('BASENAME') except: print('Error : there is no project directory in $BASENAME') dirlist = ['output'] extension = [ '.lch', '.field', '.tra', '.tud', '.tang', '.rang', '.tauk' ] rindic = False # remove file for d in dirlist: for ex in extension: files = os.listdir(path + '/' + d) for f in files: if not os.path.isdir(path + '/' + d + '/' + f) and ex in f: rindic = True if verbose: print f os.remove(path + '/' + d + '/' + f) if rindic: if verbose: print 'removed *' + ex + ' from ' + d + '\n' rindic = False # remove output into the self.filesimul ini file simcfg = ConfigParser.ConfigParser() simcfg.read(pyu.getlong(inifile, pstruc['DIRSIMUL'])) simcfg.remove_section('output') f = open(pyu.getlong(inifile, pstruc['DIRSIMUL']), 'wb') simcfg.write(f) f.close() self.dout = {} self.dlch = {} self.dtra = {} self.dtud = {} self.dtang = {} self.drang = {} self.dtauk = {} self.dfield = {} self.dcir = {} self.output = {} if verbose: print 'removed [output] entries into ' + inifile + '\n' print 'Project CLEANED' return True else: if verbose: print "clean project process ABORTED" return False def save(self): """ save simulation file Simulation files are .ini files which are saved in a dedicated directory basename/ini in the Project tree """ filesimul = pyu.getlong(self.filesimul, "ini") fd = open(filesimul, "w") # getting current spa file if any try: self.config.set("files", "tx", self.tx.fileini) except: pass try: self.config.set("files", "rx", self.rx.fileini) except: pass self.config.write(fd) fd.close() # save tx self.tx.save() # save rx self.rx.save() # save slab and mat file # -- # self.slab.save(self.fileslabini) # self.slab.mat.save(self.filematini) # -- # fix bug #189 # slab is a member of S.L not of S anymore # mat is a member of S.L.sl not of S.slab try: self.L.sl.save(self.fileslabini) except: pass try: self.L.sl.mat.save(self.filematini) except: pass # def saveold(self): # """ save simulation file # """ # filesimul = pyu.getlong(self.filesimul, "ini") # fd = open(filesimul, "w") # config = ConfigParser.ConfigParser() # # # # files section # # # #config.add_section("files") # self.config.set("files", "conf", self.fileconf) # self.config.set("files", "struc", self.filestr) # self.config.set("files", "slab", self.fileslab) # self.config.set("files", "mat", self.filemat) # try: # self.config.set("files", "tx", self.tx.filespa) # except: # pass # try: # self.config.set("files", "rx", self.rx.filespa) # except: # pass # try: # self.config.set("files", "txant", self.tx.fileant) # except: # pass # try: # self.config.set("files", "rxant", self.rx.fileant) # except: # pass # self.config.set("files", "patra", self.filepatra) # self.config.set("files", "palch", self.filepalch) # self.palch.save() # self.patra.save() # # # # tud section # # # self.config.set("tud", "purc", self.patud.purc) # self.config.set("tud", "nrmax", self.patud.nrmax) # self.config.set("tud", "num", self.patud.num) # # # # frequency section # # # self.config.set("frequency", "fghzmin", self.freq[0]) # self.config.set("frequency", "fghzmax", self.freq[-1]) # self.config.set("frequency", "Nf", len(self.freq)) # # # # output section # # # #filelch exists # if self.progress > 0: # #config.add_section("output") # for k in range(len(self.filelch)): # _fileout = "out" + "???" # filename = self.filelch[k] # self.config.set("launch", str(k + 1), filename) # # filetra exists # for k in range(len(self.filelch)): # if self.progress > 1: # #self.config.add_section("trace") # for l in arange(len(self.filetra[k])): # filename = self.filetra[k][l] # self.config.set("trace", "rx" + str(l + 1), filename) # # .tang exists # # .rang exists # # .tud exists # if self.progress > 2: # #config.add_section("tud") # #config.add_section("tang") # #config.add_section("rang") # for l in arange(len(self.filetud[k])): # ftud = self.filetud[k][l] # self.config.set("tud", "rx" + str(l + 1), ftud) # for l in arange(len(self.filetang[k])): # ftang = self.filetang[k][l] # self.config.set("tang", "rx" + str(l + 1), ftang) # for l in arange(len(self.filerang[k])): # frang = self.filerang[k][l] # self.config.set("rang", "rx" + str(l + 1), frang) # # .field exist # # .tauk exist # if self.progress > 3: # #config.add_section("tauk") # #config.add_section("field") # for l in arange(len(self.filetud[k])): # ftauk = self.filetud[k][l] # self.config.set("tauk", "rx" + str(l + 1), ftauk) # for l in arange(len(self.filefield[k])): # ffield = self.filefield[k][l] # self.config.set("field", "rx" + str(l + 1), ffield) # self.config.write(fd) # fd.close() def save_project(self): """ save Simulation files in a zipfile Simulation files are .ini files which are saved in a dedicated directory basename/ini in the Project tree """ root = Tkinter.Tk() zipfileName = tkFileDialog.asksaveasfilename( parent=root, filetypes=[("zipped file", "zip")], title="Save Project", ) pyu.zipd(basename, zipfileName) root.withdraw() print "Current project saved in", zipfileName def load_project(self): """ load Simulation files from a zipfile Simulation files are .ini files which are saved in a dedicated directory basename/ini in the Project tree """ root = Tkinter.Tk() zipfileName = tkFileDialog.askopenfile(parent=root, mode='rb', title='Choose a project') dirname = tkFileDialog.askdirectory(parent=root, initialdir=basename, title='Please select a directory', mustexist=0) pyu.unzipd(dirname, zipfileName) root.withdraw() print "Current project loaded in", dirname def choose(self): """ Choose a simulation file in simuldir """ import tkFileDialog as FD fichsimul = FD.askopenfilename(filetypes=[("Fichiers simul ", "*.simul"), ("All", "*")], title="Please choose a simulation file", initialdir=simuldir) self.filesimul = pyu.getshort(fichsimul) self.load() def load(self, _filesimul): """ load a simulation configuration file each transmiter simulation results in the creation of an .ini file with the following sections related to the results obtained for different receivers Parameters ---------- _filesimul : file in the simul directory of the Project """ self.filesimul = _filesimul filesimul = pyu.getlong(self.filesimul, "ini") self.config.read(filesimul) sections = self.config.sections() try: _filetx = self.config.get("files", "tx") except: raise NameError('Error in section tx from ' + _filesimul) try: _filerx = self.config.get("files", "rx") except: raise NameError('Error in section rx from ' + _filesimul) try: _fileini = self.config.get("files", "struc") except: raise NameError('Error in section struc from ' + _fileini) try: _fileanttx = self.config.get("files", "txant") except: raise NameError('Error in section txant from ' + _filesimul) try: _fileantrx = self.config.get("files", "rxant") except: raise NameError('Error in section rxant from ' + _filesimul) try: self.tx = RadioNode(name='', typ='tx', _fileini=_filetx, _fileant=_fileanttx) self.rx = RadioNode(name='', typ='rx', _fileini=_filerx, _fileant=_fileantrx) except: raise NameError('Error during Radionode load') # # Load Layout # try: self.L = Layout(_fileini) except: raise NameError('Layout load error') # # Frequency base # if "frequency" in sections: try: self.fGHz = np.linspace( float(self.config.getfloat("frequency", "fghzmin")), float(self.config.getfloat("frequency", "fghzmax")), int(self.config.getint("frequency", "nf")), endpoint=True) except: raise NameError('Error in section frequency from ' + _filesimul) # update .freq file in tud directory filefreq = pyu.getlong(self.filefreq, pstruc['DIRTUD']) fd = open(filefreq, "w") chaine = self.config.get("frequency", "fghzmin") + ' ' + \ self.config.get("frequency", "fghzmax") + ' ' + \ self.config.get("frequency", "nf") fd.write(chaine) fd.close # # Simulation Progress # # self.output = {} # if "output" in sections: # for itx in self.config.options("output"): # _filename = self.config.get("output", itx) # self.dout[int(itx)] = _filename # filename = pyu.getlong(_filename, "output") # output = ConfigParser.ConfigParser() # output.read(filename) # secout = output.sections() # self.dtra[int(itx)] = {} # self.dtud[int(itx)] = {} # self.dtang[int(itx)] = {} # self.drang[int(itx)] = {} # self.dtauk[int(itx)] = {} # self.dfield[int(itx)] = {} # self.dcir[int(itx)] = {} # if "launch" in secout: # self.progress = 1 # keys_launch = output.options("launch") # for kl in keys_launch: # self.dlch[int(kl)] = output.get("launch", kl) # if "trace" in secout: # self.progress = 2 # keys_tra = output.options("trace") # for kt in keys_tra: # self.dtra[int(itx)][int(kt)] = output.get("trace", kt) # # if "tang" in secout: # self.progress = 3 # keys_tang = output.options("tang") # for kt in keys_tang: # self.dtang[int(itx)][int(kt)] = output.get("tang", kt) # self.drang[int(itx)][int(kt)] = output.get("rang", kt) # self.dtud[int(itx)][int(kt)] = output.get("tud", kt) # if "field" in secout: # self.progress = 4 # keys_field = output.options("field") # for kt in keys_field: # self.dfield[int(itx)][int(kt)] = output.get( # "field", kt) # self.dtauk[int(itx)][int(kt)] = output.get("tauk", kt) # if "cir" in secout: # self.progress = 5 # keys_cir = output.options("cir") # for kt in keys_cir: # self.dcir[int(itx)][int(kt)] = output.get("cir", kt) # # self.output[int(itx)] = output # # Waveform section # self.wav = wvf.Waveform() self.wav.read(self.config) def layout(self, _filestruc): """ load a layout in the simulation oject Parameters ---------- _filestruc : string short file name of the Layout object Examples -------- >>> from pylayers.simul.simulem import * >>> S = Simul() >>> S.layout('defstr.ini') """ self.filestr = _filestruc self.L = Layout(_filestruc) # update config self.config.set("files", "struc", self.filestr) self.save() #def show(self, itx=[-1], irx=[-1], furniture=True, s=8, c='b', traj=False, num=False,fig=[],ax=[]): def show(self, **kwargs): """ show simulation Parameters ----------- itx : list of tx indices irx : list of rx indices furniture : boolean for METALIC furniture display s : scale fir scatter plot (default 8) c : color for scatter plot (default 'b') traj : boolean (def False) num : boolean (def False) display a number Examples -------- >>> import matplotlib.pyplot as plt >>> from pylayers.simul.simulem import * >>> S = Simul() >>> S.load('w1.ini') >>> S.L.loadfur('FurW1.ini') >>> S.show() >>> plt.show() """ defaults = { 'itx': [-1], 'irx': [-1], 'furniture': True, 's': 8, 'c': 'b', 'num': False, 'fig': [], 'ax': [], 'aw': False } for k in defaults: if k not in kwargs: kwargs[k] = defaults[k] st = k + "=kwargs['" + k + "']" exec(st) if type(itx) == int: itx = [itx] if type(irx) == int: irx = [irx] if fig == []: fig = plt.gcf() if ax == []: ax = fig.gca() #self.L.display['scaled']=False fig, ax = self.L.showG('s', fig=fig, ax=ax, aw=aw) # if furniture: if 'lfur' in self.L.__dict__: for fur in self.L.lfur: if fur.Matname == 'METAL': fur.show(fig, ax) else: print "Warning : no furniture file loaded" if irx[0] == -1: ax.scatter(self.rx.position[0, :], self.rx.position[1, :], c='b', s=s, alpha=0.5) #ax.scatter(self.rx.position[0,0],self.rx.position[0,1],c='k',s=s,linewidth=0) #ax.scatter(self.rx.position[1,0],self.rx.position[1,1],c='b',s=s,linewidth=0) #ax.scatter(self.rx.position[2,0],self.rx.position[2,1],c='g',s=s,linewidth=0) #ax.scatter(self.rx.position[3,0],self.rx.position[3,1],c='c',s=s,linewidth=0) else: for k in irx: ax.scatter(self.rx.position[0, k - 1], self.rx.position[1, k - 1], c='b', s=s, alpha=0.5) if num: ax.text(self.rx.position[0, k - 1], self.rx.position[1, k - 1], str(k), color='blue') if itx[0] == -1: ax.scatter(self.tx.position[0, :], self.tx.position[1, :], c='r', s=s) else: if traj: cpt = 1 for k in itx: ax.scatter(self.tx.position[0, k - 1], self.tx.position[1, k - 1], c=c, s=s, linewidth=0) if num: if traj: ax.text(self.tx.position[0, k - 1], self.tx.position[1, k - 1], str(cpt), color='black') cpt = cpt + 1 else: ax.text(self.tx.position[0, k - 1], self.tx.position[1, k - 1], str(k), color='black') return (fig, ax) #for k in range(self.tx.N): # ax.text(self.tx.position[0,k],self.tx.position[1,k],str(k+1),color='black') # ax.scatter(self.tx.position[0,:],self.tx.position[0,:], #for k in range(self.rx.N): # ax.text(self.rx.position[0,k],self.rx.position[1,k],str(k),color='black') def PL(self, itx): """ plot Path Loss itx """ td = [] tEa = [] tEo = [] for irx in self.dcir[itx].keys(): d = self.delay(itx, irx) * 0.3 cira, ciro = self.loadcir(itx, irx) td.append(d) tEa.append(Ea) tEo.append(Eo) plt.semilogx(td, 10 * np.log10(tEa), 'xr') plt.semilogx(td, 10 * np.log10(tEo), 'xb') plt.show() return td, tEa, tEo def eval(self, **kwrgs): """ """ for kt in range(Nb_tx): tx = np.array([ self.tx.position[0, kt + 1], self.tx.position[1, kt + 1], self.tx.position[2, kt + 1] ]) link.a = tx for kr in range(Nb_Run): rx = np.array([ S.rx.position[0, Run], S.rx.position[1, Run], S.rx.position[2, Run] ]) link.b = rx tic = time.clock() ak, tk = link.eval(verbose=False, diffraction=True) toc = time.clock() ciro = link.H.applywav(wav.sfg) cir = bs.TUsignal(ciro.x, np.squeeze(np.sum(ciro.y, axis=0))) tac = time.clock() tcir[Run] = cir print toc - tic, tac - toc def evalcir(self, cutoff=4, algo='new'): """ Parameters ---------- S tx rx wav cutoff """ crxp = -1 ctxp = -1 tcir = {} tx = self.tx.position Ntx = len(tx[0]) rx = self.rx.position Nrx = len(rx[0]) #for kt in range(1,Ntx-1): #print kt+1 kt = 0 tcir[kt] = {} t = np.array([ self.tx.position[0, kt], self.tx.position[1, kt], self.tx.position[2, kt] ]) for kr in range(Nrx): if (np.mod(kr, 10) == 0): print kr + 1 r = np.array([ self.rx.position[0, kr], self.rx.position[1, kr], self.rx.position[2, kr] ]) ctx = self.L.pt2cy(t) crx = self.L.pt2cy(r) if (ctx <> ctxp) | (crx <> crxp): Si = signature.Signatures(self.L, ctx, crx) ctxp = ctx crxp = crx Si.run4(cutoff=cutoff, algo=algo) r2d = Si.rays(t, r) #r2d.show(S.L) r3d = r2d.to3D(self.L) r3d.locbas(self.L) r3d.fillinter(self.L) Ct = r3d.eval(self.fGHz) sca = Ct.prop2tran(self.tx.A, self.rx.A) cir = sca.applywavB(self.wav.sfg) tcir[kt][kr] = cir return (tcir) def loadcir(self, itx, irx): """ Parameters ---------- itx : Tx index irx : Rx index Returns ------- cir(itx,irx) """ _filecir = self.dcir[itx][irx] + '.mat' ext = str(itx) if len(ext) == 1: ext = '00' + ext if len(ext) == 2: ext = '0' + ext filecir = pyu.getlong(_filecir, pstruc['DIRCIR'] + '/Tx' + ext) D = spio.loadmat(filecir) kxa = 'ta' + str(irx) kya = 'cira' + str(irx) kxo = 'to' + str(irx) kyo = 'ciro' + str(irx) cira = bs.TUsignal(D[kxa], D[kya][:, 0]) ciro = bs.TUsignal(D[kxo], D[kyo][:, 0]) return (cira, ciro) def pltcir(self, itx=1, irx=1, mode='linear', noise=False, color='b', format='a', fig=[], ax=[]): """ plot Channel Impulse Response Parameters ---------- itx : Tx index irx : Rx index mode : str {'linear','dB'} noise : boolean color : string default 'b' >>> from pylayers.simul.simulem import * >>> S = Simul() >>> S.load('where2.ini') >>> S.run(1,1) >>> S.pltcir(1,1,mode='linear',noise=False,color='k') """ if fig == []: fig = plt.gcf() #if ax==[]: # ax = fig.gca() _filecir = self.dcir[itx][irx] + '.mat' filecir = pyu.getlong(_filecir, pstruc['DIRCIR'] + '/Tx' + str('%0.3d' % itx)) D = spio.loadmat(filecir) ax = fig.add_subplot('211') fig, ax = self.show(itx, irx, fig=fig, ax=ax) ax = fig.add_subplot('212') if 'a' in format: kxa = 't' kya = 'cir' ta = D[kxa] Tobs = ta[-1] - ta[0] te = ta[1] - ta[0] if noise: na = bs.Noise(Tobs + te, 1. / te) naf = na.gating(4.493, 0.5) cira = bs.TUsignal(ta, D[kya][:, 0]) if 'o' in format: kxo = 'to' + str(irx) kyo = 'ciro' + str(irx) to = D[kxo] Tobs = to[-1] - to[0] te = to[1] - to[0] if noise: no = bs.Noise(Tobs + te, 1. / te) nof = no.gating(4.493, 0.5) ciro = bs.TUsignal(to, D[kyo][:, 0]) if mode == 'linear': #plt.plot(ta,naf.y,color='k',label='Noise') plt.plot(ta, D[kya], label='Rx ' + str(irx), color=color) plt.xlabel('Time (ns)') '''if noise: naf.plot(col='k') cira.plot(col=color)''' else: '''if noise: naf.plotdB(col='k') cira.plotdB()''' plt.plot(ta, 20 * np.log10(abs(D[kya])), label='Rx ' + str(irx), color=color) plt.xlabel('Time (ns)') # plt.legend() plt.show() #plt.savefig('Tx'+str(itx),format=pdf,dpi=300) def scatter(self, itx, irx, values, cmap=plt.cm.gray, s=30, spl=221, title='', vaxis=((-30, 10, 2, 18)), vmin=0, vmax=1, colbool=False, cblabel='dB'): """ Parameters ---------- itx irx values cmap s spl title vaxis vmin vmax colbool clabel """ fig = plt.gcf() ax = fig.add_subplot(spl) xtx = self.tx.position[itx, 0] ytx = self.tx.position[itx, 1] xrx = self.rx.position[irx, 0] yrx = self.rx.position[irx, 1] self.L.display['title'] = title self.L.showGs(ax) for furk in siradel.siradel_furniture.keys(): fur = siradel.siradel_furniture[furk] if fur.Matname == 'METAL': fur.show(fig, ax) #self.show(furniture=True) plt.axis(vaxis) b1 = ax.scatter(xtx, ytx, s=s, c=values, cmap=cmap, linewidths=0, vmin=vmin, vmax=vmax) ax.scatter(xrx, yrx, s=30, c='b', linewidths=0) if colbool: cb = colorbar(b1) cb.set_label(cblabel, fontsize=14) return (b1) def info(self, itx=[], irx=[]): """ display simulation information Parameters ---------- itx : Tx index irx : Rx index """ print self.filesimul print '------------------------------------------' try: print "Layout Info : \n", self.L.info() except: print "provide a Layout in the simulation : S.L " print ">>> S.layout(filename.str) " print "or " print ">>> S.layout(filename.str2 " print "or " print ">>> S.layout(filename.str,filematini,filematini) " print "default files exists for filematini and fileslabini " return try: print "Tx Info :\n", self.tx.info() except: print "provide a tx in the simulation : S.tx " return try: print "Rx Info :\n", self.rx.info() except: print "provide a rx in the simulation : S.rx " return # print "Tx : ",self.tx.points[itx] print "Rx : ", self.rx.points[itx] print "Delay (ns) :", self.delay(itx, irx) print "Distance (m) :", 0.3 / self.delay(itx, irx) print "" if itx in self.dlch.keys(): print "-----" print "Launching " print "-----" print " ", self.dlch[itx] if irx in self.dtra[itx].keys(): print "-----" print "Tracing " print "-----" print " ", self.dtra[itx][irx] gr = GrRay3D() gr.load(self.dtra[itx][irx], self.L) gr.info() if irx in self.dtud[itx].keys(): print "-----" print "Tud parameters " print "-----" print " ", self.dtud[itx][irx] print " ", self.dtang[itx][irx] print " ", self.drang[itx][irx] gt = GrRay3D.GrRayTud() gt.load(self.dtud[itx][irx], self.dtang[itx][irx], self.drang[itx][irx], self.sl) if irx in self.dtauk[itx].keys(): print self.dtauk[itx][irx] print self.dfield[itx][irx] VC = self.VC(itx, irx) if irx in self.dcir[itx].keys(): print self.dcir[itx][irx] def info2(self): for i, j in enumerate(self.__dict__.keys()): print j, ':', self.__dict__.values()[i] def filtray(self, itx, irx, tau0, tau1, col='b'): """ filter rays Parameters ---------- itx : irx : tau0 : tau1 : col : Display ray and nstr """ gr = GrRay3D() gr.load(self.dtra[itx][irx], self.L) self.L.display['Thin'] = True self.L.display['Node'] = False self.L.display['NodeNum'] = False self.L.display['EdgeNum'] = False plt.axis('scaled') #self.L.show(fig,ax,nodelist,seglist) self.L.showGs() delays = gr.delay() rayset = np.nonzero((delays >= tau0) & (delays <= tau1))[0] fig = plt.gcf() ax = fig.get_axes()[0] gr.show(ax, rayset, col=col, node=False) plt.title('Tx' + str(itx) + '-Rx' + str(irx) + ' : ' + str(tau0) + ' < tau < ' + str(tau1)) def showray(self, itx, irx, iray=np.array([]), fig=[], ax=[]): """ show layout and rays for a radio link Parameters ---------- itx : tx index irx : rx index iray : list of rays to be displayed ndarray """ #if ax==[]: # fig = plt.figure() # ax = fig.add_subplot('111') gr = GrRay3D() gr.load(self.dtra[itx][irx], self.L) if len(iray == 1): ray = gr.ray3d[iray[0]] nstr = ray.nstr[1:-1] uneg = np.nonzero(nstr < 0) upos = np.nonzero((nstr > 0) & (nstr <= self.L.Ne)) uceil = np.nonzero(nstr == self.L.Ne + 1) ufloor = np.nonzero(nstr == self.L.Ne + 2) #seglist = nstr[upos[0]]-1 #nodelist = -nstr[uneg[0]]-1 #seglist2 = S.L.segpt(nodelist) self.L.display['Thin'] = True self.L.display['Node'] = False self.L.display['NodeNum'] = False self.L.display['EdgeNum'] = False #self.L.show(fig,ax) #print ray.nn #print len(ray.nstr) #print ray.nstr #print nodelist #print seglist #print seglist2 #seglist = hstack((seglist,seglist2)) self.L.display['Node'] = False self.L.display['Thin'] = False self.L.display['NodeNum'] = True self.L.display['EdgeNum'] = True plt.axis('scaled') #self.L.show(fig,ax,nodelist,seglist) fig, ax = self.L.showGs(show=False) gr.show(ax, iray, col='b', node=False) if len(iray) == 1: plt.title(str(nstr)) else: plt.title('Tx' + str(itx) + '-Rx' + str(irx) + ' ' + str(min(iray)) + ' ' + str(max(iray))) def show3l(self, itx, irx): """ geomview display of a specific link g = S.show3l(itx,irx) Parameters ---------- itx transmitter index irx receiver index """ filetra = self.dtra[itx][irx] gr = GrRay3D() gr.load(filetra, self.L) gr.show3() return (gr) def _show3(self, rays=[], newfig=False, **kwargs): """ display of the simulation configuration using Mayavi Parameters ---------- rays: Ray3d object : display the rays of the simulation newfig : boolean (default : False) kwargs of Rays.show3() see also -------- pylayers.gis.layout pylayers.antprop.antenna pylayers.antprop.rays """ Atx = self.tx.A Arx = self.rx.A Ttx = self.tx.orientation Trx = self.rx.orientation ptx = self.tx.position prx = self.rx.position self.L._show3(newfig=False, opacity=0.7) Atx._show3(T=Ttx.reshape(3, 3), po=ptx, title=False, colorbar=False, newfig=False) Arx._show3(T=Trx.reshape(3, 3), po=prx, title=False, colorbar=False, newfig=False) if rays != []: rays._show3(**kwargs) def show3(self, rays=[], **kwargs): """ geomview display of the simulation configuration Parameters ---------- centered : boolean center the scene if True bdis : boolean display local basis """ try: self.tx.save() except: print('tx set is no defined') try: self.rx.save() except: print('rx set is no defined') _filename = self.filesimul.replace('.ini', '.off') filename = pyu.getlong(_filename, pstruc['DIRGEOM']) fo = open(filename, "w") fo.write("LIST\n") try: sttx = "{<" + self.tx.filegeom + "}\n" except: sttx = "\n" try: strx = "{<" + self.rx.filegeom + "}\n" except: strx = "\n" try: stst = "{<" + self.L.filegeom + "}\n" except: stst = "\n" fo.write(sttx) fo.write(strx) fo.write(stst) fo.write("{</usr/share/geomview/geom/xyz.vect}\n") if rays != []: kwargs['bdis'] = False kwargs['L'] = self.L kwargs['centered'] = False fo.write("{<" + rays.show3(**kwargs) + "}") fo.close() command = "geomview -nopanel -b 1 1 1 " + filename + " 2>/dev/null &" os.system(command) def run(self, link, cirforce=True, verbose=False, cutoff=4): """ run the simulation for 1 tx and a set of rx Parameters ---------- itx : tx index srx : list of rx index cirforce : boolean Warnings -------- index point start with 1 Example ------- >>> from pylayers.simul.simulem import * >>> itx = 1 >>> srx = [1,2,3] >>> S = Simul() >>> S.load('where2.ini') >>> out = S.run(itx,srx) """ # get file prefix dl = DLink(force=True, L=self.L, fGHz=self.fGHz, verbose=False) prefix = self.filesimul.replace('.ini', '') lsig = [] self.chan = {} for k, il in enumerate(link): tx = self.tx.points[il[0]] rx = self.rx.points[il[1]] ctx = self.L.pt2cy(tx) crx = self.L.pt2cy(rx) _filecir = prefix + '-cir-' + str(k) + '-' + str(link) + '-' + str( (ctx, crx)) D = {} D['Tx'] = tx D['Rx'] = rx dl.a = tx dl.b = rx ak, tauk = dl.eval(verbose=False, diffraction=True) self.chan[k] = dl #cira = dl.H.applywavB(self.wav.sf) #cira = dl.H.applywavB(self.wav.sfg) #D['t'] = cira.x #D['cir'] = cira.y #filename = pyu.getlong(_filecir, 'output') #spio.savemat(filename, D) def delay(self, itx, irx): """ calculate LOS link delay Parameters ---------- itx irx Returns ------- delay : float delay in ns """ tx = self.tx.points[itx] rx = self.rx.points[irx] df = tx - rx dist = np.sqrt(np.dot(df, df)) return (dist / 0.3)
class Coverage(object): """ Handle Layout Coverage Methods ------- create grid() create a uniform grid for evaluating losses cover() run the coverage calculation showPower() display the map of received power showLoss() display the map of losses Attributes ---------- All attributes are read from fileini ino the ini directory of the current project _fileini default coverage.ini L : a Layout model : a pylayers.network.model object. nx : number of point on x ny : number of point on y tx : transmitter position txpe : transmitter power emmission level show : boolean for automatic display power map """ def __init__(self,_fileini='coverage.ini'): self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini,pstruc['DIRSIMUL'])) self.plm = dict(self.config.items('pl_model')) self.layoutopt = dict(self.config.items('layout')) self.gridopt = dict(self.config.items('grid')) self.txopt = dict(self.config.items('tx')) self.rxopt = dict(self.config.items('rx')) self.showopt = dict(self.config.items('show')) self.L = Layout(self.layoutopt['filename']) self.model = PLSmodel(f=eval(self.plm['fghz']), rssnp=eval(self.plm['rssnp']), d0=eval(self.plm['d0']), sigrss=eval(self.plm['sigrss'])) self.nx = eval(self.gridopt['nx']) self.ny = eval(self.gridopt['ny']) self.mode = eval(self.gridopt['full']) self.boundary = eval(self.gridopt['boundary']) # transitter section self.fGHz = eval(self.txopt['fghz']) self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) self.ptdbm = eval(self.txopt['ptdbm']) self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section self.rxsens = eval(self.rxopt['sensitivity']) kBoltzmann = 1.3806503e-23 self.bandwidthmhz = eval(self.rxopt['bandwidthmhz']) self.temperaturek = eval(self.rxopt['temperaturek']) self.noisefactordb = eval(self.rxopt['noisefactordb']) Pn = (10**(self.noisefactordb/10.)+1)*kBoltzmann*self.temperaturek*self.bandwidthmhz*1e3 self.pndbm = 10*np.log10(Pn)+60 self.show = str2bool(self.showopt['show']) try: self.L.Gt.nodes() except: pass try: self.L.dumpr('t') except: self.L.buildGt() self.L.dumpw('t') self.creategrid(full=self.mode,boundary=self.boundary) def __repr__(self): st='' st= st+ 'tx :'+str(self.txopt) + '\n' st= st+ 'rx :'+str(self.rxopt) + '\n' def creategrid(self,full=True,boundary=[]): """ create a grid Parameters ---------- full : boolean default (True) use all the layout area boundary : (xmin,ymin,xmax,ymax) if full is False the boundary is used """ if full: mi=np.min(self.L.Gs.pos.values(),axis=0)+0.01 ma=np.max(self.L.Gs.pos.values(),axis=0)-0.01 else: mi = np.array([boundary[0],boundary[1]]) ma = np.array([boundary[2],boundary[3]]) x=np.linspace(mi[0],ma[0],self.nx) y=np.linspace(mi[1],ma[1],self.ny) self.grid=np.array((list(np.broadcast(*np.ix_(x, y))))) def cover(self): """ start the coverage calculation Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showPr() """ self.Lwo,self.Lwp,self.Edo,self.Edp = Loss0_v2(self.L,self.grid,self.model.f,self.tx) self.freespace = PL(self.grid,self.model.f,self.tx) self.prdbmo = self.ptdbm - self.freespace - self.Lwo self.prdbmp = self.ptdbm - self.freespace - self.Lwp self.snro = self.prdbmo - self.pndbm self.snrp = self.prdbmp - self.pndbm def showEd(self,polarization='o'): """ show direct path excess of delay map Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showEdo() """ fig=plt.figure() fig,ax=self.L.showGs(fig=fig) l=self.grid[0,0] r=self.grid[-1,0] b=self.grid[0,1] t=self.grid[-1,-1] if polarization=='o': cov=ax.imshow(self.Edo.reshape((self.nx,self.ny)).T, extent=(l,r,b,t), origin='lower') titre = "Map of LOS excess delay, polar orthogonal" if polarization=='p': cov=ax.imshow(self.Edp.reshape((self.nx,self.ny)).T, extent=(l,r,b,t), origin='lower') titre = "Map of LOS excess delay, polar parallel" ax.scatter(self.tx[0],self.tx[1],linewidth=0) ax.set_title(titre) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) clb = fig.colorbar(cov,cax) clb.set_label('excess delay (ns)') if self.show: plt.show() def showPower(self,rxsens=True,nfl=True,polarization='o'): """ show the map of received power Parameters ---------- rxsens : bool clip the map with rx sensitivity set in self.rxsens nfl : bool clip the map with noise floor set in self.pndbm polarization : string 'o'|'p' Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showPower() """ fig=plt.figure() fig,ax=self.L.showGs(fig=fig) l=self.grid[0,0] r=self.grid[-1,0] b=self.grid[0,1] t=self.grid[-1,-1] if polarization=='o': prdbm=self.prdbmo if polarization=='p': prdbm=self.prdbmp # tCM = plt.cm.get_cmap('jet') # tCM._init() # alphas = np.abs(np.linspace(.0,1.0, tCM.N)) # tCM._lut[:-3,-1] = alphas title='Map of received power - Pt = '+str(self.ptdbm)+' dBm' cdict = { 'red' : ((0., 0.5, 0.5), (1., 1., 1.)), 'green': ((0., 0.5, 0.5), (1., 1., 1.)), 'blue' : ((0., 0.5, 0.5), (1., 1., 1.)) } #generate the colormap with 1024 interpolated values my_cmap = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024) if rxsens : ### values between the rx sensitivity and noise floor mcPrf = np.ma.masked_where((prdbm > self.rxsens) & (prdbm < self.pndbm),prdbm) cov1 = ax.imshow(mcPrf.reshape((self.nx,self.ny)).T, extent=(l,r,b,t),cmap = my_cmap, vmin=self.rxsens,origin='lower') ### values above the sensitivity mcPrs = np.ma.masked_where(prdbm < self.rxsens,prdbm) cov = ax.imshow(mcPrs.reshape((self.nx,self.ny)).T, extent=(l,r,b,t), cmap = 'jet', vmin=self.rxsens,origin='lower') title=title + '\n gray : Pr (dBm) < %.2f' % self.rxsens + ' dBm' else : cov=ax.imshow(prdbm.reshape((self.nx,self.ny)).T, extent=(l,r,b,t), cmap = 'jet', vmin=self.pndbm,origin='lower') if nfl: ### values under the noise floor ### we first clip the value below he noise floor cl = np.nonzero(prdbm<=self.pndbm) cPr = prdbm cPr[cl] = self.pndbm mcPruf = np.ma.masked_where(cPr > self.pndbm,cPr) cov2 = ax.imshow(mcPruf.reshape((self.nx,self.ny)).T, extent=(l,r,b,t),cmap = 'binary', vmax=self.pndbm,origin='lower') title=title + '\n white : Pr (dBm) < %.2f' % self.pndbm + ' dBm' ax.scatter(self.tx[0],self.tx[1],s=10,linewidth=0) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) clb = fig.colorbar(cov,cax) clb.set_label('Power (dBm)') if self.show: plt.show() def showTransistionRegion(self,polarization='o'): """ Notes ----- See : Analyzing the Transitional Region in Low Power Wireless Links Marco Zuniga and Bhaskar Krishnamachari Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showTransitionRegion() """ frameLength = self.framelengthbytes PndBm = self.pndbm gammaU = 10*np.log10(-1.28*np.log(2*(1-0.9**(1./(8*frameLength))))) gammaL = 10*np.log10(-1.28*np.log(2*(1-0.1**(1./(8*frameLength))))) PrU = PndBm + gammaU PrL = PndBm + gammaL fig=plt.figure() fig,ax = self.L.showGs(fig=fig) l = self.grid[0,0] r = self.grid[-1,0] b = self.grid[0,1] t = self.grid[-1,-1] if polarization=='o': prdbm=self.prdbmo if polarization=='p': prdbm=self.prdbmp zones = np.zeros(len(prdbm)) uconnected = np.nonzero(prdbm>PrU)[0] utransition = np.nonzero((prdbm < PrU)&(prdbm > PrL))[0] udisconnected = np.nonzero(prdbm < PrL)[0] zones[uconnected] = 1 zones[utransition] = (prdbm[utransition]-PrL)/(PrU-PrL) cov = ax.imshow(zones.reshape((self.nx,self.ny)).T, extent=(l,r,b,t),cmap = 'BuGn',origin='lower') title='PDR region' ax.scatter(self.tx[0],self.tx[1],linewidth=0) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(cov,cax) if self.show: plt.show() def showLoss(self,polarization='o'): """ show losses map Parameters ---------- polarization : string 'o'|'p'|'both' Examples -------- .. plot:: :include-source: >>> from pylayers.antprop.coverage import * >>> C = Coverage() >>> C.cover() >>> C.showLoss(polarization='o') >>> C.showLoss(polarization='p') """ fig = plt.figure() fig,ax=self.L.showGs(fig=fig) l=self.grid[0,0] r=self.grid[-1,0] b=self.grid[0,1] t=self.grid[-1,-1] if polarization=='o': cov = ax.imshow(self.Lwo.reshape((self.nx,self.ny)).T, extent=(l,r,b,t), origin='lower') title = ('Map of losses, orthogonal (V) polarization') if polarization=='p': cov = ax.imshow(self.Lwp.reshape((self.nx,self.ny)).T, extent=(l,r,b,t), origin='lower') title = ('Map of losses, parallel (H) polarization') ax.scatter(self.tx[0],self.tx[1],linewidth=0) ax.set_title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(cov,cax) if self.show: plt.show()
def __init__(self, _fileini="coverage.ini"): """ object constructor Parameters ---------- _fileini : string name of the configuration file Notes ----- Coverage is described in an ini file. Default file is coverage.ini and is placed in the ini directory of the current project. """ self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong(_fileini, pstruc["DIRSIMUL"])) self.layoutopt = dict(self.config.items("layout")) self.gridopt = dict(self.config.items("grid")) self.apopt = dict(self.config.items("ap")) self.rxopt = dict(self.config.items("rx")) self.showopt = dict(self.config.items("show")) # get the Layout self.L = Layout(self.layoutopt["filename"]) # get the receiving grid self.nx = eval(self.gridopt["nx"]) self.ny = eval(self.gridopt["ny"]) self.mode = self.gridopt["mode"] self.boundary = eval(self.gridopt["boundary"]) self.filespa = self.gridopt["file"] # # create grid # self.creategrid(mode=self.mode, boundary=self.boundary, _fileini=self.filespa) self.dap = {} for k in self.apopt: kwargs = eval(self.apopt[k]) ap = std.AP(**kwargs) self.dap[eval(k)] = ap self.fGHz = np.array([]) # self.fGHz = eval(self.txopt['fghz']) # self.tx = np.array((eval(self.txopt['x']),eval(self.txopt['y']))) # self.ptdbm = eval(self.txopt['ptdbm']) # self.framelengthbytes = eval(self.txopt['framelengthbytes']) # receiver section # self.rxsens = eval(self.rxopt['sensitivity']) self.temperaturek = eval(self.rxopt["temperaturek"]) self.noisefactordb = eval(self.rxopt["noisefactordb"]) # show section self.bshow = str2bool(self.showopt["show"]) try: self.L.Gt.nodes() except: pass try: self.L.dumpr() except: self.L.build() self.L.dumpw()
r""" =========================================== 8 Random Layout =========================================== This example display randomly N layout from the set of all available Layout file The file extension is .lay """ from pylayers.gis.layout import Layout import matplotlib.pyplot as plt import random import warnings warnings.filterwarnings("error") L = Layout() lL = L.ls() N = 8 for tL in random.sample(lL,N): if 'Munich' not in tL: L = Layout(tL,bbuild=0,bgraphs=0) f,a = L.showG('s') plt.title(tL,fontsize=32) plt.show()
class Simul(SimulationRT): # Sympy 2 """ Attributes ---------- config : config parser instance sim_opt : dictionary of configuration option for simulation ag_opt : dictionary of configuration option for agent lay_opt : dictionary of configuration option for layout meca_opt : dictionary of configuration option for mecanic net_opt : dictionary of configuration option for network loc_opt : dictionary of configuration option for localization save_opt : dictionary of configuration option for save sql_opt : dictionary of configuration option for sql Parameters ---------- self.lAg : list of Agent(Object) list of agents involved in simulation self.L : Layout Layout used in simulation Note ---- All the previous dictionnary are obtained from the chosen simulnet.ini file in the project directory """ def __init__(self): SimulationRT.__init__(self) # Sympy 2 # sympy.RealtimeEnvironment.__init__(self) #simpy 3 self.initialize() self.config = ConfigParser.ConfigParser() filename = pyu.getlong('simulnet.ini', pstruc['DIRSIMUL']) self.config.read(filename) self.sim_opt = dict(self.config.items('Simulation')) self.lay_opt = dict(self.config.items('Layout')) self.meca_opt = dict(self.config.items('Mechanics')) self.net_opt = dict(self.config.items('Network')) self.loc_opt = dict(self.config.items('Localization')) self.save_opt = dict(self.config.items('Save')) self.sql_opt = dict(self.config.items('Mysql')) self.seed = eval(self.sim_opt['seed']) self.traj = Trajectories() self.verbose = str2bool(self.sim_opt['verbose']) if str2bool(self.net_opt['ipython_nb_show']): self.verbose = False self.roomlist = [] self.finish = False self.create() def __repr__(self): s = 'Simulation information' + '\n----------------------' s = s + '\nLayout: ' + self.lay_opt['filename'] s = s + '\nSimulation duration: ' + str(self.sim_opt['duration']) s = s + '\nRandom seed: ' + str(self.sim_opt['seed']) s = s + '\nSave simulation: ' + self.save_opt['savep'] s = s + '\n\nUpdate times' + '\n-------------' s = s + '\nMechanical update: ' + \ str(self.meca_opt['mecanic_update_time']) s = s + '\nNetwork update: ' + str(self.net_opt['network_update_time']) s = s + '\nLocalization update: ' + self.net_opt['communication_mode'] s = s + '\n\nAgents => self.lAg[i]' + '\n------' s = s + '\nNumber of agents :' + str(len(self.lAg)) s = s + '\nAgents IDs: ' + \ str([self.lAg[i].ID for i in range(len(self.lAg))]) s = s + '\nAgents names: ' + \ str([self.lAg[i].name for i in range(len(self.lAg))]) s = s + '\nDestination of chosen agents: ' + \ self.meca_opt['choose_destination'] s = s + '\n\nNetwork' + '\n-------' s = s + '\nNodes per wstd: ' + str(self.net.wstd) s = s + '\n\nLocalization' + '\n------------' s = s + '\nLocalization enable: ' + self.loc_opt['localization'] s = s + '\nPostion estimation methods: ' + self.loc_opt['method'] return s def create_layout(self): """ create Layout in Simpy the_world thanks to Tk backend """ _filename = self.lay_opt['filename'] self.L = Layout(_filename) self.L.build() self.L.buildGr() self.L.buildGw() self.the_world = world() try: self.L.dumpr() print 'Layout graphs are loaded from ' + basename + '/struc/ini' except: #self.L.sl = sl # self.L.loadGr(G1) print 'This is the first time the layout file is used\ Layout graphs are curently being built, it may take few minutes.' self.L.build() self.L.dumpw() # # Create Layout # walls = self.L.thwall(0, 0) for wall in walls: for ii in range(0, len(wall) - 1): self.the_world.add_wall(wall[ii], wall[ii + 1]) def create_agent(self): """ create simulation's Agents ..todo:: change lAg list to a dictionnary ( modification in show.py too) """ self.lAg = [] agents = [] Cf = ConfigParser.ConfigParser() Cf.read(pyu.getlong('agent.ini', 'ini')) agents = eval(dict(Cf.items('used_agent'))['list']) for i, ag in enumerate(agents): ag_opt = dict(Cf.items(ag)) self.lAg.append( Agent(ID=ag_opt['id'], name=ag_opt['name'], typ=ag_opt['typ'], color=eval(ag_opt['color']), pdshow=str2bool(self.meca_opt['pdshow']), pos=np.array(eval(ag_opt['pos'])), roomId=int(ag_opt['roomid']), froom=eval(ag_opt['froom']), meca_updt=float(self.meca_opt['mecanic_update_time']), wait=float(ag_opt['wait']), cdest=eval(self.meca_opt['choose_destination']), loc=str2bool(self.loc_opt['localization']), loc_updt=float(self.loc_opt['localization_update_time']), loc_method=eval(self.loc_opt['method']), L=self.L, network=str2bool(self.net_opt['network']), net=self.net, epwr=dict([(eval( (ag_opt['wstd']))[ep], eval((ag_opt['epwr']))[ep]) for ep in range(len(eval((ag_opt['wstd'])))) ]), sens=dict([(eval( (ag_opt['wstd']))[ep], eval( (ag_opt['sensitivity']))[ep]) for ep in range(len(eval((ag_opt['wstd'])))) ]), world=self.the_world, wstd=eval(ag_opt['wstd']), save=eval(self.save_opt['save']), gcom=self.gcom, comm_mode=eval(self.net_opt['communication_mode']), sim=self, seed=self.seed)) def create_EMS(self): """ create electromagnetic Solver object """ self.EMS = EMSolver(L=self.L) def create_network(self): """ create the whole network """ self.net = Network(EMS=self.EMS) self.gcom = Gcom(net=self.net, sim=self) self.create_agent() # create network if str2bool(self.net_opt['network']): self.net.create() # create All Personnal networks for n in self.net.nodes(): self.net.node[n]['PN']._get_wstd() self.net.node[n]['PN']._get_SubNet() self.gcom.create() # create Process Network self.Pnet = PNetwork(net=self.net, net_updt_time=float( self.net_opt['network_update_time']), L=self.L, sim=self, show_sg=str2bool(self.net_opt['show_sg']), disp_inf=str2bool(self.net_opt['dispinfo']), save=eval(self.save_opt['save'])) self.activate(self.Pnet, self.Pnet.run(), 0.0) def create_visual(self): """ create visual Tk process """ self.visu = Updater(interval=float(self.sim_opt['show_interval']), sim=self) self.activate(self.visu, self.visu.execute(), 0.0) def create(self): """ create the simulation This method is called at the end of __init__ """ # this is to redump the database at each simulation if 'mysql' in self.save_opt['save']: if str2bool(self.sql_opt['dumpdb']): os.popen( 'mysql -u ' + self.sql_opt['user'] + ' -p ' + self.sql_opt['dbname'] + '< /private/staff/t/ot/niamiot/svn2/devel/simulator/pyray/SimWHERE2.sql' ) # TODO remove the hard reference if 'txt' in self.save_opt['save']: pyu.writeDetails(self) if os.path.isfile(os.path.join(basename, 'output', 'Nodes.txt')): print 'would you like to erase previous txt files ?' A = raw_input() if A == 'y': for f in os.listdir(os.path.join(basename, 'output')): try: fi, ext = f.split('.') if ext == 'txt': os.remove(os.path.join(basename, 'output' + f)) except: pass # Layout self.create_layout() # Electro Magnetic Simulator self.create_EMS() # Network self.create_network() if str2bool(self.sim_opt['showtk']): self.create_visual() self.create_show() if str2bool(self.save_opt['savep']): self.save = Save(L=self.L, net=self.net, sim=self) self.activate(self.save, self.save.run(), 0.0) def create_show(self): """ create a vizualization """ plt.ion() fig_net = 'network' fig_table = 'table' if str2bool(self.net_opt['show']): if str2bool(self.net_opt['ipython_nb_show']): notebook = True else: notebook = False self.sh = ShowNet(net=self.net, L=self.L, sim=self, fname=fig_net, notebook=notebook) self.activate(self.sh, self.sh.run(), 1.0) if str2bool(self.net_opt['show_table']): self.sht = ShowTable(net=self.net, lAg=self.lAg, sim=self, fname=fig_table) self.activate(self.sht, self.sht.run(), 1.0) def savepandas(self): """ save mechanics in pandas hdf5 format """ filename = pyu.getlong(eval(self.sim_opt["filename"]), pstruc['DIRNETSAVE']) layfile = self.L._filename.split('.')[0] store = pd.HDFStore(filename + '_' + layfile + '.h5', 'w') for a in self.lAg: if a.typ != 'ap': store.put(a.ID, a.meca.df.convert_objects()) else: # if agent acces point, its position is saved store.put(a.ID, a.posdf) store.get_storer(a.ID).attrs.typ = a.typ store.get_storer(a.ID).attrs.name = a.name store.get_storer(a.ID).attrs.ID = a.ID store.get_storer(a.ID).attrs.layout = self.L._filename # saving metadata store.root._v_attrs.attributes = self.sim_opt store.close() self.traj.loadh5( eval(self.sim_opt["filename"]) + '_' + layfile + '.h5') def runsimul(self): """ run simulation """ if not self.finish: # random number seed seed(self.seed) self.simulate(until=float(self.sim_opt['duration']), real_time=True, rel_speed=float(self.sim_opt['speedratio'])) self.the_world._boids = {} # if str2bool(self.save_opt['savep']): # print 'Processing save results, please wait' # self.save.mat_export() if str2bool(self.save_opt['savepd']): self.savepandas() self.finish = True else: raise NameError('Reinstantiate a new simul object to run again')
from pylayers.gis.layout import Layout import networkx as nx import matplotlib.pyplot as plt #doctest.testmod(layout) #L = Layout('TA-Office.ini') L = Layout() lL = L.ls() for tL in lL: print 'Layout :',tL try: L=Layout(tL) L.save() except: print 'Layout :',tL ,' problem' #f = plt.figure(figsize=(20,10)) #plt.axis('off') #f,a = L.showG('s',nodes=False,fig=f) #plt.show() #f,a = L.showG('r',edge_color='b',linewidth=4,fig=f) #L= Layout('11Dbibli.ini') #L.show() #L = Layout('PTIN.ini') #L = Layout('DLR.ini') #L.buildGt() #Ga = L.buildGr() #L.showGs() #nx.draw(Ga,Ga.pos)