-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaving_data.py
More file actions
1004 lines (755 loc) · 26.2 KB
/
Copy pathsaving_data.py
File metadata and controls
1004 lines (755 loc) · 26.2 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import csv
import numpy as np
import os
import time
from datetime import date
import pandas as pd
import subprocess
import re
import shutil
import pickle
import copy
from text import (
printme,
agebyExperimenter,
sexbyExperimenter,
handednessbyExperimenter,
)
##################################################################
###################### Storing data ##############################
################################################################
def buildDict(*keys):
"""
Build dictionary to store data during experiment
"""
data = {}
list_llaves = list([*keys])
print(list_llaves)
for i in np.arange(len(list_llaves)):
data[list_llaves[i]] = []
return data
def copyDict(data):
"""
Function to copy dictionary
"""
return copy.deepcopy(data)
def appendDataDict(data, tempdata):
"""
Append data to dictionary
"""
keys = data.keys()
for i, k in enumerate(keys):
data[k].append(tempdata[i])
return data
def subjFile(path, file="subjs.csv"):
"""
Function to create subject file (date, time, age, valid, state (ex 1)/tb 1))
"""
of1 = open(f"{path}/data/{file}", "w")
data_writer = csv.writer(of1)
header = ["Date", "Time", "NumDayWithin", "Age", "Valid", "State"]
data_writer.writerow(header)
of1.close()
def setSubjNumDec(age, numdaywithin, situ, file="subjs.csv"):
"""
Function to save subject date, time, valid and age
"""
steps_back = depthToSrc()
path = f"{steps_back}data/{file}"
if not open(path):
subjFile(path)
sf = open(f"./{steps_back}data/{file}", "a")
data_writer = csv.writer(sf)
t = time.localtime()
time_now = time.strftime("%H_%M_%S", t)
todaydate = date.today().strftime("%Y%m%d")
if situ == "tb":
state = 0
elif situ == "ex":
state = 1
data_writer.writerow([todaydate, time_now, numdaywithin, age, 0, state])
sf.close()
return todaydate, time_now
def getSubjNumDec(file="subjs.csv", day=None):
steps_back = depthToSrc()
if day:
numdaywithin = None
else:
df = pd.read_csv(f"./{steps_back}data/{file}")
nums_within = df.loc[:, "NumDayWithin"]
numdaywithin = int(nums_within.to_numpy()[-1])
return numdaywithin
def getAgeSex(situ, path):
if situ == "tb":
age = 1
sex = 0
elif situ == "ex":
age = getOrAsk(agebyExperimenter, path, "age")
sex = getOrAsk(sexbyExperimenter, path, "sex")
return age, sex
def getAgeSexHandedness(situ, path):
if situ == "tb":
age = 1
sex = 0
handedness = 3
elif situ == "ex":
age = getOrAsk(agebyExperimenter, path, "age")
sex = getOrAsk(sexbyExperimenter, path, "sex")
handedness = getOrAsk(handednessbyExperimenter, path, "handedness")
return age, sex, handedness
def getSubjNum(situ):
if situ == "tb":
subject_n = numberSubjDay()
elif situ == "ex":
subject_n = numberSubjDay("y")
return subject_n
##################################################################
###################### Saving data ##############################
################################################################
def writeSingleValue(path, file_name, value):
of = open(f"{path}/{file_name}", "w")
of.write(str(value))
of.close()
def getSingleValue(path, file_name):
f = open(f"{path}/{file_name}", 'r')
value = f.readline()
f.close()
return value
def tempSaving(path, header, temp_file_name="temp_data"):
"""
Function to initiliase temporary file to store data in case the
script fails
"""
temp_file = open(f"{path}/{temp_file_name}.csv", "w")
temp_data_writer = csv.writer(temp_file)
temp_data_writer.writerow(header)
temp_file.close()
return [temp_data_writer, temp_file, temp_file_name + ".csv"]
def findTempFiles(path):
"""
Function to find temporary files in folder
"""
pattern = "^temp_"
patternc = re.compile(pattern)
names = []
for filename in os.listdir(f"{path}"):
if patternc.match(filename):
# print(filename)
name, form = filename.split(".")
names.append((name, form))
else:
continue
return names
def changeNameTempFile(path, outcome="failed"):
"""
Function to change the name of the temporary files with current date and time.
This functions should be placed in the except sections. It is triggered when the script fails
"""
names = findTempFiles(path)
t = time.localtime()
time_now = time.strftime("%H_%M_%S", t)
todaydate = date.today().strftime("%Y%m%d")
for i, n in enumerate(names):
temp_file_name = n[0].split("temp_", 1)[1]
print(n)
shutil.copyfile(
f"{path}/{n[0]}.{n[1]}",
f"./{path}/{temp_file_name}_{outcome}_{todaydate}_{time_now}.{n[1]}",
)
############ apending to file with all subjects ############
def apendAll(folder, subj_n, data, file="data_all"):
"""
Function to append participant's data to a csv file with the
data with all the participants for an experiment.
Data structure should be a dictionary. It can handle two levels of keys.
"""
llaves = data.keys()
one_key = [*llaves][0]
subj_n = int(subj_n)
of1 = open(f"{folder}/{file}.csv", "a")
data_writer = csv.writer(of1)
if type(data[one_key]) == list:
if subj_n == 1:
# print(subj_n)
# print(llaves)
data_writer.writerow(llaves)
for i in np.arange(len(data[[*llaves][0]])):
rowToWrite = []
for j in llaves:
datapoint = data[j][i]
rowToWrite.append(datapoint)
data_writer.writerow(rowToWrite)
elif type(data[one_key]) == dict:
one_array = data[[*llaves][0]]
one_array_llaves = one_array.keys()
# Headers if we are doing the first subject
if subj_n == 1:
rait = []
for k, v in data.items():
rait.append(k)
empy = ""
for i in np.arange((len(v) - 1)):
rait.append(empy)
data_writer.writerow(rait)
# We write the name of each data array n_condition times
# This could be done above but we do it separately for clarity
name_arrays = []
for k, v in data.items():
for c, b in v.items():
name_arrays.append(c)
data_writer.writerow(name_arrays)
# Write data
for i in np.arange(len(one_array[[*one_array_llaves][0]])):
rowToWrite = []
for k, v in data.items():
for c, b in v.items():
datapoint = data[k][c][i]
rowToWrite.append(datapoint)
data_writer.writerow(rowToWrite)
of1.close()
print(f"\nData saved to CSV with all participants\n")
############# Individual files #############
def saveIndv(file, folder, data):
"""
Function to save data of a participant to an individual file.
Data structure should be a dictionary with one level of keys.
"""
llaves = data.keys()
one_key = [*llaves][0]
of2 = open("{}/{}.csv".format(folder, file), "w")
writer_subject = csv.writer(of2)
if type(data[one_key]) == list:
writer_subject.writerow(llaves)
for i in np.arange(len(data[[*llaves][0]])):
rowToWrite = []
# print(type(llaves))
for j in llaves:
# print(type(data[j][i]))
datapoint = data[j][i]
rowToWrite.append(datapoint)
writer_subject.writerow(rowToWrite)
elif type(data[one_key]) == dict:
one_array = data[[*llaves][0]]
one_array_llaves = one_array.keys()
# We write the name of each condition
rait = []
for k, v in data.items():
rait.append(k)
empy = k
for i in np.arange((len(v) - 1)):
rait.append(empy)
writer_subject.writerow(rait)
# We write the name of each data array n_condition times
# This could be done above but we do it separately for clarity
name_arrays = []
for k, v in data.items():
for c, b in v.items():
name_arrays.append(c)
writer_subject.writerow(name_arrays)
for i in np.arange(len(one_array[[*one_array_llaves][0]])):
rowToWrite = []
for k, v in data.items():
for c, b in v.items():
datapoint = data[k][c][i]
rowToWrite.append(datapoint)
writer_subject.writerow(rowToWrite)
of2.close()
print(f"\nData saved to an individual CSV\n")
########################################################
############# Saving variables #########################
########################################################
############# SINGLE VARIABLE #############
def apendSingle(file, folder, subj_n, data_point):
"""
Function to save a single value to a csv that contains
more values from a given experiment.
"""
of3 = open("{}/{}.csv".format(folder, file), "a")
writer_subject = csv.writer(of3)
writer_subject.writerow([subj_n, data_point])
of3.close()
######## Saving Zaber
def saveZaberPos(file, path, data, header=["Zaber", "x", "y", "z"]):
"""
Function to save one position of multiple Zaber sets
"""
llaves = list(data.keys())
of1 = open(f"{path}/{file}.csv", "w")
data_writer = csv.writer(of1)
data_writer.writerow(header)
for i in llaves:
rowToWrite = [i, data[i]["x"], data[i]["y"], data[i]["z"]]
data_writer.writerow(rowToWrite)
of1.close()
print(f"\nZaber position saved\n")
######## Saving ROI
def saveROI(file, path, data, header=["Axis", "1"]):
"""
Function to save one ROI centre
"""
of1 = open(f"{path}/{file}.csv", "w")
data_writer = csv.writer(of1)
data_writer.writerow(header)
rows = ["x", "y"]
for i, r in enumerate(rows):
rowToWrite = [r, data[i]]
data_writer.writerow(rowToWrite)
of1.close()
print(f"\nROI position saved\n")
######## Saving Grid All
def saveGridAll(path, data, file="temp_grid"):
"""
Function to save all grids
"""
print(f"\nSaving grids in temporary file...\n")
for i in data.keys():
saveGridIndv(file, path, data, i)
print(f"\nGrids saved in temporary file...\n")
######## Saving Grid Indv
def saveGridIndv(file, path, data, zaber):
"""
Function to save grid of one Zaber
"""
file = file + f"_{zaber}"
of1 = open(f"{path}/{file}.csv", "w")
data_writer = csv.writer(of1)
header = [str(int(i)) for i in list(data[zaber].keys())]
header.insert(0, "Axis")
data_writer.writerow(header)
rows = ["x", "y", "z"]
for i, r in enumerate(rows):
rowToWrite = [r]
rowToWrite.extend(
[data[zaber][str(int(x))][r] for x in list(data[zaber].keys())]
)
data_writer.writerow(rowToWrite)
of1.close()
print(f"\nGrid position of {zaber} saved\n")
######## Saving ALL ROI centres
def saveROIAll(path, data, file="temp_ROIs"):
"""
Function to save all ROI centres of a grid
"""
of1 = open(f"{path}/{file}.csv", "w")
data_writer = csv.writer(of1)
grid_i = list(np.arange(1, len(data) + 0.1))
header = [str(int(i)) for i in grid_i]
header.insert(0, "Axis")
data_writer.writerow(header)
rows = ["x", "y"]
for i, r in enumerate(rows):
rowToWrite = [r]
rowToWrite.extend([data[str(int(x))][i] for x in grid_i])
data_writer.writerow(rowToWrite)
of1.close()
print(f"\nROI position saved\n")
def savePanTiltAll(path, data, file="temp_PanTilts"):
"""
Function to save all ROI centres of a grid
"""
of1 = open(f"{path}/{file}.csv", "w")
data_writer = csv.writer(of1)
grid_i = list(np.arange(1, len(data) + 0.1))
header = [str(int(i)) for i in grid_i]
header.insert(0, "Axis")
data_writer.writerow(header)
rows = ["x", "y", "z"]
for i, r in enumerate(rows):
rowToWrite = [r]
rowToWrite.extend([data[str(int(x))][i] for x in grid_i])
data_writer.writerow(rowToWrite)
of1.close()
print(f"\nROI position saved\n")
######## Saving ALL haxes
def saveHaxesAll(path, data, file="temp_haxes"):
"""
Function to save all ROI centres of a grid
"""
of1 = open(f"{path}/{file}.csv", "w")
data_writer = csv.writer(of1)
header = list(data.keys())
header.insert(0, "Position")
data_writer.writerow(header)
rows = ["1", "2", "3"]
for i, r in enumerate(rows):
rowToWrite = [r]
rowToWrite.extend([data[x][i] for x in data.keys()])
data_writer.writerow(rowToWrite)
of1.close()
print(f"\nHaxes saved\n")
def handleItiSave(temp_row, data, path, file_name):
data = appendDataDict(data, temp_row)
temp_file = open(f"{path}/{file_name}", "a")
temp_data_writer = csv.writer(temp_file)
temp_data_writer.writerow(temp_row)
temp_file.close()
return data
def createNotesFile(path):
with open(f'{path}/notes.md', "w") as f:
f.write('# Notes\n')
f.close()
print(f"Notes file created")
def booleanValueStore(path, file_name, value):
with open(f"{path}/{file_name}.txt", "wb") as f:
# write value in txt file as text
f.write(str(value).encode())
f.close()
# function to read boolean value
def booleanValueRead(path, file_name):
with open(f"{path}/{file_name}.txt", "rb") as f:
# read value from txt file as text
value = f.read().decode()
f.close()
return value
# function to modify boolean value
def booleanValueModify(path, file_name, value):
with open(f"{path}/{file_name}.txt", "wb") as f:
# write value in txt file as text
f.write(str(value).encode())
f.close()
########################################################
################## PERMISSIONS #########################
########################################################
def rootToUser(paths):
pwd = os.getcwd()
print(f"\nCurrent directory is: {pwd}\n")
paths = [*paths]
path = os.path.realpath(__file__)
root_path = path.rsplit("/", 1)[0]
print(f"\Root directory is: {root_path}\n")
for i in paths:
os.chdir(i)
subprocess.call([f"{root_path}/brapper.sh", root_path])
print(f"\nChanged permissions of following path: {i}\n")
os.chdir(pwd)
#################################################################
########## Pipeline to check & create folder architecture #######
#################################################################
def tbORtesting(testing):
"""
Function to check whether we are testing with a participant or troubleshooting the experiment
"""
if testing in ("y", "n"):
if testing == "n":
printme("You are troubleshooting")
else:
printme("We are testing a participant")
else:
raise Exception("Variable 'testing' can only take values 'y' or 'n'.")
def check_or_create(path):
"""
Function to check whether a folder exists.
If it does NOT exist, it is created
"""
folder_name = os.path.split(path)[1]
if not os.path.isdir(path):
print(f"\nFolder '{folder_name}' does NOT exist, creating it for you...\n")
os.mkdir(path)
else:
print(f"\nFolder '{folder_name}' exists, ready to continue...\n")
return path
def substring_exists(substring, string):
if substring in string:
return True
else:
return False
def depthToSrc():
"""
Function to check how deep we are with respect to src_testing
"""
path = os.path.abspath(os.getcwd())
print(f"Current working directory: {path}")
backwardsUnit = "../"
backwards = "../"
while True:
splitted = os.path.split(path)
if splitted[1] == "src_testing":
backwards = "../"
break
elif splitted[0] == "/":
backwards = "./"
break
elif substring_exists("expt", splitted[-1]):
backwards = ""
break
else:
backwards = backwards + backwardsUnit
path = splitted[0]
return backwards
def folderData(backs):
"""
Function to check whether the folder data exists.
If the folder doesn't exist it is created automatically
"""
path = f"./{backs}data"
path_anal = check_or_create(path)
return path_anal
def numberSubjDay(testing="n"):
if testing == "y":
head_folder_name = "ex"
elif testing == "n":
head_folder_name = "tb"
backwards = depthToSrc()
todaydate = date.today().strftime("%Y%m%d")
folder_name = f"{head_folder_name}_" + todaydate + "_"
patternf = re.compile(folder_name)
nums = []
for foldername in os.listdir(f"./{backwards}data/"):
print(foldername)
if patternf.match(foldername):
print(foldername)
nums.append(foldername[-1])
else:
continue
nums = sorted(nums, reverse=False)
# print('nums', nums)
if len(nums) >= 1:
return int(nums[-1]) + 1
else:
return 1
def folderTesting(path, testing, numdaysubj=None, existing_folder_name=None):
"""
Function to check whether the folder to save the data today exists.
If the folder does't exist it is created automatically
"""
if testing == "y":
head_folder_name = "ex"
elif testing == "n":
head_folder_name = "tb"
if not existing_folder_name:
todaydate = date.today().strftime("%Y%m%d")
folder_name = head_folder_name + "_" + todaydate + "_" + str(numdaysubj)
path = path + "/" + folder_name
else:
folder_name = head_folder_name + "_" + existing_folder_name
path = path + "/" + folder_name
path = check_or_create(path)
return path
def folderDataLocalFigs(path):
path_figs = path + "/" + "figures"
path_figs = check_or_create(path_figs)
path_datalocal = path + "/" + "data"
path_datalocal = check_or_create(path_datalocal)
return [path_figs, path_datalocal]
def folderChreation(numdaysubj=None, testing="n", folder_name=None):
"""
Function of functions to check whether we have all the folder architecture in place to save data and figures.
"""
print(testing)
tbORtesting(testing)
steps_back = depthToSrc()
path_data = folderData(steps_back)
path_day = folderTesting(path_data, testing, numdaysubj, folder_name)
path_figs, path_datalocal = folderDataLocalFigs(path_day)
return [path_day, path_data, path_figs, path_datalocal]
def folderVideos(path):
"""
Function to check whether the folder videos exists.
If the folder doesn't exist it is created automatically
"""
path_video = path + "/" + "videos"
path_video = check_or_create(path_video)
return path_video
def folderVhrideos(numdaysubj, testing="n", folder_name=None):
"""
Function of functions to check whether we have all the folder architecture in place to save videos.
"""
steps_back = depthToSrc()
path_data = folderData(steps_back)
path_day = folderTesting(path_data, testing, numdaysubj, folder_name)
path_video = folderVideos(path_day)
return path_video
def folderAudios(path):
"""
Function to check whether the folder videos exists.
If the folder doesn't exist it is created automatically
"""
path_audio = path + "/" + "audios"
path_audio = check_or_create(path_audio)
return path_audio
def folderArudio(numdaysubj, testing="n", folder_name=None):
"""
Function of functions to check whether we have all the folder architecture in place to save videos.
"""
steps_back = depthToSrc()
path_data = folderData(steps_back)
path_day = folderTesting(path_data, testing, numdaysubj, folder_name)
path_audio = folderAudios(path_day)
return path_audio
def rm_mk_folder_name(path_day):
if os.path.exists(f"./src_testing/temp_folder_name.txt"):
os.remove(f"./src_testing/temp_folder_name.txt")
saveIndvVar("./src_testing/", path_day, "temp_folder_name")
#################################################################
########## Pipeline to set subject number & others ##############
#################################################################
def setSubjNum(file_pattern="data_subj_(.*).csv", folder_pattern="ex_(.)"):
"""
Function to set the number of the subject automatically
"""
patternc = re.compile(file_pattern)
patternf = re.compile(folder_pattern)
names = []
subjs = []
for foldername in os.listdir(f"./../../data/"):
if patternf.match(foldername):
for filename in os.listdir(f"./../../data/{foldername}/data/"):
if patternc.match(filename):
print(filename)
name, form = filename.split(".")
names.append(filename)
r = patternc.search(filename)
subjs.append(int(r.group(1)))
else:
continue
if len(subjs) < 1:
subject_n = 1
else:
subjs.sort()
subject_n = subjs[-1] + 1
return subject_n
def saveIndvVar(path, var, file_name):
"""
Function to save subject number in temporary file
"""
with open(f"{path}/{file_name}.txt", "w") as f:
f.write(str(var))
def txtToVar(path, file):
"""
Function to recover subject number in temporary files
"""
with open(f"{path}/{file}.txt", "r") as f:
var = f.readline()
return var
def createTempName(name):
t = time.localtime()
time_now = time.strftime("%H_%M_%S", t)
todaydate = date.today().strftime("%Y%m%d")
name_temp_file = f"{name}_temp_{time_now}_{todaydate}"
return name_temp_file
#################################################################
######################## Reading from CSV #######################
#################################################################
def getOrAsk(func, path, file_name):
if os.path.exists(f"{path}/{file_name}.csv"):
value = getValue(path, file=f"{file_name}")
else:
value = func()
return value
def csvtoDictZaber(path, file="temp_zaber_pos.csv"):
"""
Transforming csv of one set of Zabers to dictionary
"""
df = pd.read_csv(f"{path}/{file}", index_col="Zaber")
dictfromcsv = {}
for i in df.index.values:
dictfromcsv[i] = {}
for colu in df.columns:
# print(colu)
for r in df[colu].index.values:
dictfromcsv[r][colu] = df[colu][r]
return dictfromcsv
def csvToDictPanTiltsAll(path, file="temp_PanTilts.csv"):
"""
Transforming csv of all ROIs to a dictionary
"""
df = pd.read_csv(f"{path}/{file}", index_col="Axis")
PanTilts = df.to_dict()
for i in PanTilts:
PanTilts[i] = PanTilts[i]["x"], PanTilts[i]["y"], PanTilts[i]["z"]
return PanTilts
def csvToDictROI(path, file="temp_ROI.csv"):
"""
Transforming csv of one ROI to a dictionary
"""
df = pd.read_csv(f"{path}/{file}", index_col="Axis")
cd = df.to_dict()
centreROI = cd["1"]["x"], cd["1"]["y"]
return centreROI
def getValue(path, file="age.csv"):
"""
Get single value age from csv file
"""
dataframe = pd.read_csv(f"{path}/{file}", header=None)
value = dataframe[1][0]
return value
def csvToDictGridAll(path, file_pattern="temp_grid_(.*).csv"):
patternc = re.compile(file_pattern)
names = []
zabers = []
for filename in os.listdir(f"{path}"):
if patternc.match(filename):
name, form = filename.split(".")
names.append(filename)
r = patternc.search(filename)
zabers.append(r.group(1))
else:
continue
grid = {}
for n, z in zip(names, zabers):
grid[z] = csvToDictGridIndv(path, n)
return grid
def csvToDictGridIndv(path, file):
"""
Transforming csv of one Grid to a dictionary
"""
df = pd.read_csv(f"{path}/{file}", index_col="Axis")
grid_indv = df.to_dict()
return grid_indv
def csvToDictROIAll(path, file="temp_ROIs.csv"):
"""
Transforming csv of all ROIs to a dictionary
"""
df = pd.read_csv(f"{path}/{file}", index_col="Axis")
ROIs = df.to_dict()
for i in ROIs:
ROIs[i] = ROIs[i]["x"], ROIs[i]["y"]
return ROIs
def csvToDictHaxes(path, file="temp_haxes.csv"):
"""
Transforming csv of haxes to a dictionary
"""
df = pd.read_csv(f"{path}/{file}", index_col="Position")
haxes = df.to_dict()
for i in haxes:
haxes[i] = [haxes[i][1], haxes[i][2], haxes[i][3]]
return haxes
def checkFilePathSave(path, file_name, variable, subject_n, format="csv"):
if not os.path.exists(f"{path}/{file_name}.{format}"):
apendSingle(file_name, path, subject_n, variable)
#################################################################
######################## Deleting ###############################
#################################################################
def delTempFiles(path):
"""
Function to delete temporary files
"""
names = findTempFiles(path)
for i, n in enumerate(names):
os.remove(f"{path}/{n[0]}.{n[1]}")
print("Temporary data file Removed!")
#################################################################
######################## Globals ################################
#################################################################
def globalsToDict(globals):
all_globals = {}
for d in dir(globals):
if type(d) is not None:
print(globals.__dict__[d])
if "__" not in d:
all_globals[d] = [globals.__dict__[d]]
return all_globals
import ast
def recoverGlobals(path, name_file='all_globals'):
"""
Function to recover globals from a file
"""
pd_globals = pd.read_csv(f"{path}/{name_file}.csv")
dict_globals = pd_globals.to_dict(orient='records')[0]
for key, value in dict_globals.items():
if type(value) is str:
try: