Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion mouse2afc/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from mouse2afc.utils import controlled_random
from mouse2afc.utils import isnan

import matplotlib.pyplot as plt
from mouse2afc.task_parameters import TaskParameters

logger = logging.getLogger(__name__)

NUM_OF_TRIALS = 800 #This can be changed. 800 is arbitrary
Expand Down Expand Up @@ -97,6 +100,38 @@ def __init__(self, task_parameters, timer, raw_data):
self.trials = Trials(task_parameters)
self.DVs_already_generated = 0


#plot
self.mot1 = 0
self.mot2 = 0
self.reward = 0
self.trial = 0
self.total = 0
self.trial_duration = 0
self.time_start = 0
self.time_end = 0
self.tot_time = 0
self.time_count = []
self.rew_count = []
self.trial_count = []

self.fig, self.ax = plt.subplots()
self.line, = self.ax.plot([], [], label="Water",marker='o', linestyle='-', color='b')
self.ax.set_xlabel("Trials")
self.ax.set_ylabel("Water Amount")
self.ax.set_title("Trials vs. Amount of Water Delivered")
self.ax.legend()

self.fig_2, self.ax_2 = plt.subplots()
self.line_2, = self.ax_2.plot([], [], label="Time",marker='o', linestyle='-', color='b')
self.ax_2.set_xlabel("Trials")
self.ax_2.set_ylabel("Amount of time")
self.ax_2.set_title("Trials vs. Amount of Time")
self.ax_2.legend()

plt.ion()
plt.show()

def assign_future_trials(self,start_from,num_trials_to_generate):
"Assigns left_rewarded as true or false for future trials "
is_left_rewarded = controlled_random((1 -self.task_parameters.left_bias),
Expand Down Expand Up @@ -164,6 +199,9 @@ def generate_next_trial(self,trial_num):
elif self.task_parameters.primary_experiment_type == \
ExperimentType.no_stimulus:
DV = 0
elif self.task_parameters.primary_experiment_type == \
ExperimentType.no_light:
DV = calc_light_intensity(self, trial_num)
else:
error('Unexpected Experiment Type')
self.trials.DV[trial_num] = DV
Expand Down Expand Up @@ -681,6 +719,10 @@ def update(self, i_trial):

self.timer.custom_finalize_update[i_trial] = time.time()

self.trial_duration = self.timer.custom_finalize_update[i_trial] - self.timer.custom_initialize[i_trial]
self.time_count.append(self.trial_duration)
print(f'TIME = {self.time_count}')

# Update RDK GUI #TODO:Figure out where this goes
self.task_parameters.omega_table.columns.rdk = [
(value - 50) * 2
Expand Down Expand Up @@ -726,7 +768,43 @@ def update(self, i_trial):
# GUI sync doesn't complain
self.task_parameters.is_catch = iff(
self.trials.catch_trial[i_trial + 1], 'true', 'false')

reward_amount = self.task_parameters.get('reward_amount', 'default_value')
prestim_reward_amount = self.task_parameters.get('pre_stim_delay_cntr_reward', 'default_value')
center_reward_amount = self.task_parameters.get('center_port_rew_amount', 'default_value')
if 'Reward' in states_visited_this_trial_names:
self.reward += (1 * reward_amount)
if 'PreStimReward' in states_visited_this_trial_names:
self.mot1 += (1 * prestim_reward_amount)
if 'CenterPortRewardDelivery' in states_visited_this_trial_names:
self.mot2 += (1 * center_reward_amount)
if 'ITI_Signal' in states_visited_this_trial_names:
self.trial += 1
self.tot_time = self.time_end - self.time_start
self.total = self.reward + self.mot1 + self.mot2
self.rew_count.append(self.total)
self.trial_count.append(self.trial)
print(f'TRI = {self.trial_count}')
print(f'REW = {self.rew_count}')

def plot_water(self):
x = self.trial_count
y = self.rew_count
self.line.set_xdata(x)
self.line.set_ydata(y)
self.ax.relim()
self.ax.autoscale_view()
self.fig.canvas.draw()
plt.pause(0.01)

def plot_time(self):
x2 = self.trial_count
y2 = self.time_count
self.line_2.set_xdata(x2)
self.line_2.set_ydata(y2)
self.ax_2.relim()
self.ax_2.autoscale_view()
self.fig_2.canvas.draw()
plt.pause(0.01)

class TimerData:
"Initialize class variables"
Expand Down
1 change: 1 addition & 0 deletions mouse2afc/definitions/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ class ExperimentType(SpecialEnum):
grating_orientation = 3
random_dots = 4
no_stimulus = 5
no_light = 6
13 changes: 12 additions & 1 deletion mouse2afc/mouse2afc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import time

from mouse2afc.data import Data
from mouse2afc.data import Data,CustomData
from mouse2afc.state_matrix import StateMatrix
from mouse2afc.task_parameters import TaskParameters

import matplotlib.pyplot as plt


logger = logging.getLogger(__name__)

Expand All @@ -24,6 +26,7 @@ def error(message):
class Mouse2AFC:
def __init__(self, bpod, config_file=None):
self._bpod = bpod
logger.debug('Initializing TaskParameters')
self._task_parameters = TaskParameters(
file_=config_file).task_parameters
self._data = Data(self._bpod.session, self._task_parameters)
Expand All @@ -46,6 +49,9 @@ def run(self):
self._data.custom.assign_future_trials(START_FROM,NUM_TRIALS_TO_GENERATE)
self._data.custom.generate_next_trial(i_trial)
self._bpod.softcode_handler_function = self.my_softcode_handler
plt.ion()
self._data.custom.plot_water()
self._data.custom.plot_time()
while True:
logger.error('Before StateMatrix()')
sma = StateMatrix(
Expand All @@ -56,4 +62,9 @@ def run(self):
if not self._bpod.run_state_machine(sma):
break
self._data.custom.update(i_trial)
self._data.custom.plot_water()
self._data.custom.plot_time()
i_trial += 1
plt.ioff()
plt.show()

37 changes: 32 additions & 5 deletions mouse2afc/state_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,24 @@ def single_experiment_stimulus(self,task_parameters,data,i_trial,experiment_leve
elif experiment_level == ExperimentType.no_stimulus:
_deliver_stimulus = []
_cont_deliver_stimulus = []
_stop_stimulus = []
_stop_stimulus = []
elif experiment_level == ExperimentType.no_light:
left_pwm_stim = round(
data.custom.trials.light_intensity_left[i_trial] * self.left_pwm / 100)
right_pwm_stim = round(
data.custom.trials.light_intensity_right[
i_trial] * self.right_pwm / 100)
_deliver_stimulus = [
(pwm_str(self.left_port), left_pwm_stim),
(pwm_str(self.right_port), right_pwm_stim)
]
if task_parameters.secondary_experiment_type == ExperimentType.no_light:
_deliver_stimulus = [
(pwm_str(6), left_pwm_stim),
(pwm_str(8), right_pwm_stim)
]
_cont_deliver_stimulus = _deliver_stimulus
_stop_stimulus = []
else:
error('Unexpected Experiment Type')

Expand Down Expand Up @@ -235,6 +252,11 @@ def __init__(self, bpod, task_parameters, data, i_trial):
deliver_stimulus = list(itertools.chain.from_iterable(stimuli[0]))
cont_deliver_stimulus = list(itertools.chain.from_iterable(stimuli[1]))
stop_stimulus = list(itertools.chain.from_iterable(stimuli[2]))

center_stim =[(pwm_str(center_port), center_pwm)]
if task_parameters.secondary_experiment_type == ExperimentType.no_light:
center_stim = [(pwm_str(7), center_pwm)] + [(pwm_str(center_port), center_pwm)]


if task_parameters.stim_after_poke_out == StimAfterPokeOut.not_used:
wait_for_decision_stim = stop_stimulus
Expand Down Expand Up @@ -294,8 +316,13 @@ def __init__(self, bpod, task_parameters, data, i_trial):
# GUI option Reward After Min Sampling
# If center - reward is enabled, then a reward is given once min_sample
# is over and no further sampling is given.
if task_parameters.primary_experiment_type == ExperimentType.no_light:
reward_valve = []
else:
reward_valve = [('Valve', center_valve)]

reward_center_port = iff(task_parameters.reward_after_min_sampling,
[('Valve', center_valve)] + stop_stimulus,
reward_valve + stop_stimulus,
cont_deliver_stimulus)
timer_cprd = iff(
task_parameters.reward_after_min_sampling, center_valve_time,
Expand Down Expand Up @@ -402,15 +429,15 @@ def __init__(self, bpod, task_parameters, data, i_trial):
state_timer=0,
state_change_conditions={
center_port_in: str(MatrixState.PreStimReward)},
output_actions=[(pwm_str(center_port), center_pwm)])
output_actions=center_stim)
self.add_state(state_name=str(MatrixState.PreStimReward),
state_timer=iff(task_parameters.pre_stim_delay_cntr_reward,
get_valve_times(task_parameters.pre_stim_delay_cntr_reward,
center_port), 0.01),
state_change_conditions={
Bpod.Events.Tup:str(MatrixState.TriggerWaitForStimulus)},
output_actions=iff(task_parameters.pre_stim_delay_cntr_reward,
[('Valve', center_valve)], []))
[reward_valve], []))
# The next method is useful to close the 2 - photon shutter. It is
# enabled by setting Optogenetics StartState to this state and end
# state to ITI.
Expand Down Expand Up @@ -523,7 +550,7 @@ def __init__(self, bpod, task_parameters, data, i_trial):
state_timer=valve_time,
state_change_conditions={
Bpod.Events.Tup: str(MatrixState.WaitRewardOut)},
output_actions=(wait_feedback_stim + [('Valve', valve_code)]))
output_actions=(wait_feedback_stim + reward_valve))
self.add_state(state_name=str(MatrixState.WaitRewardOut),
state_timer=1,
state_change_conditions={
Expand Down