add refractory time

This commit is contained in:
a.ott 2020-05-27 09:12:28 +02:00
parent 539d8605bd
commit 4b4e5c6d14

View File

@ -24,7 +24,8 @@ class LifacNoiseModel(AbstractModel):
"a_zero": 2,
"noise_strength": 0.05,
"step_size": 0.00005,
"dend_tau": 0.001}
"dend_tau": 0.001,
"refractory_period": 0.001}
def __init__(self, params: dict = None):
super().__init__(params)
@ -58,9 +59,13 @@ class LifacNoiseModel(AbstractModel):
# rectified input:
stimulus_strength = self._calculate_input_voltage_step(input_voltage[i - 1],
fu.rectify(stimulus.value_at_time_in_s(time_point)))
v_next = self._calculate_voltage_step(current_v, stimulus_strength - current_a)
a_next = self._calculate_adaption_step(current_a)
if len(spiketimes) > 0 and time[i] - spiketimes[-1] < self.parameters["refractory_period"] + self.parameters["step_size"]/2:
v_next = self.parameters["v_base"]
if v_next > self.parameters["threshold"]:
v_next = self.parameters["v_base"]
spiketimes.append(time_point)
@ -116,11 +121,12 @@ class LifacNoiseModel(AbstractModel):
noise_strength = self.parameters["noise_strength"]
input_scaling = self.parameters["input_scaling"]
dend_tau = self.parameters["dend_tau"]
ref_period = self.parameters["refractory_period"]
rectified_stimulus = rectify_stimulus_array(stimulus.as_array(time_start, total_time_s, step_size))
parameters = np.array(
[v_zero, a_zero, step_size, threshold, v_base, delta_a, tau_a, v_offset, mem_tau, noise_strength,
time_start, input_scaling, dend_tau])
time_start, input_scaling, dend_tau, ref_period])
voltage_trace, adaption, spiketimes, input_voltage = simulate_fast(rectified_stimulus, total_time_s, parameters)
@ -173,85 +179,6 @@ class LifacNoiseModel(AbstractModel):
def get_model_copy(self):
return LifacNoiseModel(self.parameters)
def calculate_fi_markers(self, contrasts, stimulus_freq):
"""
calculates the fi markers f_infinity, f_infinity_slope for given contrasts
based on simulated 2 seconds for each contrast
:return: f_inf_values_list, f_inf_slope
"""
stimulus_start = 0.3
stimulus_duration = 1
f_infinities = []
for contrast in contrasts:
stimulus = SinusoidalStepStimulus(stimulus_freq, contrast, stimulus_start, stimulus_duration)
_, spiketimes = self.simulate_fast(stimulus, stimulus_start * 2 + stimulus_duration)
time, freq = hF.calculate_time_and_frequency_trace(spiketimes, self.get_sampling_interval())
f_inf = hF.detect_f_infinity_in_freq_trace(time, freq, stimulus_start, stimulus_duration,
self.get_sampling_interval())
f_infinities.append(f_inf)
popt = hF.fit_clipped_line(contrasts, f_infinities)
f_infinities_slope = popt[0]
return f_infinities, f_infinities_slope
def calculate_fi_curve(self, contrasts, stimulus_freq):
stim_duration = 0.5
stim_start = 0.5
total_simulation_time = stim_duration + 2 * stim_start
# print("Total simulation time (vs 2.5) {:.2f}".format(total_simulation_time))
sampling_interval = self.get_sampling_interval()
f_infinities = []
f_zeros = []
f_baselines = []
for c in contrasts:
stimulus = SinusoidalStepStimulus(stimulus_freq, c, stim_start, stim_duration)
_, spiketimes = self.simulate_fast(stimulus, total_simulation_time)
# if len(spiketimes) > 0:
# print("min:", min(spiketimes), "max:", max(spiketimes), "len:", len(spiketimes))
# else:
# print("spiketimes empty")
time, frequency = hF.calculate_time_and_frequency_trace(spiketimes, sampling_interval)
# if c == contrasts[0] or c == contrasts[-1]:
# plt.plot(frequency)
# plt.show()
if len(spiketimes) < 10 or len(time) == 0 or min(time) > stim_start or max(
time) < stim_start + stim_duration:
print("Too few spikes to calculate f_inf, f_0 and f_base")
f_infinities.append(0)
f_zeros.append(0)
f_baselines.append(0)
continue
f_inf = hF.detect_f_infinity_in_freq_trace(time, frequency, stim_start, stim_duration, sampling_interval)
f_infinities.append(f_inf)
f_zero = hF.detect_f_zero_in_frequency_trace(time, frequency, stim_start, sampling_interval)
f_zeros.append(f_zero)
f_baseline = hF.detect_f_baseline_in_freq_trace(time, frequency, stim_start, sampling_interval)
f_baselines.append(f_baseline)
# import matplotlib.pyplot as plt
# fig, axes = plt.subplots(2, 1, sharex="all")
# stim_time = np.arange(0,3.5, sampling_interval)
# axes[0].set_title("Contrast: " + str(c))
# axes[0].plot(stim_time, [stimulus.value_at_time_in_s(t) for t in stim_time]) # stimulus.as_array(0, 3.5, sampling_interval))
#
# axes[1].plot(time, frequency)
# axes[1].plot((time[0], time[-1]), (f_inf, f_inf), label="inf")
# axes[1].plot((time[0], time[-1]), (f_zero, f_zero), label="zero")
# axes[1].plot((time[0], time[-1]), (f_baseline, f_baseline), label="base")
# plt.legend()
# plt.show()
return f_baselines, f_zeros, f_infinities
def find_v_offset(self, goal_baseline_frequency, base_stimulus, threshold=2, border=50000):
test_model = self.get_model_copy()
simulation_length = 5
@ -340,6 +267,7 @@ def simulate_fast(rectified_stimulus_array, total_time_s, parameters: np.ndarray
time_start = parameters[10]
input_scaling = parameters[11]
dend_tau = parameters[12]
ref_period = parameters[13]
time = np.arange(time_start, total_time_s, step_size)
length = len(time)
@ -363,6 +291,9 @@ def simulate_fast(rectified_stimulus_array, total_time_s, parameters: np.ndarray
input_voltage[i] * input_scaling) - adaption[i - 1] + noise) / mem_tau) * step_size
adaption[i] = adaption[i - 1] + ((-adaption[i - 1]) / tau_a) * step_size
if len(spiketimes) > 0 and time[i] - spiketimes[-1] < ref_period + step_size/2:
output_voltage[i] = v_base
if output_voltage[i] > threshold:
output_voltage[i] = v_base
spiketimes.append(i * step_size)