def test_plot_instantaneous_measure(tsig_comb): times = create_times(N_SECONDS, FS) plot_instantaneous_measure( times, amp_by_time(tsig_comb, FS, F_RANGE), 'amplitude', save_fig=True, file_path=TEST_PLOTS_PATH, file_name='test_plot_instantaneous_measure_amplitude.png') plot_instantaneous_measure( times, phase_by_time(tsig_comb, FS, F_RANGE), 'phase', save_fig=True, file_path=TEST_PLOTS_PATH, file_name='test_plot_instantaneous_measure_phase.png') plot_instantaneous_measure( times, freq_by_time(tsig_comb, FS, F_RANGE), 'frequency', save_fig=True, file_path=TEST_PLOTS_PATH, file_name='test_plot_instantaneous_measure_frequency.png') # Check the error for bad measure with raises(ValueError): plot_instantaneous_measure(times, tsig_comb, 'BAD')
plot_time_series(times, sig) ################################################################################################### # Instantaneous Phase # ------------------- # # Instantaneous phase is a measure of the phase of a signal, over time. # # Instantaneous phase can be analyzed with the # :func:`~neurodsp.timefrequency.hilbert.phase_by_time` function. # ################################################################################################### # Compute instaneous phase from a signal pha = phase_by_time(sig, fs, f_range) ################################################################################################### # Plot example signal _, axs = plt.subplots(2, 1, figsize=(15, 6)) plot_time_series(times, sig, xlim=[4, 5], xlabel=None, ax=axs[0]) plot_instantaneous_measure(times, pha, xlim=[4, 5], ax=axs[1]) ################################################################################################### # Instantaneous Amplitude # ----------------------- # # Instantaneous amplitude is a measure of the amplitude of a signal, over time. # # Instantaneous amplitude can be analyzed with the
# Simulation settings n_seconds = 10 fs = 1000 components = {'sim_bursty_oscillation': {'freq': 10, 'enter_burst': .1, 'leave_burst': .1, 'cycle': 'asine', 'rdsym': 0.3}, 'sim_powerlaw': {'f_range': (2, None)}} sig = sim_combined(n_seconds, fs, components=components, component_variances=(2, 1)) # Filter settings f_alpha = (8, 12) n_seconds_filter = .5 # Compute amplitude and phase sig_filt = filter_signal(sig, fs, 'bandpass', f_alpha, n_seconds=n_seconds_filter) theta_amp = amp_by_time(sig, fs, f_alpha, n_seconds=n_seconds_filter) theta_phase = phase_by_time(sig, fs, f_alpha, n_seconds=n_seconds_filter) # Plot signal times = create_times(n_seconds, fs) xlim = (2, 6) tidx = np.logical_and(times >= xlim[0], times < xlim[1]) fig, axes = plt.subplots(figsize=(15, 9), nrows=3) # Plot the raw signal plot_time_series(times[tidx], sig[tidx], ax=axes[0], ylabel='Voltage (mV)', xlabel='', lw=2, labels='raw signal') # Plot the filtered signal and oscillation amplitude plot_instantaneous_measure(times[tidx], [sig_filt[tidx], theta_amp[tidx]], ax=axes[1], measure='amplitude', lw=2, xlabel='',
def get_inst_data(data, fs, f_range): i_phase = tf.phase_by_time(data, fs, f_range) i_amp = tf.amp_by_time(data, fs, f_range) i_freq = tf.freq_by_time(data, fs, f_range) return i_phase, i_amp, i_freq
def get_alpha_instantaneous_statistics(eeg_epoch_full_df): # alpha_range = (7, 12) alpha_amps = {} alpha_pha = {} alpha_if = {} power_range = [(4, 7), (7, 12), (12, 30)] for power_i in range(len(power_range)): alpha_range = power_range[power_i] for i in range(0, len(eeg_epoch_full_df)): for ch in all_chans: sig = eeg_epoch_full_df[ch][i][:] key = ch + "_" + str(alpha_range) amp = amp_by_time( sig, eeg_fs, alpha_range) # Amplitude by time (instantaneous amplitude) if key + "_amp_med" not in alpha_amps: alpha_amps[key + "_amp_med"] = list() alpha_amps[key + "_amp_avg"] = list() alpha_amps[key + "_amp_std"] = list() alpha_amps[key + "_amp_gradmax"] = list() alpha_amps[key + "_amp_gradmin"] = list() alpha_amps[key + "_amp_med"].append(np.nanmedian(amp)) alpha_amps[key + "_amp_avg"].append(np.nanmean(amp)) alpha_amps[key + "_amp_std"].append(np.nanstd(amp)) alpha_amps[key + "_amp_gradmax"].append( np.nanmax(np.gradient(amp))) alpha_amps[key + "_amp_gradmin"].append( np.nanmin(np.gradient(amp))) pha = phase_by_time( sig, eeg_fs, alpha_range) # Phase by time (instantaneous phase) if key + "_pha_med" not in alpha_pha: alpha_pha[key + "_pha_med"] = list() alpha_pha[key + "_pha_avg"] = list() alpha_pha[key + "_pha_std"] = list() alpha_pha[key + "_pha_gradmax"] = list() alpha_pha[key + "_pha_gradmin"] = list() alpha_pha[key + "_pha_med"].append(np.nanmedian(pha)) alpha_pha[key + "_pha_avg"].append(np.nanmean(pha)) alpha_pha[key + "_pha_std"].append(np.nanstd(pha)) alpha_pha[key + "_pha_gradmax"].append( np.nanmax(np.gradient(pha))) alpha_pha[key + "_pha_gradmin"].append( np.nanmin(np.gradient(pha))) i_f = freq_by_time( sig, eeg_fs, alpha_range) # Frequency by time (instantaneous frequency) if key + "_freq_med" not in alpha_if: alpha_if[key + "_freq_med"] = list() alpha_if[key + "_freq_avg"] = list() alpha_if[key + "_freq_std"] = list() alpha_if[key + "_freq_gradmax"] = list() alpha_if[key + "_freq_gradmin"] = list() alpha_if[key + "_freq_med"].append(np.nanmedian(i_f)) alpha_if[key + "_freq_avg"].append(np.nanmean(i_f)) alpha_if[key + "_freq_std"].append(np.nanstd(i_f)) alpha_if[key + "_freq_gradmax"].append( np.nanmax(np.gradient(i_f))) alpha_if[key + "_freq_gradmin"].append( np.nanmin(np.gradient(i_f))) alpha_med_df = pd.DataFrame(alpha_amps) alpha_pha_df = pd.DataFrame(alpha_pha) alpha_if_df = pd.DataFrame(alpha_if) insta_stat_df = pd.concat([alpha_med_df, alpha_pha_df, alpha_if_df], axis=1) print(list(insta_stat_df.columns)) return insta_stat_df
neurons_range = range(shape(spikes_list)[0]) neuron_list = [] for neuron in neurons_range: spikes = spikes_list[neuron] neuron_spike_list = [] for spike in spikes: if spike > ind_min and spike < ind_max: neuron_spike_list.append(spike) neuron_list.append(neuron_spike_list) session_spike_list.append(neuron_list) trial = 295 session_spike_list = np.asarray(session_spike_list) one_trial = session_spike_list[trial, :] pha = phase_by_time(lfp_times_list[trial], Fs, f_range) min_time = min(lfp_times_list[trial]) max_time = max(lfp_times_list[trial]) colors = ["red", "orange", "grey", "green", "blue", "purple", "black"] axmin = 500 axmax = 600 for neuron in neurons_range: if len(one_trial[neuron]) > 0: if neuron == 2: axmin += 150 axmax += 150 vlines(one_trial[neuron], ymin=axmin, ymax=axmax, color=colors[neuron])
plt_time = [60, 62] # create times structure for plotting times = create_times(len(sig) / fs, fs) times = times[0:len(times) - 1] #%% Calculate and plot # filter data on the providing bands phase_filt_signal = filter_signal(data[:, ch], fs, 'bandpass', phase_providing_band) ampl_filt_signal = filter_signal(data[:, ch], fs, 'bandpass', amplitude_providing_band) # calculate the phase phase_signal = phase_by_time(data[:, ch], fs, phase_providing_band) # Compute instaneous amplitude from a signal amp_signal = amp_by_time(sig, fs, amplitude_providing_band) #%% Plot the phase # plot of phase: raw data, filtered, and phase _, axs = plt.subplots(3, 1, figsize=(15, 6)) plot_time_series(times, data[:, ch], xlim=plt_time, xlabel=None, ax=axs[0]) plot_time_series(times, phase_filt_signal, xlim=plt_time, xlabel=None, ax=axs[1]) plot_instantaneous_measure(times, phase_signal, xlim=plt_time, ax=axs[2])