-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
255 lines (233 loc) · 13.2 KB
/
project.py
File metadata and controls
255 lines (233 loc) · 13.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
import zipfile
import os
import shutil
from settings import ProjectPreferences
from patient import Patient
from group import Group
from protocol import Protocol
from dataset import Dataset
from visualization import Visualization
from typing import List
class Project:
"""description of class"""
def __init__(self, preferences: ProjectPreferences, patients: List[Patient] = None,
groups: List[Group] = None, protocols: List[Protocol] = None,
datasets: List[Dataset] = None, visualizations: List[Visualization] = None):
self.preferences = preferences
self.patients = patients if patients is not None else []
self.groups = groups if groups is not None else []
self.protocols = protocols if protocols is not None else []
self.datasets = datasets if datasets is not None else []
self.visualizations = visualizations if visualizations is not None else []
def save(self, path):
# General
project_directory_path = os.path.join(path,self.preferences.name)
try:
if os.path.exists(project_directory_path):
shutil.rmtree(project_directory_path)
os.mkdir(project_directory_path)
except OSError:
print("Creation of the directory %s failed" % project_directory_path)
else:
print("Successfully created the directory %s " % project_directory_path)
# Project Preferences
settings_path = os.path.join(project_directory_path, self.preferences.name + ".settings")
try:
self.preferences.to_json_file(settings_path)
except OSError:
print("Creation of the file %s failed" % settings_path)
else:
print("Successfully created the file %s " % settings_path)
# Patients
patients_directory_path = os.path.join(project_directory_path, "Patients")
try:
os.mkdir(patients_directory_path)
except OSError:
print("Creation of the directory %s failed" % patients_directory_path)
else:
print("Successfully created the directory %s " % patients_directory_path)
for patient in self.patients:
patient_path = os.path.join(patients_directory_path, patient.ID + ".patient")
try:
patient.to_json_file(patient_path)
except OSError:
print("Creation of the file %s failed" % patient_path)
else:
print("Successfully created the file %s " % patient_path)
# Groups
groups_directory_path = os.path.join(project_directory_path, "Groups")
try:
os.mkdir(groups_directory_path)
except OSError:
print("Creation of the directory %s failed" % groups_directory_path)
else:
print("Successfully created the directory %s " % groups_directory_path)
for group in self.groups:
group_path = os.path.join(groups_directory_path, group.ID + ".group")
try:
group.to_json_file(group_path)
except OSError:
print("Creation of the file %s failed" % group_path)
else:
print("Successfully created the file %s " % group_path)
# Protocols
protocols_directory_path = os.path.join(project_directory_path, "Protocols")
try:
os.mkdir(protocols_directory_path)
except OSError:
print("Creation of the directory %s failed" % protocols_directory_path)
else:
print("Successfully created the directory %s " % protocols_directory_path)
for protocol in self.protocols:
protocol_path = os.path.join(protocols_directory_path, protocol.ID + ".prov")
try:
protocol.to_json_file(protocol_path)
except OSError:
print("Creation of the file %s failed" % protocol_path)
else:
print("Successfully created the file %s " % protocol_path)
# Datasets
datasets_directory_path = os.path.join(project_directory_path, "Datasets")
try:
os.mkdir(datasets_directory_path)
except OSError:
print("Creation of the directory %s failed" % datasets_directory_path)
else:
print("Successfully created the directory %s " % datasets_directory_path)
for dataset in self.datasets:
dataset_path = os.path.join(datasets_directory_path, dataset.ID + ".dataset")
try:
dataset.to_json_file(dataset_path)
except OSError:
print("Creation of the file %s failed" % dataset_path)
else:
print("Successfully created the file %s " % dataset_path)
# Visualizations
visualizations_directory_path = os.path.join(project_directory_path, "Visualizations")
try:
os.mkdir(visualizations_directory_path)
except OSError:
print("Creation of the directory %s failed" % visualizations_directory_path)
else:
print("Successfully created the directory %s " % visualizations_directory_path)
for visualization in self.visualizations:
visualization_path = os.path.join(visualizations_directory_path, visualization.ID + ".visualization")
try:
visualization.to_json_file(visualization_path)
except OSError:
print("Creation of the file %s failed" % visualization_path)
else:
print("Successfully created the file %s " % visualization_path)
zip_path = project_directory_path + ".hibop"
try:
zip_file = zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(project_directory_path):
for dir in dirs:
dir_path = os.path.join(root, dir)
zip_file.write(dir_path, os.path.relpath(dir_path, project_directory_path))
for file in files:
file_path = os.path.join(root, file)
zip_file.write(file_path, os.path.relpath(file_path, project_directory_path))
zip_file.close()
if os.path.exists(project_directory_path):
shutil.rmtree(project_directory_path)
except OSError:
print("Creation of the zip %s failed" % zip_path)
else:
print("Successfully created the zip %s " % zip_path)
@classmethod
def load(cls, path):
# General
try:
with zipfile.ZipFile(path, "r") as zip:
namelist = zip.namelist()
# Get into tmp dir
if not os.path.isdir(".tmp"):
os.mkdir(".tmp")
os.chdir(".tmp")
# Load Settings
settings_path = next(name for name in namelist if str.endswith(name, ".settings"))
settings = None
try:
settings = ProjectPreferences.from_json_file(zip.extract(settings_path))
except OSError:
print("[1/1] Loading of the settings file %s failed" % settings_path)
else:
print("[1/1] Successfully loaded the settings file %s" % settings_path)
# Load Patients
patients_path = [name for name in namelist if str.endswith(name, ".patient")]
patients = []
for i, patient_path in enumerate(patients_path):
try:
patients.append(Patient.from_json_file(zip.extract(patient_path), settings.general_tags + settings.patients_tags + settings.sites_tags))
except OSError:
print("[{0}/{1}] Loading of the patient file {2} failed".format(i + 1, len(patients_path),
patient_path))
else:
print("[{0}/{1}] Successfully loaded the patient file {2}".format(i + 1, len(patients_path),
patient_path))
patients = [Patient.from_json_file(zip.extract(patient), settings.general_tags + settings.patients_tags + settings.sites_tags) for patient in patients_path]
# Load Groups
groups_path = [name for name in namelist if str.endswith(name, ".group")]
groups = []
for i, group_path in enumerate(groups_path):
try:
groups.append(Group.from_json_file(zip.extract(group_path), patients))
except OSError:
print("[{0}/{1}] Loading of the group file {2} failed".format(i + 1, len(groups_path),
group_path))
else:
print("[{0}/{1}] Successfully loaded the group file {2}".format(i + 1, len(groups_path),
group_path))
# Load Protocols
protocols_path = [name for name in namelist if str.endswith(name, ".prov")]
protocols = []
for i, protocol_path in enumerate(protocols_path):
try:
protocols.append(Protocol.from_json_file(zip.extract(protocol_path)))
except OSError:
print("[{0}/{1}] Loading of the protocol file {2} failed".format(i + 1, len(protocols_path),
protocol_path))
else:
print("[{0}/{1}] Successfully loaded the protocol file {2}".format(i + 1, len(protocols_path),
protocol_path))
# Load Datasets
datasets_path = [name for name in namelist if str.endswith(name, ".dataset")]
datasets = []
for i, dataset_path in enumerate(datasets_path):
try:
datasets.append(Dataset.from_json_file(zip.extract(dataset_path), protocols, patients))
except OSError:
print("[{0}/{1}] Loading of the dataset file {2} failed".format(i + 1, len(datasets_path),
dataset_path))
else:
print("[{0}/{1}] Successfully loaded the dataset file {2}".format(i + 1, len(datasets_path),
dataset_path))
# Load Visualizations
visualizations_path = [name for name in namelist if str.endswith(name, ".visualization")]
visualizations = []
for i, visualization_path in enumerate(visualizations_path):
try:
visualizations.append(Visualization.from_json_file(zip.extract(visualization_path),
patients, datasets))
except OSError:
print("[{0}/{1}] Loading of the visualization file {2} failed".format(i + 1,
len(visualizations_path),
visualization_path))
else:
print("[{0}/{1}] Successfully loaded the visualization file {2}".format(i + 1,
len(visualizations_path)
, visualization_path))
# Change dir back and delete tmp
os.chdir("..")
for root, dirs, files in os.walk(".tmp", topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(".tmp")
print("Successfully loaded the project %s " % path)
return cls(settings, patients, groups, protocols, datasets, visualizations)
except OSError as err:
print(err)
print("Loading of the project %s failed" % path)