-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
434 lines (362 loc) · 14.6 KB
/
maze.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import sys
import os
import copy
import xlsxwriter
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.Qt import Qt
from maze_graf import *
from pathlib import Path
import os.path
from stopwatch import Stopwatch
from datetime import datetime
import openpyxl
col=0
class MyInterface(QDialog):
# tutti cio che riguarda 'Qdialog' (metodi e valori), valogono anche per MyApp
esito={} # variabili che salvano i dati durante la sessione. Quando premo fine sessione i dati vengono copiato su un nuovo dizionario. Questi evngono azzersti
lista=[] # variabili che salvano i dati durante la sessione. Quando premo fine sessione i dati vengono copiato su un nuovo dizionario.Questi evngono azzersti
ripetizione=''
now = datetime.now()
save_path=str(os.getcwd()) #ottengo la working directory del programma
completeName= 'analisi.txt' #nome del file dove esporto.
# Creiamo il cronometro
stopwatchA = Stopwatch()
stopwatchB = Stopwatch()
stopwatchC = Stopwatch()
stopwatchA.reset()
stopwatchB.reset()
stopwatchC.reset()
f_input = '' #Variabile che contiene il numero di sessione dell'animale
# esito={} # variabili che salvano i dati durante la sessione. Quando premo fine sessione i dati vengono copiato su un nuovo dizionario. Questi evngono azzersti
# lista=[] # variabili che salvano i dati durante la sessione. Quando premo fine sessione i dati vengono copiato su un nuovo dizionario. Questi evngono azzersti
def __init__(self):
super().__init__() # indica quali attiburi da ereditare dall'oggetto passato di classe QDialog
self.ui = Ui_Dialog()
self.ui.setupUi(self)
'''fino a qua è sempre uguale'''
#BOTTONE FINE SESSIONE
self.ui.FineSessione.clicked.connect(self.fine_sessione) #collego la funzione 'fine sessione' al bottone fine sessione
# self.f_input=self.ui.lineEdit_2.text() # prendo il numero della sessione
self.lista=[]
self.esito={}
self.sessione={}
self.esportazione={}
#BOTTONE ESPORTA
self.ui.Esporta.clicked.connect(self.esporta)
#BOTTONE INIZIA
self.ui.inizia.clicked.connect(self.inizia)
#FUNZIONI PER LA SCELTA DEL BRACCIO
def centro(self,*args):
self.ripetizione=''
self.stopwatchA.stop() # Stop it again
self.stopwatchB.stop() # Stop it again
self.stopwatchC.stop() # Stop it again
mini_lista={}
if self.stopwatchA.duration != 0.0:
mini_lista['A']=round(self.stopwatchA.duration,1)
if self.stopwatchB.duration != 0.0:
mini_lista['B']=round(self.stopwatchB.duration,1)
if self.stopwatchC.duration != 0.0:
mini_lista['C']=round(self.stopwatchC.duration,1)
self.stopwatchA.reset()
self.stopwatchB.reset()
self.stopwatchC.reset()
if mini_lista !={}:
self.lista.append(mini_lista)
# chiave=self.ui.lineEdit_2.text() # prendo il numero animale
self.ui.label_stampe.setText('CENTRO')
#print('Centro')
def braccioA(self,*args):
if self.ripetizione=='A':
return
else:
self.ripetizione='A'
self.stopwatchA.stop() # Stop it again
self.stopwatchB.stop() # Stop it again
self.stopwatchC.stop() # Stop it again
mini_lista={}
if self.stopwatchA.duration != 0.0:
mini_lista['A']=round(self.stopwatchA.duration,1)
if self.stopwatchB.duration != 0.0:
mini_lista['B']=round(self.stopwatchB.duration,1)
if self.stopwatchC.duration != 0.0:
mini_lista['C']=round(self.stopwatchC.duration,1)
self.stopwatchA.reset()
self.stopwatchB.reset()
self.stopwatchC.reset()
if mini_lista !={}:
self.lista.append(mini_lista)
self.stopwatchA.start() # Start it again
chiave=self.ui.lineEdit_2.text() # prendo il numero animale
self.ui.label_stampe.setText('BRACCIO A')
self.esito['Animale_nr:_'+str(chiave)]=self.lista
#print('Braccio A')
def braccioB(self,*args):
if self.ripetizione=='B':
return
else:
self.ripetizione='B'
self.stopwatchA.stop() # Stop it again
self.stopwatchB.stop() # Stop it again
self.stopwatchC.stop() # Stop it again
mini_lista={}
if self.stopwatchA.duration != 0.0:
mini_lista['A']=round(self.stopwatchA.duration,1)
if self.stopwatchB.duration != 0.0:
mini_lista['B']=round(self.stopwatchB.duration,1)
if self.stopwatchC.duration != 0.0:
mini_lista['C']=round(self.stopwatchC.duration,1)
self.stopwatchA.reset()
self.stopwatchB.reset()
self.stopwatchC.reset()
if mini_lista !={}:
self.lista+=[mini_lista]
self.stopwatchB.start() # Start it again
chiave=self.ui.lineEdit_2.text() # prendo il numero animale
self.ui.label_stampe.setText('BRACCIO B')
self.esito['Animale_nr:_'+str(chiave)]=self.lista
#print('Braccio B')
def braccioC(self,*args):
if self.ripetizione=='C':
return
else:
self.ripetizione='C'
self.stopwatchA.stop() # Stop it again
self.stopwatchB.stop() # Stop it again
self.stopwatchC.stop() # Stop it again
mini_lista={}
if self.stopwatchA.duration != 0.0:
mini_lista['A']=round(self.stopwatchA.duration,1)
if self.stopwatchB.duration != 0.0:
mini_lista['B']=round(self.stopwatchB.duration,1)
if self.stopwatchC.duration != 0.0:
mini_lista['C']=round(self.stopwatchC.duration,1)
self.stopwatchA.reset()
self.stopwatchB.reset()
self.stopwatchC.reset()
if mini_lista !={}:
self.lista+=[mini_lista]
self.stopwatchC.start() # Start it again
chiave=self.ui.lineEdit_2.text() # prendo il numero animale
self.ui.label_stampe.setText('BRACCIO C')
self.esito['Animale_nr:_'+str(chiave)]=self.lista
#print('Braccio C')
def fsociety(self, lista_da_testo):
lista_da_testo=lista_da_testo[:-1]
#print(lista_da_testo)
os.remove(os.path.join(str(cwd),'analisi_maze.txt'))
#print('File eliminato')
return lista_da_testo
def inizia(self):
#print('inizio')
#ammazza(esito,lista)
cwd = os.getcwd()
#os.remove(os.path.join(str(cwd),'analisi_maze.txt'))
#print('ciao')
tutto=[]
try:
with open (cwd+'/analisi_maze.txt', 'r') as f:
tutto = f.readlines()
#corretto=fsociety(tutto)
#print('dentro')
corretto= tutto[:-1]
with open (cwd+'/analisi_maze.txt', 'w') as f:
for e in corretto:
f.write(e + "\n")
return
except:
pass
def ammazza(self, esito,lista):
'''Azzera tutti i valori per far partire la sessione di un nuovo topo'''
self.esito.clear()
self.lista.clear()
#def inizia(self):
#self.ammazza(self.sessione,self.lista)
#return
def fine_sessione(self,col):
'''Termina l'analisi del video aggiungendo FINE come ultimo valore rilevato.
Annulla anche una sessione se ho fatto un errore. Dopo aver premuto riaprto
da una nuova sessione.'''
self.stopwatchA.stop() # Stop it again
self.stopwatchB.stop() # Stop it again
self.stopwatchC.stop() # Stop it again
mini_lista={}
if self.stopwatchA.duration != 0.0:
mini_lista['A']=round(self.stopwatchA.duration,1)
if self.stopwatchB.duration != 0.0:
mini_lista['B']=round(self.stopwatchB.duration,1)
if self.stopwatchC.duration != 0.0:
mini_lista['C']=round(self.stopwatchC.duration,1)
self.stopwatchA.reset()
self.stopwatchB.reset()
self.stopwatchC.reset()
stringa=''
if mini_lista !={}:
self.lista.append(mini_lista)
for k,v in self.esito.items():
stringa+=k
stringa+=' '
stringa+=str(v) # del tempo elimino la 's'
stringa+='\n'
nome_file='analisi_maze.txt'
save_path=str(os.getcwd())
completeName= 'analisi_maze.txt'
scrivere=''
simboli=["[", "]", "'", "s", ","]
try:
for carattere in stringa:
if carattere in simboli:
pass
else:
scrivere+=carattere
stringa=scrivere
scrivere=''
for carattere in stringa:
if carattere == '.':
scrivere += ","
else:
scrivere+=carattere
stringa=scrivere
c=0
for file in os.listdir(save_path):
if str(file) == nome_file:
c+=1
if c==1:
with open(os.path.join(save_path,completeName), "a", encoding="utf-8") as f:
f.write(stringa)
else:
with open(os.path.join(save_path,completeName), "w", encoding="utf-8") as f:
f.write(stringa)
self.ammazza(self.sessione,self.lista)
except:
pass
self.ui.label_stampe.setText('SESSIONE CONCLUSA')
#print('Sessione conclusa')
# chiave=self.ui.lineEdit_2.text()
self.ripetizione=''
return
def autoIncrement(self):
global col
pStart = 1
pInterval = 1
if (col == 0):
col = pStart
else:
col += pInterval
return col
def ott_colonna(self):
global col
#print('valore iniziale della colonna:')
#print(col)
cwd = os.getcwd()
interruttore=self.interruttore()
if interruttore == 1:
#print('Esiste già un file excel')
xfile = openpyxl.load_workbook(cwd+'/Analisi_maze.xlsx')
sheet = xfile['Sheet1']
#print(xfile['Sheet1']['A1'].value)
rosetta_base=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ', 'AR','AS', 'AT', 'AU','AV','AW',
'AX','AY','AZ']
for lettera in rosetta_base:
colonna=str(lettera)+'1'
if xfile['Sheet1'][colonna].value != None:
self.autoIncrement()
#print(col)
else:
#print('casella vuota')
#print(col)
return col
else:
#print('Non esiste il file excel')
return 0
def interruttore(self):
there = 0
cwd = os.getcwd()
for file in os.listdir(cwd):
if str(file) == 'Analisi_maze.xlsx':
there=1
else:
pass
return there
def esporta(self):
'''Conclude la sessione di un singolo topo e trascrive i risultati in un file txt
poi li passa in un excel'''
global col
col=self.ott_colonna()
cwd = os.getcwd()
workbook = xlsxwriter.Workbook('Analisi_maze.xlsx')
worksheet = workbook.add_worksheet()
self.ui.label_stampe.setText('Dati Esportati')
'''Prende il file di testo delle sessioni e le mette in un excel'''
with open('analisi_maze.txt', 'r') as f:
tutto=f.readlines()
row = 0
there=self.interruttore()
if there == 0:
#print('NON esiste Excel')
filepath=str(cwd+'/Analisi_maze.xlsx')
wb = openpyxl.Workbook()
wb.save(filepath)
for riga in tutto:
sep=riga.split(' ')
for i in range(len(sep)):
sep[i]=sep[i].replace('{','')
sep[i]=sep[i].replace('}','')
sep[i]=sep[i].replace('\n','')
worksheet.write(row+i, col, sep[i])
workbook.close()
#self.autoIncrement()
else:
#print('ESISTE Excel')
#print(col)
rosetta={1:'A', 2:'B', 3:'C', 4:'D', 5:'E', 6:'F', 7:'G', 8:'H',
9:'I', 10: 'J', 11:'K', 12:'L', 13: 'M', 14: 'N', 15:'O', 16: 'P',
17:'Q', 18:'R', 19:'S', 20:'T', 21:'U', 22:'V', 23:'W',24:'X', 25:'Y', 26:'Z',
27:'AA', 28:'AB', 29:'AC', 30:'AD', 31:'AE', 32:'AF', 33:'AG', 34:'AH',
35:'AI', 36: 'AJ', 37:'AK', 38:'AL', 39: 'AM', 40: 'AN', 41:'AO', 42: 'AP',
43:'AQ', 44:'AR', 45:'AS', 46:'AT', 47:'AU', 48:'AV', 49:'AW', 50:'AX', 51:'AY',
52:'AZ'}
xfile = openpyxl.load_workbook(cwd+'/Analisi_maze.xlsx')
sheet = xfile['Sheet1']
for riga in tutto:
sep=riga.split(' ')
for i in range(len(sep)):
sep[i]=sep[i].replace('{','')
sep[i]=sep[i].replace('}','')
sep[i]=sep[i].replace('\n','')
sheet[str(rosetta[col+1])+str(i+1)] = sep[i]
xfile.save('Analisi_maze.xlsx')
#self.autoIncrement()
col=0
return col
# def pulisci(self, stringa):
# '''Prende la stringa da scrivere nel file di testo ed elimina i caratter che non servono'''
# scrivere=''
# simboli=["[", "]", "'", "s", ","]
# for carattere in stringa:
# if carattere in simboli:
# scrivere+=' '
# else:
# scrivere+= carattere
# return scrivere
def keyPressEvent(self,event):
'''Questa è la magica funzione che mi permette di prendere gli input da tastiera'''
if event.key() == Qt.Key_J:
self.braccioA()
if event.key() == Qt.Key_K:
self.braccioB()
if event.key() == Qt.Key_L:
self.braccioC()
if event.key() == Qt.Key_S:
self.centro()
def test_method(self):
#print('Premuto spazio')
self.show() ##NON ELIMINARE
if __name__=="__main__":
app = QApplication(sys.argv)
demo = MyInterface()
demo.show()
sys.exit(app.exec_())
workbook = xlsxwriter.Workbook('Expe.xlsx')
worksheet = workbook.add_worksheet()