-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyse_log.py
143 lines (124 loc) · 5.59 KB
/
analyse_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 10 09:23:04 2019
@author: jorge
Takes the log generated by kilobox, and transforms it into a much more user friendly spreadsheet
-- I expect the script would work MUCH faster if it wrote a .csv, or even a clear txt file rather than .xlsx
-- the multiple sheets that a .xlsx file allows was useful, but the Kilobts could be added starting every 10
-- columns, for example rather than a new sheet. (Kilobot 1 has column 1:10, Kilboto 2 has 11:20.....).
-- Then write this to a .csv which would be MUCH faster. And would require that the matlab script read the
-- file only once.
"""
from datetime import datetime
import matplotlib.pyplot as plt
import re
import subprocess
import fire
import numpy as np
from openpyxl import Workbook
class analyse(object):
def analyse(self, name = 0, numb = ""):#, number):
wb = Workbook()
if name == 0:
dest_filename = 'log' + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '.xlsx'
else:
dest_filename = 'log_for_energy.xlsx' # this writes over the log each time so I can leave it running overnight without generating large logs
def colnum_string(n):
string = ''
n, remainder = divmod(n, 26)
string = chr(65 + remainder) + string # if it goes over 26
return string
def coord_to_cell(tup):
string = colnum_string(tup[0]) + str(tup[1]+1)
return string
bots = ['1']
WB = [wb.create_sheet('Kilobot 1', 0)]
ws = WB[0]
ws.cell(1,1).value = 't'
ws.cell(1,2).value = 'X'
ws.cell(1,3).value = 'Y'
# ws.cell(1,4).value = 'nest'
# ws.cell(1,5).value = 'food'
# ws.cell(1,6).value = 'carrying'
ws.cell(1,4).value = 'nbr bold'
ws.cell(1,5).value = 'messages'
ws.cell(1,6).value = 'boldness'
ws.cell(1,7).value = 'distance'
ws.cell(1,8).value = 'target'
with open("test.log") as file:
lines = file.readlines()
list = l = [None] * (len(lines))#[]
j = 0
last_perc = 0
for i in lines:
list[j] = i #list = list + [i]
if int(j*100/len(lines)) != last_perc:
print("writing list from log " + str(j) + "/" + str(len(lines)) + " " + str(int(j*100/len(lines))) + "%")
last_perc = int(j*100/len(lines))
j = j + 1
j = 0
last_perc = 0
#for i in list[int(3*len(list)/4)-128:int(len(list))]:
for i in list:
if int(j*100/len(list)) != last_perc:
print("formatting list into spreadsheet " + str(j) + "/" + str(len(list)) + " " + str(int(j*100/len(list))) + "%")
last_perc = int(j*100/len(list))
line = i.split(',')
for i in range(0,len(line)):
l = line[i]
line[i] = l.strip()
# kilobot index:
# print(line[0] + '\n')
ind = line[0]
ind = re.findall('\d+',ind)
ind = ind[0]
index = int(ind)-1
if ind not in bots:
bots = bots + [ind]
WB = WB + [wb.create_sheet('Kilobot ' + ind, index)]
print(index)
ws = WB[index]
ws.cell(1,1).value = 't'
ws.cell(1,2).value = 'X'
ws.cell(1,3).value = 'Y'
#ws.cell(1,4).value = 'nest'
#ws.cell(1,5).value = 'food'
#ws.cell(1,6).value = 'carrying'
ws.cell(1,4).value = 'nbr bold'
ws.cell(1,5).value = 'messages'
ws.cell(1,6).value = 'boldness'
ws.cell(1,7).value = 'distance'
ws.cell(1,8).value = 'target'
# linies = linies + [l.strip()]
# wb.create_sheet('test', 4)
time = line[1].split('.')
time = time[0]
time = int(time)+1
ws = WB[index]
for i in range(1,len(line)):
#print(line[i])
#print(i)
ws.cell(time,i).value = float(line[i])
j = j + 1
# ws.title = ('Kilobot ' + ind)
#ws2 = wb.create_sheet()
#for i in range(number):
# X = []
# for j in range(2,len(WB[i]['B'])):
# X = X + [WB[i][j][2].value]
# Y = []
# for j in range(2,len(WB[i]['C'])):
# Y = Y + [WB[i][j][2].value]
# plt.plot(X,Y)
wb.save(filename = dest_filename)
with open("executematlab", 'w') as file:
file.write("matlab -nodisplay -nosplash -nodesktop -r \"draw_paths(\\\"{0}\\\", {1}); exit;\"".format(dest_filename, str(index+1)))
file.close
with open("executematlab_energies", 'w') as file:
file.write("matlab -nodisplay -nosplash -nodesktop -r \"get_energies(\\\"{0}\\\", {1}, \\\"{2}\\\"); exit;\"".format(dest_filename, str(index+1), numb))
file.close
#subprocess.Popen(["matlab -nodisplay -nosplash -nodesktop -r \"draw_paths(\\\"{0}\\\", {1}); exit;\"".format(dest_filename, str(index+1))], shell=True)
#print("You may now execute: \"matlab -nodisplay -nosplash -nodesktop -r \"draw_paths(\\\"{0}\\\", {1}); exit;\" | tail -n +11\"".format(dest_filename, str(index+1)))
if __name__ == '__main__':
fire.Fire(analyse)