def water_drop(open_time, ntrials=100, iti=1, bpod='bpod_instance'):
    if bpod == 'bpod_instance':
        print('Need a Bpod instance to run a protocol')
        return 0

    # Start state machine definition
    for i in range(ntrials):

        sma = StateMachine(bpod)

        # open and close valve
        sma.add_state(
            state_name='reward',
            state_timer=open_time,
            state_change_conditions={'Tup': 'iti'},
            output_actions=[('Valve1', 255)])

        sma.add_state(
            state_name='iti',
            state_timer=iti,
            state_change_conditions={'Tup': 'exit'},
            output_actions=[])

        # Send state machine description to Bpod device and run
        bpod.send_state_machine(sma)
        bpod.run_state_machine(sma)
def water_drop(open_time, ntrials=100, iti=1, bpod="bpod_instance"):
    if bpod == "bpod_instance":
        print("Need a Bpod instance to run a protocol")
        return 0

    # Start state machine definition
    for i in range(ntrials):

        sma = StateMachine(bpod)

        # open and close valve
        sma.add_state(
            state_name="reward",
            state_timer=open_time,
            state_change_conditions={"Tup": "iti"},
            output_actions=[("Valve1", 255)],
        )

        sma.add_state(
            state_name="iti",
            state_timer=iti,
            state_change_conditions={"Tup": "exit"},
            output_actions=[],
        )

        # Send state machine description to Bpod device and run
        bpod.send_state_machine(sma)
        if not bpod.run_state_machine(
                sma):  # Locks until state machine 'exit' is reached
            break
Exemple #3
0
        # If bias check: at the begining of the session, force the mouse to navigate all the lickports in sequence (two rounds)
        if start_with_bias_check and blocki < bias_check_blocknum:
            trialnum_now = 1  # Override block length = 1
            auto_train_min_rewarded_trial_num = bias_check_auto_train_min_rewarded_trial_num  # At least get XX reward (XX = 1);
            # Make sure the animal indeed chooses each port sequentially during bias check
            reward_L_accumulated = False  # We don't need baiting during bias check
            reward_R_accumulated = False
            reward_M_accumulated = False
            # Regular timing
            iti_now = 2
            baselinetime_now = 1
        # (Else: If no bias check or bias check has been done --> Normal trial begins)

        # ------- Start of a trial ---------
        sma = StateMachine(my_bpod)

        # ---- 1. Delay period ----
        if variables['early_lick_punishment']:
            # Lick before timeup of the delay timer ('baselinetime_now') --> Reset the delay timer
            sma.add_state(state_name='Start',
                          state_timer=baselinetime_now,
                          state_change_conditions={
                              variables['WaterPort_L_ch_in']: 'BackToBaseline',
                              variables['WaterPort_R_ch_in']: 'BackToBaseline',
                              variables['WaterPort_M_ch_in']: 'BackToBaseline',
                              EventName.Tup: 'GoCue'
                          },
                          output_actions=[])

            # Add timeout (during which more early licks will be ignored), then restart the trial
Exemple #4
0
def trial():
    for trial in range(trials):
        # send ttl to bnc1
        sma = StateMachine(bpod)
        # sart state blink green two times
        sma.add_state(
            state_name="start1",
            state_timer=1,
            state_change_conditions={"Tup": "start2"},
            output_actions=[("Serial1", 1), ("SoftCode", 2)],
        )
        sma.add_state(
            state_name="start2",
            state_timer=1,
            state_change_conditions={"Tup": "start3"},
            output_actions=[("BNC1", 1)],
        )
        sma.add_state(
            state_name="start3",
            state_timer=1,
            state_change_conditions={"Tup": "reset_rotary_encoder2"},
            output_actions=[],
        )
        # detect rotary encoder movement left -> blink red light
        sma.add_state(
            state_name="reset_rotary_encoder2",
            state_timer=0,
            state_change_conditions={"Tup": "detect"},
            output_actions=[("Serial1", 1)],
        )
        sma.add_state(
            state_name="detect",
            state_timer=30,
            state_change_conditions={
                movement_left: "blink_red_1x",
                movement_right: "blink_red_1x",
                "Tup": "start1"
            },
            output_actions=[("BNC1", 1)],
        )
        # blink red
        sma.add_state(
            state_name="blink_red_1x",
            state_timer=1,
            state_change_conditions={"Tup": "reset_rotary_encoder"},
            output_actions=[("BNC2", 1), ("Serial1", 2), ("SoftCode", 1)],
        )
        # reset rotary encoder to positin 0
        sma.add_state(
            state_name="reset_rotary_encoder",
            state_timer=0,
            state_change_conditions={"Tup": "exit"},
            output_actions=[("Serial1", 1)],
        )

        bpod.send_state_machine(sma)
        # Run state machine
        bpod.run_state_machine(sma)
    message1.stop()
Exemple #5
0
from pybpodapi.bpod import Bpod
from pybpodapi.state_machine import StateMachine

ntrials = 1
valve_on_time = 30
iti = 0.5

bpod = Bpod()

for i in range(ntrials):
    print("Starting trial: ", i + 1)
    sma = StateMachine(bpod)
    sma.add_state(
        state_name="init",
        state_timer=0,
        state_change_conditions={"Tup": "reward"},
        output_actions=[],
    )

    sma.add_state(
        state_name="reward",
        state_timer=valve_on_time,
        state_change_conditions={"Tup": "iti"},
        output_actions=[("Valve1", 255)],
    )

    sma.add_state(
        state_name="iti",
        state_timer=iti,
        state_change_conditions={"Tup": "exit"},
        output_actions=[],
water_ch = 3

valvetime = 0.1
dropnum = 100
iti = 0.3


sound_ch = OutputChannel.PWM5
############################################################

my_bpod = Bpod()

for i in range(dropnum):  # Main loop
    print('Trial: ', i + 1)
    
    sma = StateMachine(my_bpod)
   
    sma.add_state(
    		state_name='Open',
    		state_timer=valvetime,
    		state_change_conditions={EventName.Tup: 'ITI'},
    		output_actions = [('Valve', water_ch)])
    
    if i == 0 or i == dropnum - 1:  # Sound the start and end
        sma.add_state(
        		state_name='ITI',
        		state_timer=iti,
        		state_change_conditions={EventName.Tup: 'exit'},
        		output_actions = [(sound_ch, 255)])
    else:
        sma.add_state(
A protocol to calibrate the water system. In addition, to contro the lights.
"""

from pybpodapi.bpod import Bpod
from pybpodapi.state_machine import StateMachine
from pybpodapi.bpod.hardware.events import EventName
from pybpodapi.bpod.hardware.output_channels import OutputChannel
import timeit

my_bpod = Bpod()

# ----> Start the task
for i in range(2):  # Main loop
    print('Trial: ', i + 1)

    sma = StateMachine(my_bpod)

    sma.add_state(state_name='GetWater_P1',
                  state_timer=1,
                  state_change_conditions={EventName.Tup: 'GetWater_P2'},
                  output_actions=[('Valve', 1), (OutputChannel.PWM1, 255)])
    sma.add_state(state_name='GetWater_P2',
                  state_timer=1,
                  state_change_conditions={EventName.Tup: 'GetWater_P3'},
                  output_actions=[('Valve', 2), (OutputChannel.PWM2, 255)])
    sma.add_state(state_name='GetWater_P3',
                  state_timer=1,
                  state_change_conditions={EventName.Tup: 'End'},
                  output_actions=[('Valve', 3), (OutputChannel.PWM3, 255)])
    sma.add_state(state_name='End',
                  state_timer=1,
my_bpod = Bpod()

# ----> Start the task
for valvetime in valvetimes:

    ValveOpenTime_L = valvetime

    ValveOpenTime_R = valvetime
    ValveOpenTime_M = valvetime

    print(ValveOpenTime_R)
    for i in range(Dropnum):  # Main loop
        print('Trial: ', i + 1)

        sma = StateMachine(my_bpod)
        sma.add_state(state_name='Wait',
                      state_timer=100,
                      state_change_conditions={
                          EventName.Port1In: 'Open Left',
                          EventName.Port2In: 'Open Left',
                          EventName.Port7In: 'Open Left',
                          EventName.Port8In: 'Open Left',
                          EventName.Tup: 'exit'
                      },
                      output_actions=[])
        sma.add_state(state_name='Open Left',
                      state_timer=ValveOpenTime_L,
                      state_change_conditions={EventName.Tup: 'wait1'},
                      output_actions=[('Valve', 1)])
        sma.add_state(state_name='wait1',
Exemple #9
0
from pybpodapi.bpod import Bpod
from pybpodapi.state_machine import StateMachine

bpod = Bpod()

trials = 10

for trial in range(trials):
    # send ttl to bnc1
    sma = StateMachine(bpod)
    sma.add_state(state_name="signal_bnc1",
                  state_timer=2,
                  state_change_conditions={"Tup": "signal_bnc2"},
                  output_actions=[("BNC1", 1)])
    sma.add_state(
        state_name="signal_bnc2",
        state_timer=2,
        state_change_conditions={"Tup": "exit"},
        output_actions=[("BNC1", 0)],
    )
    # Send state machine description to Bpod device
    bpod.send_state_machine(sma)

    # Run state machine
    if not bpod.run_state_machine(
            sma):  # Locks until state machine 'exit' is reached
        break
    print("test ob das geprintet wird\n")
    print("Current trial info: {0}".format(bpod.session.current_trial))

bpod.close()
Exemple #10
0
from pybpodapi.bpod import Bpod
from pybpodapi.state_machine import StateMachine

# create bpod object all necessary settings are imported from GUI
bpod = Bpod()

trials = 10

# main loop for 10 trials
for trial in range(trials):

    # create state machine object
    sma = StateMachine(bpod)

    # initial state, all states follow the same notation
    sma.add_state(
        # sate nemae can be freely chosen but must be unique
        state_name="start_state",
        # time how long the state is active can be 0 or seconds or fractions of seconds
        # if 0 state will only change if state change condition is met
        state_timer=1,
        # state change condition for switching to respective next state
        # hast to be a dictionary with keys from allowed conditions and values
        state_change_conditions={"Tup": "next_state"},
        # output actions can be none or list of touples with allowed names and values
        output_actions=[("BNC1", 1), ("BNC2", 1)],
    )

    sma.add_state(
        state_name="next_state",
        state_timer=1,
Exemple #11
0
from pybpodapi.bpod import Bpod
from pybpodapi.state_machine import StateMachine

# create bpod object all necessary settings are imported from GUI
bpod = Bpod()

trials = 10

# main loop for 10 trials
for trial in range(trials):

    # create state machine object
    sma = StateMachine(bpod)
    # test output signal 00
    sma.add_state(
        state_name="start",
        state_timer=1,
        state_change_conditions={"Tup": "state_02"},
        output_actions=[("BNC1", 1), ("BNC2", 1)],
    )

    # start testing for recifing signals
    sma.add_state(
        state_name="end_state",
        state_timer=10,
        # last state always next state to exit
        state_change_conditions=[{
            "BNC1High": "exit"
        }, {
            "BNC2High": "exit"
        }],
Exemple #12
0
from ../modules/rotaryencoder import BpodRotaryEncoder
from ../modules/helperfunctions import find_rotary_com_port, tryer

# Main test for Mpod state machine test 
bpod=Bpod()

# state machine configs
for trial in range(trials):

    com_port = find_rotary_com_port()
    #com_port = '/dev/cu.usbmodem65305701' #TODO:
    rotary_encoder_module = BpodRotaryEncoder(com_port, settings_obj, bpod)
    rotary_encoder_module.load_message()
    rotary_encoder_module.configure()

    sma = StateMachine(bpod)
    # start state to define block of trial
    sma.add_state(
        state_name="start",
        state_timer=0,
        state_change_conditions={"Tup": "reset_rotary_encoder_wheel_stopping_check"},
        output_actions=[]
    )

    # reset rotary encoder bevore checking for wheel not stoping
    sma.add_state(
        state_name="reset_rotary_encoder_wheel_stopping_check",
        state_timer=0,
        state_change_conditions={"Tup": "open_loop"},
        output_actions=[("Serial1", settings_obj.RESET_ROTARY_ENCODER)], # activate white light while waiting
    )
Exemple #13
0
from pybpodapi.bpod import Bpod
from pybpodapi.state_machine import StateMachine

# create bpod object all necessary settings are imported from GUI
bpod = Bpod()

trials = 10

# main loop for 10 trials
for trial in range(trials):

    # create state machine object
    sma = StateMachine(bpod)
    # test output signal 00
    sma.add_state(
        state_name="start",
        state_timer=1,
        state_change_conditions={"Tup": "state_02"},
        output_actions=[("BNC1", 1), ("BNC2", 1)],
    )

    # test output signal 10
    sma.add_state(
        state_name="state_02",
        state_timer=1,
        state_change_conditions={"Tup": "state_03"},
        output_actions=[("BNC1", 1), ("BNC2", 0)],
    )

    # test output signal 01
    sma.add_state(
Exemple #14
0
    # create main state machine aka trial loop ====================================================================
    # state machine configs
    for trial in range(settings_obj.trial_number):
        probability_obj.get_random_side()
        # get random punish time
        punish_time = round(
            random.uniform(
                float(settings_obj.time_dict['time_range_noreward_punish'][0]),
                float(
                    settings_obj.time_dict['time_range_noreward_punish'][1])),
            2)
        times_punish_li.append(punish_time)
        # construct states

        sma = StateMachine(bpod)
        # start state to define block of trial
        sma.add_state(
            state_name="start",
            state_timer=settings_obj.time_dict["time_start"],
            state_change_conditions={
                "Tup": "reset_rotary_encoder_wheel_stopping_check"
            },
            output_actions=[("SoftCode", settings_obj.SC_START_LOGGING)],
        )
        # reset rotary encoder bevore checking for wheel not stoping
        sma.add_state(
            state_name="reset_rotary_encoder_wheel_stopping_check",
            state_timer=0,
            state_change_conditions={"Tup": "wheel_stopping_check"},
            output_actions=[("Serial1", settings_obj.RESET_ROTARY_ENCODER)
Exemple #15
0
     'Trialnumber_in_block'] or rewarded_trial_num < variables[
         'auto_train_min_rewarded_trial_num']:
 triali += 1
 reward_L = np.random.uniform(0., 1.) < p_L
 reward_R = np.random.uniform(0., 1.) < p_R
 iti_now = np.random.normal(variables['iti_base'] +
                            ignore_trial_num_in_a_row,
                            variables['iti_sd'])  #
 #iti_now = 0
 if iti_now < variables['iti_min']:
     iti_now = variables['iti_min']
 baselinetime_now = np.random.normal(variables['baseline_time'],
                                     variables['baseline_time_sd'])
 if baselinetime_now < variables['baseline_time_min']:
     baselinetime_now = variables['baseline_time_min']
 sma = StateMachine(my_bpod)
 if variables['early_lick_punishment']:
     sma.add_state(state_name='Start',
                   state_timer=baselinetime_now,
                   state_change_conditions={
                       variables['WaterPort_L_ch_in']: 'BackToBaseline',
                       variables['WaterPort_R_ch_in']: 'BackToBaseline',
                       EventName.Tup: 'GoCue'
                   },
                   output_actions=[])
 else:
     sma.add_state(state_name='Start',
                   state_timer=baselinetime_now,
                   state_change_conditions={EventName.Tup: 'GoCue'},
                   output_actions=[])
 sma.add_state(state_name='BackToBaseline',
# =============================================================================
# CONNECT TO BPOD
# =============================================================================
bpod = Bpod()

# =============================================================================
# TRIAL PARAMETERS AND STATE MACHINE
# =============================================================================

for i in range(ntrials):
    print('Starting trial: ', i + 1)
    # =============================================================================
    #     Start state machine definition
    # =============================================================================
    sma = StateMachine(bpod)
    sma.add_state(state_name='init',
                  state_timer=0,
                  state_change_conditions={'Tup': 'reward'},
                  output_actions=[])

    sma.add_state(state_name='reward',
                  state_timer=valve_on_time,
                  state_change_conditions={'Tup': 'iti'},
                  output_actions=[('Valve1', 255)])

    sma.add_state(state_name='iti',
                  state_timer=iti,
                  state_change_conditions={'Tup': 'exit'},
                  output_actions=[])
#arcom = ArCOM().open("COM6", 115200)
#arcom.write_array(ArduinoTypes.get_uint8_array([ord("P"), 1, 0]))
#arcom.close()

# trial loop

trials = 10

#LoadSerialMessages('WavePlayer1', ['P', 15, 1])

# main loop for 10 trials
for trial in range(trials):

    # create state machine object
    sma = StateMachine(bpod)

    # initial state, all states follow the same notation
    sma.add_state(
        state_name="start_state",
        state_timer=2,
        state_change_conditions={"Tup": "next_state"},
        output_actions=[('Serial4', 1)],
    )

    # blink state
    sma.add_state(
        state_name="next_state",
        state_timer=2,
        state_change_conditions={"Tup": "end_state"},
        output_actions=[("BNC1", 1), ("BNC2", 1)],
Exemple #18
0
from pybpodapi.bpod import Bpod
from pybpodapi.state_machine import StateMachine

bpod = Bpod()

trials = 10
iti = 2

for trial in range(trials):

    # send ttl to bnc1
    sma = StateMachine(bpod)

    # initial state
    sma.add_state(
        state_name="start",
        state_timer=5,
        state_change_conditions={"Tup": "reward"},
        output_actions=[("BNC1", 1)],
    )

    # open valve1 for 20 seconds
    sma.add_state(
        state_name="reward",
        # output action will be performed for whole time state is active
        state_timer=20,
        state_change_conditions={"Tup": "exit"},
        # output action for valve open = 255
        # notation for valve alsways Valve + numer of port connected to
        output_actions=[("Valve1", 255)],
    )
        # If bias check: at the begining of the session, force the mouse to navigate all the lickports in sequence (two rounds)
        if start_with_bias_check and blocki < bias_check_blocknum:
            trialnum_now = 1  # Override block length = 1
            auto_train_min_rewarded_trial_num = bias_check_auto_train_min_rewarded_trial_num  # At least get XX reward (XX = 1);
            # Make sure the animal indeed chooses each port sequentially during bias check
            reward_L_accumulated = False  # We don't need baiting during bias check
            reward_R_accumulated = False
            reward_M_accumulated = False
            # Regular timing
            iti_now = 2
            baselinetime_now = 1
        # (Else: If no bias check or bias check has been done --> Normal trial begins)

        # ------- Start of a trial ---------
        sma = StateMachine(my_bpod)

        # ---- 1. Delay period ----
        if variables['early_lick_punishment']:
            # Lick before timeup of the delay timer ('baselinetime_now') --> Reset the delay timer
            sma.add_state(state_name='Start',
                          state_timer=baselinetime_now,
                          state_change_conditions={
                              variables['WaterPort_L_ch_in']: 'BackToBaseline',
                              variables['WaterPort_R_ch_in']: 'BackToBaseline',
                              variables['WaterPort_M_ch_in']: 'BackToBaseline',
                              EventName.Tup: 'GoCue'
                          },
                          output_actions=[])

            # Add 2 second timeout (during which more early licks will be ignored), then restart the trial