.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/datasets/ndvar-creating.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_datasets_ndvar-creating.py: .. _exa-generate-ndvar: Creating NDVars =============== .. currentmodule:: eelbrain Shows how to initialize an :class:`NDVar` with the structure of EEG data from (randomly generate) data arrays. The data is intended for illustrating EEG analysis techniques and meant to vaguely resemble data from an N400 experiment, but it is not meant to be a physiologically realistic simulation. .. contents:: Contents :local: .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python # sphinx_gallery_thumbnail_number = 3 import numpy as np import scipy.spatial from eelbrain import * .. GENERATED FROM PYTHON SOURCE LINES 26-34 NDVars from arrays ------------------ An :class:`NDVar` combines an n-dimensional :class:`numpy.ndarray` with :class:`Dimension` objects that describe what the different data axes mean, and provide meta information that is used, for example, for plotting. Here we start by create a Sensor dimension from a built-in EEG montage (a montage pairs sensor names with spatial locations on the head surface): .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python sensor = Sensor.from_montage('standard_alphabetic') p = plot.SensorMap(sensor) .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_001.png :alt: ndvar creating :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-43 The dimension also contains information about the adjacency of its elements (i.e., specifying which elements are adjacent), which is used, for example, for cluster-based analysis. This information is imported automatically from :mod:`mne` when available; otherwise it can be defined manually when creating the sensor object, or based on pairwise sensor distance, as here: .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python sensor.set_adjacency(connect_dist=1.66) p = plot.SensorMap(sensor, adjacency=True) .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_002.png :alt: ndvar creating :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 47-51 Using information from the :class:`Sensor` description about sensor coordinates, we can now generate an N400-like topography. After associating the data array with the Sensor description by creating an :class:`NDVar`, the topography can be plotted without any further information: .. GENERATED FROM PYTHON SOURCE LINES 51-59 .. code-block:: Python i_cz = sensor.names.index('Cz') cz_loc = sensor.locations[i_cz] dists = scipy.spatial.distance.cdist([cz_loc], sensor.locations)[0] dists /= dists.max() topo = -0.7 + dists n400_topo = NDVar(topo, sensor) p = plot.Topomap(n400_topo, clip='circle') .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_003.png :alt: ndvar creating :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-64 A time axis is specified using a :class:`UTS` ("uniform time series") object. As with the topography, the UTS object allows the :class:`NDVar` to automatically format the time axis of a figure. Here we create a simple time series based on a Gaussian: .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: Python window_data = scipy.signal.windows.gaussian(200, 12)[:140] time = UTS(tmin=-0.100, tstep=0.005, nsamples=140) n400_timecourse = NDVar(window_data, time) p = plot.UTS(n400_timecourse) .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_004.png :alt: ndvar creating :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-75 Combining NDVars ---------------- More complex NDVars can often be created by combining simpler NDVars. As example data, we generate random values for an independent variable (consistent with simulating an N400 response, we call it "cloze probability") .. GENERATED FROM PYTHON SOURCE LINES 75-84 .. code-block:: Python rng = np.random.RandomState(0) n_trials = 100 cloze = np.concatenate([ rng.uniform(0, 0.3, n_trials // 2), rng.uniform(0.8, 1.0, n_trials // 2), ]) rng.shuffle(cloze) p = plot.Histogram(cloze) .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_005.png :alt: ndvar creating :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-92 Stacking NDVars --------------- A simple way of combining multiple NDVars is stacking them. Here we generate a separate topography for each cloze value, add some random noise, and then stack the resulting NDVars using :func:`combine`. The resulting stacked :class:`NDVar` has a :class:`Case` dimension reflecting the different cases (or trials): .. GENERATED FROM PYTHON SOURCE LINES 92-100 .. code-block:: Python ndvars = [] for cloze_i in cloze: topo_i = NDVar(topo * cloze_i + rng.normal(0, .5, len(topo)), sensor) ndvars.append(topo_i) topographies = combine(ndvars) topographies .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 101-102 The resulting NDvar can directly be used for a statistical test: .. GENERATED FROM PYTHON SOURCE LINES 102-107 .. code-block:: Python result = testnd.TTestOneSample(topographies) p = plot.Topomap(result, clip='circle') result .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_006.png :alt: ndvar creating :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ', samples=10000, p < .001> .. GENERATED FROM PYTHON SOURCE LINES 108-115 Casting NDVars -------------- Multi-dimensional NDVars can also be created through multiplication of NDVars with different dimensions. Here, we put all the dimensions together to simulate the EEG signal. On the first line, turn cloze into :class:`Var` to make clear that cloze represents a :class:`Case` dimension, i.e. different trials: .. GENERATED FROM PYTHON SOURCE LINES 115-132 .. code-block:: Python signal = Var(1 - cloze) * n400_timecourse * n400_topo # Add noise noise = powerlaw_noise(signal, 1) noise = noise.smooth('sensor', 0.02, 'gaussian') signal += noise # Apply the average mastoids reference signal -= signal.mean(sensor=['M1', 'M2']) # Store EEG data in a Dataset with trial information ds = Dataset({ 'eeg': signal, 'cloze': Var(cloze), 'predictability': Factor(cloze > 0.5, labels={True: 'high', False: 'low'}), }) .. GENERATED FROM PYTHON SOURCE LINES 133-134 Plot the average simulated response .. GENERATED FROM PYTHON SOURCE LINES 134-137 .. code-block:: Python p = plot.TopoButterfly('eeg', data=ds, vmax=1.5, clip='circle', frame='t', axh=3) p.set_time(0.400) .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_007.png :alt: ndvar creating :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-139 Plot averages separately for high and low cloze .. GENERATED FROM PYTHON SOURCE LINES 139-142 .. code-block:: Python p = plot.TopoButterfly('eeg', 'predictability', data=ds, vmax=1.5, clip='circle', frame='t', axh=3) p.set_time(0.400) .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_008.png :alt: ndvar creating :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 143-144 Average over time in the N400 time window .. GENERATED FROM PYTHON SOURCE LINES 144-146 .. code-block:: Python p = plot.Topomap('eeg.mean(time=(0.300, 0.500))', 'predictability', data=ds, vmax=1, clip='circle') .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_009.png :alt: high, low :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_009.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 147-148 Plot the first 20 trials, labeled with cloze propability .. GENERATED FROM PYTHON SOURCE LINES 148-150 .. code-block:: Python labels = [f'{i} ({c:.2f})' for i, c in enumerate(cloze[:20])] p = plot.Butterfly('eeg[:20]', '.case', data=ds, axtitle=labels) .. image-sg:: /auto_examples/datasets/images/sphx_glr_ndvar-creating_010.png :alt: 0 (0.25), 1 (0.97), 2 (0.92), 3 (0.29), 4 (0.16), 5 (0.13), 6 (0.01), 7 (0.85), 8 (0.91), 9 (0.87), 10 (0.82), 11 (0.06), 12 (0.82), 13 (0.18), 14 (0.02), 15 (0.11), 16 (0.20), 17 (0.83), 18 (0.08), 19 (0.93) :srcset: /auto_examples/datasets/images/sphx_glr_ndvar-creating_010.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_examples_datasets_ndvar-creating.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ndvar-creating.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ndvar-creating.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ndvar-creating.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_