def example_protocol_run(condition_q: mp.Queue):
    current_trial = None
    # dmod_device = DigitalModDevice('Dev1/PFI0')
    while True:
        if condition_q.full():
            current_trial = condition_q.get()
        if current_trial is not None:
            show_visual_stim_img(type=current_trial, name="DlStream")
            # dmod_device.toggle()
        else:
            show_visual_stim_img(name="DlStream")
            # dmod_device.turn_off()

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
def example_protocol_run(condition_q: mp.Queue):
    current_trial = None
    # dmod_device = DigitalModDevice('Dev1/PFI0')
    # led_machine = DigitalArduinoDevice("COM5")
    while True:
        # if no protocol is selected, running default picture (background)
        if condition_q.full():
            current_trial = condition_q.get()
        if current_trial is not None:
            show_visual_stim_img(type=current_trial, name="DlStream")
            # dmod_device.toggle()
            # led_machine.turn_on()
        else:
            show_visual_stim_img(name="DlStream")
            # dmod_device.turn_off()
            # led_machine.turn_off()

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
Beispiel #3
0
 def check_skeleton(self, frame, skeleton):
     # if not all(self._completion_counter.values()):
     # if not all stages are completed
     for trial in self._trials:
         # check for all trials if condition is met
         result, response = self._trials[trial]["trigger"](
             skeleton=skeleton)
         if self._event is None:
             # if there is no current trial as event already
             if result:
                 # if condition is met set current trial as event
                 self._event = trial
                 self._count[trial] += 1
         else:
             # if there is a current trial set as event
             if not result and self._event == trial:
                 # if the condition for current trial is not met, reset event
                 self._event = None
             elif result and self._event != trial:
                 # if condition is met but event is not current trial(but last trial), set current trial as event
                 self._event = trial
                 self._count[trial] += 1
         # plot_triggers_response(frame, response)
     print(self._event)
     print("green: {}".format(self._count["Greenbar_whiteback"]))
     print("blue: {}".format(self._count["Bluebar_whiteback"]))
     if self._event is not None:
         # if there is a trial set as event, show stimulus
         print("I am not none!")
         show_visual_stim_img(type=self._event, name="inside")
     elif self._event is None:
         # if there is no trial set as event, show background
         show_visual_stim_img(name="inside")
     if all(trials >= EXP_COMPLETION for trials in self._count.values()):
         # if all trials reached number of repeats of completion criterion, set stage as completed and go higher
         # self._completion_counter[self._stage] = True
         # finish the experiment if stage is completed
         print("Stage " + str(self._stage) + " completed!")
         self.stop_experiment()
     self.check_exp_timer()
def classic_protocol_run(trial_q: mp.Queue, condition_q: mp.Queue,
                         success_q: mp.Queue, trials: dict):
    """
    The function to use in ProtocolProcess class
    Designed to be run continuously alongside the main loop
    Three parameters are three mp.Queue classes, each passes corresponding values
    :param trial_q: the protocol name (inwards)
    :param condition_q: the condition (inwards)
    :param success_q: the result of each protocol (outwards)
    :param trials: dict of possible trials
    """
    # setting up different trials
    current_trial = None
    # starting the main loop without any protocol running
    while True:
        # if no protocol is selected, running default picture (background)
        if trial_q.empty() and current_trial is None:
            # print('No protocol running')
            show_visual_stim_img(name="inside")
        # if some protocol is passed, set up protocol timers and variables
        elif trial_q.full():
            current_trial = trial_q.get()
            finished_trial = False
            delivery = False
            reward_del = False
            # starting timers
            stimulus_timer = trials[current_trial]["stimulus_timer"]
            collection_timer = trials[current_trial]["collection_timer"]
            success_timer = trials[current_trial]["success_timer"]
            delivery_timer = Timer(3.5)
            shock_timer = Timer(3.5)
            # withdraw_timer = Timer(3.5)
            print("Starting protocol {}".format(current_trial))
            stimulus_timer.start()
            success_timer.start()
            condition_list = []
            collection_list = []
        # this branch is for already running protocol
        elif current_trial is not None:
            # checking for stimulus timer and outputting correct image
            if stimulus_timer.check_timer():
                # if stimulus timer is running, show stimulus
                show_visual_stim_img(current_trial, name="inside")
            else:
                # if the timer runs out, finish protocol and reset timer
                trials[current_trial]["stimulus_timer"].reset()
                show_visual_stim_img(name="inside")
            # checking if any condition was passed
            if condition_q.full():
                stimulus_condition = condition_q.get()
                # checking if timer for condition is running and condition=True
                if success_timer.check_timer():
                    condition_list.append(stimulus_condition)
                elif not success_timer.check_timer(
                ) and collection_timer.check_timer():
                    collection_list.append(stimulus_condition)

            # checking if the timer for condition has run out
            if not success_timer.check_timer() and not finished_trial:

                if not delivery:
                    if current_trial is not None:
                        print("Timer for condition ran out")
                        print_check = True
                        # check wether animal collected within success timer
                        success = trials[current_trial]["result_func"](
                            condition_list)
                        trials[current_trial]["success_timer"].reset()

                        print("Stimulation.")

                        if current_trial == "Bluebar_whiteback":
                            deliver_tone_shock()
                            print("Aversive")
                            shock_timer.start()
                        elif current_trial == "Greenbar_whiteback":
                            deliver_liqreward()
                            delivery_timer.start()
                            reward_del = True
                            print("Reward")
                        delivery = True
                        collection_timer.start()
                elif delivery:
                    # resetting the timer
                    if not collection_timer.check_timer():
                        finished_trial = True
                        # check whether animal collected at all
                        collect = any(collection_list)
                        if not collect and reward_del:
                            # if the animal didnt go to collect reward, withdraw reward again.
                            withdraw_liqreward()
                            # withdraw_timer.start()
                        trials[current_trial]["collection_timer"].reset()
                        current_trial = None
                        # put success in queue and finish trial
                        success_q.put(success)

            if (not delivery_timer.check_timer()
                    and delivery_timer.get_start_time() is not None):
                deliver_liqreward()
                delivery_timer.reset()
            if (not shock_timer.check_timer()
                    and shock_timer.get_start_time() is not None):
                deliver_tone_shock()
                shock_timer.reset()

            # if not withdraw_timer.check_timer() and withdraw_timer.get_start_time() is not None:
            #     withdraw_liqreward(False)
            #     withdraw_timer.reset()
            #     delivery = False

        # don't delete that
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
def classic_protocol_run_old(trial_q: mp.Queue, condition_q: mp.Queue,
                             success_q: mp.Queue, trials: dict):
    """
    The function to use in ProtocolProcess class
    Designed to be run continuously alongside the main loop
    Three parameters are three mp.Queue classes, each passes corresponding values
    :param trial_q: the protocol name (inwards)
    :param condition_q: the condition (inwards)
    :param success_q: the result of each protocol (outwards)
    :param trials: dict of possible trials
    """
    # setting up different trials
    current_trial = None
    # starting the main loop without any protocol running
    while True:
        # if no protocol is selected, running default picture (background)
        if trial_q.empty() and current_trial is None:
            # print('No protocol running')
            show_visual_stim_img(name="inside")
        # if some protocol is passed, set up protocol timers and variables
        elif trial_q.full():
            current_trial = trial_q.get()
            finished_trial = False
            # starting timers
            stimulus_timer = trials[current_trial]["stimulus_timer"]
            success_timer = trials[current_trial]["success_timer"]
            print("Starting protocol {}".format(current_trial))
            stimulus_timer.start()
            success_timer.start()
            condition_list = []
        # this branch is for already running protocol
        elif current_trial is not None:
            # checking for stimulus timer and outputting correct image
            if stimulus_timer.check_timer():
                # if stimulus timer is running, show stimulus
                show_visual_stim_img(current_trial, name="inside")
            else:
                # if the timer runs out, finish protocol and reset timer
                trials[current_trial]["stimulus_timer"].reset()
                current_trial = None

            # checking if any condition was passed
            if condition_q.full():
                stimulus_condition = condition_q.get()
                # checking if timer for condition is running and condition=True
                if success_timer.check_timer():
                    # print('That was a success!')
                    condition_list.append(stimulus_condition)
                # elif success_timer.check_timer() and not stimulus_condition:
                #     # print('That was not a success')
                #     condition_list.append(False)

            # checking if the timer for condition has run out
            if not success_timer.check_timer() and not finished_trial:
                if CTRL:
                    # start a random time interval
                    # TODO: working ctrl timer that does not set new time each frame...
                    ctrl_time = random.randint(0, INTERTRIAL_TIME + 1)
                    ctrl_timer = Timer(ctrl_time)
                    ctrl_timer.start()
                    print("Waiting for extra" + str(ctrl_time) + " sec")
                    if not ctrl_timer.check_timer():
                        # in ctrl just randomly decide between the two
                        print("Random choice between both stimuli")
                        if random.random() >= 0.5:
                            # very fast random choice between TRUE and FALSE
                            deliver_liqreward()
                            print("Delivered Reward")

                        else:
                            deliver_tone_shock()
                            print("Delivered Aversive")

                        ctrl_timer.reset()
                        finished_trial = True
                        # outputting the result, whatever it is
                        success = trials[current_trial]["result_func"](
                            condition_list)
                        success_q.put(success)
                        trials[current_trial]["success_timer"].reset()

                else:
                    if current_trial == "Bluebar_whiteback":
                        deliver_tone_shock()
                        print("Delivered Aversive")
                    elif current_trial == "Greenbar_whiteback":
                        if trials[current_trial]["random_reward"]:
                            if random.random() >= 0.5:
                                # very fast random choice between TRUE and FALSE
                                deliver_liqreward()
                                print("Delivered Reward")
                            else:
                                print("No Reward")
                        else:
                            deliver_liqreward()
                    # resetting the timer
                    print("Timer for condition run out")
                    finished_trial = True
                    # outputting the result, whatever it is
                    success = trials[current_trial]["result_func"](
                        condition_list)
                    success_q.put(success)
                    trials[current_trial]["success_timer"].reset()

        # don't delete that
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break