forked from Substra/substra-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.py
435 lines (356 loc) · 15.5 KB
/
populate.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
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
import argparse
import os
import json
import shutil
import tempfile
import time
import zipfile
import logging
import substra
from termcolor import colored
logging.basicConfig(filename='populate.log',
format='[%(asctime)-15s: %(levelname)s] %(message)s')
dir_path = os.path.dirname(os.path.realpath(__file__))
USER, PASSWORD = ('admin', 'admin')
SUBSTRA_FOLDER = os.getenv('SUBSTRA_PATH', '/substra')
server_path = f'{SUBSTRA_FOLDER}/servermedias'
client = substra.Client()
PUBLIC_PERMISSIONS = {'public': True, 'authorized_ids': []}
def setup_config(network='docker'):
print('Init config for owkin and chunantes')
if network == 'docker':
client.add_profile('owkin', 'http://owkin.substra-backend:8000', '0.0',
user=USER, password=PASSWORD)
client.add_profile('chunantes', 'http://chunantes.substra-backend:8001', '0.0',
user=USER, password=PASSWORD)
client.add_profile('clb', 'http://clb.substra-backend:8002', '0.0',
user=USER, password=PASSWORD)
if network == 'skaffold':
# the usernames and passwords are defined in the skaffold.yaml file
client.add_profile('owkin', 'http://substra-backend.node-1', '0.0',
user='node-1', password='node-1pwd')
client.add_profile('chunantes', 'http://substra-backend.node-2', '0.0',
user='node-2', password='node-2pwd')
client.add_profile('clb', 'http://substra-backend.node-3', '0.0',
user='node-3', password='node-3pwd')
def zip_folder(path, destination):
zipf = zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(path):
for f in files:
abspath = os.path.join(root, f)
archive_path = os.path.relpath(abspath, start=path)
zipf.write(abspath, arcname=archive_path)
zipf.close()
def get_or_create(data, profile, asset, local=True):
client.set_profile(profile)
method_kwargs = {}
if not local:
method_kwargs['local'] = False
method = getattr(client, f'add_{asset}')
try:
r = method(data, **method_kwargs)
except substra.exceptions.AlreadyExists as e:
print(colored(e, 'cyan'))
key_or_keys = e.pkhash
else:
print(colored(json.dumps(r, indent=2), 'green'))
key_or_keys = [x.get('pkhash', x.get('key'))
for x in r] if isinstance(r, list) else r.get('pkhash', r.get('key'))
return key_or_keys
def update_datamanager(data_manager_key, data, profile):
client.set_profile(profile)
try:
r = client.update_dataset(data_manager_key, data)
except substra.exceptions.InvalidRequest as e:
# FIXME if the data manager is already associated with the objective
# backend answer with a 400 and a raw error coming from the
# ledger.
# this case will be handled soon, with the fabric SDK.
print(colored(str(e), 'red'))
else:
print(colored(json.dumps(r, indent=2), 'green'))
def do_populate():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-o', '--one-org', action='store_const', dest='nb_org', const=1,
help='Launch populate with one org')
group.add_argument('-tw', '--two-orgs', action='store_const', dest='nb_org', const=2,
help='Launch populate with two orgs')
group.add_argument('-th', '--three-orgs', action='store_const', dest='nb_org', const=3,
help='Launch populate with three orgs')
parser.add_argument('-a', '--archive', action='store_true',
help='Launch populate with archive data samples only')
parser.add_argument('-s', '--skaffold', action='store_true',
help='Launch populate with skaffold (K8S) network')
parser.set_defaults(nb_org=2)
args = vars(parser.parse_args())
network_type = 'skaffold' if args['skaffold'] else 'docker'
setup_config(network_type)
if args['nb_org'] == 1:
org_0 = org_1 = org_2 = 'owkin'
elif args['nb_org'] == 2:
org_0 = org_2 = 'owkin'
org_1 = 'chunantes'
elif args['nb_org'] == 3:
org_0 = 'owkin'
org_1 = 'chunantes'
org_2 = 'clb'
else:
raise Exception(f"Number of orgs {args['nb_org']} not in [1, 2, 3]")
print(f'will create datamanager with {org_1}')
# create datamanager with org1
data = {
'name': 'ISIC 2018',
'data_opener': os.path.join(dir_path, './fixtures/chunantes/datamanagers/datamanager0/opener.py'),
'type': 'Images',
'description': os.path.join(dir_path, './fixtures/chunantes/datamanagers/datamanager0/description.md'),
'permissions': PUBLIC_PERMISSIONS,
}
data_manager_org1_key = get_or_create(data, org_1, 'dataset')
####################################################
train_data_sample_keys = []
if not args['archive']:
print(f'register train data (from server) on datamanager {org_1} (will take datamanager creator as worker)')
data_samples_path = ['./fixtures/chunantes/datasamples/train/0024306',
'./fixtures/chunantes/datasamples/train/0024307',
'./fixtures/chunantes/datasamples/train/0024308']
for d in data_samples_path:
try:
shutil.copytree(os.path.join(dir_path, d),
os.path.join(server_path, d))
except FileExistsError:
pass
data = {
'paths': [os.path.join(server_path, d) for d in data_samples_path],
'data_manager_keys': [data_manager_org1_key],
'test_only': False,
}
train_data_sample_keys = get_or_create(data, org_1, 'data_samples', local=False)
else:
print(f'register train data on datamanager {org_1} (will take datamanager creator as worker)')
data = {
'paths': [
os.path.join(dir_path, './fixtures/chunantes/datasamples/train/0024306'),
os.path.join(dir_path, './fixtures/chunantes/datasamples/train/0024307'),
os.path.join(dir_path, './fixtures/chunantes/datasamples/train/0024308')
],
'data_manager_keys': [data_manager_org1_key],
'test_only': False,
}
train_data_sample_keys = get_or_create(data, org_1, 'data_samples')
####################################################
print(f'create datamanager, test data and objective on {org_0}')
data = {
'name': 'Simplified ISIC 2018',
'data_opener': os.path.join(dir_path, './fixtures/owkin/datamanagers/datamanager0/opener.py'),
'type': 'Images',
'description': os.path.join(dir_path, './fixtures/owkin/datamanagers/datamanager0/description.md'),
'permissions': PUBLIC_PERMISSIONS,
}
data_manager_org0_key = get_or_create(data, org_0, 'dataset')
print(f'create datamanager, test data and objective on {org_1} (should say "already exists")')
data = {
'name': 'Simplified ISIC 2018',
'data_opener': os.path.join(dir_path, './fixtures/owkin/datamanagers/datamanager0/opener.py'),
'type': 'Images',
'description': os.path.join(dir_path, './fixtures/owkin/datamanagers/datamanager0/description.md'),
'permissions': PUBLIC_PERMISSIONS,
}
get_or_create(data, org_1, 'dataset')
####################################################
print('register test data')
data = {
'paths': [
os.path.join(dir_path, './fixtures/owkin/datasamples/test/0024900'),
os.path.join(dir_path, './fixtures/owkin/datasamples/test/0024901')
],
'data_manager_keys': [data_manager_org0_key],
'test_only': True,
}
test_data_sample_keys = get_or_create(data, org_0, 'data_samples')
####################################################
print('register test data 2')
data = {
'paths': [
os.path.join(dir_path, './fixtures/owkin/datasamples/test/0024902'),
os.path.join(dir_path, './fixtures/owkin/datasamples/test/0024903')
],
'data_manager_keys': [data_manager_org0_key],
'test_only': True,
}
get_or_create(data, org_0, 'data_samples')
####################################################
print('register test data 3')
data = {
'paths': [
os.path.join(dir_path, './fixtures/owkin/datasamples/test/0024904'),
os.path.join(dir_path, './fixtures/owkin/datasamples/test/0024905')
],
'data_manager_keys': [data_manager_org0_key],
'test_only': True,
}
get_or_create(data, org_0, 'data_samples')
####################################################
with tempfile.TemporaryDirectory() as tmp_dir:
print('register objective')
objective_path = os.path.join(
dir_path, './fixtures/chunantes/objectives/objective0/')
zip_path = os.path.join(tmp_dir, 'metrics.zip')
zip_folder(objective_path, zip_path)
data = {
'name': 'Skin Lesion Classification Objective',
'description': os.path.join(dir_path, './fixtures/chunantes/objectives/objective0/description.md'),
'metrics_name': 'macro-average recall',
'metrics': zip_path,
'permissions': PUBLIC_PERMISSIONS,
'test_data_sample_keys': test_data_sample_keys,
'test_data_manager_key': data_manager_org0_key
}
objective_key = get_or_create(data, org_0, 'objective')
####################################################
print('register objective without data manager and data sample')
objective_path = os.path.join(
dir_path, './fixtures/chunantes/objectives/objective0/')
zip_path = os.path.join(tmp_dir, 'metrics2.zip')
zip_folder(objective_path, zip_path)
data = {
'name': 'Skin Lesion Classification Objective',
'description': os.path.join(dir_path, './fixtures/owkin/objectives/objective0/description.md'),
'metrics_name': 'macro-average recall',
'metrics': zip_path,
'permissions': PUBLIC_PERMISSIONS,
}
get_or_create(data, org_0, 'objective')
####################################################
# update datamanager
print('update datamanager')
data = {
'objective_key': objective_key
}
update_datamanager(data_manager_org1_key, data, org_0)
####################################################
# register algo
print('register algo')
data = {
'name': 'Logistic regression',
'file': os.path.join(dir_path, './fixtures/chunantes/algos/algo3/algo.tar.gz'),
'description': os.path.join(dir_path, './fixtures/chunantes/algos/algo3/description.md'),
'permissions': PUBLIC_PERMISSIONS,
}
algo_key = get_or_create(data, org_2, 'algo')
####################################################
print('register algo 2')
data = {
'name': 'Neural Network',
'file': os.path.join(dir_path, './fixtures/chunantes/algos/algo0/algo.tar.gz'),
'description': os.path.join(dir_path, './fixtures/chunantes/algos/algo0/description.md'),
'permissions': PUBLIC_PERMISSIONS,
}
algo_key_2 = get_or_create(data, org_1, 'algo')
####################################################
data = {
'name': 'Random Forest',
'file': os.path.join(dir_path, './fixtures/chunantes/algos/algo4/algo.tar.gz'),
'description': os.path.join(dir_path, './fixtures/chunantes/algos/algo4/description.md'),
'permissions': PUBLIC_PERMISSIONS,
}
algo_key_3 = get_or_create(data, org_1, 'algo')
####################################################
# create traintuple
print('create traintuple')
data = {
'algo_key': algo_key,
'objective_key': objective_key,
'data_manager_key': data_manager_org1_key,
'train_data_sample_keys': train_data_sample_keys[:2]
# This traintuple should succeed.
# It doesn't have a tag, so it can be used as a test
# of the "non-bundled" display in substra-frontend.
}
traintuple_key = get_or_create(data, org_1, 'traintuple')
print('create second traintuple')
data = {
'algo_key': algo_key_2,
'data_manager_key': data_manager_org1_key,
'objective_key': objective_key,
'train_data_sample_keys': train_data_sample_keys[:2],
'tag': '(should fail) My super tag'
}
get_or_create(data, org_1, 'traintuple')
print('create third traintuple')
data = {
'algo_key': algo_key_3,
'data_manager_key': data_manager_org1_key,
'objective_key': objective_key,
'train_data_sample_keys': train_data_sample_keys[:2],
'tag': '(should fail) My other tag'
}
get_or_create(data, org_1, 'traintuple')
####################################################
client.set_profile(org_1)
res = client.get_traintuple(traintuple_key)
print(colored(json.dumps(res, indent=2), 'green'))
# create testtuple
print('create testtuple')
data = {
'traintuple_key': traintuple_key,
'tag': 'substra',
}
testtuple_key = get_or_create(data, org_1, 'testtuple')
client.set_profile(org_1)
res_t = client.get_testtuple(testtuple_key)
print(colored(json.dumps(res_t, indent=2), 'yellow'))
testtuple_status = None
traintuple_status = None
client.set_profile(org_1)
while traintuple_status not in ('done', 'failed') or testtuple_status not in ('done', 'failed'):
res = client.get_traintuple(traintuple_key)
res_t = client.get_testtuple(testtuple_key)
if traintuple_status != res['status'] or testtuple_status != res_t['status']:
traintuple_status = res['status']
testtuple_status = res_t['status']
print('')
print('-' * 100)
print(colored(json.dumps(res, indent=2), 'green'))
print(colored(json.dumps(res_t, indent=2), 'yellow'))
else:
print('.', end='', flush=True)
time.sleep(3)
####################################################
# Compute plan
print('create compute plan')
traintuples_data = [
{
"data_manager_key": data_manager_org1_key,
"train_data_sample_keys": [train_data_sample_keys[0], train_data_sample_keys[2]],
"traintuple_id": "dummy_traintuple_id",
"in_models_ids": [],
"tag": "",
},
]
testtuples_data = [
# {
# "traintuple_id": "dummy_traintuple_id",
# "tag": "",
# }
]
compute_plan_data = {
"algo_key": algo_key, # logistic regression, org2
"objective_key": objective_key, # org 0
"traintuples": traintuples_data,
"testtuples": testtuples_data,
}
# until both chaincode, backend and sdk can handle compute plan collisions, we need to have a
# generic try-except so that this script can run multiple times in a row
try:
client.set_profile(org_1)
res = client.add_compute_plan(compute_plan_data)
print(colored(json.dumps(res, indent=2), 'green'))
except: # noqa: E722
print(colored('Could not create compute plan', 'red'))
if __name__ == '__main__':
try:
do_populate()
except substra.exceptions.HTTPError as e:
print(colored(str(e), 'red'))
exit(1)