Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

scrollable rb alloc plot

By: a guest on Feb 28th, 2012  |  syntax: Python  |  size: 4.32 KB  |  views: 50  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import numpy as np
  2.  
  3. import matplotlib
  4. from matplotlib.ticker import NullLocator
  5. from matplotlib.patches import Rectangle
  6. matplotlib.use('WXAgg')
  7. from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
  8. from matplotlib.figure import Figure
  9.  
  10. import wx
  11. import re
  12.  
  13. class Struct(object):
  14.     def __init__(self, **kwds):
  15.         self.__dict__.update(kwds)
  16.  
  17. colors = ['red', 'orange', 'blue', 'green', 'black', 'yellow', 'violet', 'brown', 'pink']
  18. inputFilePath=""
  19.  
  20. class MyFrame(wx.Frame):
  21.     def __init__(self, parent, id, fPath):
  22.         wx.Frame.__init__(self,parent, id, 'scrollable plot',
  23.                 style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER,
  24.                 size=(1024, 768))
  25.         self.panel = wx.Panel(self, -1)
  26.  
  27.         self.fig = Figure((5, 4), 75)
  28.         self.canvas = FigureCanvasWxAgg(self.panel, -1, self.fig)
  29.         self.scroll_range = 768
  30.         self.canvas.SetScrollbar(wx.HORIZONTAL, 0, 5,
  31.                                  self.scroll_range)
  32.  
  33.         sizer = wx.BoxSizer(wx.VERTICAL)
  34.         sizer.Add(self.canvas, -1, wx.EXPAND)
  35.  
  36.         self.panel.SetSizer(sizer)
  37.         self.panel.Fit()
  38.  
  39.         self.init_data(dataFilePath=fPath)
  40.         self.init_plot()
  41.  
  42.         self.canvas.Bind(wx.EVT_SCROLLWIN, self.OnScrollEvt)
  43.  
  44.     def readData(self,filePath):
  45.         f = open(filePath,'r')
  46.         lines = f.readlines()
  47.         uplinkLines = []
  48.         for line in lines:
  49.             uplink = []
  50.             if line.startswith("ENB:SCHED:DL:"):
  51.                 info = line[13:]
  52.                 res = re.compile(r"T\[(\d+\.\d+)\] ([0-9 ]*)").findall(info)
  53.                
  54.                 if len(res)==0:
  55.                     continue
  56.                
  57.                 allocMap = res[0][1].split()
  58.                 lng = len(allocMap)
  59.                
  60.                 for i in range(25):
  61.                     if i<lng:
  62.                         info_block = Struct(empty=False,id=int(allocMap[i]),direction="DL")
  63.                         uplink.append(info_block)
  64.                     else:
  65.                         info_block = Struct(empty=True,id=0)
  66.                         uplink.append(info_block)
  67.                
  68.                 uplinkLines.append(uplink)
  69.                 #print("uplinknextline")
  70.                
  71.         self.info_array = np.array(uplinkLines)
  72.         #print(info_array)
  73.         #allocation(info_array)
  74.  
  75.     def init_data(self, dataFilePath):
  76.        
  77.         self.readData(filePath=dataFilePath)
  78.         (rows,cols) = self.info_array.shape
  79.        
  80.         # Extents of data sequence:
  81.         self.i_min = 0
  82.         self.i_max = cols
  83.  
  84.         # Size of plot window:      
  85.         self.i_window = 100
  86.  
  87.         # Indices of data interval to be plotted:
  88.         self.i_start = 0
  89.         self.i_end = self.i_start + self.i_window
  90.    
  91.     def plot_next_part(self):
  92.         del self.ax.patches[:]
  93.         for (x,y),info in np.ndenumerate(self.info_array[ : , self.i_start:self.i_end]):
  94.             if info.empty:
  95.                 color = "white"
  96.             else:
  97.                 color = colors[info.id]
  98.            
  99.             size = 3
  100.             rect = Rectangle([x-size/2,y-size/2],size,size,facecolor=color,edgecolor="black")
  101.             self.ax.add_patch(rect)
  102.         #????????
  103.         self.ax.set_ylim(*self.ax.get_ylim()[::-1])
  104.        
  105.    
  106.     def init_plot(self):
  107.         self.ax = self.fig.add_subplot(111)
  108.         self.ax.patch.set_facecolor('white')
  109.         #ax.set_aspect('equal','box')
  110.         self.ax.xaxis.set_major_locator(NullLocator())
  111.         self.ax.yaxis.set_major_locator(NullLocator())
  112.        
  113.         #initial plot
  114.         self.plot_next_part()
  115.  
  116.     def draw_plot(self):
  117.         self.plot_next_part()
  118.  
  119.         # Redraw:                  
  120.         self.canvas.draw()
  121.  
  122.     def OnScrollEvt(self, event):
  123.  
  124.         # Update the indices of the plot:
  125.         self.i_start = self.i_min + event.GetPosition()
  126.         self.i_end = self.i_min + self.i_window + event.GetPosition()
  127.         self.draw_plot()
  128.  
  129. class MyApp(wx.App):
  130.     def OnInit(self):
  131.         self.frame = MyFrame(parent=None,id=-1,fPath=inputFilePath)
  132.         self.frame.Show()
  133.         self.SetTopWindow(self.frame)
  134.         return True
  135.  
  136. if __name__ == '__main__':
  137.     inputFilePath = raw_input("Podaj sciezke do pliku: ")
  138.     app = MyApp()
  139.     app.MainLoop()
clone this paste RAW Paste Data