def __init__(self, logger, params): self.params = params self.logger = logger self.size = (800, 480) # window size self.screen = TouchInterface() self.result = dict() self.total_pulses=0
def __init__(self, logger): from utils.TouchInterface import TouchInterface self.logger = logger self.screen = TouchInterface() self.state = '' self.animal = 0 self.task = 0 self.setup() while logger.setup_status != 'running' and logger.setup_status != 'exit': # wait for remote start self.eval_input() time.sleep(0.5) logger.ping() self.close()
class ProbeTest: def __init__(self, logger, params): self.params = params self.logger = logger self.size = (800, 480) # window size self.screen = TouchInterface() self.result = dict() self.total_pulses=0 def run(self): """ Lickspout liquid delivery test """ self.valve = RPProbe(self.logger, callbacks=True, logging=False) print('Running probe test') for probe in self.params['probes']: self.total_pulses = 0 self.result[probe] = False tmst = self.logger.logger_timer.elapsed_time() for cal_idx in range(0, numpy.size(self.params['pulsenum'])): self.screen.cleanup() self.valve.create_pulse(probe, self.params['duration'][cal_idx]) pulse = 0 while pulse < self.params['pulsenum'][cal_idx] and not self.result[probe]: self.screen.cleanup() msg = 'Pulse %d/%d' % (pulse + 1, self.params['pulsenum'][cal_idx]) self.screen.draw(msg) print(msg) self.valve.create_pulse(probe, self.params['duration'][cal_idx]) self.valve.pulse_out(probe) # release liquid time.sleep(self.params['duration'][cal_idx] / 1000 + self.params['pulse_interval'][cal_idx] / 1000) pulse += 1 # update trial self.total_pulses += 1 self.result[probe] = self.get_response(tmst, probe) if self.result[probe]: self.log_test(probe, self.total_pulses, 'Passed') break if not self.result[probe]: self.log_test(probe, self.total_pulses, 'Failed') self.screen.cleanup() self.screen.draw('Done testing!') self.valve.cleanup() self.logger.update_setup_info({'status': 'ready'}) time.sleep(1) self.screen.exit() def get_response(self, since=0, probe=0): licked_probe, tmst = self.valve.get_last_lick() return tmst >= since and licked_probe == probe def log_test(self, probe=0, pulses=0, result='Passed'): self.screen.cleanup() self.screen.draw('Probe %d %s!' % (probe, result)) self.logger.log('ProbeTest', dict(setup=self.logger.setup, probe=probe, result=result, pulses=pulses)) time.sleep(1)
def setup(self, logger, params): self.params = params self.logger = logger self.size = (800, 480) # window size self.screen = TouchInterface()
class Experiment: def setup(self, logger, params): self.params = params self.logger = logger self.size = (800, 480) # window size self.screen = TouchInterface() def run(self): """ Lickspout liquid delivery calibration """ self.valve = RPProbe(exp=self, callbacks=False) print('Running calibration') if self.params['save']: pressure, exit_flag = self.button_input('Enter air pressure (PSI)') self.screen.cleanup() if exit_flag: self.screen.draw('Exiting!') time.sleep(.5) self.exit() return for cal_idx in range(0, numpy.size(self.params['pulsenum'])): self.screen.cleanup() if self.params['save']: self.screen.draw('Place zero-weighted pad under the port', 0, 0, 800, 280) button = self.screen.add_button(name='OK', x=300, y=300, w=200, h=100, color=(0, 128, 0)) while not button.is_pressed(): time.sleep(0.2) if self.logger.setup_status == 'stop': self.exit() return pulse = 0 while pulse < self.params['pulsenum'][cal_idx]: self.screen.cleanup() msg = 'Pulse %d/%d' % (pulse + 1, self.params['pulsenum'][cal_idx]) self.screen.draw(msg) print('\r' + msg, end='') for port in self.params['ports']: try: self.valve.give_liquid( port, self.params['duration'][cal_idx]) except Exception as e: self.screen.draw('ERROR:', str(e)) time.sleep(1) self.exit() time.sleep(self.params['duration'][cal_idx] / 1000 + self.params['pulse_interval'][cal_idx] / 1000) pulse += 1 # update trial if self.params['save']: for port in self.params['ports']: value, exit_flag = self.button_input( 'Enter weight for port %d' % port) if value and not exit_flag: self.log_pulse_weight(self.params['duration'][cal_idx], port, self.params['pulsenum'][cal_idx], value, pressure) elif exit_flag: self.exit() return self.exit() def exit(self): self.screen.cleanup() self.screen.draw('Done calibrating') self.valve.cleanup() self.logger.update_setup_info({'status': 'ready'}) time.sleep(2) self.screen.exit() def button_input(self, message): self.screen.cleanup() self.screen.draw(message, 0, 0, 400, 300) self.screen.add_numpad() button = self.screen.add_button(name='OK', x=150, y=250, w=100, h=100, color=(0, 128, 0)) exit_button = self.exit_input() exit_flag = False while not button.is_pressed() or self.screen.numpad == '': time.sleep(0.2) if exit_button.is_pressed(): exit_flag = True value = None break if self.screen.numpad and not exit_flag: value = float(self.screen.numpad) return value, exit_flag def exit_input(self): exit_button = self.screen.add_button(name='X', x=750, y=0, w=50, h=50, color=(25, 25, 25)) return exit_button def log_pulse_weight(self, pulse_dur, port, pulse_num, weight=0, pressure=0): key = dict(setup=self.logger.setup, port=port, date=systime.strftime("%Y-%m-%d")) self.logger.put(table='PortCalibration', tuple=key, schema='behavior', priority=5, ignore_extra_fields=True, validate=True, block=True, replace=False) self.logger.put(table='PortCalibration.Liquid', schema='behavior', replace=True, ignore_extra_fields=True, tuple=dict(key, pulse_dur=pulse_dur, pulse_num=pulse_num, weight=weight, pressure=pressure))
class PortTest: def __init__(self, logger, params): self.params = params self.logger = logger self.size = (800, 480) # window size self.screen = TouchInterface() self.result = dict() self.total_pulses=0 def run(self): """ Lickspout liquid delivery test """ self.valve = RPProbe(exp=self, callbacks=True, logging=False) print('Running port test') for port in self.params['ports']: self.total_pulses = 0 self.result[port] = False tmst = self.logger.logger_timer.elapsed_time() for cal_idx in range(0, numpy.size(self.params['pulsenum'])): self.screen.cleanup() pulse = 0 while pulse < self.params['pulsenum'][cal_idx] and not self.result[port]: self.screen.cleanup() msg = 'Pulse %d/%d' % (pulse + 1, self.params['pulsenum'][cal_idx]) self.screen.draw(msg) print(msg) self.valve.give_liquid(port, self.params['duration'][cal_idx]) time.sleep(self.params['duration'][cal_idx] / 1000 + self.params['pulse_interval'][cal_idx] / 1000) pulse += 1 # update trial self.total_pulses += 1 self.result[port] = self.get_response(tmst, port) if self.result[port]: self.log_test(port, self.total_pulses, 'Passed') break if not self.result[port]: self.log_test(port, self.total_pulses, 'Failed') self.screen.cleanup() self.screen.draw('Done testing!') self.valve.cleanup() self.logger.update_setup_info({'status': 'ready'}) time.sleep(1) self.screen.exit() def get_response(self, since=0, port=0): licked_port, tmst = self.valve.get_last_lick() return tmst >= since and licked_port == port def log_test(self, port=0, pulses=0, result='Passed'): self.screen.cleanup() self.screen.draw('Probe %d %s!' % (port, result)) key = dict(setup=self.logger.setup, port=port, result=result, pulses=pulses, date=systime.strftime("%Y-%m-%d"), timestamp=systime.strftime("%Y-%m-%d %H:%M:%S")) self.logger.put(table='PortCalibration', tuple=key, schema='behavior', priority=5) self.logger.put(table='PortCalibration.Test', schema='behavior', replace=True, tuple=key) time.sleep(1)
class Calibrate: def __init__(self, logger, params): self.params = params self.logger = logger self.size = (800, 480) # window size self.screen = TouchInterface() def run(self): """ Lickspout liquid delivery calibration """ valve = RPProbe(self.logger, callbacks=False) print('Running calibration') try: for cal_idx in range(0, numpy.size(self.params['pulsenum'])): self.screen.cleanup() if self.params['save']: self.screen.draw('Place zero-weighted pad under the probe', 0, 0, 800, 280) button = self.screen.add_button(name='OK', x=300, y=300, w=200, h=100, color=(0, 128, 0)) while not button.is_pressed(): time.sleep(0.2) if self.logger.setup_status == 'stop': valve.cleanup() self.screen.exit() return for probe in self.params['probes']: valve.create_pulse(probe, self.params['duration'][cal_idx]) pulse = 0 while pulse < self.params['pulsenum'][cal_idx]: self.screen.cleanup() msg = 'Pulse %d/%d' % (pulse + 1, self.params['pulsenum'][cal_idx]) self.screen.draw(msg) print(msg) for probe in self.params['probes']: valve.create_pulse(probe, self.params['duration'][cal_idx]) valve.pulse_out(probe) # release liquid time.sleep(self.params['duration'][cal_idx] / 1000 + self.params['pulse_interval'][cal_idx] / 1000) pulse += 1 # update trial if self.params['save']: for probe in self.params['probes']: self.screen.cleanup() self.screen.draw('Enter weight for probe %d' % probe, 0, 0, 400, 300) self.screen.add_numpad() button = self.screen.add_button(name='OK', x=150, y=250, w=100, h=100, color=(0, 128, 0)) exit_button = self.screen.add_button(name='X', x=750, y=0, w=50, h=50, color=(25, 25, 25)) exit_flag = False while not button.is_pressed( ) or self.screen.numpad == '': time.sleep(0.2) if exit_button.is_pressed(): exit_flag = True break if self.screen.numpad and not exit_flag: self.logger.log_pulse_weight( self.params['duration'][cal_idx], probe, self.params['pulsenum'][cal_idx], float(self.screen.numpad)) # insert self.screen.cleanup() self.screen.draw('Done calibrating') except: self.screen.cleanup() self.screen.draw('Error calibrating!') valve.cleanup() self.logger.update_setup_info({'status': 'ready'}) time.sleep(2) self.screen.exit()
class PyWelcome(Welcome): def __init__(self, logger): from utils.TouchInterface import TouchInterface self.logger = logger self.screen = TouchInterface() self.state = '' self.animal = 0 self.task = 0 self.setup() while logger.setup_status != 'running' and logger.setup_status != 'exit': # wait for remote start self.eval_input() time.sleep(0.5) logger.ping() self.close() def setup(self): self.cleanup() self.screen.add_button(name='Animal %d' % self.animal, action=self.change_animal, x=250, y=80, w=200, h=100, color=(0, 0, 0), font_size=30) self.screen.add_button(name='Task %d' % self.task, action=self.change_task, x=250, y=160, w=200, h=100, color=(0, 0, 0), font_size=30) self.screen.draw('%s %s' % (self.logger.get_ip(), self.logger.setup), 0, 0, 150, 100, (128, 128, 128), size=15) self.screen.add_button(name='Start experiment', action=self.start_experiment, x=250, y=330, w=200, h=100, color=(0, 128, 0)) self.screen.add_button(name='Restart', action=self.reboot, x=700, y=340, w=100, h=70, color=(50, 25, 25), font_size=15) self.screen.add_button(name='Power off', action=self.shutdown, x=700, y=410, w=100, h=70, color=(50, 50, 25), font_size=15) self.screen.add_button(name='Weigh Animal', action=self.weigh_animal, x=0, y=410, w=100, h=70, color=(50, 50, 50), font_size=15) self.screen.add_button(name='X', action=self.exit, x=750, y=0, w=50, h=50, color=(25, 25, 25)) def cleanup(self): self.screen.cleanup() self.state = '' def eval_input(self): if self.state == 'change_animal': self.cleanup() self.screen.draw('Enter animal ID', 0, 0, 400, 280) self.screen.add_numpad() button = self.screen.add_button(name='OK', x=150, y=250, w=100, h=100, color=(0, 128, 0)) exit_button = self.screen.add_button(name='X', x=750, y=0, w=50, h=50, color=(25, 25, 25)) exit_flag = False while not button.is_pressed() or self.screen.numpad == '': time.sleep(0.2) if exit_button.is_pressed(): exit_flag = True break if self.screen.numpad and not exit_flag: self.logger.update_setup_info( {'animal_id': int(self.screen.numpad)}) self.setup() elif self.state == 'change_task': self.cleanup() self.screen.draw('Enter task idx', 0, 0, 400, 280) self.screen.add_numpad() button = self.screen.add_button(name='OK', x=150, y=250, w=100, h=100, color=(0, 128, 0)) exit_button = self.screen.add_button(name='X', x=750, y=0, w=50, h=50, color=(25, 25, 25)) exit_flag = False while not button.is_pressed() or self.screen.numpad == '': time.sleep(0.2) if exit_button.is_pressed(): exit_flag = True break if self.screen.numpad and not exit_flag: self.logger.update_setup_info( {'task_idx': int(self.screen.numpad)}) self.setup() elif self.state == 'weigh_animal': self.cleanup() self.screen.draw('Enter animal weight', 0, 0, 400, 280) self.screen.add_numpad() button = self.screen.add_button(name='OK', x=150, y=250, w=100, h=100, color=(0, 128, 0)) exit_button = self.screen.add_button(name='X', x=750, y=0, w=50, h=50, color=(25, 25, 25)) exit_flag = False while not button.is_pressed() or self.screen.numpad == '': time.sleep(0.2) if exit_button.is_pressed(): exit_flag = True break if self.screen.numpad and not exit_flag: self.logger.put(table='MouseWeight', tuple=dict(self.logger.session_key, weight=float(self.screen.numpad)), schema='mice') self.setup() elif self.state == 'start_experiment': self.logger.update_setup_info({'status': 'running'}) self.screen.ts.stop() elif self.state == 'exit': self.logger.update_setup_info({'status': 'exit'}) self.close() else: self.set_setup_info() def set_setup_info(self): animal = self.logger.get_setup_info('animal_id') task = self.logger.get_setup_info('task_idx') if self.animal != animal or self.task != task: self.animal = animal self.task = task self.setup() def start_experiment(self): self.state = 'start_experiment' def exit(self): self.state = 'exit' def change_animal(self): self.state = 'change_animal' def weigh_animal(self): self.state = 'weigh_animal' def change_task(self): self.state = 'change_task' def reboot(self): os.system('systemctl reboot -i') def shutdown(self): os.system('systemctl poweroff') def close(self): self.screen.exit()