diff --git a/doc/sam.qmd b/doc/sam.qmd new file mode 100644 index 0000000..63074a8 --- /dev/null +++ b/doc/sam.qmd @@ -0,0 +1,111 @@ +--- +title: SAM +format: + html: + toc: true + toc-title: Contents + code-block-bg: true + code-block-border-left: "#31BAE9" + code-line-numbers: true + highlight-style: atom-one + link-external-icon: true + link-external-newwindow: true + eqn-number: true +--- + +### SAM + +```{python} +import pathlib + +import numpy as np +import plotly.graph_objects as go +import rlxnix as rlx +import scipy.signal as signal +from plotly.subplots import make_subplots +from util import plot_line_comparision, trial_plot + +dataset_path = pathlib.Path("../oephys2nix/test/AllStimuli/2025-10-20-aa-invivo-2-recording.nix") +relacs_path = pathlib.Path( + "../oephys2nix/test/AllStimuli/2025-10-20-aa-invivo-2_relacs/2025-10-20-aa-invivo-2_relacs.nix" +) + +dataset = rlx.Dataset(str(dataset_path)) +relacs = rlx.Dataset(str(relacs_path)) + +# INFO: Select the first stimulus of the calibration repro +repro_d = dataset.repro_runs("SAM")[0].stimuli[2] +repro_r = relacs.repro_runs("SAM")[0].stimuli[2] + +sinus, t = repro_d.trace_data("sinus") +sinus_r, t_r = repro_r.trace_data("V-1") + +stimulus_oe, t = repro_d.trace_data("stimulus") +stimulus_re, t_r = repro_r.trace_data("GlobalEFieldStimulus") + +local_eod_oe, t = repro_d.trace_data("local-eod") +local_eod_re, t_r = repro_r.trace_data("LocalEOD-1") + +global_eod_oe, t = repro_d.trace_data("global-eod") +global_eod_re, t_r = repro_r.trace_data("EOD") + +ttl, t = repro_d.trace_data("ttl-line") +``` +### Plotting the First trial +If you zoom in you can see a little delay between the different recording systems. It seems that open-ephys is before the relacs recording. + +```{python} +# | echo: False +fig = trial_plot(repro_d, repro_r, 0.2) +fig.update_xaxes(range=[0, 0.2]) +fig.show() +``` +### Correlation between the Signals + +```{python} +print(f"Duration of the dataset {repro_d.duration}") +print(f"Duration of the relacs {repro_r.duration}") +# Resample the open-ephys data +sinus_resampled = signal.resample(sinus, len(sinus_r)) +``` + +```{python} +# | echo: False +fig = plot_line_comparision( + t_r, t_r, sinus_r, sinus_resampled, ["sinus-relacs", "sinus-resampled-open-ephys"] +) +fig.show() +``` +We need to scale the two signals + +```{python} +oephys_lanes = [sinus, local_eod_oe, global_eod_oe, stimulus_oe] +relacs_lanes = [sinus_r, local_eod_re, global_eod_re, stimulus_re] +names_lanes = ["sinus", "local-eod", "global-eod", "stimulus"] +lags_lanes = [] +for oephys_lane, relacs_lane, names_lane in zip( + oephys_lanes, relacs_lanes, names_lanes, strict=True +): + print(oephys_lane.shape) + print(relacs_lane.shape) + oephys_lane_resampled = signal.resample(oephys_lane, len(relacs_lane)) + correlation = signal.correlate(oephys_lane_resampled, relacs_lane, mode="full") + lags = signal.correlation_lags(oephys_lane_resampled.size, relacs_lane.size, mode="full") + lag = lags[np.argmax(correlation)] + lags_lanes.append(lag) + print(f"{names_lane} has a lag of {lag}") +``` + +```{python} +# | echo: False +fig = plot_line_comparision( + t_r, + t, + np.roll(stimulus_re, lags_lanes[-1]), + stimulus_oe - np.mean(stimulus_oe), + ["rolled sinus-relacs", "sinus-resampled-open-ephys"], +) +fig.show() + +print(f"The lag of the whitenoise is {lags_lanes[-1] * (1 / 20_000) * 1000} milli seconds") +```