-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxnat_eeg_upload.py
191 lines (123 loc) · 4.99 KB
/
xnat_eeg_upload.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
#!\\usr\\bin\\python\\env
import os.path
import os
import sys
from xnatutils import put as xnat_put
from xnatutils.base import connect as xnat_connect
import tkinter.filedialog
from tkinter import messagebox
import shutil
from xnat.exceptions import XNATResponseError
def eeg_upload():
root = tkinter.Tk()
root.withdraw() # use to hide tkinter window
def show_error_and_quit(message):
messagebox.showinfo("Upload Error", message)
sys.exit()
currdir = os.getcwd()
base_path = tkinter.filedialog.askdirectory(
parent=root, initialdir=currdir,
title='Please select directory where data is saved')
if len(base_path) > 0:
print("You chose %s" % base_path)
# Set unique variables (researcherName, subID, studyID, xnatID, xnatSESS)
# researcherName= input('Reseacher Name (as written in PROJECTS folder): ')
hdd_backup = input('Do you also backup your data to a Hard drive? (y/n): ')
if hdd_backup == 'y':
root = tkinter.Tk()
root.withdraw() # use to hide tkinter window
currdir = os.getcwd()
hdd_path = tkinter.filedialog.askdirectory(
parent=root, initialdir=currdir,
title='Please select backup hard drive')
if len(hdd_path) > 0:
print("You chose %s" % hdd_path)
studyID = input('Study prefix (eg.HUB): ')
dataType = input('Data Type (eg.Cognitive_data or TMS_EEG_data): ')
subID = input('Subject ID: ').split()
xnatID = input('MBI XNAT project number (eg.MRH001): ')
xnatSESS = input('Session number (eg.EEG01): ')
# Sets xnatutils to never save password
# Keep on always when on shared computer
mbi_xnat = xnat_connect()
# Sets path. Change if using script on personal computer
# base_path = '/Volumes/RAW_DATA/'
# Loop over IDs and seacrh for files containing spaces and remove the space
loopText = "LOOPING OVER FILES AND CLEANING FILENAMES FOR: "
dots = "..."
for ID in subID:
loopPrint = '{}{}{}'.format(loopText, ID, dots)
print(loopPrint)
path = os.path.join(base_path, studyID, dataType, ID)
print(path)
try:
os.chdir(path)
except Exception:
show_error_and_quit(
("You have entered the wrong study ID ('{}'), datatype ('{}') "
"or ID ('{}') or the directory doesn't exist")
.format(studyID, dataType, ID))
filenames = os.listdir('.')
for f in os.listdir("."):
r = f.replace(" ", "")
if(r != f):
os.rename(f, r)
source = os.listdir(path)
temp = 'temp'
path_temp = os.path.join(path, temp)
if not os.path.isdir(path_temp):
os.makedirs(path_temp)
for files in source:
if files.startswith("."):
shutil.move(files, path_temp)
if hdd_backup == 'y':
hdd_full_path = os.path.join(hdd_path, studyID, ID)
os.makedirs(hdd_full_path, exist_ok=True)
for files in source:
shutil.copy2(files, hdd_full_path)
# Loop over IDs and if file begins with ID, place prefix (studyID) at the
# start
print("FILE NAMES CLEANED....")
if hdd_backup == 'y':
print("Data being backed up to Hard drive...")
print("BEGINNING UPLOAD...")
# Loop over IDs and upload to xnat
try:
for ID in subID:
path = os.path.join(base_path, studyID, dataType, ID)
print(path)
fullsessID = '{}_{}{}_{}'.format(xnatID, studyID, ID, xnatSESS)
all_fnames = os.listdir(path)
print(all_fnames)
all_fname_bases = set(os.path.splitext(f)[0] for f in all_fnames)
for base in all_fname_bases:
print(base)
fnames = [
os.path.join(path, f)
for f in all_fnames if os.path.splitext(f)[0] == base]
scan_name = '_'.join(base.split('_')[1:])
if not scan_name:
scan_name = base
try:
xnat_put(
fullsessID,
scan_name,
*fnames,
create_session=True,
resource_name='CURRY',
connection=mbi_xnat)
except XNATResponseError as e:
if '(status 409)' in str(e):
print("'{}' scan already exists for '{}' session, "
"skipping".format(base, fullsessID))
else:
raise
except Exception as e:
print(e)
show_error_and_quit(
"You have entered an incorrect XNAT ID ('{}') or XNAT session "
"('{}'), the XNAT project ID doesn't exist or there is already "
"data uploaded with that ID".format(xnatID, xnatSESS))
print("FINISHED")
if __name__ == '__main__':
eeg_upload()