forked from ome/training-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_projects.py
261 lines (199 loc) · 9.05 KB
/
copy_projects.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
## known limitations
# Does not take into account images in multiple datasets
import os
import subprocess
from random import choice
from string import ascii_letters
import csv
import toolbox
import argh
import logging
logger = logging.Logger(name=__name__, level=logging.INFO)
FULL_NAMES = ["Francis Crick",
"Linda Buck",
"Charles Darwin",
"Marie Curie",
"Alexander Fleming",
"Rosalind Franklin",
"Robert Hooke",
"Jane Goodall",
"Gregor Mendel",
"Barbara McClintock",
"Louis Pasteur",
"Ada Lovelace",
"Linus Pauling",
"Frances Kelsey",
"Maurice Wilkins",
"Florence Nightingale",
"John Sulston",
"Elizabeth Blackwell",
"Richard Dawkins",
"Caroline Dean",
"Stephen Reicher",
"Wendy Barclay",
"Paul Nurse",
"Jennifer Doudna",
"Adrian Thomas",
"Ann Clarke",
"Oswald Avery",
"Liz Sockett",
"Erwin Chargaff",
"Tracey Rogers",
"Ronald Fisher",
"Rachel Carson",
"William Harvey",
"Nettie Stevens",
"Jeffrey Hall",
"Youyou Tu",
"Michael Rosbash",
"Carol Greider",
"Yoshinori Ohsumi",
"Rosalyn Yalow",
"Amedeo Avogadro",
"Virginia Apgar",
"Kristian Birkeland",
"Mary Anning",
"Chen-Ning Yang",
"Stephanie Kwolek",
"Jagadish Bose",
"Rita Levi-Montalcini",
"Susumu Tonegawa",
"Irene Joliot-Curie",
]
def create_users(admin_conn, save_dir: str, nb_users: int, nb_trainers: int):
users = {}
# creating trainer names and passwords
for trainer_nb in range(nb_trainers):
users[f'trainer-{trainer_nb + 1}'] = "".join([choice(ascii_letters) for _ in range(8)])
# creating user names and paswords
for user_nb in range(nb_users):
users[f'user-{user_nb}'] = "".join([choice(ascii_letters) for _ in range(8)])
# save file with user names and passwords
save_path = os.path.join(save_dir, 'user_passwords.csv')
with open(save_path, 'w') as f:
writer = csv.writer(f)
for user_name, password in users.items():
writer.writerow([user_name, password])
admin_uuid = admin_conn.getSession().getUuid().getValue()
host = admin_conn.host
port = admin_conn.port
# creating groups
run_command(f"omero group -k {admin_uuid} -s {host} -p {port} add Lab1 --perms 'rwra--' --ignore-existing")
run_command(f"omero group -k {admin_uuid} -s {host} -p {port} add Lab2 --perms 'rwr---' --ignore-existing")
run_command(f"omero group -k {admin_uuid} -s {host} -p {port} add Lab3 --perms 'rwrw--' --ignore-existing")
run_command(f"omero group -k {admin_uuid} -s {host} -p {port} add Lab4 --perms 'rw----' --ignore-existing")
logger.info('Groups created')
# creating users
for n, (u, w) in enumerate(users.items()):
run_command(f"omero user -k {admin_uuid} -s {host} -p {port} add {u} {FULL_NAMES[n]} Lab1 Lab2 Lab3 Lab4 -P {w} --ignore-existing")
logger.info(f'User {u} created')
return users
def run_command(command):
try:
output = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
print(output)
except subprocess.CalledProcessError as e:
logger.error(f'Error: {e.output}')
logger.error(f'Command: {e.cmd}')
raise RuntimeError(f'Command could not be run: {command}')
return output.stdout.decode('utf-8')
def copy_annotations(source_object, dest_object):
copy_tags(source_object, dest_object)
copy_kvps(source_object, dest_object)
copy_attachments(source_object, dest_object)
def copy_tags(source_object, dest_object):
pass
def copy_kvps(dest_conn, source_object, dest_object):
for ann in source_object.listAnnotations():
if type(ann).__name__ == 'MapAnnotationWrapper':
new_ann = toolbox.create_annotation_map(dest_conn, ann.getValue())
toolbox.link_annotation(dest_object, new_ann)
def copy_attachments(source_object, dest_object):
pass
def copy_rois(source_image, dest_image):
pass
def copy_description(dest_conn, source_obj, dest_object):
dest_object.setDescription(source_obj.getDescription())
return dest_conn.getUpdateService().saveAndReturnObject(dest_object)
def copy_object_annotations(source_conn, dest_conn, source_obj_id, dest_obj_id, obj_type):
source_obj = source_conn.getObject(obj_type, source_obj_id)
dest_obj = dest_conn.getObject(obj_type, dest_obj_id)
dest_obj = copy_description(dest_conn, source_obj, dest_obj)
copy_kvps(dest_conn, source_obj, dest_obj)
def get_original_file_names(image):
fileset = image.getFileset()
return [f.getName() for f in fileset.listFiles()]
def copy_image(source_conn, dest_conn, source_image, dest_dataset):
source_uuid = source_conn.getSession().getUuid().getValue()
dest_uuid = dest_conn.getSession().getUuid().getValue()
source_host = source_conn.host
source_port = source_conn.port
dest_host = dest_conn.host
dest_port = dest_conn.port
image_path = os.path.join(os.environ['OMERO_DATA_DIR'], get_original_file_names(source_image)[0].replace(' ', ''))
print(image_path)
if not os.path.exists(f'{image_path}'):
run_command(f'omero download -k {source_uuid} -s {source_host} -p {source_port} Image:{source_image.getId()} {image_path}')
output = run_command(f'omero import -k {dest_uuid} -s {dest_host} -p {dest_port} -d {dest_dataset.getId().getValue()} {image_path}')
print(output)
try:
dest_images = [toolbox.get_image(dest_conn, i) for i in eval(output[6:])]
except TypeError:
dest_images = [toolbox.get_image(dest_conn, eval(output[6:]))]
if len(dest_images) == 1:
copy_image_annotations(source_conn, dest_conn. source_image, dest_images[0])
def copy_image_annotations(source_conn, dest_conn, source_image, dest_image):
copy_kvps(dest_conn, source_image, dest_image)
def match_images(source_conn, dest_conn, source_dataset_id, dest_dataset_id):
return [0], [0]
def copy_dataset(source_conn, dest_conn, source_dataset, dest_project):
dest_dataset = toolbox.create_dataset(connection=dest_conn,
dataset_name=source_dataset.getName(),
dataset_description=source_dataset.getDescription(),
parent_project=dest_project)
copy_dataset_annotations(source_conn, dest_conn, source_dataset.getId(), dest_dataset.getId())
images = source_dataset.listChildren()
orig_file_names = []
for image in images:
if get_original_file_names(image)[0] not in orig_file_names:
orig_file_names.append(get_original_file_names(image)[0])
copy_image(source_conn, dest_conn, image, dest_dataset)
def copy_dataset_annotations(source_conn, dest_conn, source_dataset_id, dest_dataset_id):
pass
def copy_project(source_conn, dest_conn, source_project):
dest_project = toolbox.create_project(dest_conn, source_project.getName())
copy_project_annotations(source_conn, dest_conn, source_project.getId(), dest_project.getId())
datasets = source_project.listChildren()
for dataset in datasets:
copy_dataset(source_conn, dest_conn, dataset, dest_project)
def copy_project_annotations(source_conn, dest_conn, source_project_id, dest_project_id):
pass
def run(source_conf, dest_conf, admin_conf, source_project_ids: list, nb_users: int, nb_trainers: int, passwords=None):
if nb_users > len(FULL_NAMES):
raise ValueError(f'The number of users may not be larger than {len(FULL_NAMES)}')
try:
admin_conn = toolbox.open_connection(**admin_conf)
if passwords is None:
users = create_users(admin_conn, os.environ['OMERO_DATA_DIR'], nb_users=nb_users, nb_trainers=nb_trainers)
else:
users = passwords
admin_conn.close()
try:
source_conn = toolbox.open_connection(**source_conf)
for user_nb, (user_name, user_pw) in enumerate(users.items()):
dest_conf['username'] = user_name
dest_conf['password'] = user_pw
dest_conf['group'] = f'Lab{user_nb + 1//(nb_users//2) + 1}'
dest_conn = toolbox.open_connection(**dest_conf)
for project_id in source_project_ids:
project = toolbox.get_project(source_conn, project_id)
copy_project(source_conn, dest_conn, project)
dest_conn.close()
finally:
source_conn.close()
dest_conn.close()
finally:
if admin_conn.isConnected():
admin_conn.close()
if __name__ == '__main__':
argh.dispatch(run)