def test_write_fig_spindles(self): """Test function write_fig_spindles.""" sf, n_pts = 100., 10014 hypno = np.repeat(np.arange(6), 1669) - 1. sig = np.squeeze(generate_eeg(sf=sf, n_pts=n_pts, random_state=1)[0]) file = self.to_tmp_dir('write_fig_spindles.png') file_hyp = self.to_tmp_dir('write_fig_spindles_hypno.png') write_fig_spindles(sig, sf, file=file, thr=.1) write_fig_spindles(sig, sf, hypno=hypno, file=file_hyp, thr=.1)
sc = SceneObj(bgcolor='black', size=(1400, 1000)) ############################################################################### # Generate random data ############################################################################### # Here we generate a random 3D dataset with 5 channels and 4 trials per channel # Note that here we geneate a 3D dataset but you can also pass a 1D or 2D # dataset sf = 512. # sampling frequency n_pts = 1000 # number of time points n_channels = 5 # number of channels n_trials = 4 # number of trials data_3d = generate_eeg(n_pts=n_pts, n_channels=n_channels, n_trials=n_trials, f_max=20., smooth=200, noise=1000)[0] channels = ['Channel %i' % (k + 1) for k in range(n_channels)] trials = ['Trial %i' % k for k in range(n_trials)] # Generate the titles of each signal title = ['%s - %s' % (k, i) for k, i in product(channels, trials)] # Plot the data as a grid and add it to the scene g_obj_grid = GridSignalsObj('3d', data_3d, title=title, plt_as='grid') sc.add_to_subplot(g_obj_grid, title='Grid of 3D data') ############################################################################### # Plot MNE-Python data ###############################################################################
================================ Import annotations from a text file and annotate trials. All annotations are added to the Annotations/ tab. Then, select a row of the table to jump to it. Download an example of annotation file : https://drive.google.com/file/d/0B6vtJiCQZUBvMG95RHNXbDEwaGs/view?usp=sharing .. image:: ../../_static/examples/ex_annotations.png """ from visbrain.gui import Signal from visbrain.utils import generate_eeg sf = 512. # sampling frequency n_pts = 4000 # number of time points n_trials = 125 # number of trials in the dataset """Generate a random EEG vector of shape (n_trials, n_pts). This time, we smooth signals and decrease the noise on it. """ data, _ = generate_eeg(sf=sf, n_pts=n_pts, n_trials=n_trials, smooth=200, noise=1000) """Specify the path to the annotation file : """ annotations = 'signal_annotations.txt' Signal(data, sf=sf, axis=-1, line_lw=2., annotations=annotations).show()
# Scene creation sc = SceneObj(bgcolor='black', size=(1400, 1000)) ############################################################################### # Generate random data ############################################################################### # Here we generate a random 3D dataset with 5 channels and 4 trials per channel # Note that here we geneate a 3D dataset but you can also pass a 1D or 2D # dataset sf = 512. # sampling frequency n_pts = 1000 # number of time points n_channels = 5 # number of channels n_trials = 4 # number of trials data_3d = generate_eeg(n_pts=n_pts, n_channels=n_channels, n_trials=n_trials, f_max=20., smooth=200, noise=1000)[0] channels = ['Channel %i' % (k + 1) for k in range(n_channels)] trials = ['Trial %i' % k for k in range(n_trials)] # Generate the titles of each signal title = ['%s - %s' % (k, i) for k, i in product(channels, trials)] # Plot the data as a grid and add it to the scene g_obj_grid = GridSignalsObj('3d', data_3d, title=title, plt_as='grid') sc.add_to_subplot(g_obj_grid, title='Grid of 3D data') ############################################################################### # Plot MNE-Python data ############################################################################### # The GridSignalsObj is also compatible with several MNE-Python instance (i.e
from visbrain.objects import TimeSeries3DObj, Picture3DObj, SceneObj from visbrain.utils import generate_eeg ############################################################################### # Define sample data and scene ############################################################################### # 3D time-series and pictures need to be attached to sources define in a 3D # space. # Define 5 sources xyz = np.array([[0, .5, 0], [1, -4, 3], [10, 2, 8], [1, 7, 12], [-4, 5, 6]]) n_sources = xyz.shape[0] # Define the time-series of those 5 sources ts_data = generate_eeg(sf=512., n_channels=n_sources, noise=5., smooth=5)[0] # Compute the spectrogram of the 5 time-series pic_data = [] for k in range(n_sources): pic_data += [spectrogram(ts_data[k, :], 512., nperseg=128, noverlap=10)[2]] pic_data = np.asarray(pic_data) clim = (.01 * pic_data.min(), .01 * pic_data.max()) # Scene definition sc = SceneObj() ############################################################################### # Basic plot ############################################################################### # Basic plot without further customizations
"""Test functions in detections.py.""" import numpy as np from visbrain.utils.sleep.detection import (kcdetect, spindlesdetect, remdetect, slowwavedetect, mtdetect, peakdetect) from visbrain.utils import generate_eeg """If tests continue to failed, one idea could be to save in a npz file the signal to test. """ sf, n_pts, _rnd = 100., 10014, 1 signal = np.squeeze(generate_eeg(sf=sf, n_pts=n_pts, random_state=_rnd)[0]) n_per_seg = int(n_pts / 6) wake = np.zeros((n_per_seg,)) n1 = np.ones((n_per_seg,)) n2 = np.full((n_per_seg,), 2) n3 = np.full((n_per_seg,), 3) rem = np.full((n_per_seg,), 4) art = np.full((n_per_seg,), -1) hypno = np.hstack((wake, n1, n2, n3, rem, art)) class TestDetections(object): """Test functions in detection.py.""" def test_kcdetect(self): """Test function kcdetect.""" kcdetect(signal, sf, .8, 1., hypno, True, 100, 200, .2, .6)
"""Test Signal module and related methods.""" from vispy.app.canvas import MouseEvent, KeyEvent from visbrain.gui import Signal from visbrain.utils import generate_eeg from visbrain.tests._tests_visbrain import _TestVisbrain # Generate random EEG dataset : sf = 512. data, time = generate_eeg(sf=sf, n_channels=3, n_trials=2) # ---------------- Kwargs ---------------- kwargs = {'xlabel': 'My xlabel', 'ylabel': 'My ylabel', 'title': 'My title', 'display_grid': True, 'color': 'darkgray', 'symbol': 'x', 'title_font_size': 20, 'axis_font_size': 18, 'tick_font_size': 8, 'axis_color': 'blue', 'bgcolor': 'white', 'enable_grid': True, 'display_signal': True, 'form': 'psd'} # ---------------- Application ---------------- sig = Signal(data, sf=sf, axis=-1, time=time) class TestSignal(_TestVisbrain): """Test signal.py.""" ########################################################################### # LABELS ########################################################################### def test_xlabel(self): """Test setting xlabel.""" # Text :
"""Test Signal module and related methods.""" from vispy.app.canvas import MouseEvent, KeyEvent from visbrain import Signal from visbrain.utils import generate_eeg from visbrain.tests._tests_visbrain import _TestVisbrain # Generate random EEG dataset : sf = 512. data, time = generate_eeg(sf=sf, n_channels=3, n_trials=2) # ---------------- Kwargs ---------------- kwargs = { 'xlabel': 'My xlabel', 'ylabel': 'My ylabel', 'title': 'My title', 'display_grid': True, 'color': 'darkgray', 'symbol': 'x', 'title_font_size': 20, 'axis_font_size': 18, 'tick_font_size': 8, 'axis_color': 'blue', 'bgcolor': 'white', 'enable_grid': True, 'display_signal': True, 'form': 'psd' } # ---------------- Application ---------------- sig = Signal(data, sf=sf, axis=-1, time=time)
Shortcuts --------- * Mouse wheel : to zoom over the canvas * Mouse press and hold : to move the center of the camera * Double click : insert an annotation under the mouse cursor * <delete> : reset the camera .. image:: ../../picture/picsignal/ex_1d_signal.png """ from visbrain import Signal from visbrain.utils import generate_eeg sf = 512. # sampling frequency n_pts = 4000 # number of time points """Generate a random EEG vector of shape (n_pts,). Also get the associated time vector with the same length as the data. """ data, time = generate_eeg(sf=sf, n_pts=n_pts) time += 8. # force the time vector to start at 8 seconds time *= 1000. # millisecond conversion """Add a label to the x-axis (xlabel), y-axis (ylabel) and a title """ xlabel = 'Time (ms)' ylabel = 'Amplitude (uV)' title = 'Plot of a 1-d signal' Signal(data, sf=sf, time=time, xlabel=xlabel, ylabel=ylabel, title=title).show()
--------- * Mouse wheel : to zoom over the canvas * Mouse press and hold : to move the center of the camera * Double click : insert an annotation under the mouse cursor * <delete> : reset the camera .. image:: ../../_static/examples/ex_1d_signal.png """ from visbrain.gui import Signal from visbrain.utils import generate_eeg sf = 512. # sampling frequency n_pts = 4000 # number of time points """Generate a random EEG vector of shape (n_pts,). Also get the associated time vector with the same length as the data. """ data, time = generate_eeg(sf=sf, n_pts=n_pts) time += 8. # force the time vector to start at 8 seconds time *= 1000. # millisecond conversion """Add a label to the x-axis (xlabel), y-axis (ylabel) and a title """ xlabel = 'Time (ms)' ylabel = 'Amplitude (uV)' title = 'Plot of a 1-d signal' Signal(data, sf=sf, time=time, xlabel=xlabel, ylabel=ylabel, title=title).show()
KW = dict(title_size=14., zoom=2) colormap = 'inferno' # load brain model verts = np.load('bnd4_pos1.npy') faces = np.load('bnd4_tri1.npy') verts *= 1000 verts, faces, normals = convert_meshdata(verts, faces, invert_normals=True) # generate random toy data n_channels = 40 sampling_frequency = 512 data, time = generate_eeg(n_channels=n_channels, sf=sampling_frequency) print(data.shape) NSources = 200 index = np.random.choice(verts.shape[0], n_channels, replace=False) xyz = verts[index] brain_obj = BrainObj('Custom', vertices=verts, faces=faces, normals=normals, translucent=False) source_object = SourceObj('iEEG', xyz, data=data[:, 0], cmap=colormap)
from scipy.signal import spectrogram from visbrain.objects import TimeSeries3DObj, Picture3DObj, SceneObj from visbrain.utils import generate_eeg ############################################################################### # Define sample data and scene ############################################################################### # 3D time-series and pictures need to be attached to sources define in a 3D # space. # Define 5 sources xyz = np.array([[0, .5, 0], [1, -4, 3], [10, 2, 8], [1, 7, 12], [-4, 5, 6]]) n_sources = xyz.shape[0] # Define the time-series of those 5 sources ts_data = generate_eeg(sf=512., n_channels=n_sources, noise=5., smooth=5)[0] # Compute the spectrogram of the 5 time-series pic_data = [] for k in range(n_sources): pic_data += [spectrogram(ts_data[k, :], 512., nperseg=128, noverlap=10)[2]] pic_data = np.asarray(pic_data) clim = (.01 * pic_data.min(), .01 * pic_data.max()) # Scene definition sc = SceneObj() ############################################################################### # Basic plot ############################################################################### # Basic plot without further customizations
.. image:: ../../_static/examples/ex_3d_signal.png """ from itertools import product from visbrain.gui import Signal from visbrain.utils import generate_eeg sf = 512. # sampling frequency n_pts = 4000 # number of time points n_channels = 20 # number of EEG channels n_trials = 10 # number of trials in the dataset """Generate a random EEG dataset of shape (n_channels, n_trials, n_pts). Also get the associated time vector with the same length as the data. """ data, time = generate_eeg(sf=sf, n_pts=n_pts, n_trials=n_trials, n_channels=n_channels, smooth=200, noise=1000) time += 8. # force the time vector to start at 8 seconds time *= 1000. # millisecond conversion """The data have a shape of (20, 10, 4000). Hence, the time dimension is defined as the last axis i.e `axis=2` """ axis = 2 # localization of the time axis """Add a label to the x-axis (xlabel), y-axis (ylabel) and a title """ xlabel = 'Time (ms)' ylabel = 'Amplitude (uV)' title = 'Plot of a 1-d signal' """Build the title to add to each time-series in the grid
* Press <delete> on both to reset the camera .. image:: ../../_static/examples/ex_2d_signal.png """ from visbrain.gui import Signal from visbrain.utils import generate_eeg sf = 512. # sampling frequency n_pts = 4000 # number of time points n_trials = 120 # number of trials in the dataset """Generate a random EEG dataset of shape (n_trials, n_pts). Also get the associated time vector with the same length as the data. """ data, time = generate_eeg(sf=sf, n_pts=n_pts, n_trials=n_trials, smooth=200, noise=100) time += 8. # force the time vector to start at 8 seconds time *= 1000. # millisecond conversion """The data have a shape of (125 channels, 4000 time points). In order to work, the program need to know that the time axis is not the first dimension. Hence, we use the `axis` parameter to specify that the time axis is the second one `axis=1` """ axis = 1 # localization of the time axis """Add a label to the x-axis (xlabel), y-axis (ylabel) and a title """ xlabel = 'Time (ms)' ylabel = 'Amplitude (uV)' title = 'Plot of a 1-d signal'
sc.add_to_subplot(BrainObj('B1'), use_this_cam=True, row=0, col=1) ############################################################################### # Sources ############################################################################### s_obj = SourceObj('FirstSources', xyz, data=data) s_obj.color_sources(data=data, cmap='Spectral_r') sc.add_to_subplot(s_obj, row=1, col=1, title='Sources') sc.add_to_subplot(BrainObj('B3'), use_this_cam=True, row=1, col=1) ############################################################################### # 3D Time-series ############################################################################### ts, _ = generate_eeg(n_pts=100, n_channels=xyz.shape[0]) select = np.zeros((xyz.shape[0],), dtype=bool) select[slice(0, 100, 10)] = True ts_obj = TimeSeries3DObj('TS3D', ts, xyz, select=select, color='pink', ts_amp=24.) sc.add_to_subplot(ts_obj, row=0, col=2, title='3D time series') sc.add_to_subplot(BrainObj('B2'), use_this_cam=True, row=0, col=2) ############################################################################### # 3D Pictures ############################################################################### pic = np.random.rand(xyz.shape[0], 20, 20) pic_obj = Picture3DObj('PIC', pic, xyz, select=select, pic_width=21., pic_height=21., cmap='viridis') sc.add_to_subplot(pic_obj, row=1, col=2, title='3D pictures')
============================================================================= Sources ============================================================================= """) s_obj = SourceObj('FirstSources', xyz, data=data) s_obj.color_sources(data=data, cmap='Spectral_r') sc.add_to_subplot(s_obj, row=1, col=1, title='Sources') sc.add_to_subplot(BrainObj('B3'), use_this_cam=True, row=1, col=1) print(""" # ============================================================================= # 3D Time-series # ============================================================================= """) ts, _ = generate_eeg(n_pts=100, n_channels=xyz.shape[0]) select = np.zeros((xyz.shape[0],), dtype=bool) select[slice(0, 100, 10)] = True ts_obj = TimeSeries3DObj('TS3D', ts, xyz, select=select, color='pink', ts_amp=24.) sc.add_to_subplot(ts_obj, row=0, col=2, title='3D time series') sc.add_to_subplot(BrainObj('B2'), use_this_cam=True, row=0, col=2) print(""" # ============================================================================= # 3D Pictures # ============================================================================= """) pic = np.random.rand(xyz.shape[0], 20, 20) pic_obj = Picture3DObj('PIC', pic, xyz, select=select, pic_width=21.,