-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxp_manager.py
520 lines (420 loc) · 21.1 KB
/
xp_manager.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import os
import inspect
HERE_PATH = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
import logging
logging.basicConfig(level=logging.INFO)
# adding parent directory to path, so we can access the utils easily
import sys
root_path = os.path.join(HERE_PATH, '..')
sys.path.append(root_path)
import time
import json
import tempfile
import threading
import subprocess
from tools import email_notification
from tools.watchdog import Watchdog
from constants import N_POSITION
from constants import OIL_PUMP_CHEMICALS
from constants import SURFACTANT_PUMP_CHEMICALS
from constants import ARENA_TYPE
from xp_queue import XPQueue
SLEEP_TIME = 0.1
HOMING_EVERY_N_XP = 30
MANAGER_STORAGE_FILE = os.path.join(HERE_PATH, 'manager_storage.json')
MAX_WASTE_VOLUME = 10000 # 10L in ml
WASTE_CORRECTION = 0.9 # waste correction
TIMEOUT_WASTE = 300
# EMAILS_TO_NOTIFY = ['[email protected]'] # must be a list
# EMAILS_TO_NOTIFY = ['[email protected]', '[email protected]'] # must be a list
WATCHDOG_TIMEOUT = 1000
def save_to_json(data, filename):
with open(filename, 'w') as f:
json.dump(data, f)
def read_XP_from_file(filename):
with open(filename) as f:
return json.load(f)
def send_email_notification(subject, body):
for toaddr in EMAILS_TO_NOTIFY:
email_notification.send_email_notification(toaddr, subject, body)
def send_watchdog_email():
send_email_notification('[Dropfactory] Watchdog timeout', 'Watchdog raised, something might be wrong with dropfactory')
def timeout_editor_input(timeout=20, sleep_time=0.1, editor='gedit'):
# generate tmp file
tmpwastefilename = tempfile.NamedTemporaryFile().name
# open a gedit file
proc = subprocess.Popen([editor, tmpwastefilename])
# Wait until process terminates
start_time = time.time()
elapsed = 0
while elapsed < timeout:
if proc.poll() is not None:
if os.path.exists(tmpwastefilename):
with open(tmpwastefilename) as f:
return f.readline().strip()
else:
return ''
time.sleep(sleep_time)
elapsed = time.time() - start_time
time.sleep(sleep_time)
proc.terminate()
return ''
class XPManager(threading.Thread):
def __init__(self, pump, robot, working_station_dict, verbose=True):
"""
The robot, pumps and working station must be initalized already
working_station_dict contain all the instance of the useful station, that is, with exact name:
- fill_dish_station
- fill_oil_station
- clean_dish_station
- clean_oil_station
- make_droplet_station
- record_video_station
- wait_station
"""
threading.Thread.__init__(self)
self.daemon = True
self.interrupted = threading.Lock()
self.xp_queue = XPQueue(N_POSITION)
self.pump = pump
self.robot = robot
self.working_station_dict = working_station_dict
self.is_paused = False
self.verbose = verbose
self._n_xp_with_droplet_done = 0
self.bypass_waste_security = False
self.start()
def add_XP(self, XP_dict):
if self.check_XP_valid(XP_dict):
self.xp_queue.add_XP(XP_dict)
else:
print 'XP is not valid and will not be run on the platform, see messages above'
def check_XP_valid(self, XP_dict):
# check oils
if 'oil_formulation' in XP_dict:
oil_formulation = XP_dict['oil_formulation']
for oil_name in oil_formulation.keys():
if oil_name not in OIL_PUMP_CHEMICALS.values():
print '{} is not loaded in the any of the oil pumps'.format(oil_name)
return False
# check surfactants
if 'surfactant_formulation' in XP_dict:
surfactant_formulation = XP_dict['surfactant_formulation']
for surfactant_name in surfactant_formulation.keys():
if surfactant_name not in SURFACTANT_PUMP_CHEMICALS.values():
print '{} is not loaded in the any of the surfactant pumps'.format(surfactant_name)
return False
# check arena
if 'arena_type' in XP_dict:
arena_type = XP_dict['arena_type']
if arena_type != ARENA_TYPE:
print '{} arenas are not loaded on dropfactory'.format(arena_type)
return False
#
return True
def remove_XP(self, XP_dict):
self.xp_queue.remove_XP(XP_dict)
def empty_XP_queue(self):
self.xp_queue.empty_XP_waiting()
def count_XP_ongoing(self):
return self.xp_queue.count_XP_ongoing()
def count_XP_waiting(self):
return self.xp_queue.count_XP_waiting()
def wait_until_XP_finished(self):
while self.xp_queue.any_XP_ongoing() or self.xp_queue.any_XP_waiting():
time.sleep(SLEEP_TIME)
def run(self):
self.interrupted.acquire()
self.watchdog = Watchdog(WATCHDOG_TIMEOUT, send_watchdog_email)
while self.interrupted.locked():
if self.xp_queue.any_XP_ongoing():
self.handle_XP_ongoing()
self.robot.rotate_geneva_wheels()
# ping the watchdog here so we get a message when no more experiments are running
self.watchdog.reset() # we do a reset here because watchdog can be raised for reasons other than a bug/exception/no more XP, e.g. when the waste is full or manager is paused
else:
# a pause is done inside the handle_XP_ongoing main, but if there is not XP ongoing, it is still useful to be able to pause
self.apply_pause()
self.xp_queue.cycle()
time.sleep(SLEEP_TIME)
def stop(self):
self.interrupted.release()
def pause(self):
self.is_paused = True
def unpause(self):
self.is_paused = False
def apply_pause(self):
if self.is_paused and self.verbose:
print 'Manager paused...'
while self.is_paused:
time.sleep(SLEEP_TIME)
if self.is_paused and self.verbose:
print 'Manager running again'
def create_default_manager_storage_file(self):
manager_storage = {}
manager_storage['waste_volume'] = MAX_WASTE_VOLUME
self.save_manager_storage(manager_storage)
if self.verbose:
print '-----WARNING-----'
print 'Created default manager storage file at {}'.format(MANAGER_STORAGE_FILE)
print manager_storage
def get_manager_storage(self):
if not os.path.exists(MANAGER_STORAGE_FILE):
self.create_default_manager_storage_file()
return read_XP_from_file(MANAGER_STORAGE_FILE)
def save_manager_storage(self, manager_storage_dict):
save_to_json(manager_storage_dict, MANAGER_STORAGE_FILE)
def get_waste_volume(self):
manager_storage = self.get_manager_storage()
return manager_storage['waste_volume']
def set_waste_volume(self, volume_in_ml):
manager_storage = self.get_manager_storage()
manager_storage['waste_volume'] = volume_in_ml
self.save_manager_storage(manager_storage)
def add_waste_volume(self, volume_in_ml):
if self.verbose:
print 'Adding {} mL to waste'.format(volume_in_ml)
current_waste_volume = self.get_waste_volume()
self.set_waste_volume(current_waste_volume + volume_in_ml)
def check_waste_volume(self):
current_waste_volume = self.get_waste_volume()
if self.verbose:
print 'Current waste volume is {} mL'.format(current_waste_volume)
if current_waste_volume >= MAX_WASTE_VOLUME:
send_email_notification('[Dropfactory] Waste is full', 'Waste seems to be full, go change it.')
print '-----WARNING-----'
print 'Waste seems to be full!, option are:'
print '1- The waste is really full, change it, update new volume to zero'
print '2- The waste is not full, update new volume'
user_input_validated = False
while not user_input_validated:
if self.bypass_waste_security:
print 'Warning: Bypassing waste security!'
user_input_validated = True
else:
print 'Enter the new volume in mL on the first line of the open file, save and quit, put 0 if empty (timeout {}sec): '.format(TIMEOUT_WASTE)
response = timeout_editor_input(TIMEOUT_WASTE)
if response.isdigit():
new_waste_volume_in_ml = int(response)
if 0 <= new_waste_volume_in_ml <= MAX_WASTE_VOLUME:
self.set_waste_volume(new_waste_volume_in_ml)
user_input_validated = True
else:
print 'New volume must be >=0 and <={}'.format(MAX_WASTE_VOLUME)
else:
print '{} is not a valid number, you must provide a positive int or 0'.format(response)
print 'Great! manager continue his routine with waste volume = {}'.format(self.get_waste_volume())
print '-----------------'
def add_start_info_to_XP_dict(self, XP_dict):
time_now = time.time()
XP_dict['manager_info'] = {}
XP_dict['manager_info']['start_time'] = time_now
XP_dict['manager_info']['start_ctime'] = time.ctime(time_now)
def add_temperature_info_to_XP_dict(self, XP_dict):
XP_dict['manager_info']['temperature'] = self.robot.TEMPERATURE_SENSOR.get_celsius()
XP_dict['manager_info']['humidity'] = self.robot.TEMPERATURE_SENSOR.get_humidity()
temp_time = time.time()
XP_dict['manager_info']['temp_time'] = temp_time
XP_dict['manager_info']['temp_ctime'] = time.ctime(temp_time)
def add_end_info_to_XP_dict(self, XP_dict):
start_time = XP_dict['manager_info']['start_time']
end_time = time.time()
XP_dict['manager_info']['end_time'] = end_time
XP_dict['manager_info']['end_ctime'] = time.ctime(end_time)
XP_dict['manager_info']['duration'] = end_time - start_time
def save_run_info(self, XP_dict):
if 'run_info' in XP_dict:
filename = XP_dict['run_info']['filename']
data = XP_dict['manager_info']
save_to_json(data, filename)
def handle_XP_ongoing(self):
start_time = time.time()
if self.verbose:
print '###\n{} XP ongoing and {} XP waiting'.format(self.count_XP_ongoing(), self.count_XP_waiting())
# variable for checking what needs to be cleaned
clean_tube = False
clean_syringe = False
clean_oil_waste_volume = 0
clean_dish_waste_volume = 0
# launch station 3 (recording video) before all else, we home from time to time and this delays the start of the other station
# also check if droplet was just made before, hence is step 3 filming
station_id = 3
XP_dict = self.xp_queue.get_XP_ongoing(station_id)
if XP_dict is not None:
self.working_station_dict['record_video_station'].launch(XP_dict)
if 'droplets' in XP_dict:
if len(XP_dict['droplets']) > 0:
clean_syringe = True
# before starting and every HOMING_EVERY_N_XP, we home again the robot in case of slight shift on execution
if self._n_xp_with_droplet_done >= HOMING_EVERY_N_XP:
if self.verbose:
print 'Robot homing..'
self.robot.init(user_query=False, init_syringe=True, init_syringe_above_vial=True, init_geneva_wheel=False)
self._n_xp_with_droplet_done = 0
# station 5, 6, and 7 are doing nothing just waiting for min_waiting_time for evaporation
# find max waiting time
max_min_waiting_time = 0
for station_id in [5, 6, 7]:
XP_dict = self.xp_queue.get_XP_ongoing(station_id)
if XP_dict is not None:
if 'min_waiting_time' in XP_dict:
min_waiting_time = XP_dict['min_waiting_time']
if min_waiting_time > max_min_waiting_time:
max_min_waiting_time = min_waiting_time
# apply max min waiting time
waiting_XP_dict = {'min_waiting_time': max_min_waiting_time}
self.working_station_dict['wait_station'].launch(waiting_XP_dict)
# check if any droplet to be made, thus syringe cleaned
station_id = 2
droplet_XP_dict = self.xp_queue.get_XP_ongoing(station_id)
if droplet_XP_dict is not None:
self.working_station_dict['make_droplet_station'].load_XP(droplet_XP_dict)
if 'droplets' in droplet_XP_dict:
if len(droplet_XP_dict['droplets']) > 0:
clean_syringe = True
if 'force_clean_syringe' in droplet_XP_dict:
if droplet_XP_dict['force_clean_syringe'] is True:
clean_syringe = True
# launch station 0, filling step and record time of start
station_id = 0
XP_dict = self.xp_queue.get_XP_ongoing(station_id)
if XP_dict is not None:
self.add_start_info_to_XP_dict(XP_dict)
self.working_station_dict['fill_oil_station'].launch(XP_dict)
self.working_station_dict['fill_dish_station'].launch(XP_dict)
# launch station 4, cleaning
station_id = 4
XP_dict = self.xp_queue.get_XP_ongoing(station_id)
if XP_dict is not None:
self.working_station_dict['clean_dish_station'].launch(XP_dict)
clean_dish_waste_volume = self.working_station_dict['clean_dish_station'].get_added_waste_volume()
clean_tube = True
self.working_station_dict['clean_oil_station'].launch(XP_dict, clean_tube=clean_tube, clean_syringe=clean_syringe) # only clean tube or syringe if needed. They share a pump so are combined in one working station with condition. XP_dict does not matter
clean_oil_waste_volume = self.working_station_dict['clean_oil_station'].get_added_waste_volume()
# launch station 2, prepare droplets only once syringe is cleaned
if droplet_XP_dict is not None:
self.working_station_dict['clean_oil_station'].wait_until_idle()
self.working_station_dict['make_droplet_station'].start_filling_syringe_step()
# wait for all to finish
self.working_station_dict['wait_station'].wait_until_idle()
self.working_station_dict['clean_oil_station'].wait_until_idle()
self.working_station_dict['clean_dish_station'].wait_until_idle()
self.working_station_dict['fill_oil_station'].wait_until_idle()
self.working_station_dict['fill_dish_station'].wait_until_idle()
self.working_station_dict['make_droplet_station'].wait_until_idle()
self.working_station_dict['record_video_station'].wait_until_idle()
# check time to execute all stations
if self.verbose:
elapsed = time.time() - start_time
print 'Running all station before droplet placing took {} seconds'.format(round(elapsed, 2))
# update the waste counter
to_add_waste_volume = clean_oil_waste_volume + clean_dish_waste_volume
self.add_waste_volume(WASTE_CORRECTION * to_add_waste_volume)
self.check_waste_volume()
# if there was an XP at station 7, the last one, save and print XP info
station_id = 7
XP_dict = self.xp_queue.get_XP_ongoing(station_id)
if XP_dict is not None:
self.add_end_info_to_XP_dict(XP_dict)
self.save_run_info(XP_dict)
if self.verbose:
if 'manager_info' in XP_dict:
print 'XP started at {}, ended at {}, lasted {} seconds'.format(XP_dict['manager_info']['start_ctime'], XP_dict['manager_info']['end_ctime'], round(XP_dict['manager_info']['duration'], 2))
# this is the moment to pause all station finished and before placing new droplets
self.apply_pause()
# launch station 2, make droplets
if droplet_XP_dict is not None:
self.add_temperature_info_to_XP_dict(droplet_XP_dict)
if 'droplets' in droplet_XP_dict:
if len(droplet_XP_dict['droplets']) > 0:
self._n_xp_with_droplet_done += 1
self.working_station_dict['make_droplet_station'].make_droplets() # this is a blocking call
def add_clean_syringe_XP(self):
XP_dict = {}
XP_dict['force_clean_syringe'] = True
self.add_XP(XP_dict)
def add_clean_containers_XP(self):
XP_dict = {}
XP_dict['min_waiting_time'] = 60
for _ in range(N_POSITION):
self.add_XP(XP_dict)
def add_purge_sequence_XP(self, surfactant_volume=2, n_purge=16):
XP_dict = {}
XP_dict['min_waiting_time'] = 60
XP_dict['oil_formulation'] = {}
for oil_name in OIL_PUMP_CHEMICALS.values():
XP_dict['oil_formulation'][oil_name] = 1
XP_dict['surfactant_volume'] = surfactant_volume
XP_dict['surfactant_formulation'] = {}
for surfactant_name in SURFACTANT_PUMP_CHEMICALS.values():
XP_dict['surfactant_formulation'][surfactant_name] = 1
#
for _ in range(n_purge):
self.add_XP(XP_dict)
def clean_oil_head(self, n_clean=4):
if self.xp_queue.any_XP_ongoing() or self.xp_queue.any_XP_waiting():
print 'XP still running or waiting, for security reasons, we do not allow to run this function!'
return
from constants import CLEAN_HEAD_MIXTURE_DOWN
VOLUME_TUBE = 0.7
FILL_HEAD_CLEAN_LEVEL = 38
VOLUME_OIL_CLEAN = 0.1
VALVE_OIL_CLEAN = 'O'
INLET_ACETONE = 'E'
OUTLET_ACETONE_TUBE = 'I'
for _ in range(n_clean):
# fill tube with acetone
self.pump.controller.acetone_oil.pump(VOLUME_TUBE, from_valve=INLET_ACETONE)
self.robot.CLEAN_HEAD_MIXTURE.move_to(CLEAN_HEAD_MIXTURE_DOWN)
if self.robot.CLEAN_HEAD_MIXTURE.get_switch_state():
raise Exception('Clean head oil mixture did not go down, stepper might be broken...')
self.pump.controller.acetone_oil.wait_until_idle()
self.pump.controller.acetone_oil.deliver(VOLUME_TUBE, to_valve=OUTLET_ACETONE_TUBE, wait=True)
self.robot.CLEAN_HEAD_MIXTURE.home()
# move tube to oil filling station
for _ in range(4):
self.robot.rotate_geneva_wheels()
# clean fill head in acetone
self.robot.FILL_HEAD_MIXTURE.move_to(FILL_HEAD_CLEAN_LEVEL)
if self.robot.FILL_HEAD_MIXTURE.get_switch_state():
raise Exception('Fill head oil mixture did not go down, stepper might be broken...')
for _ in range(4):
self.pump.controller.transfer(self.pump.controller.groups['oils'], VOLUME_OIL_CLEAN, from_valve=VALVE_OIL_CLEAN, to_valve=VALVE_OIL_CLEAN)
self.robot.FILL_HEAD_MIXTURE.home()
# move tube to cleaning station
for _ in range(4):
self.robot.rotate_geneva_wheels()
# clean
self.working_station_dict['clean_oil_station'].launch({}, clean_tube=True, clean_syringe=False)
self.working_station_dict['clean_oil_station'].wait_until_idle()
clean_oil_waste_volume = self.working_station_dict['clean_oil_station'].get_added_waste_volume()
total_volume_to_waste = VOLUME_TUBE + clean_oil_waste_volume
self.add_waste_volume(WASTE_CORRECTION * total_volume_to_waste)
def clean_surfactant_pump(self):
if self.xp_queue.any_XP_ongoing() or self.xp_queue.any_XP_waiting():
print 'XP still running or waiting, for security reasons, we do not allow to run this function!'
return
VOLUME_DISH = 3.5
INLET_WATER = 'I'
OUTLET_DISH = 'O'
SYRINGE_VOLUME = self.pump.controller.surfactant.total_volume
N_SYRINGE_UP_DOWN = 4
# ensure water all over tubing
for _ in range(2):
self.pump.controller.surfactant.transfer(VOLUME_DISH, INLET_WATER, OUTLET_DISH)
self.robot.rotate_geneva_wheels()
# go and back N_SYRINGE_UP_DOWN time with water to clean
self.pump.controller.surfactant.transfer(N_SYRINGE_UP_DOWN * SYRINGE_VOLUME, INLET_WATER, INLET_WATER)
# ensure to get rid of all remaining by flushing water all over tubing
for _ in range(2):
self.pump.controller.surfactant.transfer(VOLUME_DISH, INLET_WATER, OUTLET_DISH)
self.robot.rotate_geneva_wheels()
# clean
for _ in range(4):
self.working_station_dict['clean_dish_station'].launch({})
self.working_station_dict['clean_dish_station'].wait_until_idle()
clean_dish_waste_volume = self.working_station_dict['clean_dish_station'].get_added_waste_volume()
total_volume_to_waste = VOLUME_DISH + clean_dish_waste_volume
self.add_waste_volume(WASTE_CORRECTION * total_volume_to_waste)
self.robot.rotate_geneva_wheels()