def test_plot_windows(self): """Test function plot_windows.""" n_pts = 1000 times = np.linspace(-1, 1, n_pts, endpoint=True) kw = dict(verbose=False) ts = define_windows(times, slwin_len=.1, **kw)[0] plot_windows(times, ts)
def test_define_windows(self): """Test function define_windows.""" n_pts = 1000 times = np.linspace(-1, 1, n_pts, endpoint=True) kw = dict(verbose=False) # --------------------------------------------------------------------- # full window # --------------------------------------------------------------------- ts = define_windows(times, **kw)[0] np.testing.assert_array_equal(ts, np.array([[0, n_pts - 1]])) # --------------------------------------------------------------------- # custom windows # --------------------------------------------------------------------- # single window win = [0, .5] ts = define_windows(times, windows=win, **kw)[0] np.testing.assert_almost_equal(times[ts.ravel()], win, decimal=2) # multiple win = [[-.5, -.1], [-.1, 0.], [0, .5]] ts = define_windows(times, windows=win, **kw)[0] np.testing.assert_almost_equal(times[ts], np.array(win), decimal=2) # --------------------------------------------------------------------- # sliding windows # --------------------------------------------------------------------- # length only ts = define_windows(times, slwin_len=.1, **kw)[0] tts = times[ts] ttsd = np.diff(tts, axis=1) np.testing.assert_almost_equal(ttsd, np.full_like(ttsd, .1), decimal=2) # with starting point ts = define_windows(times, slwin_len=.1, slwin_start=.5, **kw)[0] np.testing.assert_almost_equal(times[ts][0, 0], .5, decimal=2) # with stoping point ts = define_windows(times, slwin_len=.1, slwin_stop=.9, **kw)[0] assert times[ts][-1, -1] <= .9 # with step between temporal windows ts = define_windows(times, slwin_len=.1, slwin_step=.2, **kw)[0] tts = times[ts] ttsd = np.diff(tts, axis=0) np.testing.assert_almost_equal(ttsd, np.full_like(ttsd, .2), decimal=2)
times = np.linspace(-1, 1.5, n_pts, endpoint=True) x = np.sin(2 * np.pi * (1. / period) * times) ############################################################################### # Manually define windows # ----------------------- # # The first feature we illustrate here, is to define windows manually which # means, based on the time vector, where each window should start and finish. plt.figure(figsize=(10, 8)) # by default, if you don't provide any input, the full time window is going to # be considered win = define_windows(times)[0] plt.subplot(311) plot_windows(times, win, x, title='No input = full time window') # however, you can also specify where each window start and finish. Here, we # define a simple unique window between [-.5, .5] win = define_windows(times, windows=[-.5, .5])[0] plt.subplot(312) plot_windows(times, win, x, title='Unique window [-.5, .5]') # but you can also specify multiple windows win = define_windows(times, windows=[[-.75, -.25], [.25, .75]])[0] plt.subplot(313) plot_windows(times, win, x, title='Multiple windows manually defined') plt.tight_layout() plt.show()
############################################################################### # Stimulus-specificity of the undirected connectivity # --------------------------------------------------- # # From the figure above, nodes X and Y present an activity that is modulated # according to the stimulus, but not Z. The next question we can ask is # whether the connectivity strength is also modulated by the stimulus. To this # end, we are going to compute the undirected Dynamic Functional Connectivity # (DFC) which is simply defined as the information shared between two nodes # inside sliding windows. Hence, here, the DFC is computed for each trial # inside consecutive windows # define the sliding windows slwin_len = .3 # 100ms window length slwin_step = .02 # 80ms between consecutive windows win_sample = define_windows(times, slwin_len=slwin_len, slwin_step=slwin_step)[0] # compute the DFC for each subject dfc = [] for n_s in range(n_subjects): _dfc = conn_dfc(x[n_s].data, win_sample, times=times, roi=roi, verbose=False) # reset trials dimension _dfc['trials'] = x[n_s]['trials'].data dfc += [_dfc] ############################################################################### # now we can plot the dfc by concatenating all of the subjects dfc_suj = xr.concat(dfc, 'trials').groupby('trials').mean('trials') dfc_suj.plot.line(x='times', col='roi', hue='trials')