forked from ricgambino/XNAT-PIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessory_functions.py
332 lines (296 loc) · 14.3 KB
/
accessory_functions.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
# -*- coding: utf-8 -*-
"""
Created on May 30, 2022
@author: Riccardo Gambino
"""
import os
import pandas as pd
from tabulate import tabulate
from PIL import Image, ImageTk
from read_visupars import read_visupars_parameters
import pydicom
import datetime
import datefinder
import time
# Accessory functions
def disable_buttons(list_of_buttons):
for btn in list_of_buttons:
try:
btn.configure(state="disabled")
except:
pass
def enable_buttons(list_of_buttons):
for btn in list_of_buttons:
try:
btn.configure(state="normal")
except:
pass
def destroy_widgets(widgets):
for widget in widgets:
try:
widget.destroy()
except:
pass
def delete_widgets(canva, widgets_id):
for id in widgets_id:
try:
canva.delete(id)
except:
pass
def get_dir_size(path):
total_weight = 0
if os.path.isdir(path):
for l, level1 in enumerate(os.listdir(path)):
if os.path.isfile(os.path.join(path, level1)):
total_weight += os.path.getsize(os.path.join(path, level1))
elif os.path.isdir(os.path.join(path, level1)):
for m, level2 in enumerate(os.listdir(os.path.join(path, level1))):
if os.path.isfile(os.path.join(path, level1, level2)):
total_weight += os.path.getsize(os.path.join(path, level1, level2))
elif os.path.isdir(os.path.join(path, level1, level2)):
for n, level3 in enumerate(os.listdir(os.path.join(path, level1, level2))):
if os.path.isfile(os.path.join(path, level1, level2, level3)):
total_weight += os.path.getsize(os.path.join(path, level1, level2, level3))
elif os.path.isdir(os.path.join(path, level1, level2, level3)):
for p, level4 in enumerate(os.listdir(os.path.join(path, level1, level2, level3))):
if os.path.isfile(os.path.join(path, level1, level2, level3, level4)):
total_weight += os.path.getsize(os.path.join(path, level1, level2, level3, level4))
# elif os.path.isdir(os.path.join(path, level1, level2, level3,level4)):
# for q, level5 in enumerate(os.listdir(os.path.join(path, level1, level2, level3, level4))):
# if os.path.isfile(os.path.join(path, level1, level2, level3, level4, level5)):
# total_weight += os.path.getsize(os.path.join(path, level1, level2, level3, level4, level5))
# elif os.path.isdir(os.path.join(path, level1, level2, level3,level4, level5)):
# for q, level6 in enumerate(os.listdir(os.path.join(path, level1, level2, level3, level4, level5))):
# if os.path.isfile(os.path.join(path, level1, level2, level3, level4, level5, level6)):
# total_weight += os.path.getsize(os.path.join(path, level1, level2, level3, level4, level5, level6))
return round(total_weight, 2)
else:
return 0
def read_table(path_to_read):
data_dict = {}
data = pd.read_table(path_to_read, delimiter='\s\s+', header=[0], skiprows=1,
dtype={'Variable': str}, engine='python').values.tolist()
for elem in data:
data_dict[elem[0]] = elem[1]
return data_dict
def write_table(path_to_write, edit):
try:
# If there is already a file, edit it
data = read_table(path_to_write)
for j in edit.keys():
for k in data.keys():
if j == k:
data[k] = edit[j]
with open(path_to_write.replace('\\', '/'), 'w+') as out_file:
out_file.write(tabulate(data.items(), headers=['Variable', 'Value']))
except:
# If the file does not exist yet
with open(path_to_write.replace('\\', '/'), 'w+') as out_file:
# out_file.write(tabulate(info.items(), headers=['Variable', 'Value']))
out_file.write(tabulate(edit.items(), headers=['Variable', 'Value']))
def open_image(path, width=0, height=0):
image = Image.open(path).convert("RGBA")
if width != 0 or height != 0:
image = image.resize((int(width), int(height)), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
return image
#Read the acq. date from visu_pars file for Bruker file or from DICOM files
def read_acq_date(path):
match_date = ''
for dirpath, dirnames, filenames in os.walk(path.replace('\\', '/')):
# Check if the visu pars file is in the scans
for filename in [f for f in filenames if f.startswith("visu_pars")]:
acq_date = read_visupars_parameters((dirpath + "\\" + filename).replace('\\', '/'))["VisuAcqDate"]
# Read the date
matches = datefinder.find_dates(str(acq_date))
for match in matches:
match_date = match.strftime('%Y-%m-%d')
return match_date
# Check if the DICOM is in the scans
for filename in [f for f in filenames if f.endswith(".dcm")]:
dataset = pydicom.dcmread((dirpath + "\\" + filename).replace('\\', '/'))
match_date = datetime.datetime.strptime(dataset.AcquisitionDate, '%Y%m%d').strftime('%Y-%m-%d')
return match_date
return match_date
def metadata_params(information_folder, value):
# If value = 0 --> project
# If value = 1 --> subject
# If value = 2 --> experiment
if value == 0:
tmp_dict = {}
results_dict = {}
# Get a list of workbook paths
path_list = []
todos = {}
todos_tmp = {}
exp = []
# Scan all files contained in the folder that the user has provided
for item in os.listdir(information_folder):
path = str(information_folder + "\\" + item).replace('\\', '/')
# Check if the content of the project is a folder and therefore a patient or a file not to be considered
if os.path.isdir(path):
# Architecture of the project: project-subject-experiment
for item2 in os.listdir(path):
path1 = str(path + "\\" + item2).replace('\\', '/')
if os.path.isdir(path1):
path_list.append(path1)
exp.append(str(item2))
todos_tmp = {item: exp}
exp = []
todos.update(todos_tmp)
todos_tmp = {}
elif value == 1:
tmp_dict = {}
results_dict = {}
sub = str(information_folder).rsplit("/",1)[1]
# Get a list of workbook paths
path_list = []
todos = {}
exp = []
# Scan all files contained in the folder that the user has provided
for item in os.listdir(information_folder):
path = str(information_folder + "\\" + item).replace('\\', '/')
# Check if the content of the subject is a folder and therefore a patient or a file not to be considered
if os.path.isdir(path):
# Architecture of the project: project-subject-experiment
if os.path.isdir(path):
path_list.append(path)
exp.append(str(item))
todos = {sub : exp}
elif value == 2:
tmp_dict = {}
results_dict = {}
# Get a list of workbook paths
path_list = [information_folder]
todos = {str(information_folder).rsplit("/",3)[2] : [str(information_folder).rsplit("/",3)[3]]}
path_list1 = {}
# Scan all files contained in the folder that the user has provided
for path in path_list:
exp = str(path).rsplit("/",3)[3]
sub = str(path).rsplit("/",3)[2]
prj = str(path).rsplit("/",3)[1]
name = exp + "_" + "Custom_Variables.txt"
keys = sub + "#" + exp
path_list1.update({keys : path})
# Check if the txt file is in folder of the patient
tmp_path = ''
for dirpath, dirnames, filenames in os.walk(path.replace('\\', '/')):
# Check if the visu pars file is in the scans
for filename in [f for f in filenames if f.startswith("visu_pars")]:
tmp_path = path
break
# Check if the DICOM is in the scans
for filename in [f for f in filenames if f.endswith(".dcm")]:
tmp_path = path + "\\MR\\"
break
if tmp_path:
break
# If the file exists, read le info
if os.path.exists((tmp_path + "\\" + name).replace('\\', '/')):
subject_data = read_table((tmp_path + "\\" + name).replace('\\', '/'))
tmp_dict = {keys: subject_data}
else:
# If the txt file do not exist, load default value
# Project: name of main folder
# Subject: name of internal folders
# Acq date: from visu_pars file for BRUKER, from DICOM from DICOM file
#
# Load the acq. date for BRUKER file
try:
tmp_acq_date = read_acq_date(path)
except Exception as e:
tmp_acq_date = ''
subject_data = {"Project": prj,
"Subject": sub,
"Experiment": exp,
"Acquisition_date": tmp_acq_date,
"SubjectsCV": "",
"SubjectsGroup": "",
"SubjectsTimepoint":"",
"SubjectsDose":"",
"SessionsCV": "",
"SessionsGroup": "",
"SessionsTimepoint":"",
"SessionsDose":""
}
tmp_dict = {keys: subject_data}
results_dict.update(tmp_dict)
return [results_dict, todos, path_list, path_list1]
def write_tree(folder_path):
dict_items = {}
j = 0
dict_items[str(j)] = {}
dict_items[str(j)]['parent'] = ""
dict_items[str(j)]['text'] = folder_path.split('/')[-1]
subdir = os.listdir(folder_path)
subdirectories = [x for x in subdir if x.endswith('.ini') == False]
total_weight = 0
last_edit_time = ''
j = 1
for sub in subdirectories:
if os.path.isfile(os.path.join(folder_path, sub)):
# Check for last edit time
edit_time = str(time.strftime("%d/%m/%Y,%H:%M:%S", time.localtime(os.path.getmtime(os.path.join(folder_path, sub)))))
if last_edit_time == '' or edit_time > last_edit_time:
# Update the last edit time
last_edit_time = edit_time
# Check for file dimension
file_weight = round(os.path.getsize(os.path.join(folder_path, sub))/1024, 2)
total_weight += round(file_weight/1024, 2)
# Add the item like a file
dict_items[str(j)] = {}
dict_items[str(j)]['parent'] = '0'
dict_items[str(j)]['text'] = sub
dict_items[str(j)]['values'] = (edit_time, str(file_weight) + "KB", "File")
# Update the j counter
j += 1
elif os.path.isdir(os.path.join(folder_path, sub)):
current_weight = 0
last_edit_time_lev2 = ''
branch_idx = j
dict_items[str(j)] = {}
dict_items[str(j)]['parent'] = '0'
dict_items[str(j)]['text'] = sub
j += 1
# Scan directories to get subfolders
sub_level2 = os.listdir(os.path.join(folder_path, sub))
subdirectories2 = [x for x in sub_level2 if x.endswith('.ini') == False]
for sub2 in subdirectories2:
if os.path.isfile(os.path.join(folder_path, sub, sub2)):
# Check for last edit time
edit_time = str(time.strftime("%d/%m/%Y,%H:%M:%S", time.localtime(os.path.getmtime(os.path.join(folder_path, sub, sub2)))))
if last_edit_time_lev2 == '' or edit_time > last_edit_time_lev2:
# Update the last edit time
last_edit_time_lev2 = edit_time
if last_edit_time_lev2 > last_edit_time:
last_edit_time = last_edit_time_lev2
# Check for file dimension
file_weight = round(os.path.getsize(os.path.join(folder_path, sub, sub2))/1024, 2)
current_weight += round(file_weight/1024, 2)
# Add the item like a file
dict_items[str(j)] = {}
dict_items[str(j)]['parent'] = '1'
dict_items[str(j)]['text'] = sub2
dict_items[str(j)]['values'] = (edit_time, str(file_weight) + "KB", "File")
# Update the j counter
j += 1
elif os.path.isdir(os.path.join(folder_path, sub, sub2)):
# Check for last edit time
edit_time = str(time.strftime("%d/%m/%Y,%H:%M:%S", time.localtime(os.path.getmtime(os.path.join(folder_path, sub, sub2)))))
if last_edit_time_lev2 == '' or edit_time > last_edit_time_lev2:
# Update the last edit time
last_edit_time_lev2 = edit_time
if last_edit_time_lev2 > last_edit_time:
last_edit_time = last_edit_time_lev2
folder_size = round(get_dir_size(os.path.join(folder_path, sub, sub2))/1024/1024, 2)
current_weight += folder_size
dict_items[str(j)] = {}
dict_items[str(j)]['parent'] = '1'
dict_items[str(j)]['text'] = sub2
dict_items[str(j)]['values'] = (edit_time, str(folder_size) + 'MB', "Folder")
j += 1
total_weight += current_weight
dict_items[str(branch_idx)]['values'] = (last_edit_time_lev2, str(round(current_weight, 2)) + "MB", "Folder")
# Update the fields of the parent object
dict_items['0']['values'] = (last_edit_time, str(round(total_weight/1024, 2)) + "GB", "Folder")
return dict_items