Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try:
p_gamma[i, :], _ = scipy.optimize.curve_fit(fit_gamma, x, events[:, i], p0=[1, 2, 2, 3])
p_bateman[i, :], _ = scipy.optimize.curve_fit(fit_bateman, x, events[:, i], p0=[1, 1, 0.75, 2])
p_scr[i, :], _ = scipy.optimize.curve_fit(fit_scr, x, events[:, i], p0=[1, 3, 0.7, 3, 5])
p_poly[i, :] = params_poly(events[:, i], order=4)
except RuntimeError:
pass
# Visualize for one particpant
cleaned = nk.eog_clean(signals[0], sampling_rate=100, method='neurokit')
blinks = nk.eog_findpeaks(cleaned, sampling_rate=100, method="mne")
events = nk.epochs_create(cleaned, blinks, sampling_rate=100, epochs_start=-0.4, epochs_end=0.6)
events = nk.epochs_to_array(events)
for i in range(events.shape[1]):
events[:, i] = nk.rescale(events[:, i], to=[0, 1]) # Reshape to 0-1 scale
x = np.linspace(0, 100, num=len(events))
template_gamma = fit_gamma(x, *np.nanmedian(p_gamma, axis=0))
template_bateman = fit_bateman(x, *np.nanmedian(p_bateman, axis=0))
template_scr = fit_scr(x, *np.nanmedian(p_scr, axis=0))
template_poly = fit_poly(x, np.nanmedian(p_poly, axis=0))
plt.plot(events, linewidth=0.25, color="black")
plt.plot(template_gamma, linewidth=2, linestyle='-', color="red", label='Gamma')
plt.plot(template_bateman, linewidth=2, linestyle='-', color="blue", label='Bateman')
plt.plot(template_scr, linewidth=2, linestyle='-', color="orange", label='SCR')
plt.plot(template_poly, linewidth=2, linestyle='-', color="green", label='Polynomial')
plt.legend(loc="upper right")
cleaned = nk.eog_clean(signal, sampling_rate=200, method='neurokit')
blinks = nk.signal_findpeaks(cleaned, relative_height_min=1.5)["Peaks"]
events = nk.epochs_create(cleaned, blinks, sampling_rate=200, epochs_start=-0.4, epochs_end=0.6)
events = nk.epochs_to_array(events) # Convert to 2D array
x = np.linspace(0, 100, num=len(events))
p_gamma = np.full((events.shape[1], 3), np.nan)
p_scr = np.full((events.shape[1], 4), np.nan)
for i in range(events.shape[1]):
if np.isnan(events[:, i]).any():
break
events[:, i] = nk.rescale(events[:, i], to=[0, 1]) # Reshape to 0-1 scale
if nk.fit_rmse(events[:, i], template_gamma) < 0.25:
try:
p_gamma[i, :], _ = scipy.optimize.curve_fit(fit_gamma, x, events[:, i], p0=optimal_gamma)
except RuntimeError:
pass
if nk.fit_rmse(events[:, i], template_scr) < 0.25:
try:
p_scr[i, :], _ = scipy.optimize.curve_fit(fit_scr, x, events[:, i], p0=optimal_scr)
except RuntimeError:
pass
p_gamma = pd.DataFrame(p_gamma[~np.isnan(p_gamma).any(axis=1)], columns=["loc", "a", "scale"])
p_gamma["Participant"] = participant
p_gamma["Task"] = data["Task"][0]
def fit_scr(x, time_peak, rise, decay1, decay2):
x = nk.rescale(x, to=[0, 10])
gt = np.exp(-((x - time_peak) ** 2) / (2 * rise ** 2))
ht = np.exp(-x / decay1) + np.exp(-x / decay2)
ft = np.convolve(gt, ht)
ft = ft[0 : len(x)]
y = ft / np.max(ft)
return y
def fit_gamma(x, loc, a, scale):
x = nk.rescale(x, to=[0, 10])
gamma = scipy.stats.gamma.pdf(x, a=a, loc=loc, scale=scale)
y = gamma / np.max(gamma)
return y
def fit_scr(x, size, time_peak, rise, decay1, decay2):
"""
>>> x = np.arange(100)
>>> nk.signal_plot(fit_scr(x, 0, 1, 3, 0.7, 3, 5))
"""
x = nk.rescale(x, to=[0, 20])
gt = np.exp(-((x - time_peak) ** 2) / (2 * rise ** 2))
ht = np.exp(-x / decay1) + np.exp(-x / decay2)
ft = np.convolve(gt, ht)
ft = ft[0 : len(x)]
y = size * ft
return y
p_scr["Task"] = data["Task"][0]
params_scr = pd.concat([params_scr, p_scr], axis=0)
data = pd.read_csv("../../data/eogdb/eogdb_task3.csv")
cleaned = nk.eog_clean(data["vEOG"], sampling_rate=200, method='neurokit')
blinks = nk.signal_findpeaks(cleaned, relative_height_min=1.5)["Peaks"][:-1]
events = nk.epochs_create(cleaned, blinks, sampling_rate=200, epochs_start=-0.4, epochs_end=0.6)
events = nk.epochs_to_array(events)
for i in range(events.shape[1]):
events[:, i] = nk.rescale(events[:, i], to=[0, 1]) # Reshape to 0-1 scale
x = np.linspace(0, 100, num=len(events))
template_gamma = fit_gamma(x, *np.nanmedian(params_gamma.iloc[:, [0, 1, 2]], axis=0))
template_scr = fit_scr(x, *np.nanmedian(params_scr.iloc[:, [0, 1, 2, 3]], axis=0))
plt.plot(events, linewidth=0.02, color="black")
plt.plot(template_gamma, linewidth=2, linestyle='-', color="#4CAF50", label='Gamma')
plt.plot(template_scr, linewidth=2, linestyle='-', color="#9C27B0", label='SCR')
plt.legend(loc="upper right")
plt.show()
def fit_gamma(x, size, loc, a, scale):
"""
>>> x = np.arange(100)
>>> nk.signal_plot(fit_gamma(x, 0, 1, 2, 2, 3))
"""
x = nk.rescale(x, to=[0, 20])
gamma = scipy.stats.gamma.pdf(x, a=a, loc=loc, scale=scale)
y = size * gamma
return y
def fit_bateman(x, size=1, loc=0, t1=0.75, t2=2):
"""
>>> x = np.arange(100)
>>> nk.signal_plot(fit_bateman(x, 0, 1, 1, 0.75, 2))
"""
x = nk.rescale(x, to=[-loc, 10])
bateman = np.exp(-x / t2) - np.exp(-x / t1)
bateman[np.where(x < 0)] = 0
y = size * bateman
return y