Package mosp_tools :: Package heatmap :: Module logfilereader
[hide private]
[frames] | no frames]

Source Code for Module mosp_tools.heatmap.logfilereader

 1  """Helper funtions to read log files""" 
 2   
 3  from sys import maxint 
 4  from csv import reader 
 5   
 6  __author__ = "B. Henne" 
 7  __contact__ = "henne@dcsec.uni-hannover.de" 
 8  __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany" 
 9  __license__ = "GPLv3" 
10   
11   
12 -def read_simple(filename, delimiter, t, x, y):
13 """Reads complete CSV log file with time, x-value and y-value and return only this. 14 @author: B. Henne""" 15 logReader = reader(open(filename), delimiter=delimiter) 16 for row in logReader: 17 yield row[t], [row[x], row[y]]
18
19 -def read(filename, delimiter, t, x, y, t_start=0, t_end=maxint, empty=[]):
20 """Reads CSV log file with time, x-value and y-value for specified time interval. 21 Return values for all t_start < t < t_end. 22 23 Times with no log entries are filled with value of empty. 24 Returns iterable pair of time and list of coordinates 25 @param filename: name of csv file 26 @param delimiter: delimiter between columns in csv file 27 @param t: number of column containing time 28 @param x: number of column containing x-value 29 @param y: number of column containing y-value 30 @param t_start: start of output time interval 31 @param t_end: end of output time interval 32 @param empty: value yielded if no log entry exists for a time, default is empty list, maybe use None 33 @author: B. Henne""" 34 logReader = reader(open(filename), delimiter=delimiter) 35 t_now = t_start #: current iteration time 36 row = logReader.next() #: list of csv file as list of columns 37 while t_now <= t_end: 38 dxy = [] 39 dt = int(row[t]) #: read time 40 dx = row[x] #: read x-value 41 dy = row[y] #: read y-value 42 if dt < t_start: # read time before start: we do not want this values before start 43 try: 44 row = logReader.next() # read next and start again 45 except: 46 pass 47 continue 48 if dt < t_now: # read time before current iteration time 49 yield t_now, empty 50 t_now += 1 51 continue 52 if dt > t_now: # read time is after current time 53 yield t_now, empty 54 t_now += 1 55 continue 56 while t_now == dt: # read time is current time 57 dxy.append([dx, dy]) 58 try: 59 row = logReader.next() 60 except: 61 dt = maxint 62 break 63 dt = int(row[t]) 64 dx = row[x] 65 dy = row[y] 66 yield t_now, dxy 67 t_now += 1
68
69 -def accumulated_read(filename, delimiter, t, x, y, t_start=0, t_end=maxint, step=10):
70 """Reads CSV file as read(), but accumulates return values in step size packets. 71 @author: B. Henne""" 72 acc = [] 73 substep = 1 74 for t, xy in read(filename, delimiter, t, x, y, t_start=t_start, t_end=t_end, empty=None): 75 if xy is not None: 76 acc.extend(xy) 77 if substep % step == 0: 78 yield t, acc 79 acc = [] 80 last_acc = t 81 substep += 1 82 if last_acc != t: 83 yield t, acc
84 85 86 if __name__ == '__main__': 87 for t,xy in read('data/logfilereader_testdata.txt', ' ', 0, 1, 2, 0, 35): 88 print t,xy 89 for xy in accumulated_read('data/logfilereader_testdata.txt', ' ', 0, 1, 2, 10, 49, 10): 90 print xy 91