def motif_outs(): comps_ref = {'sim_oscillation': {'freq' : 1, 'cycle': 'sine'}} motif_ref = sim_combined(1, 100, comps_ref) comps_target = {'sim_oscillation': {'freq' : 1, 'cycle': 'asine','rdsym': .6}} motif_target = sim_combined(1, 100, comps_target) yield dict(motif_ref=motif_ref, motif_target=motif_target)
def sim_sig(): # Simulate a 1d timeseries that contains an oscillation + 1/f sig = sim_combined(N_SECONDS, FS, {'sim_powerlaw': {'exponent': EXP}, 'sim_oscillation': {'freq': FREQ}}) yield dict(n_seconds=N_SECONDS, fs=FS, exp=EXP, freq=FREQ, f_range=F_RANGE, sig=sig)
def sim_args_comb(): components = { 'sim_bursty_oscillation': { 'freq': FREQ }, 'sim_powerlaw': { 'exp': 2 } } sig = sim_combined(N_SECONDS, FS, components=components) df_shapes = compute_shape_features(sig, FS, F_RANGE) df_burst = compute_burst_features(df_shapes, sig) df_features = compute_features(sig, FS, F_RANGE) threshold_kwargs = { 'amp_fraction_threshold': 0., 'amp_consistency_threshold': .5, 'period_consistency_threshold': .5, 'monotonicity_threshold': .5, 'min_n_cycles': 3 } yield { 'sig': sig, 'fs': FS, 'f_range': F_RANGE, 'df_features': df_features, 'df_shapes': df_shapes, 'df_burst': df_burst, 'threshold_kwargs': threshold_kwargs }
def tsig_comb(): components = { 'sim_powerlaw': { 'exponent': EXP1 }, 'sim_oscillation': { 'freq': FREQ1 } } yield sim_combined(n_seconds=N_SECONDS_LONG, fs=FS, components=components)
def tsig_burst(): components = { 'sim_powerlaw': { 'exponent': EXP1 }, 'sim_bursty_oscillation': { 'freq': FREQ1 } } yield sim_combined(n_seconds=N_SECONDS, fs=FS, components=components, component_variances=[0.5, 1])
def return_pseudo_neuro_sig(peakFreq=None, peakBw=None, burstProp=0.3, sigDuration=None, sfreq=None, ooofExp=-2, sigAmplitudeNam=None): '''Fix - scale to a specified amount of neural power''' components = { 'sim_bursty_oscillation': { 'freq': peakFreq, 'enter_burst': burstProp }, 'sim_powerlaw': { 'exponent': ooofExp, 'f_range': (1, None) } } signal = sim_combined(sigDuration, sfreq, components) return signal * sigAmplitudeNam * 1e-9
# ################################################################################################### # Simulation settings fs = 1000 n_seconds = 5 # Define simulation components components = {'sim_synaptic_current' : {'n_neurons':1000, 'firing_rate':2, 't_ker':1.0, 'tau_r':0.002, 'tau_d':0.02}, 'sim_bursty_oscillation' : {'freq' : 10, 'prob_enter_burst' : .2, 'prob_leave_burst' : .2}} # Simulate a signal with a bursty oscillation with an aperiodic component & a time vector sig = sim_combined(n_seconds, fs, components) times = create_times(n_seconds, fs) ################################################################################################### # Plot the simulated data plot_time_series(times, sig, 'Simulated EEG') ################################################################################################### # # In the simulated signal above, we can see some bursty 10 Hz oscillations. # ################################################################################################### # Dual Amplitude Threshold Algorithm # ----------------------------------
# Set the frequency in our simulated signal freq = 6 # Set up simulation for a signal with aperiodic activity and an oscillation components = { 'sim_powerlaw': { 'exponent': exp }, 'sim_oscillation': { 'freq': 6 } } variances = [0.1, 1] # Simulate our signal sig = sim_combined(n_seconds, fs, components, variances) ################################################################################################### # Define a frequency range to filter the data f_range = (4, 8) # Bandpass filter the data, across the band of interest sig_filt = filter_signal(sig, fs, 'bandpass', f_range) ################################################################################################### # Plot filtered signal plot_time_series(times, [sig, sig_filt], ['Raw', 'Filtered']) ###################################################################################################
# by a period of only aperiodic activity. # ################################################################################################### # Set time and sampling rate n_seconds_burst = 1 n_seconds_noise = 2 fs = 1000 # Create a times vector times = create_times(n_seconds_burst + n_seconds_noise, fs) # Simulate a signal component with an oscillation components = {'sim_powerlaw': {'exponent': 0}, 'sim_oscillation': {'freq': 10}} s1 = sim_combined(n_seconds_burst, fs, components, [0.1, 1]) # Simulate a signal component with just noise s2 = sim_powerlaw(n_seconds_noise, fs, 0, variance=0.1) # Join signals together to approximate a 'burst' sig = np.append(s1, s2) ################################################################################################### # Plot example signal plot_time_series(times, sig) ################################################################################################### # Compute lagged coherence for an alpha oscillation # -------------------------------------------------
'sim_synaptic_current': { 'n_neurons': 1000, 'firing_rate': 2, 't_ker': 1.0, 'tau_r': 0.002, 'tau_d': 0.02 }, 'sim_oscillation': { 'freq': 8 } } ################################################################################################### # Simulate an oscillation over an aperiodic component signal = sim.sim_combined(n_seconds, fs, components) ################################################################################################### # Plot the simulated data, in the time domain plot_time_series(times, signal) ################################################################################################### # Plot the simulated data, in the frequency domain freqs, psd = spectral.compute_spectrum(signal, fs) plot_power_spectra(freqs, psd) ################################################################################################### # # We can switch out any components that we want, for example trading the stationary oscillation
import matplotlib.pyplot as plt from neurodsp.sim import sim_combined from neurodsp.utils import create_times from neurodsp.filt import filter_signal from neurodsp.timefrequency import amp_by_time, phase_by_time from neurodsp.plts import plot_time_series, plot_instantaneous_measure # 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])
exp = -1 # Define settings for creating the simulated signal comps = { 'sim_powerlaw': { 'exponent': exp, 'f_range': (2, None) }, 'sim_bursty_oscillation': { 'freq': freq } } comp_vars = [0.25, 1] # Simulate a signal with bursty oscillations at 20 Hz sig = sim_combined(n_seconds, fs, comps, comp_vars) times = create_times(n_seconds, fs) ################################################################################################### # Plot a segment of our simulated time series plot_time_series(times, sig, xlim=[0, 2]) ################################################################################################### # Compute Wavelet Transform # ------------------------- # # Now, let's use the compute Morlet wavelet transform algorithm to compute a # time-frequency representation of our simulated data, using Morlet wavelets. # # To apply the continuous Morlet wavelet transform, we need to specify frequencies of
'sim_powerlaw': dict(exponent=-2), 'sim_bursty_oscillation': dict(cycle='asine', rdsym=rdsym_task) } sigs_rest = np.zeros((n_channels, n_epochs, n_seconds * fs)) sigs_task = np.zeros((n_channels, n_epochs, n_seconds * fs)) freqs = np.linspace(5, 45, 5) for ch_idx, freq in zip(range(n_channels), freqs): sim_components_rest['sim_bursty_oscillation']['freq'] = freq sim_components_task['sim_bursty_oscillation']['freq'] = freq for ep_idx in range(n_epochs): sigs_task[ch_idx][ep_idx] = sim_combined( n_seconds, fs, components=sim_components_task) sigs_rest[ch_idx][ep_idx] = sim_combined( n_seconds, fs, components=sim_components_rest) #################################################################################################### # Compute features with an higher than default period consistency threshold. # This allows for more accurate estimates of burst frequency. thresholds = dict(amp_fraction_threshold=0., amp_consistency_threshold=.5, period_consistency_threshold=.9, monotonicity_threshold=.6, min_n_cycles=3) compute_kwargs = {'burst_method': 'cycles', 'threshold_kwargs': thresholds}
# with and without a rhythm. # ################################################################################################### # Simulate a signal component with an oscillation components = { 'sim_oscillation': { 'freq': freq }, 'sim_powerlaw': { 'exponent': exp, 'f_range': (1, None) } } s1 = sim_combined(t_osc, fs, components, [1, 0.5]) # Simulate a signal component with only aperiodic activity s2 = sim_powerlaw(t_ap, fs, exp, variance=0.5) # Join signals together to approximate a 'burst' sig = np.append(s1, s2) ################################################################################################### # Plot example signal plot_time_series(times, sig) ################################################################################################### # Compute lagged coherence on simulated data # ------------------------------------------
# ################################################################################################### # Define component of a combined signal: an oscillation and an aperiodic component components = { 'sim_oscillation': { 'freq': 10 }, 'sim_powerlaw': { 'exponent': -1 } } # Generate a combined signal combined_sig = sim.sim_combined(n_seconds, s_rate, components) ################################################################################################### # Plot the combined time series plot_time_series(times, combined_sig) ################################################################################################### # Compute the power spectrum of the combined signal freqs, powers = compute_spectrum_welch(combined_sig, s_rate) ################################################################################################### # Visualize the power spectrum of the combined signal plot_power_spectra(freqs, powers)
sim.set_random_seed(0) # Set some general settings, to be used across all simulations # sampling rate fs = 500 # n_seconds will be 60 seconds * n minutes of data n_minutes = 35 n_seconds = 60 * n_minutes times = create_times(n_seconds, fs) # make 1/f slope range between 0 and -2, with steps of -0.1 oof_range = np.arange(start=0, stop=2.1, step=0.1, dtype=float) * -1 oof_range[0] = 0 # some settings for where to save the data, filename stem, and peaks for oscillations resultdir = "/Users/mlombardo/Dropbox/Manuscripts/AIMS_Hurst_Sex/github_repo/data/gao_model" # loop over range of 1/f slopes for oof in oof_range: # simulate a time-series with only 1/f slope and no oscillations components = {'sim_powerlaw': {'exponent': oof}} # Simulate a combined signal with multiple oscillations sim_ts = sim.sim_combined(n_seconds, fs, components) # Save simulated time-series to a file np.savetxt(fname="%s/sim_neuralts_sig_slope%0.2f_oof.txt" % (resultdir, oof), X=sim_ts)