Esempio n. 1
0
class Wheel(Hardware):
    """
    A continuously measured mouse wheel.

    Uses a USB computer mouse.

    Warning:
        'vel' thresh_type not implemented
    """

    input = True
    type = "Wheel"
    trigger = False  # even though this is a triggerable option, typically don't want to assign a cb and instead us a GPIO
    # TODO: Make the standard-style trigger.
    # TODO: Make wheel movements available locally with a deque

    THRESH_TYPES = ['dist', 'x', 'y', 'vel']

    MODES = ('vel_total', 'steady', 'dist', 'timed')

    MOVE_DTYPE = [('vel', 'i4'), ('dir', 'U5'), ('timestamp', 'f8')]

    def __init__(self,
                 mouse_idx=0,
                 fs=10,
                 thresh=100,
                 thresh_type='dist',
                 start=True,
                 digi_out=False,
                 mode='vel_total',
                 integrate_dur=5):
        """
        Args:
            mouse_idx (int):
            fs (int):
            thresh (int):
            thresh_type ('dist'):
            start (bool):
            digi_out (:class:`~.Digital_Out`, bool):
            mode ('vel_total'):
            integrate_dur (int):
        """

        # try to get mouse from inputs
        # TODO: More robust - specify mouse by hardware attrs
        try:
            self.mouse = devices.mice[mouse_idx]
        except IndexError:
            Warning(
                'Could not find requested mouse with index {}\nAttempting to use mouse idx 0'
                .format(mouse_idx))
            self.mouse = devices.mice[0]

        # frequency of our updating
        self.fs = fs
        # time between updates
        self.update_dur = 1. / float(self.fs)

        self.thresh = thresh
        # thresh type can be 'dist', 'x', 'y', or 'vel'
        if thresh_type not in self.THRESH_TYPES:
            ValueError('thresh_type must be one of {}, given {}'.format(
                self.THRESH_TYPES, thresh_type))
        self.thresh_type = thresh_type

        # mode can be 'vel_total', 'vel_x', 'vel_y' or 'dist' - report either velocity or distance
        # mode can also be '
        # TODO: Do two parameters - type 'vel' or 'dist' and measure 'x', 'y', 'total'z
        self.mode = mode
        # TODO: Implement this

        if self.mode == "steady":
            self.thresh_val = np.array([(0, "REL_Y", 0)],
                                       dtype=self.MOVE_DTYPE)
        else:
            self.thresh_val = 0.0

        self.integrate_dur = integrate_dur

        # event to signal quitting
        self.quit_evt = threading.Event()
        self.quit_evt.clear()
        # event to signal when to start accumulating movements to trigger
        self.measure_evt = threading.Event()
        self.measure_time = 0
        # queue to I/O mouse movements summarized at fs Hz
        self.q = Queue()
        # lock to prevent race between putting and getting
        self.qlock = threading.Lock()

        self.listens = {
            'MEASURE': self.l_measure,
            'CLEAR': self.l_clear,
            'STOP': self.l_stop
        }
        self.node = Net_Node(
            'wheel_{}'.format(mouse_idx),
            upstream=prefs.get('NAME'),
            port=prefs.get('MSGPORT'),
            listens=self.listens,
        )

        # if we are being used in a child object, we send our trigger via a GPIO pin
        self.digi_out = digi_out

        self.thread = None

        if start:
            self.start()

    def start(self):
        self.thread = threading.Thread(target=self._record)
        self.thread.daemon = True
        self.thread.start()

    def _mouse(self):
        while self.quit_evt:
            events = self.mouse.read()
            self.q.put(events)

    def _record(self):
        moves = np.array([], dtype=self.MOVE_DTYPE)

        threading.Thread(target=self._mouse).start()

        last_update = time.time()

        while not self.quit_evt.is_set():

            try:
                events = self.q.get_nowait()
            except Empty:
                events = None

            if events is None:
                move = np.array([(0, "REL_Y", 0)], dtype=self.MOVE_DTYPE)
            else:
                # make a numpy record array of events with 3 fields:
                # velocity, dir(ection), timestamp (system seconds)
                move = np.array([(int(event.state), event.code, float(event.timestamp))\
                                 for event in events if event.code in ('REL_X', 'REL_Y')],
                                dtype=self.MOVE_DTYPE)
            moves = np.concatenate([moves, move])

            # If we have been told to start measuring for a trigger...
            if self.measure_evt.is_set():
                do_trigger = self.check_thresh(move)
                if do_trigger:
                    self.thresh_trig()
                    self.measure_evt.clear()
                # take the integral of velocities

            # If it's time to report velocity, do it.
            nowtime = time.time()
            if (nowtime - last_update) > self.update_dur:

                # TODO: Implement distance/position reporting
                y_vel = self.calc_move(moves, 'y')
                x_vel = self.calc_move(moves, 'x')

                self.node.send(key='CONTINUOUS',
                               value={
                                   'x': x_vel,
                                   'y': y_vel,
                                   't': nowtime
                               },
                               repeat=False)

                moves = np.array([], dtype=self.MOVE_DTYPE)

                last_update = nowtime

    def check_thresh(self, move):
        """
        Updates thresh_val and checks whether it's above/below threshold

        Args:
            move (np.array): Structured array with fields ('vel', 'dir', 'timestamp')

        Returns:

        """

        # Determine whether the threshold was surpassed
        do_trigger = False
        if self.mode == 'vel_total':
            thresh_update = self.calc_move(move)
            # If instantaneous velocity is above thresh...
            if thresh_update > self.thresh:
                do_trigger = True

        elif self.mode == 'steady':
            # If movements in the recent past are below a certain value
            # self.thresh_val should be set to a structured array by l_measure
            try:
                self.thresh_val = np.concatenate([self.thresh_val, move])
            except TypeError:
                print('THRESH_VAL:', self.thresh_val, 'MOVE:', move)
            # trim to movements in the time window
            thresh_val = self.thresh_val[self.thresh_val['timestamp'] >
                                         time.time() - self.integrate_dur]

            thresh_update = self.calc_move(thresh_val)

            if (thresh_update < self.thresh) and (
                    self.measure_time + self.integrate_dur < time.time()):
                do_trigger = True

        elif self.mode == 'dist':
            thresh_update = self.calc_move(move)
            self.thresh_val += thresh_update

            if self.thresh_val > self.thresh:
                do_trigger = True

        else:
            Warning("mode is not defined! mode is {}".format(self.mode))

        return do_trigger

    def calc_move(self, move, thresh_type=None):
        """
        Calculate distance move depending on type (x, y, total dist)

        Args:
            move ():
            thresh_type ():

        Returns:

        """

        if thresh_type is None:
            thresh_type = self.thresh_type

        # FIXME: rly inefficient
        # get the value of the movement depending on what we're measuring
        if thresh_type == 'x':

            distance = np.sum(move['vel'][move['dir'] == "REL_X"])
        elif thresh_type == 'y':
            distance = np.sum(move['vel'][move['dir'] == "REL_Y"])
        elif thresh_type == "dist":
            x_dist = np.sum(move['vel'][move['dir'] == "REL_X"])
            y_dist = np.sum(move['vel'][move['dir'] == "REL_Y"])
            distance = np.abs(np.sqrt(float(x_dist**2) + float(y_dist**2)))

        return distance

    def thresh_trig(self):

        if self.digi_out:
            self.digi_out.pulse()

        self.measure_evt.clear()

    def assign_cb(self, trigger_fn):
        # want to have callback write an output pin -- so callback should go back to
        # the task to write a GPIO pin.
        self.trig_fn = trigger_fn

    def l_measure(self, value):
        """
        Task has signaled that we need to start measuring movements for a trigger


        Args:
            value ():
        """

        if 'mode' in value.keys():
            if value['mode'] in self.MODES:
                self.mode = value['mode']
            else:
                Warning(
                    'incorrect mode sent: {}, needs to be one of {}'.format(
                        value['mode'], self.MODES))

        if 'thresh' in value.keys():
            self.thresh = float(value['thresh'])

        if self.mode == "steady":
            self.thresh_val = np.array([(0, "REL_Y", 0)],
                                       dtype=self.MOVE_DTYPE)
        else:
            self.thresh_val = 0.0
        self.measure_time = time.time()

        self.measure_evt.set()

        sys.stdout.flush()

    def l_clear(self, value):
        """
        Stop measuring!

        Args:
            value ():

        Returns:

        """
        self.measure_evt.clear()

    def l_stop(self, value):
        """
        Stop measuring and clear system resources
        Args:
            value ():

        Returns:

        """

        self.measure_evt.set()
        self.release()

    def release(self):
        self.quit_evt.clear()
Esempio n. 2
0
class GoNoGo(Task):
    """
    A Visual Go/No-Go task using a :class:`.Pilot` and a :class:`.Wheel_Child`.

    .. note::

        This task was written as a proof-of-concept for the Autopilot manuscript,
        and is thus underdeveloped and underdocumented, submit and issue if you would like to
        use it yourself :)

    """

    STAGE_NAMES = ["request", "discrim", "reinforcement"]

    # Class attributes

    # List of needed params, returned data and data format.
    # Params are [name]={'tag': Human Readable Tag, 'type': 'int', 'float', 'bool', etc.}
    PARAMS = odict()
    PARAMS['reward'] = {'tag': 'Reward Duration (ms)', 'type': 'int'}
    PARAMS['timeout'] = {'tag': 'Delay Timeout (ms)', 'type': 'int'}
    PARAMS['stim'] = {'tag': 'Visuals', 'type': 'visuals'}

    # Set plot params, which data should be plotted, its default shape, etc.
    PLOT = {
        'data': {
            'y': 'shaded',
            'target': 'point',
            'response': 'segment'
        },
        'continuous': True
    }

    # PyTables Data descriptor
    # for numpy data types see http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#arrays-dtypes-constructing
    class TrialData(tables.IsDescription):
        # This class allows the Subject object to make a data table with the correct data types. You must update it for any new data you'd like to store
        trial_num = tables.Int32Col()
        target = tables.BoolCol()
        response = tables.StringCol(1)
        correct = tables.Int32Col()
        RQ_timestamp = tables.StringCol(26)
        DC_timestamp = tables.StringCol(26)
        shift = tables.Float32Col()
        angle = tables.Float32Col()
        delay = tables.Float32Col()

    # class ContinuousData(tables.IsDescription):
    #     x = tables.Float64Col()
    #     y = tables.Float64Col()
    #     t = tables.Float64Col()

    HARDWARE = {
        'POKES': {
            'C': autopilot.hardware.gpio.Digital_In,
        },
        'LEDS': {
            'C': autopilot.hardware.gpio.LED_RGB,
        },
        'PORTS': {
            'C': autopilot.hardware.gpio.Solenoid,
        },
        'FLAGS': {
            'F': autopilot.hardware.gpio.Digital_Out
        }
    }

    CHILDREN = {
        'WHEEL': {
            'task_type': "Wheel Child",
        }
    }

    def __init__(self,
                 stim=None,
                 reward=50,
                 timeout=1000,
                 stage_block=None,
                 **kwargs):
        super(GoNoGo, self).__init__()

        self.stage_block = stage_block
        self.trial_counter = itertools.count()

        self.punish_dur = 500.0

        self.reward = reward
        self.timeout = timeout

        self.init_hardware()
        self.set_reward(self.reward)

        self.node = Net_Node(id="T_{}".format(prefs.get('NAME')),
                             upstream=prefs.get('NAME'),
                             port=prefs.get('MSGPORT'),
                             listens={},
                             instance=True)

        # get our child started
        self.subject = kwargs['subject']
        value = {
            'child': {
                'parent': prefs.get('NAME'),
                'subject': kwargs['subject']
            },
            'task_type': 'Wheel Child',
            'subject': kwargs['subject']
        }

        self.node.send(to=prefs.get('NAME'), key='CHILD', value=value)

        # hardcoding stimulus for testing
        self.stim = Grating(angle=0,
                            freq=(4, 0),
                            rate=1,
                            size=(1, 1),
                            debug=True)

        self.stages = itertools.cycle(
            [self.request, self.discrim, self.reinforce])

    def request(self):
        # wait for the subject to hold the wheel still
        # Set the event lock
        self.stage_block.clear()
        # wait on any ongoing punishment stimulus
        self.punish_block.wait()

        # Reset all the variables that need to be
        #for v in self.resetting_variables:
        #    v = None

        # reset triggers if there are any left
        self.triggers = {}

        # calculate orientation change
        # half the time, don't change, otherwise, do change
        if random() < 0.5:
            self.shift = 0
            self.target = False
        else:
            self.shift = random() * 180.0
            self.target = True

        # Set sound trigger and LEDs
        # We make two triggers to play the sound and change the light color
        change_to_blue = lambda: self.hardware['LEDS']['C'].set_color(
            [0, 0, 255])

        # set triggers
        self.triggers['F'] = [
            change_to_blue, lambda: self.stim.play('shift', self.shift)
        ]

        # set to green in the meantime
        self.set_leds({'C': [0, 255, 0]})

        # tell our wheel to start measuring
        self.node.send(to=[prefs.get('NAME'),
                           prefs.get('CHILDID'), 'wheel_0'],
                       key="MEASURE",
                       value={
                           'mode': 'steady',
                           'thresh': 100
                       })

        self.current_trial = next(self.trial_counter)
        data = {
            'target': self.target,
            'shift': self.shift,
            'trial_num': self.current_trial
        }

        self.current_stage = 0
        return data

    def discrim(self):
        self.stage_block.clear()

        # if the subject licks on a good trial, reward.
        # set a trigger to respond false if delay time elapses
        if self.target:
            self.triggers['C'] = [
                lambda: self.respond(True), self.hardware['PORTS']['C'].open
            ]
            self.triggers['T'] = [lambda: self.respond(False), self.punish]
        # otherwise punish
        else:
            self.triggers['C'] = [lambda: self.respond(True), self.punish]
            self.triggers['T'] = [
                lambda: self.respond(False), self.hardware['PORTS']['C'].open
            ]

        # the stimulus has just started playing, wait a bit and then shift it (if we're gonna
        # choose a random delay
        delay = 0.0
        if self.shift != 0:
            delay = (random() * 3000.0) + 1000.0
            self.delayed_set(delay, 'shift', self.shift)

        self.timer = threading.Timer(5.0,
                                     self.handle_trigger,
                                     args=('T', True, None)).start()

        data = {
            'delay': delay,
            'RQ_timestamp': datetime.datetime.now().isoformat(),
            'trial_num': self.current_trial
        }

        return data

    def reinforce(self):
        # don't clear stage block here to quickly move on
        if self.response == self.target:
            self.correct = 1
        else:
            self.correct = 0

        self.set_leds({'C': [0, 0, 0]})
        # stop timer if it's still going
        try:
            self.timer.cancel()
        except AttributeError:
            pass
        self.timer = None

        data = {
            'DC_timestamp': datetime.datetime.now().isoformat(),
            'response': self.response,
            'correct': self.correct,
            'trial_num': self.current_trial,
            'TRIAL_END': True
        }
        return data

    def respond(self, response):
        self.response = response

    def delayed_set(self, delay, attr, val):
        threading.Timer(float(delay) / 1000.0,
                        self._delayed_shift,
                        args=(attr, val)).start()

    def _delayed_shift(self, attr, val):
        self.stim.q.put(('shift', val))

    def punish(self):
        """
        Flash lights, play punishment sound if set
        """
        # TODO: If we're not in the last stage (eg. we were timed out after stim presentation), reset stages
        self.punish_block.clear()

        if self.punish_stim:
            self.stim_manager.play_punishment()

        # self.set_leds()
        self.flash_leds()
        threading.Timer(self.punish_dur / 1000., self.punish_block.set).start()
Esempio n. 3
0
class Pilot:
    """
    Drives the Raspberry Pi

    Coordinates the hardware and networking objects to run tasks.

    Typically used with a connection to a :class:`.Terminal` object to
    coordinate multiple subjects and tasks, but a high priority for future releases
    is to do the (trivial amount of) work to make this class optionally
    standalone.

    Called as a module with the -f flag to give the location of a prefs file, eg::

        python pilot.py -f prefs_file.json

    if the -f flag is not passed, looks in the default location for prefs
    (ie. `/usr/autopilot/prefs.json`)

    Needs the following prefs (typically established by :mod:`.setup.setup_pilot`):

    * **NAME** - The name used by networking objects to address this Pilot
    * **BASEDIR** - The base directory for autopilot files (/usr/autopilot)
    * **PUSHPORT** - Router port used by the Terminal we connect to.
    * **TERMINALIP** - IP Address of our upstream Terminal.
    * **MSGPORT** - Port used by our own networking object
    * **HARDWARE** - Any hardware and its mapping to GPIO pins. No pins are required to be set, instead each
      task defines which pins it needs. Currently the default configuration asks for

        * POKES - :class:`.hardware.Beambreak`
        * LEDS - :class:`.hardware.LED_RGB`
        * PORTS - :class:`.hardware.Solenoid`

    * **AUDIOSERVER** - Which type, if any, audio server to use (`'jack'`, `'pyo'`, or `'none'`)
    * **NCHANNELS** - Number of audio channels
    * **FS** - Sampling rate of audio output
    * **JACKDSTRING** - string used to start the jackd server, see `the jack manpages <https://linux.die.net/man/1/jackd>`_ eg::

        jackd -P75 -p16 -t2000 -dalsa -dhw:sndrpihifiberry -P -rfs -n3 -s &

    * **PIGPIOMASK** - Binary mask of pins for pigpio to control, see `the pigpio docs <http://abyz.me.uk/rpi/pigpio/pigpiod.html>`_ , eg::

        1111110000111111111111110000

    * **PULLUPS** - Pin (board) numbers to pull up on boot
    * **PULLDOWNS** - Pin (board) numbers to pull down on boot.

    Attributes:
        name (str): The name used to identify ourselves in :mod:`.networking`
        task (:class:`.tasks.Task`): The currently instantiated task
        running (:class:`threading.Event`): Flag used to control task running state
        stage_block (:class:`threading.Event`): Flag given to a task to signal when task stages finish
        file_block (:class:`threading.Event`): Flag used to wait for file transfers
        state (str): 'RUNNING', 'STOPPING', 'IDLE' - signals what this pilot is up to
        pulls (list): list of :class:`~.hardware.Pull` objects to keep pins pulled up or down
        server: Either a :func:`~.sound.pyoserver.pyo_server` or :class:`~.jackclient.JackClient` , sound server.
        node (:class:`.networking.Net_Node`): Our Net_Node we use to communicate with our main networking object
        networking (:class:`.networking.Pilot_Station`): Our networking object to communicate with the outside world
        ip (str): Our IPv4 address
        listens (dict): Dictionary mapping message keys to methods used to process them.
        logger (:class:`logging.Logger`): Used to log messages and network events.
        log_handler (:class:`logging.FileHandler`): Handler for logging
        log_formatter (:class:`logging.Formatter`): Formats log entries as::

            "%(asctime)s %(levelname)s : %(message)s"
    """

    logger = None
    log_handler = None
    log_formatter = None

    # Events for thread handling
    running = None
    stage_block = None
    file_block = None

    # networking - our internal and external messengers
    node = None
    networking = None

    # audio server
    server = None

    def __init__(self, splash=True):

        if splash:
            with open(
                    os.path.join(os.path.dirname(os.path.dirname(__file__)),
                                 'setup', 'welcome_msg.txt'),
                    'r') as welcome_f:
                welcome = welcome_f.read()
                print('')
                for line in welcome.split('\n'):
                    print(line)
                print('')
                sys.stdout.flush()

        self.name = prefs.NAME
        if prefs.LINEAGE == "CHILD":
            self.child = True
            self.parentid = prefs.PARENTID
        else:
            self.child = False
            self.parentid = 'T'

        self.init_logging()

        # Locks, etc. for threading
        self.running = threading.Event()  # Are we running a task?
        self.stage_block = threading.Event(
        )  # Are we waiting on stage triggers?
        self.file_block = threading.Event()  # Are we waiting on file transfer?

        # init pigpiod process
        self.init_pigpio()

        # Init audio server
        if hasattr(prefs, 'AUDIOSERVER') and 'AUDIO' in prefs.CONFIG:
            self.init_audio()

        # Init Station
        # Listen dictionary - what do we do when we receive different messages?
        self.listens = {
            'START':
            self.l_start,  # We are being passed a task and asked to start it
            'STOP': self.l_stop,  # We are being asked to stop running our task
            'PARAM': self.l_param,  # A parameter is being changed
            'CALIBRATE_PORT': self.l_cal_port,  # Calibrate a water port
            'CALIBRATE_RESULT':
            self.l_cal_result,  # Compute curve and store result
            'BANDWIDTH': self.l_bandwidth  # test our bandwidth
        }

        # spawn_network gives us the independent message-handling process
        self.networking = Pilot_Station()
        self.networking.start()
        self.node = Net_Node(id="_{}".format(self.name),
                             upstream=self.name,
                             port=prefs.MSGPORT,
                             listens=self.listens,
                             instance=False)

        # if we need to set pins pulled up or down, do that now
        self.pulls = []
        if hasattr(prefs, 'PULLUPS'):
            for pin in prefs.PULLUPS:
                self.pulls.append(gpio.Digital_Out(int(pin), pull='U'))
        if hasattr(prefs, 'PULLDOWNS'):
            for pin in prefs.PULLDOWNS:
                self.pulls.append(gpio.Digital_Out(int(pin), pull='D'))

        # check if the calibration file needs to be updated

        # Set and update state
        self.state = 'IDLE'  # or 'Running'
        self.update_state()

        # Since we're starting up, handshake to introduce ourselves
        self.ip = self.get_ip()
        self.handshake()

        #self.blank_LEDs()

        # TODO Synchronize system clock w/ time from terminal.

    def init_logging(self):
        """
        Start logging to a timestamped file in `prefs.LOGDIR`
        """

        timestr = datetime.datetime.now().strftime('%y%m%d_%H%M%S')
        log_file = os.path.join(prefs.LOGDIR,
                                'Pilots_Log_{}.log'.format(timestr))

        self.logger = logging.getLogger('main')
        self.log_handler = logging.FileHandler(log_file)
        self.log_formatter = logging.Formatter(
            "%(asctime)s %(levelname)s : %(message)s")
        self.log_handler.setFormatter(self.log_formatter)
        self.logger.addHandler(self.log_handler)
        self.logger.setLevel(logging.INFO)
        self.logger.info('Pilot Logging Initiated')

    #################################################################
    # Station
    #################################################################

    def get_ip(self):
        """
        Get our IP
        """

        # shamelessly stolen from https://www.w3resource.com/python-exercises/python-basic-exercise-55.php
        # variables are badly named because this is just a rough unwrapping of what was a monstrous one-liner

        # get ips that aren't the loopback
        unwrap00 = [
            ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
            if not ip.startswith("127.")
        ][:1]
        # ???
        unwrap01 = [[
            (s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close())
            for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]
        ][0][1]]

        unwrap2 = [l for l in (unwrap00, unwrap01) if l][0][0]

        return unwrap2

    def handshake(self):
        """
        Send the terminal our name and IP to signal that we are alive
        """
        # send the terminal some information about ourselves

        # TODO: Report any calibrations that we have

        hello = {'pilot': self.name, 'ip': self.ip, 'state': self.state}

        self.node.send(self.parentid, 'HANDSHAKE', value=hello)

    def update_state(self):
        """
        Send our current state to the Terminal,
        our Station object will cache this and will handle any
        future requests.
        """
        self.node.send(self.parentid,
                       'STATE',
                       self.state,
                       flags={'NOLOG': True})

    def l_start(self, value):
        """
        Start running a task.

        Get the task object by using `value['task_type']` to select from
        :data:`.tasks.TASK_LIST` , then feed the rest of `value` as kwargs
        into the task object.

        Calls :meth:`.autopilot.run_task` in a new thread

        Args:
            value (dict): A dictionary of task parameters
        """
        # TODO: If any of the sounds are 'file,' make sure we have them. If not, request them.
        # Value should be a dict of protocol params
        # The networking object should have already checked that we have all the files we need

        if self.state == "RUNNING" or self.running.is_set():
            self.logger.warning("Asked to a run a task when already running")
            return

        self.state = 'RUNNING'
        self.running.set()
        try:
            # Get the task object by its type
            if 'child' in value.keys():
                task_class = tasks.CHILDREN_LIST[value['task_type']]
            else:
                task_class = tasks.TASK_LIST[value['task_type']]
            # Instantiate the task
            self.stage_block.clear()

            # Make a group for this subject if we don't already have one
            self.subject = value['subject']
            prefs.add('SUBJECT', self.subject)

            # Run the task and tell the terminal we have
            # self.running.set()
            threading.Thread(target=self.run_task,
                             args=(task_class, value)).start()

            self.update_state()
        except Exception as e:
            self.state = "IDLE"
            self.logger.exception("couldn't start task: {}".format(e))

        # TODO: Send a message back to the terminal with the runtime if there is one so it can handle timed stops

    def l_stop(self, value):
        """
        Stop the task.

        Clear the running event, set the stage block.

        TODO:
            Do a coherence check between our local file and the Terminal's data.

        Args:
            value: ignored
        """
        # Let the terminal know we're stopping
        # (not stopped yet because we'll still have to sync data, etc.)
        self.state = 'STOPPING'
        self.update_state()

        # We just clear the stage block and reset the running flag here
        # and call the cleanup routine from run_task so it can exit cleanly
        self.running.clear()
        self.stage_block.set()

        # TODO: Cohere here before closing file
        if hasattr(self, 'h5f'):
            self.h5f.close()

        self.state = 'IDLE'
        self.update_state()

    def l_param(self, value):
        """
        Change a task parameter mid-run

        Warning:
            Not Implemented

        Args:
            value:
        """
        pass

    def l_cal_port(self, value):
        """
        Initiate the :meth:`.calibrate_port` routine.

        Args:
            value (dict): Dictionary of values defining the port calibration to be run, including
                - ``port`` - which port to calibrate
                - ``n_clicks`` - how many openings should be performed
                - ``open_dur`` - how long the valve should be open
                - ``iti`` - 'inter-trial interval`, or how long should we wait between valve openings.

        """
        port = value['port']
        n_clicks = value['n_clicks']
        open_dur = value['dur']
        iti = value['click_iti']

        threading.Thread(target=self.calibrate_port,
                         args=(port, n_clicks, open_dur, iti)).start()

    def calibrate_port(self, port_name, n_clicks, open_dur, iti):
        """
        Run port calibration routine

        Open a :class:`.hardware.gpio.Solenoid` repeatedly,
        measure volume of water dispersed, compute lookup table mapping
        valve open times to volume.

        Continuously sends progress of test with ``CAL_PROGRESS`` messages

        Args:
            port_name (str): Port name as specified in ``prefs``
            n_clicks (int): number of times the valve should be opened
            open_dur (int, float): how long the valve should be opened for in ms
            iti (int, float): how long we should :func:`~time.sleep` between openings

        """
        pin_num = prefs.HARDWARE['PORTS'][port_name]
        port = gpio.Solenoid(pin_num, duration=int(open_dur))
        msg = {'click_num': 0, 'pilot': self.name, 'port': port_name}

        iti = float(iti) / 1000.0

        cal_name = "Cal_{}".format(self.name)

        for i in range(int(n_clicks)):
            port.open()
            msg['click_num'] = i + 1
            self.node.send(to=cal_name, key='CAL_PROGRESS', value=msg)
            time.sleep(iti)

        port.release()

    def l_cal_result(self, value):
        """
        Save the results of a port calibration

        """

        # files for storing raw and fit calibration results
        cal_fn = os.path.join(prefs.BASEDIR, 'port_calibration.json')

        if os.path.exists(cal_fn):
            try:
                with open(cal_fn, 'r') as cal_file:
                    calibration = json.load(cal_file)
            except ValueError:
                # usually no json can be decoded, that's fine calibrations aren't expensive
                calibration = {}
        else:
            calibration = {}

        for port, results in value.items():
            if port in calibration.keys():
                calibration[port].extend(results)
            else:
                calibration[port] = results

        with open(cal_fn, 'w+') as cal_file:
            json.dump(calibration, cal_file)

    def l_bandwidth(self, value):
        """
        Send messages with a poissonian process according to the settings in value
        """
        #turn off logging for now
        self.networking.logger.setLevel(logging.ERROR)
        self.node.logger.setLevel(logging.ERROR)

        n_msg = int(value['n_msg'])
        rate = float(value['rate'])
        payload = int(value['payload'])
        confirm = bool(value['confirm'])

        payload = np.zeros(payload * 1024, dtype=np.bool)
        payload_size = sys.getsizeof(payload)

        message = {
            'pilot': self.name,
            'payload': payload,
        }

        # make a fake message to test how large the serialized message is
        test_msg = Message(to='bandwith',
                           key='BANDWIDTH_MSG',
                           value=message,
                           repeat=confirm,
                           flags={'MINPRINT': True},
                           id="test_message",
                           sender="test_sender")
        msg_size = sys.getsizeof(test_msg.serialize())

        message['message_size'] = msg_size
        message['payload_size'] = payload_size

        if rate > 0:
            spacing = 1.0 / rate
        else:
            spacing = 0

        # wait for half a second to let the terminal get messages out
        time.sleep(0.25)

        if spacing > 0:
            last_message = time.perf_counter()
            for i in range(n_msg):
                message['n_msg'] = i
                message['timestamp'] = datetime.datetime.now().isoformat()
                self.node.send(to='bandwidth',
                               key='BANDWIDTH_MSG',
                               value=message,
                               repeat=confirm,
                               flags={'MINPRINT': True})
                this_message = time.perf_counter()
                waitfor = np.clip(spacing - (this_message - last_message), 0,
                                  spacing)

                #time.sleep(np.random.exponential(1.0/rate))
                # just do linear spacing lol.

                time.sleep(waitfor)
                last_message = time.perf_counter()
        else:
            for i in range(n_msg):
                message['n_msg'] = i
                message['timestamp'] = datetime.datetime.now().isoformat()
                self.node.send(to='bandwidth',
                               key='BANDWIDTH_MSG',
                               value=message,
                               repeat=confirm,
                               flags={'MINPRINT': True})

        self.node.send(to='bandwidth',
                       key='BANDWIDTH_MSG',
                       value={
                           'pilot': self.name,
                           'test_end': True,
                           'rate': rate,
                           'payload': payload,
                           'n_msg': n_msg,
                           'confirm': confirm
                       },
                       flags={'MINPRINT': True})

        #self.networking.set_logging(True)
        #self.node.do_logging.set()

    def calibration_curve(self, path=None, calibration=None):
        """
        # compute curve to compute duration from desired volume

        Args:
            calibration:
            path: If present, use calibration file specified, otherwise use default.
        """

        lut_fn = os.path.join(prefs.BASEDIR, 'port_calibration_fit.json')

        if not calibration:
            # if we weren't given calibration results, load them
            if path:
                open_fn = path
            else:
                open_fn = os.path.join(prefs.BASEDIR, "port_calibration.json")

            with open(open_fn, 'r') as open_f:
                calibration = json.load(open_f)

        luts = {}
        for port, samples in calibration.items():
            sample_df = pd.DataFrame(samples)
            # TODO: Filter for only most recent timestamps

            # volumes are saved in mL because of how they are measured, durations are stored in ms
            # but reward volumes are typically in the uL range, so we make the conversion
            # by multiplying by 1000
            line_fit = linregress(
                (sample_df['vol'] / sample_df['n_clicks']) * 1000.,
                sample_df['dur'])
            luts[port] = {
                'intercept': line_fit.intercept,
                'slope': line_fit.slope
            }

        # write to file, overwriting any previous
        with open(lut_fn, 'w') as lutf:
            json.dump(luts, lutf)

    #################################################################
    # Hardware Init
    #################################################################

    def init_pigpio(self):
        try:
            self.pigpiod = external.start_pigpiod()
        except ImportError as e:
            self.pigpiod = None
            self.logger.exception(e)

    def init_audio(self):
        """
        Initialize an audio server depending on the value of
        `prefs.AUDIOSERVER`

        * 'pyo' = :func:`.pyoserver.pyo_server`
        * 'jack' = :class:`.jackclient.JackClient`
        """
        if prefs.AUDIOSERVER == 'pyo':
            self.server = pyoserver.pyo_server()
            self.logger.info("pyo server started")
        elif prefs.AUDIOSERVER == 'jack':
            self.jackd = external.start_jackd()
            self.server = jackclient.JackClient()
            self.server.start()
            self.logger.info('Started jack audio server')

    def blank_LEDs(self):
        """
        If any 'LEDS' are defined in `prefs.HARDWARE` ,
        instantiate them, set their color to [0,0,0],
        and then release them.
        """
        if 'LEDS' not in prefs.HARDWARE.keys():
            return

        for position, pins in prefs.HARDWARE['LEDS'].items():
            led = gpio.LED_RGB(pins=pins)
            time.sleep(1.)
            led.set_color(col=[0, 0, 0])
            led.release()

    #################################################################
    # Trial Running and Management
    #################################################################
    def open_file(self):
        """
        Setup a table to store data locally.

        Opens `prefs.DATADIR/local.h5`, creates a group for the current subject,
        a new table for the current day.

        Returns:
            (:class:`tables.File`, :class:`tables.Table`,
            :class:`tables.tableextension.Row`): The file, table, and row for the local data table
        """
        local_file = os.path.join(prefs.DATADIR, 'local.h5')
        try:
            h5f = tables.open_file(local_file, mode='a')
        except IOError as e:
            self.logger.warning("local file was broken, making new")
            self.logger.warning(e)
            os.remove(local_file)
            h5f = tables.open_file(local_file, mode='w')
            os.chmod(local_file, 0o777)

        try:
            h5f.create_group("/", self.subject,
                             "Local Data for {}".format(self.subject))
        except tables.NodeError:
            # already made it
            pass
        subject_group = h5f.get_node('/', self.subject)

        # Make a table for today's data, appending a conflict-avoidance int if one already exists
        datestring = datetime.date.today().isoformat()
        conflict_avoid = 0
        while datestring in subject_group:
            conflict_avoid += 1
            datestring = datetime.date.today().isoformat() + '-' + str(
                conflict_avoid)

        # Get data table descriptor
        if hasattr(self.task, 'TrialData'):
            table_descriptor = self.task.TrialData

            table = h5f.create_table(
                subject_group, datestring, table_descriptor,
                "Subject {} on {}".format(self.subject, datestring))

            # The Row object is what we write data into as it comes in
            row = table.row
            return h5f, table, row

        else:
            return h5f, None, None

    def run_task(self, task_class, task_params):
        """
        Called in a new thread, run the task.

        Opens a file with :meth:`~.autopilot.open_file` , then
        continually calls `task.stages.next` to process stages.

        Sends data back to the terminal between every stage.

        Waits for the task to clear `stage_block` between stages.
        """
        # TODO: give a net node to the Task class and let the task run itself.
        # Run as a separate thread, just keeps calling next() and shoveling data
        self.task = task_class(stage_block=self.stage_block, **task_params)

        # do we expect TrialData?
        trial_data = False
        if hasattr(self.task, 'TrialData'):
            trial_data = True

        # Open local file for saving
        h5f, table, row = self.open_file()

        # TODO: Init sending continuous data here

        while True:
            # Calculate next stage data and prep triggers
            data = next(self.task.stages)(
            )  # Double parens because next just gives us the function, we still have to call it

            if data:
                data['pilot'] = self.name
                data['subject'] = self.subject

                # Send data back to terminal (subject is identified by the networking object)
                self.node.send('T', 'DATA', data)

                # Store a local copy
                # the task class has a class variable DATA that lets us know which data the row is expecting
                if trial_data:
                    for k, v in data.items():
                        if k in self.task.TrialData.columns.keys():
                            row[k] = v

                # If the trial is over (either completed or bailed), flush the row
                if 'TRIAL_END' in data.keys():
                    row.append()
                    table.flush()

            # Wait on the stage lock to clear
            self.stage_block.wait()

            # If the running flag gets set, we're closing.
            if not self.running.is_set():
                self.task.end()
                self.task = None
                row.append()
                table.flush()
                break

        h5f.flush()
        h5f.close()
class Terminal(QtWidgets.QMainWindow):
    """
    Central host to a fleet of :class:`.Pilot` s and user-facing
    :mod:`~.core.gui` objects.

    Called as a module with the -f flag to give the location of a prefs file, eg::

        python terminal.py -f prefs_file.json

    if the -f flag is not passed, looks in the default location for prefs
    (ie. `/usr/autopilot/prefs.json`)

    **Listens used by the internal :class:`.Net_Node` **

    +---------------+--------------------------------+--------------------------------------------------------+
    | Key           | Method                         | Description                                            |
    +===============+================================+========================================================+
    | `'STATE'`     | :meth:`~.Terminal.l_state`     | A Pi has changed state                                 |
    +---------------+--------------------------------+--------------------------------------------------------+
    | `'PING'`      | :meth:`~.Terminal.l_ping`      |  Someone wants to know if we're alive                  |
    +---------------+--------------------------------+--------------------------------------------------------+
    | `'DATA'`      | :meth:`~.Terminal.l_data`      | Receiving data to store                                |
    +---------------+--------------------------------+--------------------------------------------------------+
    | `'HANDSHAKE'` | :meth:`~.Terminal.l_handshake` | Pilot first contact, telling us it's alive and its IP  |
    +---------------+--------------------------------+--------------------------------------------------------+

    ** Prefs needed by Terminal **
    Typically set by :mod:`.setup.setup_terminal`

    * **BASEDIR** - Base directory for all local autopilot data, typically `/usr/autopilot`
    * **MSGPORT** - Port to use for our ROUTER listener, default `5560`
    * **DATADIR** -  `os.path.join(params['BASEDIR'], 'data')`
    * **SOUNDDIR** - `os.path.join(params['BASEDIR'], 'sounds')`
    * **PROTOCOLDIR** - `os.path.join(params['BASEDIR'], 'protocols')`
    * **LOGDIR** - `os.path.join(params['BASEDIR'], 'logs')`
    * **REPODIR** - Path to autopilot git repo
    * **PILOT_DB** - Location of `pilot_db.json` used to populate :attr:`~.Terminal.pilots`

    Attributes:
        node (:class:`~.networking.Net_Node`): Our Net_Node we use to communicate with our main networking object
        networking (:class:`~.networking.Terminal_Station`): Our networking object to communicate with the outside world
        subjects (dict): A dictionary mapping subject ID to :class:`~.subject.Subject` object.
        layout (:class:`QtWidgets.QGridLayout`): Layout used to organize widgets
        control_panel (:class:`~.gui.Control_Panel`): Control Panel to manage pilots and subjects
        data_panel (:class:`~.plots.Plot_Widget`): Plots for each pilot and subject.
        logo (:class:`QtWidgets.QLabel`): Label holding our beautiful logo ;X
        logger (:class:`logging.Logger`): Used to log messages and network events.
    """
    def __init__(self):
        # type: () -> None
        super(Terminal, self).__init__()

        # store instance
        globals()['_TERMINAL'] = self

        # networking
        self.node = None
        self.networking = None
        self.heartbeat_dur = 10  # check every n seconds whether our pis are around still

        # data
        self.subjects = {}  # Dict of our open subject objects

        # gui
        self.layout = None
        self.widget = None
        self.file_menu = None
        self.tool_menu = None
        self.control_panel = None
        self.data_panel = None
        self.logo = None

        # property private attributes
        self._pilots = None

        # logging
        self.logger = init_logger(self)

        # Listen dictionary - which methods to call for different messages
        # Methods are spawned in new threads using handle_message
        self.listens = {
            'STATE': self.l_state,  # A Pi has changed state
            'PING': self.l_ping,  # Someone wants to know if we're alive
            'DATA': self.l_data,
            'CONTINUOUS':
            self.l_data,  # handle continuous data same way as other data
            'STREAM': self.l_data,
            'HANDSHAKE':
            self.l_handshake  # a pi is making first contact, telling us its IP
        }

        # Make invoker object to send GUI events back to the main thread
        # self.invoker = Invoker()
        self.invoker = get_invoker()
        # prefs.add('INVOKER', self.invoker)

        self.initUI()

        # Start Networking
        # Networking is in two parts,
        # "internal" networking for messages sent to and from the Terminal object itself
        # "external" networking for messages to and from all the other components,
        # The split is so the external networking can run in another process, do potentially time-consuming tasks
        # like resending & confirming message delivery without blocking or missing messages

        self.node = Net_Node(id="_T",
                             upstream='T',
                             port=prefs.get('MSGPORT'),
                             listens=self.listens)
        self.logger.info("Net Node Initialized")

        # Start external communications in own process
        # Has to be after init_network so it makes a new context
        self.networking = Terminal_Station(self.pilots)
        self.networking.start()
        self.logger.info("Station object Initialized")

        # send an initial ping looking for our pilots
        self.node.send('T', 'INIT')

        # start beating ur heart
        # self.heartbeat_timer = threading.Timer(self.heartbeat_dur, self.heartbeat)
        # self.heartbeat_timer.daemon = True
        # self.heartbeat_timer.start()
        #self.heartbeat(once=True)
        self.logger.info('Terminal Initialized')

    def initUI(self):
        """
        Initializes graphical elements of Terminal.

        Including...

        * Toolbar
        * :class:`.gui.Control_Panel`
        * :class:`.plots.Plot_Widget`
        """

        # set central widget
        self.widget = QtWidgets.QWidget()
        self.setCentralWidget(self.widget)

        # Start GUI
        self.layout = QtWidgets.QGridLayout()
        self.layout.setSpacing(0)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.widget.setLayout(self.layout)

        self.setWindowTitle('Terminal')
        #self.menuBar().setFixedHeight(40)

        # Main panel layout
        #self.panel_layout.setContentsMargins(0,0,0,0)

        # Init toolbar
        # File menu
        # make menu take up 1/10 of the screen
        winsize = app.desktop().availableGeometry()

        if sys.platform == 'darwin':
            bar_height = 0
        else:
            bar_height = (winsize.height() / 30) + 5
            self.menuBar().setFixedHeight(bar_height)

        self.file_menu = self.menuBar().addMenu("&File")
        self.file_menu.setObjectName("file")
        new_pilot_act = QtWidgets.QAction("New &Pilot",
                                          self,
                                          triggered=self.new_pilot)
        new_prot_act = QtWidgets.QAction("New Pro&tocol",
                                         self,
                                         triggered=self.new_protocol)
        #batch_create_subjects = QtWidgets.QAction("Batch &Create subjects", self, triggered=self.batch_subjects)
        # TODO: Update pis
        self.file_menu.addAction(new_pilot_act)
        self.file_menu.addAction(new_prot_act)
        #self.file_menu.addAction(batch_create_subjects)

        # Tools menu
        self.tool_menu = self.menuBar().addMenu("&Tools")
        subject_weights_act = QtWidgets.QAction("View Subject &Weights",
                                                self,
                                                triggered=self.subject_weights)
        update_protocol_act = QtWidgets.QAction(
            "Update Protocols", self, triggered=self.update_protocols)
        reassign_act = QtWidgets.QAction("Batch Reassign Protocols",
                                         self,
                                         triggered=self.reassign_protocols)
        calibrate_act = QtWidgets.QAction("Calibrate &Water Ports",
                                          self,
                                          triggered=self.calibrate_ports)
        self.tool_menu.addAction(subject_weights_act)
        self.tool_menu.addAction(update_protocol_act)
        self.tool_menu.addAction(reassign_act)
        self.tool_menu.addAction(calibrate_act)

        # Plots menu
        self.plots_menu = self.menuBar().addMenu("&Plots")
        psychometric = QtGui.QAction("Psychometric Curve",
                                     self,
                                     triggered=self.plot_psychometric)
        self.plots_menu.addAction(psychometric)

        # Tests menu
        self.tests_menu = self.menuBar().addMenu("Test&s")
        bandwidth_test_act = QtWidgets.QAction("Test Bandwidth",
                                               self,
                                               triggered=self.test_bandwidth)
        self.tests_menu.addAction(bandwidth_test_act)

        ## Init main panels and add to layout
        # Control panel sits on the left, controls pilots & subjects
        self.control_panel = Control_Panel(pilots=self.pilots,
                                           subjects=self.subjects,
                                           start_fn=self.toggle_start)

        # Data panel sits on the right, plots stuff.
        self.data_panel = Plot_Widget()
        self.data_panel.init_plots(self.pilots.keys())

        # Logo goes up top
        # https://stackoverflow.com/questions/25671275/pyside-how-to-set-an-svg-icon-in-qtreewidgets-item-and-change-the-size-of-the

        #
        # pixmap_path = os.path.join(os.path.dirname(prefs.get('AUTOPILOT_ROOT')), 'graphics', 'autopilot_logo_small.svg')
        # #svg_renderer = QtSvg.QSvgRenderer(pixmap_path)
        # #image = QtWidgets.QImage()
        # #self.logo = QtSvg.QSvgWidget()
        #
        #
        # # set size, preserving aspect ratio
        # logo_height = round(44.0*((bar_height-5)/44.0))
        # logo_width = round(139*((bar_height-5)/44.0))
        #
        # svg_renderer = QtSvg.QSvgRenderer(pixmap_path)
        # image = QtGui.QImage(logo_width, logo_height, QtGui.QImage.Format_ARGB32)
        # # Set the ARGB to 0 to prevent rendering artifacts
        # image.fill(0x00000000)
        # svg_renderer.render(QtGui.QPainter(image))
        # pixmap = QtGui.QPixmap.fromImage(image)
        # self.logo = QtWidgets.QLabel()
        # self.logo.setPixmap(pixmap)

        if sys.platform != 'darwin':
            self.menuBar().setCornerWidget(self.logo, QtCore.Qt.TopRightCorner)
            self.menuBar().adjustSize()

        #self.logo.load(pixmap_path)
        # Combine all in main layout
        #self.layout.addWidget(self.logo, 0,0,1,2)
        self.layout.addWidget(self.control_panel, 0, 0, 1, 1)
        self.layout.addWidget(self.data_panel, 0, 1, 1, 1)
        self.layout.setColumnStretch(0, 1)
        self.layout.setColumnStretch(1, 3)

        # Set size of window to be fullscreen without maximization
        # Until a better solution is found, if not set large enough, the pilot tabs will
        # expand into infinity. See the Expandable_Tabs class
        #pdb.set_trace()
        screensize = app.desktop().screenGeometry()
        winsize = app.desktop().availableGeometry()

        # want to subtract bounding title box, our title bar, and logo height.
        # our y offset will be the size of the bounding title box

        # Then our tilebar
        # multiply by three to get the inner (file, etc.) bar, the top bar (min, maximize, etc)
        # and then the very top system tray bar in ubuntu
        #titleBarHeight = self.style().pixelMetric(QtWidgets.QStyle.PM_TitleBarHeight,
        #                                          QtWidgets.QStyleOptionTitleBar(), self) * 3
        title_bar_height = screensize.height() - winsize.height()

        #titleBarHeight = bar_height*2
        # finally our logo
        logo_height = bar_height

        winheight = winsize.height(
        ) - title_bar_height - logo_height  # also subtract logo height
        winsize.setHeight(winheight)
        self.max_height = winheight
        self.setGeometry(winsize)
        self.setSizePolicy(QtWidgets.QSizePolicy.Maximum,
                           QtWidgets.QSizePolicy.Maximum)

        # Set heights on control panel and data panel

        # move to primary display and show maximized
        primary_display = app.desktop().availableGeometry(0)
        self.move(primary_display.left(), primary_display.top())
        # self.resize(primary_display.width(), primary_display.height())
        #
        self.control_panel.setMaximumHeight(winheight)
        self.data_panel.setMaximumHeight(winheight)

        # set stylesheet for main window
        self.setStyleSheet(styles.TERMINAL)

        # set fonts to antialias
        self.setFont(self.font().setStyleStrategy(QtGui.QFont.PreferAntialias))

        self.show()
        logging.info('UI Initialized')

    def reset_ui(self):
        """
        Clear Layout and call :meth:`~.Terminal.initUI` again
        """

        # type: () -> None
        self.layout = QtWidgets.QGridLayout()
        self.layout.setSpacing(0)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.widget.setLayout(self.layout)
        self.setCentralWidget(self.widget)
        self.initUI()

    ################
    # Properties

    @property
    def pilots(self) -> odict:
        """
        A dictionary mapping pilot ID to its attributes, including a list of its subjects assigned to it, its IP, etc.

        Returns:
            dict: like ``self.pilots['pilot_id'] = {'subjects': ['subject_0', 'subject_1'], 'ip': '192.168.0.101'}``
        """

        # try to load, if none exists make one
        if self._pilots is None:

            pilot_db_fn = Path(prefs.get('PILOT_DB'))

            # if pilot file doesn't exist, make blank one
            if not pilot_db_fn.exists():
                self.logger.warning(
                    f'No pilot_db.json file was found at {pilot_db_fn}, creating a new one'
                )
                self._pilots = odict()
                with open(pilot_db_fn, 'w') as pilot_file:
                    json.dump(self._pilots, pilot_file)

            # otherwise, try to load it
            else:
                try:
                    # Load pilots db as ordered dictionary
                    with open(pilot_db_fn, 'r') as pilot_file:
                        self._pilots = json.load(pilot_file,
                                                 object_pairs_hook=odict)
                    self.logger.info(
                        f'successfully loaded pilot_db.json file from {pilot_db_fn}'
                    )
                    self.logger.debug(pformat(self._pilots))
                except Exception as e:
                    self.logger.exception((
                        f"Exception opening pilot_db.json file at {pilot_db_fn}, got exception: {e}.\n",
                        "Not proceeding to prevent possibly overwriting corrupt pilot_db.file"
                    ))
                    raise e

        return self._pilots

    @property
    def protocols(self) -> list:
        """
        List of protocol names available in ``PROTOCOLDIR``

        Returns:
            list: list of protocol names in ``prefs.get('PROTOCOLDIR')``
        """
        # get list of protocol files
        protocols = os.listdir(prefs.get('PROTOCOLDIR'))
        protocols = [
            os.path.splitext(p)[0] for p in protocols if p.endswith('.json')
        ]
        return protocols

    @property
    def subject_protocols(self) -> dict:
        """

        Returns:
            subject_protocols (dict): a dictionary of subjects: [protocol, step]
        """
        # get subjects and current protocols
        subjects = self.subject_list
        subjects_protocols = {}
        for subject in subjects:
            if subject not in self.subjects.keys():
                self.subjects[subject] = Subject(subject)

            subjects_protocols[subject] = [
                self.subjects[subject].protocol_name,
                self.subjects[subject].step
            ]

        return subjects_protocols

    @property
    def subject_list(self) -> list:
        """
        Get a list of all subject IDs

        Returns:
            list: list of all subject IDs present in :attr:`.Terminal.pilots`
        """
        subjects = []
        for pilot, vals in self.pilots.items():
            subjects.extend(vals['subjects'])

        # use sets to get a unique list
        subjects = list(set(subjects))

        return subjects

    ##########################3
    # Listens & inter-object methods

    def heartbeat(self, once=False):
        """
        Perioducally send an ``INIT`` message that checks the status of connected pilots

        sent with frequency according to :attr:`.Terminal.heartbeat_dur`

        Args:
            once (bool): if True, do a single heartbeat but don't start a thread to do more.

        """
        self.node.send('T', 'INIT', repeat=False, flags={'NOREPEAT': True})

        if not once:
            self.heartbeat_timer = threading.Timer(self.heartbeat_dur,
                                                   self.heartbeat)
            self.heartbeat_timer.daemon = True
            self.heartbeat_timer.start()

    def toggle_start(self, starting, pilot, subject=None):
        """Start or Stop running the currently selected subject's task. Sends a
        message containing the task information to the concerned pilot.

        Each :class:`Pilot_Panel` is given a lambda function that calls this
        one with the arguments specified See :class:`Pilot_Button`, as it is
        what calls this function.

        Args:
            starting (bool): Does this button press mean we are starting (True)
                or stopping (False) the task?
            pilot: Which Pilot is starting or stopping?
            subject: Which Subject is currently selected?
        """
        # stopping is the enemy of starting so we put them in the same function to learn about each other
        if starting is True:
            # Get Weights
            start_weight, ok = QtWidgets.QInputDialog.getDouble(
                self, "Set Starting Weight", "Starting Weight:")
            if ok:
                # Ope'nr up if she aint
                if subject not in self.subjects.keys():
                    self.subjects[subject] = Subject(subject)

                task = self.subjects[subject].prepare_run()
                task['pilot'] = pilot
                self.subjects[subject].update_weights(
                    start=float(start_weight))

                self.node.send(to=pilot, key="START", value=task)
                # also let the plot know to start
                self.node.send(to="P_{}".format(pilot),
                               key="START",
                               value=task)

            else:
                # pressed cancel, don't start
                return

        else:
            # Get Weights
            stop_weight, ok = QtWidgets.QInputDialog.getDouble(
                self, "Set Stopping Weight", "Stopping Weight:")

            if ok:
                # Send message to pilot to stop running,
                # it should initiate a coherence checking routine to make sure
                # its data matches what the Terminal got,
                # so the terminal will handle closing the subject object
                self.node.send(to=pilot, key="STOP")
                # also let the plot know to start
                self.node.send(to="P_{}".format(pilot), key="STOP")
                # TODO: Start coherence checking ritual
                # TODO: Auto-select the next subject in the list.

                self.subjects[subject].stop_run()
                self.subjects[subject].update_weights(stop=float(stop_weight))

            else:
                # pressed cancel
                return

    ############################
    # MESSAGE HANDLING METHODS

    def l_data(self, value):
        """
        A Pilot has sent us data.

        `value` field of message should have `subject` and `pilot` added to dictionary for identification.

        Any key in `value` that matches a column in the subject's trial data table will be saved.

        If the subject graduates after receiving this piece of data, stop the current
        task running on the Pilot and send the new one.

        Args:
            value (dict): A dict of field-value pairs to save
        """
        # A Pi has sent us data, let's save it huh?
        subject_name = value['subject']
        self.subjects[subject_name].save_data(value)
        if self.subjects[subject_name].did_graduate.is_set() is True:
            self.node.send(to=value['pilot'],
                           key="STOP",
                           value={'graduation': True})
            self.subjects[subject_name].stop_run()
            self.subjects[subject_name].graduate()
            task = self.subjects[subject_name].prepare_run()
            task['pilot'] = value['pilot']

            self.node.send(to=value['pilot'], key="START", value=task)

    def l_ping(self, value):
        """
        TODO:
            Reminder to implement heartbeating.

        Note:
            Currently unused, as Terminal Net_Node stability hasn't been
            a problem and no universal system of heartbeating has been
            established (global stability has not been an issue).

        Args:
            value: (unused)
        """
        # Only our Station object should ever ping us, because
        # we otherwise want it handling any pings on our behalf.

        # self.send_message('ALIVE', value=b'T')
        pass

    def l_state(self, value):
        """A Pilot has changed state, keep track of it.

        Args:
            value (dict): dict containing `state` .
        """
        # TODO: If we are stopping, we enter into a cohere state
        # TODO: If we are stopped, close the subject object.
        # TODO: Also tell the relevant dataview to clear

        # update the pilot button
        if value['pilot'] in self.pilots.keys():
            if 'state' not in self.pilots[value['pilot']].keys():
                self.pilots[value['pilot']]['state'] = value['state']
                #self.control_panel.panels[value['pilot']].button.set_state(value['state'])
            elif value['state'] != self.pilots[value['pilot']]['state']:
                #self.control_panel.panels[value['pilot']].button.set_state(value['state'])
                self.pilots[value['pilot']]['state'] = value['state']

    def l_handshake(self, value):
        """
        Pilot is sending its IP and state on startup.

        If we haven't heard of this pilot before, make a new entry in :attr:`~.Terminal.pilots`
        and :meth:`.gui.Control_Panel.update_db` .

        Args:
            value (dict): dict containing `ip` and `state`
        """
        if value['pilot'] in self.pilots.keys():
            if 'ip' in value.keys():
                self.pilots[value['pilot']]['ip'] = value['ip']
            if 'state' in value.keys():
                self.pilots[value['pilot']]['state'] = value['state']

        else:
            self.new_pilot(name=value['pilot'], ip=value['ip'])

        # update the pilot button
        if value['pilot'] in self.control_panel.panels.keys():
            self.control_panel.panels[value['pilot']].button.set_state(
                value['state'])

        self.control_panel.update_db()

    #############################
    # GUI & etc. methods

    def new_pilot(self, ip='', name=None):
        """
        Make a new entry in :attr:`.Terminal.pilots` and make appropriate
        GUI elements.

        Args:
            ip (str): Optional. if given, stored in db.
            name (str): If None, prompted for a name, otherwise used for entry in pilot DB.
        """
        if name is None:
            name, ok = QtWidgets.QInputDialog.getText(self, "Pilot ID",
                                                      "Pilot ID:")

        # make sure we won't overwrite ourself
        if name in self.pilots.keys():
            # TODO: Pop a window confirming we want to overwrite
            pass

        if name != '':
            new_pilot = {name: {'subjects': [], 'ip': ip}}
            self.control_panel.update_db(new=new_pilot)
            self.reset_ui()
        else:
            # Idk maybe pop a dialog window but i don't really see why
            pass

    def new_protocol(self):
        """
        Open a :class:`.gui.Protocol_Wizard` to create a new protocol.

        Prompts for name of protocol, then saves in `prefs.get('PROTOCOLDIR')`
        """
        self.new_protocol_window = Protocol_Wizard()
        self.new_protocol_window.exec_()

        if self.new_protocol_window.result() == 1:
            steps = self.new_protocol_window.steps

            # The values useful to the step functions are stored with a 'value' key in the param_dict
            save_steps = []
            for s in steps:
                param_values = {}
                for k, v in s.items():
                    if 'value' in v.keys():
                        param_values[k] = v['value']
                    elif k == 'stim':
                        # TODO: Super hacky - don't do this. Refactor params already.
                        param_values[k] = {}
                        for stimtype, stim in v.items():
                            param_values[k][stimtype] = stim
                save_steps.append(param_values)

            # Name the protocol
            name, ok = QtWidgets.QInputDialog.getText(self, "Name Protocol",
                                                      "Protocol Name:")
            if ok and name != '':
                protocol_file = os.path.join(prefs.get('PROTOCOLDIR'),
                                             name + '.json')
                with open(protocol_file, 'w') as pfile_open:
                    json.dump(save_steps,
                              pfile_open,
                              indent=4,
                              separators=(',', ': '),
                              sort_keys=True)
            elif name == '' or not ok:
                placeholder_name = 'protocol_created_{}'.format(
                    datetime.date.today().isoformat())
                protocol_file = os.path.join(prefs.get('PROTOCOLDIR'),
                                             placeholder_name + '.json')
                with open(protocol_file, 'w') as pfile_open:
                    json.dump(save_steps,
                              pfile_open,
                              indent=4,
                              separators=(',', ': '),
                              sort_keys=True)

    def subject_weights(self):
        """
        Gets recent weights from all :attr:`~.Terminal.subjects` and
        open a :class:`.gui.Weights` window to view or set weights.
        """
        subjects = self.subject_list

        # open objects if not already
        for subject in subjects:
            if subject not in self.subjects.keys():
                self.subjects[subject] = Subject(subject)

        # for each subject, get weight
        weights = []
        for subject in subjects:
            weight = self.subjects[subject].get_weight(include_baseline=True)
            weight['subject'] = subject
            weights.append(weight)

        self.weight_widget = Weights(weights, self.subjects)
        self.weight_widget.show()

    def update_protocols(self):
        """
        If we change the protocol file, update the stored version in subject files
        """
        #
        # get list of protocol files
        protocols = os.listdir(prefs.get('PROTOCOLDIR'))
        protocols = [p for p in protocols if p.endswith('.json')]

        updated_subjects = []
        subjects = self.subject_list
        for subject in subjects:
            if subject not in self.subjects.keys():
                self.subjects[subject] = Subject(subject)

            protocol_bool = [
                self.subjects[subject].protocol_name == os.path.splitext(p)[0]
                for p in protocols
            ]
            if any(protocol_bool):
                which_prot = np.where(protocol_bool)[0][0]
                protocol = protocols[which_prot]
                self.subjects[subject].assign_protocol(
                    os.path.join(prefs.get('PROTOCOLDIR'), protocol),
                    step_n=self.subjects[subject].step)
                updated_subjects.append(subject)

        msgbox = QtWidgets.QMessageBox()
        msgbox.setText("Subject Protocols Updated for:")
        msgbox.setDetailedText("\n".join(sorted(updated_subjects)))
        msgbox.exec_()

    def reassign_protocols(self):
        """
        Batch reassign protocols and steps.

        Opens a :class:`.gui.Reassign` window after getting protocol data,
        and applies any changes made in the window.
        """

        reassign_window = Reassign(self.subject_protocols, self.protocols)
        reassign_window.exec_()

        if reassign_window.result() == 1:
            subject_protocols = reassign_window.subjects

            for subject, protocol in subject_protocols.items():
                step = protocol[1]
                protocol = protocol[0]

                # since assign_protocol also changes the step, stash the step number here to tell if it's changed
                subject_orig_step = self.subjects[subject].step

                if self.subjects[subject].protocol_name != protocol:
                    self.logger.info(
                        'Setting {} protocol from {} to {}'.format(
                            subject, self.subjects[subject].protocol_name,
                            protocol))
                    protocol_file = os.path.join(prefs.get('PROTOCOLDIR'),
                                                 protocol + '.json')
                    self.subjects[subject].assign_protocol(protocol_file, step)

                if subject_orig_step != step:
                    self.logger.info('Setting {} step from {} to {}'.format(
                        subject, subject_orig_step, step))
                    step_name = self.subjects[subject].current[step][
                        'step_name']
                    #update history also flushes current - aka it also actually changes the step number
                    self.subjects[subject].update_history(
                        'step', step_name, step)

    def calibrate_ports(self):
        """
        Calibrate :class:`.hardware.gpio.Solenoid` objects.

        See :class:`.gui.Calibrate_Water`.

        After calibration routine, send results to pilot for storage.
        """

        calibrate_window = Calibrate_Water(self.pilots)
        calibrate_window.exec_()

        if calibrate_window.result() == 1:
            for pilot, p_widget in calibrate_window.pilot_widgets.items():
                p_results = p_widget.volumes
                # p_results are [port][dur] = {params} so running the same duration will
                # overwrite a previous run. unnest here so pi can keep a record
                unnested_results = {}
                for port, result in p_results.items():
                    unnested_results[port] = []
                    # result is [dur] = {params}
                    for dur, inner_result in result.items():
                        inner_result['dur'] = dur
                        unnested_results[port].append(inner_result)

                # send to pi
                self.node.send(to=pilot,
                               key="CALIBRATE_RESULT",
                               value=unnested_results)

            msgbox = QtWidgets.QMessageBox()
            msgbox.setText("Calibration results sent!")
            msgbox.exec_()

    def test_bandwidth(self):
        """
        Test bandwidth of Pilot connection with variable sized arrays as paylods

        See :class:`.gui.Bandwidth_Test`

        """
        # turn off logging while we run
        prev_networking_loglevel = self.networking.logger.level
        prev_node_loglevel = self.node.logger.level
        self.networking.logger.setLevel(logging.ERROR)
        self.node.logger.setLevel(logging.ERROR)

        bandwidth_test = Bandwidth_Test(self.pilots)
        bandwidth_test.exec_()

        self.networking.logger.setLevel(prev_networking_loglevel)
        self.node.logger.setLevel(prev_node_loglevel)

    def plot_psychometric(self):
        """
        Select subject, step, and variables to plot a psychometric curve

        """

        if not IMPORTED_VIZ:
            _ = pop_dialog("Vizualisation function couldn't be imported!",
                           "error", VIZ_ERROR)
            return

        psychometric_dialog = Psychometric(self.subject_protocols)
        psychometric_dialog.exec_()

        # if user cancels, return
        if psychometric_dialog.result() != 1:
            return

        chart = viz.plot_psychometric(psychometric_dialog.plot_params)

        text, ok = QtGui.QInputDialog.getText(self, 'save plot?',
                                              'what to call this thing')
        if ok:
            chart.save(text)

        #chart.serve()

        #viz.plot_psychometric(self.subjects_protocols)
        #result = psychometric_dialog.exec_()

    def closeEvent(self, event):
        """
        When Closing the Terminal Window, close any running subject objects,
        'KILL' our networking object.

        Since the `:class:`.Net_Node` keeping us alive is a `daemon`, no need
        to explicitly kill it.

        """
        # TODO: Check if any subjects are currently running, pop dialog asking if we want to stop

        # Close all subjects files
        for m in self.subjects.values():
            if m.running is True:
                m.stop_run()

        # Stop networking
        # send message to kill networking process
        self.node.send(key="KILL")

        event.accept()
Esempio n. 5
0
class DLC_Hand(Task):
    STAGE_NAMES = ['noop']

    PARAMS = odict()
    PARAMS['child_id'] = {
        'tag': 'id of Child to process frames',
        'type': 'str'
    }
    PARAMS['model_name'] = {
        'tag': 'name of deeplabcut project (located in <autopilot_dir>/dlc',
        'type': 'str'
    }
    PARAMS['point_name_1'] = {
        'tag': 'name of first deeplabcut point to track',
        'type': 'str'
    }
    PARAMS['point_name_2'] = {
        'tag': 'name of second deeplabcut point to track',
        'type': 'str'
    }
    PARAMS['crop_box'] = {
        'tag':
        'Bounding box of image capture for FLIR camera [x_offset, y_offset, x_width, y_height]',
        'type': 'str'
    }

    PLOT = {
        'data': {
            'distance': 'point',
            'angle': 'point'
        },
        'continuous': True
    }

    ContinuousData = {
        'distance': tables.Float64Col(),
        'angle': tables.Float64Col(),
        'timestamp': tables.StringCol(26),
    }

    HARDWARE = {
        'LEDS': {
            'C': gpio.LED_RGB
        },
        'CAMERAS': {
            'WEBCAM': cameras.Camera_CV
        }
    }

    CHILDREN = {'DLC': {'task_type': 'Transformer', 'transform': []}}

    def __init__(self, child_id: str, model_name: str, point_name_1: str,
                 point_name_2: str, crop_box: list, *args, **kwargs):

        super(DLC_Hand, self).__init__(*args, **kwargs)

        self.child_id = child_id
        self.model_name = model_name
        self.point_name_1 = point_name_1
        self.point_name_2 = point_name_2
        self.crop_box = crop_box

        self.led_lock = threading.Lock()

        self.init_hardware()

        # configure the child
        transform_descriptor = [{
            'transform': 'image.DLC',
            'kwargs': {
                'model_dir': self.model_name
            }
        }, {
            'transform': 'selection.DLCSlice',
            'kwargs': {
                'select': [self.point_name_1, self.point_name_2],
                'min_probability': 0
            }
        }]

        self.node_id = f"T_{prefs.get('NAME')}"
        self.node = Net_Node(id=self.node_id,
                             upstream=prefs.get('NAME'),
                             port=prefs.get('MSGPORT'),
                             listens={
                                 'STATE': self.l_state,
                                 'UPDATE': self.l_update
                             },
                             instance=False)

        self.subject = kwargs['subject']
        value = {
            'child': {
                'parent': prefs.get('NAME'),
                'subject': kwargs['subject']
            },
            'task_type': 'Transformer',
            'subject': kwargs['subject'],
            'operation': 'stream',
            'transform': transform_descriptor,
            'return_id': self.node_id,
            'value_subset': 'WEBCAM',
            'return_key': 'UPDATE'
        }

        self.node.send(to=prefs.get('NAME'), key='CHILD', value=value)

        # configure the camera
        self.cam = self.hardware['CAMERAS']['WEBCAM']
        self.cam.crop = crop_box

        self.cam.stream(to=f"{self.child_id}_TRANSFORMER",
                        ip=prefs.get('CHILDIP'),
                        port=prefs.get('CHILDPORT'),
                        min_size=1)

        video_fn = os.path.join(
            prefs.get('DATADIR'),
            'dlc_hand_{}.mp4'.format(datetime.now().isoformat()))
        self.cam.write(video_fn)

        # setup our own transforms
        max_dim = max(crop_box[2:4])
        self.transforms = {
            'distance': t.geometry.Distance() + \
                        t.units.Rescale((0, 100), (0, 1), clip=True),
            'angle': t.geometry.Angle() + \
                     t.units.Rescale((0, 360), (0, 1)),
            'color': t.units.Color(t.units.Colorspaces.HSV, t.units.Colorspaces.RGB)
        }

        # get a stream to send data to terminal with
        # self.stream = self.node.get_stream('T', 'CONTINUOUS',
        #                                    upstream='T', ip=prefs.get('TERMINALIP'),port=prefs.get('PUSHPORT'),
        #                                    subject=self.subject)

        self.stages = cycle([self.noop])

    def noop(self):
        if self.stage_block:
            self.stage_block.clear()

    def l_state(self, value):
        self.logger.debug(f'STATE from transformer: {value}')

        if value == 'READY':
            if not self.cam.capturing.is_set():
                self.cam.capture()
                self.logger.debug(f"started capture")

    def l_update(self, value):
        # receive two points, convert to distance, angle, and then to color
        if any(value[:, 2] < 0.2):
            return

        angle = self.transforms['angle'].process(value)
        distance = self.transforms['distance'].process(value)

        color = self.transforms['color'].process((angle, 1, distance))
        acquired = self.led_lock.acquire(blocking=False)
        if acquired:
            try:
                self.hardware['LEDS']['C'].set(r=color[0],
                                               g=color[1],
                                               b=color[2])
            finally:
                self.led_lock.release()

        # self.stream.put({
        #     'angle': angle,
        #     'distance': distance,
        #     'timestamp': datetime.now().isoformat(),
        #     'subject': self.subject
        # })
        self.node.send(
            'T', 'DATA', {
                'angle': angle,
                'distance': distance,
                'timestamp': datetime.now().isoformat(),
                'subject': self.subject,
                'pilot': prefs.get('NAME'),
                'continuous': True,
                't': time()
            })
Esempio n. 6
0
class DLC_Latency(Task):
    """
    Test Deeplabcut live end-to-end latency

    * Capture video -- all dark but with LED triggered by pilot
    * Send frames to Jetson (or other compute child)
    * Process with DLC
    * If LED is detected, jetson sends trigger back to Pilot
    * Pilot sends GPIO pulse

    """
    STAGE_NAMES = ['trig', 'wait']

    PARAMS = odict()
    PARAMS['child_id'] = {
        'tag': 'id of Child to process frames',
        'type': 'str'
    }
    PARAMS['model_name'] = {
        'tag': 'name of deeplabcut project (located in <autopilot_dir>/dlc',
        'type': 'str'
    }
    PARAMS['point_name'] = {
        'tag': 'name of deeplabcut point used to trigger',
        'type': 'str'
    }
    PARAMS['trigger_freq'] = {
        'tag': 'Frequency of LED trigger (Hz)',
        'type': 'float'
    }
    PARAMS['trigger_thresh'] = {
        'tag': 'Probability Threshold of Detection (0-1)',
        'type': 'float'
    }
    PARAMS['trigger_limits_x'] = {
        'tag':
        'Range of x (pixels) for object detection - list like [min, max]',
        'type': 'str'
    }
    PARAMS['trigger_limits_y'] = {
        'tag':
        'Range of x (pixels) for object detection - list like [min, max]',
        'type': 'str'
    }
    PARAMS['trigger_max_y'] = {
        'tag': 'Maximum y (pixels) for object detection',
        'type': 'int'
    }
    PARAMS['crop_box'] = {
        'tag':
        'Bounding box of image capture for FLIR camera [x_offset, y_offset, x_width, y_height]',
        'type': 'str'
    }
    PARAMS['fps'] = {'tag': 'FPS of image acquisition', 'type': 'int'}
    PARAMS['exposure'] = {
        'tag': 'Exposure of camera (see cameras.Camera_Spinnaker.exposure)',
        'type': 'float'
    }
    PARAMS['delay'] = {'tag': 'Delay between trials (ms)', 'type': 'int'}

    PLOT = {
        'data': {
            'plot_trigger': 'segment',
            'plot_response': 'point'
        },
        'continuous': True
    }

    class TrialData(tables.IsDescription):
        trial_num = tables.Int32Col()
        trigger = tables.StringCol(26)
        response = tables.StringCol(26)
        plot_trigger = tables.Int8Col()
        plot_response = tables.Int8Col()

    HARDWARE = {
        'LEDS': {
            'C': gpio.LED_RGB
        },
        'CAMERAS': {
            'MAIN': cameras.Camera_Spinnaker
        },
        'DIGITAL_OUT': {
            'C': gpio.Digital_Out
        }
    }

    CHILDREN = {'DLC': {'task_type': 'Transformer', 'transform': []}}

    def __init__(self,
                 child_id: str,
                 model_name: str,
                 point_name: str,
                 trigger_limits_x: list,
                 trigger_limits_y: list,
                 trigger_freq: float = 0.5,
                 trigger_thresh: float = 0.6,
                 crop_box: list = None,
                 fps: int = 30,
                 exposure: float = 0.5,
                 delay: int = 5000,
                 *args,
                 **kwargs):
        """

        Args:
            child_id:
            trigger_freq:
            trigger_thresh:
            trigger_limits_x:
            trigger_limitx_y:
            crop_box:
            fps:
            exposure:
            delay (int): time to wait between trials (ms)
        """

        # if we have left and right LEDs, init them and turn them off
        HAS_LR_LEDS = False
        if 'L' in prefs.get('HARDWARE')['LEDS'].keys() and 'R' in prefs.get(
                'HARDWARE')['LEDS'].keys():
            HAS_LR_LEDS = True

            self.HARDWARE['LEDS']['L'] = gpio.LED_RGB
            self.HARDWARE['LEDS']['R'] = gpio.LED_RGB

        super(DLC_Latency, self).__init__(*args, **kwargs)

        self.child_id = child_id
        self.model_name = model_name
        self.point_name = point_name
        self.trigger_limits_x = trigger_limits_x
        self.trigger_limits_y = trigger_limits_y
        self.trigger_freq = trigger_freq
        self.trigger_thresh = trigger_thresh
        self.crop_box = crop_box
        self.fps = fps
        self.exposure = exposure
        self.delay = delay
        self.trial_counter = count()
        self.current_trial = 0

        self.init_hardware()

        # if we have extra LEDs attached (as in the tripoke mount), turn them off
        if HAS_LR_LEDS:
            self.hardware['LEDS']['L'].set(0.0001)
            self.hardware['LEDS']['R'].set(0.0001)

        # configure the child
        transform_descriptor = [{
            'transform': 'image.DLC',
            'kwargs': {
                'model_dir': self.model_name
            }
        }, {
            'transform': 'selection.DLCSlice',
            'kwargs': {
                'select': self.point_name,
                'min_probability': self.trigger_thresh
            }
        }, {
            'transform': 'logical.Condition',
            'kwargs': {
                'minimum':
                [self.trigger_limits_x[0], self.trigger_limits_y[0]],
                'maximum':
                [self.trigger_limits_x[1], self.trigger_limits_y[1]],
                'elementwise': False
            }
        }]

        self.node_id = f"T_{prefs.get('NAME')}"
        self.node = Net_Node(id=self.node_id,
                             upstream=prefs.get('NAME'),
                             port=prefs.get('MSGPORT'),
                             listens={
                                 'STATE': self.l_state,
                                 'TRIGGER': self.l_trigger
                             },
                             instance=False)

        # get our child started
        self.subject = kwargs['subject']
        value = {
            'child': {
                'parent': prefs.get('NAME'),
                'subject': kwargs['subject']
            },
            'task_type': 'Transformer',
            'subject': kwargs['subject'],
            'operation': 'stream',
            'transform': transform_descriptor,
            'return_id': self.node_id,
            'value_subset': 'MAIN'
        }

        self.node.send(to=prefs.get('NAME'), key='CHILD', value=value)

        # configure camera
        self.cam = self.hardware['CAMERAS']['MAIN']

        self.cam.fps = self.fps
        self.cam.exposure = self.exposure
        if self.crop_box:
            self.cam.set('Width', self.crop_box[2])
            self.cam.set('Height', self.crop_box[3])
            self.cam.set('OffsetX', self.crop_box[0])
            self.cam.set('OffsetY', self.crop_box[1])

        print(f"""
        Width: {self.cam.get('Width')}
        Height: {self.cam.get('Height')}
        OffsetX: {self.cam.get('OffsetX')}
        OffsetY: {self.cam.get('OffsetY')}
        FPS: {self.cam.get('AcquisitionFrameRate')}
        """)

        self.cam.stream(
            to=f"{self.child_id}_TRANSFORMER",
            ip=prefs.get(
                'CHILDIP'),  # FIXME: Hack before network discovery is fixed
            port=prefs.get('CHILDPORT'),
            min_size=1)

        self.stages = cycle([self.trig, self.wait])
        self.stage_block = kwargs['stage_block']

    def trig(self, *args, **kwargs):
        """
        Blink the LED and await a trigger

        Returns:

        """
        self.stage_block.clear()

        self.current_trial = next(self.trial_counter)

        # pulse gpio and turn off LED when triggered
        self.triggers['CAMERA'] = [self.hardware['DIGITAL_OUT']['C'].pulse]

        trig_time = datetime.now().isoformat()
        self.hardware['LEDS']['C'].set(1)

        return {
            'trigger': trig_time,
            'trial_num': self.current_trial,
            'plot_trigger': 1
        }

    def wait(self):
        # self.stage_block.clear()
        response_time = datetime.now().isoformat()
        self.hardware['LEDS']['C'].set(0.0001)

        sleep(self.delay / 1000)
        # timer = threading.Timer(interval=self.delay/1000, target=self.stage_block.clear)
        # timer.start()

        return {
            'response': response_time,
            'plot_response': 1,
            'TRIAL_END': True
        }

    def l_state(self, value):
        self.logger.debug(f'STATE from transformer: {value}')

        if value == 'READY':
            if not self.cam.capturing.is_set():
                self.cam.capture()
                self.logger.debug(f"started capture")

    def l_trigger(self, value):
        if value:
            self.handle_trigger(pin="CAMERA")
            self.hardware['DIGITAL_OUT']['C'].pulse()
Esempio n. 7
0
class Video_Child(object):
    PARAMS = odict()
    PARAMS['cams'] = {
        'tag': 'Dictionary of camera params, or list of dicts',
        'type': ('dict', 'list')
    }

    def __init__(self, cams=None, stage_block=None, start_now=True, **kwargs):
        """
        Args:
            cams (dict, list): Should be a dictionary of camera parameters or a list of dicts. Dicts should have, at least::

                {
                    'type': 'string_of_camera_class',
                    'name': 'name_of_camera_in_task',
                    'param1': 'first_param'
                }
        """

        if cams is None:
            Exception('Need to give us a cams dictionary!')

        self.cams = {}

        self.start_now = start_now

        if isinstance(cams, dict):

            try:
                cam_class = getattr(cameras, cams['type'])
                self.cams[cams['name']] = cam_class(**cams)
                # if start:
                #     self.cams[cams['name']].capture()
            except AttributeError:
                AttributeError("Camera type {} not found!".format(
                    cams['type']))

        elif isinstance(cams, list):
            for cam in cams:
                try:
                    cam_class = getattr(cameras, cam['type'])
                    self.cams[cam['name']] = cam_class(**cam)
                    # if start:
                    #     self.cams[cam['name']].capture()
                except AttributeError:
                    AttributeError("Camera type {} not found!".format(
                        cam['type']))

        self.stages = cycle([self.noop])
        self.stage_block = stage_block

        if self.start_now:
            self.start()
        # self.thread = threading.Thread(target=self._stream)
        # self.thread.daemon = True
        # self.thread.start()

    def start(self):
        for cam in self.cams.values():
            cam.capture()

    def stop(self):
        for cam_name, cam in self.cams.items():
            try:
                cam.release()
            except Exception as e:
                Warning('Couldnt release camera {},\n{}'.format(cam_name, e))

    def _stream(self):
        self.node = Net_Node("T_CHILD",
                             upstream=prefs.NAME,
                             port=prefs.MSGPORT,
                             listens={},
                             instance=True)

        while True:
            for name, cam in self.cams.items():
                try:
                    frame, timestamp = cam.q.get_nowait()
                    self.node.send(key='CONTINUOUS',
                                   value={
                                       cam.name: frame,
                                       'timestamp': timestamp
                                   },
                                   repeat=False,
                                   flags={'MINPRINT': True})
                except Empty:
                    pass

    def noop(self):
        # just fitting in with the task structure.
        self.stage_block.clear()
        return {}
Esempio n. 8
0
class Transformer(object):
    def __init__(self,
                 transform,
                 operation: str = "trigger",
                 return_id='T',
                 return_key=None,
                 stage_block=None,
                 value_subset=None,
                 **kwargs):
        """

        Args:
            transform:
            operation (str): either

                * "trigger", where the last transform is a :class:`~autopilot.transform.transforms.Condition`
                and a trigger is returned to sender only when the return value of the transformation changes, or
                * "stream", where each result of the transformation is returned to sender

            return_id:
            stage_block:
            value_subset (str): Optional - subset a value from from a dict/list sent to :meth:`.l_process`
            **kwargs:
        """
        assert operation in ('trigger', 'stream', 'debug')
        self.operation = operation
        self._last_result = None

        if return_key is None:
            self.return_key = self.operation.upper()
        else:
            self.return_key = return_key

        self.return_id = return_id
        self.stage_block = stage_block
        self.stages = cycle([self.noop])
        # self.input_q = LifoQueue()
        self.input_q = deque(maxlen=1)
        self.value_subset = value_subset

        self.logger = logging.getLogger('main')

        self.process_thread = threading.Thread(target=self._process,
                                               args=(transform, ))
        self.process_thread.daemon = True
        self.process_thread.start()

    def noop(self):
        # just fitting in with the task structure.
        self.stage_block.clear()
        return {}

    def _process(self, transform):

        self.transform = autopilot.transform.make_transform(transform)

        self.node = Net_Node(f"{prefs.NAME}_TRANSFORMER",
                             upstream=prefs.NAME,
                             port=prefs.MSGPORT,
                             listens={'CONTINUOUS': self.l_process},
                             instance=False)

        self.node.send(self.return_id, 'STATE', value='READY')

        while True:
            try:
                # value = self.input_q.get_nowait()
                value = self.input_q.popleft()
            # except Empty:
            except IndexError:
                sleep(0.001)
                continue
            result = self.transform.process(value)

            self.node.logger.debug(f'Processed frame, result: {result}')

            if self.operation == "trigger":
                if result != self._last_result:
                    self.node.send(self.return_id, self.return_key, result)
                    self._last_result = result

            elif self.operation == 'stream':
                # FIXME: Another key that's not TRIGGER
                self.node.send(self.return_id, self.return_key, result)

            elif self.operation == 'debug':
                pass

    def l_process(self, value):
        # get array out of value

        # FIXME hack for dlc
        self.node.logger.debug('Received and queued processing!')
        # self.input_q.put_nowait(value['MAIN'])
        if self.value_subset:
            value = value[self.value_subset]
        self.input_q.append(value)