forked from yves-weissenberger/twoptb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_to_hdf5.py
executable file
·127 lines (99 loc) · 3.86 KB
/
convert_to_hdf5.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
#!/home/yves/anaconda2/bin/python
#
#
#
#
#
#
# ==========================================
import os
import sys
import re
import time
import numpy as np
import h5py
import matplotlib.pyplot as plt
def findpath():
cDir = os.path.dirname(os.path.realpath(__file__))
found = False
while not found:
cDir,ext = os.path.split(cDir)
if ext=='twoptb':
found = False
twoptb_path = cDir
print
break
return twoptb_path
twoptb_path = findpath()
sys.path.append(twoptb_path)
import twoptb as MP
def convert_day_data_to_hdf5(base_path):
exclude_list = ['cent','Cent','proc_log.txt','processed','random','.tar','false_starts']
#base_path = os.path.abspath('.')
animal_ID = os.path.split(base_path)[-1]
folders = os.listdir(base_path)
print folders
#print fs
logLoc = os.path.join(base_path,'proc_log.txt')
if 'proc_log.txt' not in folders:
logF = open(logLoc,'wb')
logF.write(animal_ID+'\n')
logF.close()
print '...starting log'
else:
logF = open(logLoc,'r')
for l in logF:
if re.findall(r'converted (.*) to hdf5',l):
exclude_list.append(re.findall(r'converted (.*) to hdf5',l)[0])
logF.close()
#HDF_File,file_path = MP.file_management.create_base_hdf(animal_ID=animal_name,file_loc='/media/yves/Storage 2/')
print exclude_list
print 'loading folders \n\n'
for fold_nm in folders:
if all([crit not in fold_nm for crit in exclude_list]):
print fold_nm
##################
with open(logLoc,'a') as logF:
for fold_nm in folders:
#print folders
fold_dir = os.path.join(base_path,fold_nm)
if all([crit not in fold_dir for crit in exclude_list]):
if (('tonemapping' in fold_dir and fold_dir!=animal_ID+'_tonemapping') or
('Tonotopy' in fold_dir and fold_dir!=animal_ID+'_Tonotopy')):
fs = os.listdir(fold_dir)
fs = [i for i in fs if i !=((animal_ID + '_tonemapping') or (animal_ID + '_Tonotopy'))]
fs = [i for i in fs if i!=('proc_log.txt')]
print fs
session_ID = 'tonemapping'
else:
session_ID = fold_nm
pass
procDir = os.path.join(base_path,'processed')
if not os.path.exists(procDir):
os.mkdir(procDir)
print fold_nm
HDF_File,file_path = MP.file_management.create_base_hdf(animal_ID=animal_ID+'_'+fold_nm,file_loc=procDir)
HDF_File = MP.file_management.add_session_groups(file_handle = HDF_File,
session_ID=session_ID)
print 'converting data to HDF5...'
st = time.time()
fs = os.listdir(fold_dir)
fs.sort() ######make sure things are loaded in the correct order
#print fs
MP.file_management.add_raw_series(baseDir=fold_dir,
file_Dirs=fs,
HDF_File=HDF_File,
session_ID=session_ID)
logF.write('converted %s to hdf5 \n' %fold_nm)
print time.time() - st
return None
if __name__ == "__main__":
if sys.argv[1]=='-help':
print 'first argument specifies what folders to exclude'
elif sys.argv[1]==None:
base_path = os.path.abspath('.')
print 'No explicit directory to load specified, using currenct directory \n %s' %base_path
else:
base_path = os.path.abspath(sys.argv[1])
print 'converting data from \n %s \n to hdf5' %base_path
convert_day_data_to_hdf5(base_path)