commit all..
This commit is contained in:
@@ -5,7 +5,7 @@ import helperFunctions as hf
|
||||
from models.FirerateModel import FirerateModel
|
||||
from models.LIFACnoise import LifacNoiseModel
|
||||
from stimuli.StepStimulus import StepStimulus
|
||||
from stimuli.SinusAmplitudeModulation import SinusAmplitudeModulationStimulus
|
||||
from stimuli.SinusoidalStepStimulus import SinusoidalStepStimulus
|
||||
import functions as fu
|
||||
|
||||
|
||||
@@ -52,16 +52,16 @@ def test_lifac_noise():
|
||||
|
||||
model = LifacNoiseModel()
|
||||
|
||||
start = 0.2
|
||||
duration = 30
|
||||
start = 1
|
||||
duration = 3
|
||||
total_time = duration + 2*start
|
||||
step_size = model.get_parameters()["step_size"]/1000
|
||||
step_size = model.get_parameters()["step_size"]
|
||||
time = np.arange(0, total_time, step_size)
|
||||
stimulus = SinusAmplitudeModulationStimulus(700, 0.2, 10, 20, start, duration)
|
||||
stimulus = SinusoidalStepStimulus(700, 0.2, start, duration)
|
||||
|
||||
model.simulate(stimulus, total_time)
|
||||
model.simulate_fast(stimulus, total_time)
|
||||
|
||||
fig, axes = plt.subplots(nrows=3, sharex="col")
|
||||
fig, axes = plt.subplots(nrows=4, sharex="col")
|
||||
sparse_time = np.arange(0, total_time, 1/5000)
|
||||
axes[0].plot(sparse_time, [stimulus.value_at_time_in_s(x) for x in sparse_time], label="given stimulus")
|
||||
axes[0].plot(sparse_time, [fu.rectify(stimulus.value_at_time_in_s(x)) for x in sparse_time], label="seen stimulus")
|
||||
@@ -69,54 +69,63 @@ def test_lifac_noise():
|
||||
axes[0].set_ylabel("stimulus strength")
|
||||
axes[0].legend()
|
||||
|
||||
axes[1].plot(time, model.get_voltage_trace())
|
||||
axes[1].set_title("Voltage trace")
|
||||
axes[1].set_ylabel("voltage")
|
||||
input_voltage = model.input_voltage
|
||||
axes[1].plot(time, input_voltage)
|
||||
axes[1].set_title("Stimulus after dendtritic filter")
|
||||
axes[1].set_ylabel("stimulus strength")
|
||||
|
||||
t, f = hf.calculate_isi_frequency_trace(model.get_spiketimes(), 0, step_size)
|
||||
axes[2].plot(t, f)
|
||||
axes[2].set_title("ISI frequency trace")
|
||||
axes[2].set_ylabel("Frequency")
|
||||
voltage_trace = model.get_voltage_trace()
|
||||
axes[2].plot(time, voltage_trace)
|
||||
axes[2].set_title("Voltage trace")
|
||||
axes[2].set_ylabel("voltage")
|
||||
|
||||
spiketimes_small_step = model.get_spiketimes()
|
||||
|
||||
model.set_variable("step_size", 0.02)
|
||||
model.simulate(stimulus, total_time)
|
||||
print(model.get_adaption_trace()[int(0.1/(0.01/1000))])
|
||||
step_size = model.get_parameters()["step_size"] / 1000
|
||||
time = np.arange(0, total_time, step_size)
|
||||
t, f = hf.calculate_isi_frequency_trace(model.get_spiketimes(), 0, step_size)
|
||||
|
||||
axes[1].plot(time, model.get_voltage_trace())
|
||||
axes[2].plot(t, f)
|
||||
|
||||
spiketimes_big_step = model.get_spiketimes()
|
||||
|
||||
print("CV:")
|
||||
print("small step:", hf.calculate_coefficient_of_variation(spiketimes_small_step))
|
||||
print("big step:", hf.calculate_coefficient_of_variation(spiketimes_big_step))
|
||||
spiketimes = model.get_spiketimes()
|
||||
t, f = hf.calculate_time_and_frequency_trace(spiketimes, step_size)
|
||||
axes[3].plot(t, f)
|
||||
axes[3].set_title("ISI frequency trace")
|
||||
axes[3].set_ylabel("Frequency")
|
||||
|
||||
plt.show()
|
||||
plt.close()
|
||||
|
||||
max_lag = 5
|
||||
x = np.arange(1, max_lag+1, 1)
|
||||
serial_cor_small = hf.calculate_serial_correlation(spiketimes_small_step, max_lag)
|
||||
serial_cor_big = hf.calculate_serial_correlation(spiketimes_big_step, max_lag)
|
||||
# spiketimes_small_step = model.get_spiketimes()
|
||||
#
|
||||
# model.set_variable("step_size", 0.02)
|
||||
# model.simulate(stimulus, total_time)
|
||||
# print(model.get_adaption_trace()[int(0.1/(0.01/1000))])
|
||||
# step_size = model.get_parameters()["step_size"] / 1000
|
||||
# time = np.arange(0, total_time, step_size)
|
||||
# t, f = hf.calculate_time_and_frequency_trace(model.get_spiketimes(), 0.02)
|
||||
#
|
||||
# axes[1].plot(time, model.get_voltage_trace())
|
||||
# axes[2].plot(t, f)
|
||||
#
|
||||
# spiketimes_big_step = model.get_spiketimes()
|
||||
#
|
||||
# print("CV:")
|
||||
# print("small step:", hf.calculate_coefficient_of_variation(spiketimes_small_step))
|
||||
# print("big step:", hf.calculate_coefficient_of_variation(spiketimes_big_step))
|
||||
|
||||
print(serial_cor_small)
|
||||
print(serial_cor_big)
|
||||
plt.plot(x, serial_cor_small, 'o', label='small step',)
|
||||
plt.plot(x, serial_cor_big, 'o', label='big step')
|
||||
plt.ylim(-1, 1)
|
||||
plt.legend()
|
||||
plt.show()
|
||||
plt.close()
|
||||
|
||||
bins = np.arange(0, max(np.diff(spiketimes_small_step)), 0.0001)
|
||||
plt.hist(np.diff(spiketimes_small_step), bins=bins, alpha=0.5)
|
||||
plt.hist(np.diff(spiketimes_big_step), bins=bins, alpha=0.5)
|
||||
plt.show()
|
||||
# plt.show()
|
||||
# plt.close()
|
||||
#
|
||||
# max_lag = 5
|
||||
# x = np.arange(1, max_lag+1, 1)
|
||||
# serial_cor_small = hf.calculate_serial_correlation(spiketimes_small_step, max_lag)
|
||||
# serial_cor_big = hf.calculate_serial_correlation(spiketimes_big_step, max_lag)
|
||||
#
|
||||
# print(serial_cor_small)
|
||||
# print(serial_cor_big)
|
||||
# plt.plot(x, serial_cor_small, 'o', label='small step',)
|
||||
# plt.plot(x, serial_cor_big, 'o', label='big step')
|
||||
# plt.ylim(-1, 1)
|
||||
# plt.legend()
|
||||
# plt.show()
|
||||
# plt.close()
|
||||
#
|
||||
# bins = np.arange(0, max(np.diff(spiketimes_small_step)), 0.0001)
|
||||
# plt.hist(np.diff(spiketimes_small_step), bins=bins, alpha=0.5)
|
||||
# plt.hist(np.diff(spiketimes_big_step), bins=bins, alpha=0.5)
|
||||
# plt.show()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
28
tests/functionalityTests.py
Normal file
28
tests/functionalityTests.py
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import functions as fu
|
||||
|
||||
|
||||
def test_plot_inverses(ficurve):
|
||||
var = ficurve.boltzmann_fit_vars
|
||||
|
||||
fig, ax1 = plt.subplots(1, 1, figsize=(4.5, 4.5), tight_layout=True)
|
||||
start = min(ficurve.stimulus_value)
|
||||
end = max(ficurve.stimulus_value)
|
||||
x_values = np.arange(start, end, (end-start)/5000)
|
||||
ax1.plot(x_values, [fu.full_boltzmann(x, var[0], var[1], var[2], var[3]) for x in x_values], label="fit")
|
||||
ax1.set_ylabel('freq')
|
||||
ax1.set_xlabel('stimulus')
|
||||
|
||||
start = var[1]
|
||||
end = var[0]
|
||||
x_values = np.arange(start, end, (end - start) / 50)
|
||||
ax1.plot([fu.inverse_full_boltzmann(x, var[0], var[1], var[2], var[3]) for x in x_values], x_values,
|
||||
'.', c="red", label='inverse')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
226
tests/generalTests.py
Normal file
226
tests/generalTests.py
Normal file
@@ -0,0 +1,226 @@
|
||||
|
||||
import helperFunctions as hf
|
||||
from CellData import icelldata_of_dir
|
||||
import functions as fu
|
||||
import numpy as np
|
||||
import time
|
||||
import matplotlib.pyplot as plt
|
||||
import os
|
||||
from scipy.signal import argrelmax
|
||||
from thunderfish.eventdetection import detect_peaks
|
||||
from stimuli.SinusAmplitudeModulation import SinusAmplitudeModulationStimulus
|
||||
from models.LIFACnoise import LifacNoiseModel
|
||||
from FiCurve import FICurve
|
||||
from AdaptionCurrent import Adaption
|
||||
from stimuli.StepStimulus import StepStimulus
|
||||
from stimuli.SinusoidalStepStimulus import SinusoidalStepStimulus
|
||||
|
||||
|
||||
def time_test_function():
|
||||
for n in [1000]: # number of calls
|
||||
print("calls:", n)
|
||||
start = time.time()
|
||||
for i in range(n):
|
||||
data = np.random.normal(size=10000)
|
||||
y = [fu.rectify(x) for x in data]
|
||||
end = time.time()
|
||||
print("time:", end - start)
|
||||
|
||||
|
||||
def test_cell_data():
|
||||
for cell_data in icelldata_of_dir("../data/"):
|
||||
#if "2012-12-20-ad" not in cell_data.get_data_path():
|
||||
# continue
|
||||
print()
|
||||
print(cell_data.get_data_path())
|
||||
if len(cell_data.get_base_traces(cell_data.TIME)) != 0:
|
||||
# print("works!")
|
||||
#print("VS:", cell_data.get_vector_strength())
|
||||
#print("SC:", cell_data.get_serial_correlation(5))
|
||||
print("Eod freq:", cell_data.get_eod_frequency())
|
||||
else:
|
||||
pass
|
||||
#print("NNNOOOOOOOOOO!")
|
||||
#print("spiketimes:", len(cell_data.get_base_spikes()))
|
||||
#print("Times:", len(cell_data.get_base_traces(cell_data.TIME)))
|
||||
#print("EOD:", len(cell_data.get_base_traces(cell_data.EOD)))
|
||||
|
||||
|
||||
def test_peak_detection():
|
||||
for cell_data in icelldata_of_dir("../data/"):
|
||||
print()
|
||||
print(cell_data.get_data_path())
|
||||
times = cell_data.get_base_traces(cell_data.TIME)
|
||||
eod = cell_data.get_base_traces(cell_data.EOD)
|
||||
v1 = cell_data.get_base_traces(cell_data.V1)
|
||||
for i in range(len(v1)):
|
||||
pieces = 20
|
||||
v1_trace = v1[i]
|
||||
total = len(v1_trace)
|
||||
all_peaks = []
|
||||
plt.plot(times[i], v1[i])
|
||||
|
||||
for n in range(pieces):
|
||||
length = int(total/pieces)
|
||||
first_index = n*length
|
||||
last_index = (n+1)*length
|
||||
std = np.std(v1_trace[first_index:last_index])
|
||||
peaks, _ = detect_peaks(v1_trace[first_index:last_index], std * 3)
|
||||
peaks = peaks + first_index
|
||||
all_peaks.extend(peaks)
|
||||
plt.plot(times[i][first_index], v1_trace[first_index], 'o', color="green")
|
||||
|
||||
all_peaks = np.array(all_peaks)
|
||||
|
||||
plt.plot(times[i][all_peaks], v1[i][all_peaks], 'o', color='red')
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
def test_simulation_speed():
|
||||
parameters = {'mem_tau': 21.348990483539083, 'delta_a': 20.41809814660199, 'input_scaling': 3.0391541280864196, 'v_offset': 26.25, 'threshold': 1, 'v_base': 0, 'step_size': 0.00005, 'tau_a': 158.0404259501454, 'a_zero': 0, 'v_zero': 0, 'noise_strength': 2.87718460648148}
|
||||
model = LifacNoiseModel(parameters)
|
||||
repetitions = 1
|
||||
seconds = 10
|
||||
stimulus = SinusAmplitudeModulationStimulus(750, 1, 10, 1, 8)
|
||||
time_start = 0
|
||||
t_start = time.time()
|
||||
for i in range(repetitions):
|
||||
|
||||
v, spikes = model.simulate_fast(stimulus, seconds, time_start)
|
||||
print(len(v))
|
||||
print(len(spikes))
|
||||
|
||||
#time_v = np.arange(time_start, seconds, model.get_sampling_interval())
|
||||
#plt.plot(time_v, v, '.')
|
||||
#plt.show()
|
||||
#freq = hf.mean_freq_of_spiketimes_after_time_x(spikes, parameters["step_size"], 0)
|
||||
#print(freq)
|
||||
t_end = time.time()
|
||||
#print("baseline markers:", model.calculate_baseline_markers(750, 3))
|
||||
print("took:", round((t_end-t_start)/repetitions, 5), "seconds for " + str(seconds) + "s simulation", "step size:", parameters["step_size"]*1000, "ms")
|
||||
|
||||
|
||||
def test_fi_curve_class():
|
||||
for cell_data in icelldata_of_dir("../data/"):
|
||||
fi_curve = FICurve(cell_data)
|
||||
fi_curve.get_f_zero_and_f_inf_intersection()
|
||||
fi_curve.plot_fi_curve()
|
||||
# fi_curve.plot_f_point_detections()
|
||||
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def test_adaption_class():
|
||||
for cell_data in icelldata_of_dir("../data/"):
|
||||
print()
|
||||
print(cell_data.get_data_path())
|
||||
fi_curve = FICurve(cell_data)
|
||||
adaption = Adaption(cell_data, fi_curve)
|
||||
|
||||
adaption.plot_exponential_fits()
|
||||
|
||||
print("tau_effs:", adaption.get_tau_effs())
|
||||
print("tau_real:", adaption.get_tau_real())
|
||||
fi_curve.plot_fi_curve()
|
||||
|
||||
|
||||
def test_parameters():
|
||||
parameters = {'mem_tau': 21., 'delta_a': 0.1, 'input_scaling': 400.,
|
||||
'v_offset': 85.25, 'threshold': 0.1, 'v_base': 0, 'step_size': 0.00005, 'tau_a': 0.01,
|
||||
'a_zero': 0, 'v_zero': 0, 'noise_strength': 3}
|
||||
model = LifacNoiseModel(parameters)
|
||||
|
||||
base_stimulus_freq = 350
|
||||
stimulus = SinusAmplitudeModulationStimulus(base_stimulus_freq, 1.2, 5, 5, 20)
|
||||
|
||||
plot_model_during_stimulus(model, stimulus, 30)
|
||||
|
||||
bf, vs, sc = model.calculate_baseline_markers(base_stimulus_freq)
|
||||
contrasts = [0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3]
|
||||
modulation_frequency = 1
|
||||
f_infs, f_inf_slope = model.calculate_fi_markers(contrasts, base_stimulus_freq, modulation_frequency)
|
||||
|
||||
print("Baseline frequency: {:.2f}".format(bf))
|
||||
print("Vector strength: {:.2f}".format(vs))
|
||||
print("serial correlation: {:.2f}".format(sc[0]))
|
||||
|
||||
print("f infinity slope: {:.2f}".format(f_inf_slope))
|
||||
print("f infinities: \n", f_infs)
|
||||
|
||||
|
||||
def test_vector_strength_calculation():
|
||||
model = LifacNoiseModel({"noise_strength": 0})
|
||||
|
||||
|
||||
bf, vs1, sc = model.calculate_baseline_markers(600)
|
||||
|
||||
base_stim = SinusAmplitudeModulationStimulus(600, 0, 0)
|
||||
_, spiketimes = model.simulate_fast(base_stim, 30)
|
||||
stimulus_trace = base_stim.as_array(0, 30, model.get_sampling_interval())
|
||||
time_trace = np.arange(0, 30, model.get_sampling_interval())
|
||||
|
||||
vs2 = hf.calculate_vector_strength_from_spiketimes(time_trace, stimulus_trace, spiketimes, model.get_sampling_interval())
|
||||
|
||||
print("with assumed eod durations vs: {:.3f}".format(vs1))
|
||||
print("with detected eod durations vs: {:.3f}".format(vs2))
|
||||
|
||||
|
||||
def plot_model_during_stimulus(model: LifacNoiseModel, stimulus:SinusAmplitudeModulationStimulus, total_time):
|
||||
_, spiketimes = model.simulate_fast(stimulus, total_time)
|
||||
|
||||
time = np.arange(0, total_time, model.get_sampling_interval())
|
||||
fig, axes = plt.subplots(5, 1, figsize=(9, 4*2), sharex="all")
|
||||
|
||||
stimulus_array = stimulus.as_array(0, total_time, model.get_sampling_interval())
|
||||
axes[0].plot(time, stimulus_array)
|
||||
axes[0].set_title("Stimulus")
|
||||
axes[1].plot(time, rectify_stimulus_array(stimulus_array))
|
||||
axes[1].set_title("rectified Stimulus")
|
||||
axes[2].plot(time, model.get_voltage_trace())
|
||||
axes[2].set_title("Voltage")
|
||||
axes[3].plot(time, model.get_adaption_trace())
|
||||
axes[3].set_title("Adaption")
|
||||
|
||||
f_time, f = hf.calculate_time_and_frequency_trace(spiketimes, model.get_sampling_interval())
|
||||
axes[4].plot(f_time, f)
|
||||
|
||||
axes[4].set_title("Frequency")
|
||||
plt.show()
|
||||
|
||||
|
||||
def rectify_stimulus_array(stimulus_array: np.ndarray):
|
||||
return np.array([x if x > 0 else 0 for x in stimulus_array])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# X = [0.05, 0.02, 50, 0.1, 0.03]
|
||||
model = LifacNoiseModel()
|
||||
# model.set_variable("mem_tau", X[0])
|
||||
# model.set_variable("noise_strength", X[1])
|
||||
# model.set_variable("input_scaling", X[2])
|
||||
# model.set_variable("tau_a", X[3])
|
||||
# model.set_variable("delta_a", X[4])
|
||||
stim = SinusoidalStepStimulus(700, 0.2, start_time=1, duration=1)
|
||||
bf, vs, sc = model.calculate_baseline_markers(700)
|
||||
print("baseline freq:{:.2f}\nVector strength: {:.3f}\nSerial cor:".format(bf, vs), sc)
|
||||
contrasts = np.arange(-0.3, 0.31, 0.05)
|
||||
model.calculate_fi_curve(contrasts, 700)
|
||||
f_infinities, slope = model.calculate_fi_markers(contrasts, 700)
|
||||
print("FI-Curve\nSlope: {:.2f}\nValues:".format(slope), f_infinities)
|
||||
plot_model_during_stimulus(model, stim, 3)
|
||||
|
||||
quit()
|
||||
|
||||
|
||||
# time_test_function()
|
||||
# test_cell_data()
|
||||
# test_peak_detection()
|
||||
# test_simulation_speed()
|
||||
# test_parameters()
|
||||
# test_fi_curve_class()
|
||||
# test_adaption_class()
|
||||
test_vector_strength_calculation()
|
||||
pass
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
import unittest
|
||||
from stimuli.StepStimulus import StepStimulus
|
||||
import numpy as np
|
||||
|
||||
|
||||
class TestStepStimulus(unittest.TestCase):
|
||||
|
||||
def test_time_getters(self):
|
||||
starts = [2, -5, 0, 10]
|
||||
durations = [2, 1000, 0.5]
|
||||
value = 10
|
||||
|
||||
for start in starts:
|
||||
for duration in durations:
|
||||
stimulus = StepStimulus(start, duration, value)
|
||||
|
||||
self.assertEqual(start, stimulus.get_stimulus_start_s(), "reported start (s) was wrong")
|
||||
self.assertEqual(duration, stimulus.get_stimulus_duration_s(), "reported duration (s) was wrong")
|
||||
self.assertEqual(start + duration, stimulus.get_stimulus_end_s(), "reported end (s) was wrong")
|
||||
|
||||
self.assertEqual(start * 1000, stimulus.get_stimulus_start_ms(), "reported start (ms) was wrong")
|
||||
self.assertEqual(duration * 1000, stimulus.get_stimulus_duration_ms(), "reported duration (ms) was wrong")
|
||||
self.assertEqual((start + duration)*1000, stimulus.get_stimulus_end_ms(), "reported end (ms) was wrong")
|
||||
|
||||
def test_duration_must_be_positive(self):
|
||||
self.assertRaises(ValueError, StepStimulus, 1, -1, 3)
|
||||
|
||||
def test_value_at(self):
|
||||
start = 0
|
||||
duration = 2
|
||||
value = 5
|
||||
base_value = -1
|
||||
stimulus = StepStimulus(start, duration, value, base_value)
|
||||
|
||||
for i in np.arange(start-1, start+duration+1, 0.1):
|
||||
if i < start or i > start+duration:
|
||||
self.assertEqual(stimulus.value_at_time_in_s(i), base_value)
|
||||
self.assertEqual(stimulus.value_at_time_in_ms(i*1000), base_value)
|
||||
else:
|
||||
self.assertEqual(stimulus.value_at_time_in_s(i), value)
|
||||
self.assertEqual(stimulus.value_at_time_in_ms(i * 1000), value)
|
||||
|
||||
def test_amplitude(self):
|
||||
stim_values = [-10, -5, 0, 15, 20]
|
||||
base_values = [-10, -5, -2, 0, 1, 15]
|
||||
|
||||
for s_value in stim_values:
|
||||
for b_value in base_values:
|
||||
stimulus = StepStimulus(0, 1, s_value, b_value)
|
||||
|
||||
self.assertEqual(stimulus.get_amplitude(), s_value-b_value)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user