def _decide(self):
        roi_id = self._tracker._roi.idx
        now = self._tracker.last_time_point
        if now - self._last_stimulus_time < self._refractory_period * 1000:
            return HasInteractedVariable(False), {}

        try:
            channel = self._roi_to_channel[roi_id]
        except KeyError:
            return HasInteractedVariable(False), {}

        positions = self._tracker.positions

        if len(positions) < 2:
            return HasInteractedVariable(False), {}

        if len(positions[-1]) != 1:
            raise Exception("This stimulator can only work with a single animal per ROI")

        roi_w = float(self._tracker._roi.longest_axis)
        x_t_zero = positions[-1][0]["x"] / roi_w - 0.5
        x_t_minus_one = positions[-2][0]["x"] / roi_w - 0.5

        # if roi_id == 12:
        #     print (roi_id, channel, roi_w, positions[-1][0]["x"], positions[-2][0]["x"], x_t_zero, x_t_minus_one)
        if (x_t_zero > 0) ^ (x_t_minus_one >0): # this is a change of sign

            if random.uniform(0,1) < self._p:
                self._last_stimulus_time = now
                return HasInteractedVariable(True), {"channel": channel}

        return HasInteractedVariable(False), {"channel": channel}
    def _decide(self):

        has_moved = self._has_moved()

        t = self._tracker.times
        if  has_moved:# or xor_diff > self._xor_speed_threshold :
            self._last_active = t[-1]
            return HasInteractedVariable(False), {}
        return HasInteractedVariable(True), {}
 def _decide(self):
     if len(self._times) > 0:
         if time.time() > self._times[-1]:
             current = self._times.pop()
             return HasInteractedVariable(True), {
                 "pulses_in_seconds": self._vibration_time,
                 "current_time": current
             }
     return HasInteractedVariable(False), {}
    def _decide(self):
        roi_id = self._tracker._roi.idx
        try:
            channel = self._roi_to_channel[roi_id]
        except KeyError:
            return HasInteractedVariable(False), {}
        now = self._tracker.last_time_point + roi_id * 100
        if now - self._t0 > self._interval:
            dic = {"channel": channel}
            dic["duration"] = self._pulse_duration
            self._t0 = now
            return HasInteractedVariable(True), dic

        return HasInteractedVariable(False), {}
Esempio n. 5
0
 def _decide(self):
     roi_id = self._tracker._roi.idx
     now = self._tracker.last_time_point
     # every 100 times:
     interact = random.uniform(0.0, 1.0) < 0.01
     return HasInteractedVariable(interact), {
         "channel": roi_id,
         "time": now
     }
    def _decide(self):
        roi_id = self._tracker._roi.idx
        try:
            channel = self._roi_to_channel[roi_id]
        except KeyError:
            return HasInteractedVariable(False), {}

        if self._scheduler.check_time_range() is False:
            return HasInteractedVariable(False), {}

        has_changed_side = self._has_changed_side()

        if has_changed_side == 0:
            return HasInteractedVariable(False), {}
        pos = self._side_to_pos[has_changed_side]
        return HasInteractedVariable(pos), {
            "channel": channel,
            "pos": self._side_to_pos[has_changed_side]
        }
Esempio n. 7
0
    def _decide(self):
        roi_id = self._tracker._roi.idx
        now = self._tracker.last_time_point

        try:
            channel = self._roi_to_channel[roi_id]
        except KeyError:
            return HasInteractedVariable(False), {}

        has_moved = self._has_moved()

        if self._t0 is None:
            self._t0 = now

        if not has_moved:
            if float(now - self._t0) > self._inactivity_time_threshold_ms:
                self._t0 = None
                return HasInteractedVariable(True), {"channel": channel}
        else:
            self._t0 = now
        return HasInteractedVariable(False), {}
    def _decide(self):
        has_changed = self._has_changed_side()

        if has_changed:
            return HasInteractedVariable(True), {}
        return HasInteractedVariable(False), {}
Esempio n. 9
0
    def run(self, result_writer = None, drawer = None):
        """
        Runs the monitor indefinitely.

        :param result_writer: A result writer used to control how data are saved. `None` means no results will be saved.
        :type result_writer: :class:`~ethoscope.utils.io.ResultWriter`
        :param drawer: A drawer to plot the data on frames, display frames and/or save videos. `None` means none of the aforementioned actions will performed.
        :type drawer: :class:`~ethoscope.drawers.drawers.BaseDrawer`
        """

        try:
            logging.info("Monitor starting a run")
            self._is_running = True

            for i, (t, frame) in enumerate(self._camera):

                #logging.info("Monitor: frame: %d, time: %d" % (i, t))
                if self._force_stop:
                    logging.info("Monitor object stopped from external request")
                    break

                self._last_frame_idx = i
                self._last_time_stamp = t
                self._frame_buffer = frame
                #logStr = "Monitor: frame: %d, time: %d" % (i, t)
                empty_cnt = 0
                for j, track_u in enumerate(self._unit_trackers):
                    data_rows = track_u.track(t, frame)
                    if len(data_rows) == 0:
                        self._last_positions[track_u.roi.idx] = []
                        empty_cnt += 1
                        # L. Zi do not skip trackers not returning a detection
                        # but fill in a data row with zero values
                        #continue
                        data_rows = [DataPoint([XPosVariable(0),
                                                YPosVariable(0),
                                                WidthVariable(0),
                                                HeightVariable(0),
                                                PhiVariable(0),
                                                XYDistance(0),
                                                IsInferredVariable(0),
                                                HasInteractedVariable(0)]) ]

                    abs_pos = track_u.get_last_positions(absolute=True)

                    # if abs_pos is not None:
                    self._last_positions[track_u.roi.idx] = abs_pos

                    if not result_writer is None:
                        #logging.info(logStr + ", Roi: %d, %s" % (track_u.roi.idx, data_rows))
                        result_writer.write(t, track_u.roi, data_rows)

                #logging.info(logStr + " empty data cnt: %d" % (empty_cnt))
                if result_writer is not None:
                    result_writer.flush(t, frame)

                if drawer is not None:
                    drawer.draw(frame, t, self._last_positions, self._unit_trackers)
                self._last_t = t

        except Exception as e:
            logging.error("Monitor closing with an exception: '%s'" % traceback.format_exc(e))
            raise e

        finally:
            self._is_running = False
            logging.info("Monitor closing")