[med-svn] [python-mne] 275/353: ENH : simplify evoked data generate + some docstring updaes
Yaroslav Halchenko
debian at onerussian.com
Fri Nov 27 17:25:15 UTC 2015
This is an automated email from the git hooks/post-receive script.
yoh pushed a commit to tag 0.4
in repository python-mne.
commit 0793fb0e3628978517006ad6445d6a610835bd6c
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date: Mon Jul 16 16:50:10 2012 +0200
ENH : simplify evoked data generate + some docstring updaes
---
examples/plot_simulate_evoked_data.py | 18 +++-----
mne/simulation/__init__.py | 2 +-
mne/simulation/sim_evoked.py | 86 +++++++++++++++++++++++++++++------
3 files changed, 81 insertions(+), 25 deletions(-)
diff --git a/examples/plot_simulate_evoked_data.py b/examples/plot_simulate_evoked_data.py
index 788a12a..e68de68 100644
--- a/examples/plot_simulate_evoked_data.py
+++ b/examples/plot_simulate_evoked_data.py
@@ -14,12 +14,10 @@ import pylab as pl
import mne
from mne.fiff.pick import pick_types_evoked, pick_types_forward
-from mne.forward import apply_forward
from mne.datasets import sample
from mne.time_frequency import fir_filter_raw, morlet
from mne.viz import plot_evoked, plot_sparse_source_estimates
-from mne.simulation import generate_stc, generate_noise_evoked, \
- add_noise_evoked
+from mne.simulation import generate_stc, generate_evoked
###############################################################################
# Load real data as templates
@@ -37,7 +35,7 @@ cov_fname = data_path + '/MEG/sample/sample_audvis-cov.fif'
fwd = mne.read_forward_solution(fwd_fname, force_fixed=True, surf_ori=True)
fwd = pick_types_forward(fwd, meg=True, eeg=True, exclude=raw.info['bads'])
-noise_cov = mne.read_cov(cov_fname)
+cov = mne.read_cov(cov_fname)
evoked_template = mne.fiff.read_evoked(ave_fname, setno=0, baseline=None)
evoked_template = pick_types_evoked(evoked_template, meg=True, eeg=True,
@@ -65,16 +63,14 @@ stc_data *= 100 * 1e-9 # use nAm as unit
# time translation
stc_data[1] = np.roll(stc_data[1], 80)
-
stc = generate_stc(fwd, labels, stc_data, tmin, tstep, random_state=0)
-evoked = apply_forward(fwd, stc, evoked_template)
###############################################################################
-# Add noise
+# Generate noisy evoked data
picks = mne.fiff.pick_types(raw.info, meg=True)
fir_filter = fir_filter_raw(raw, order=5, picks=picks, tmin=60, tmax=180)
-noise = generate_noise_evoked(evoked, noise_cov, n_samples, fir_filter)
-evoked_noise = add_noise_evoked(evoked, noise, snr, times, tmin=0.0, tmax=0.2)
+evoked = generate_evoked(fwd, stc, evoked_template, cov, snr,
+ tmin=0.0, tmax=0.2, fir_filter=fir_filter)
###############################################################################
# Plot
@@ -82,7 +78,7 @@ plot_sparse_source_estimates(fwd['src'], stc, bgcolor=(1, 1, 1),
opacity=0.5, high_resolution=True)
pl.figure()
-pl.psd(evoked_noise.data[0])
+pl.psd(evoked.data[0])
pl.figure()
-plot_evoked(evoked_noise)
+plot_evoked(evoked)
diff --git a/mne/simulation/__init__.py b/mne/simulation/__init__.py
index 64fdc38..975658a 100644
--- a/mne/simulation/__init__.py
+++ b/mne/simulation/__init__.py
@@ -2,4 +2,4 @@
"""
from .sim_evoked import select_source_in_label, generate_stc, \
- generate_noise_evoked, add_noise_evoked
+ generate_evoked
diff --git a/mne/simulation/sim_evoked.py b/mne/simulation/sim_evoked.py
index 4924517..b4f9135 100644
--- a/mne/simulation/sim_evoked.py
+++ b/mne/simulation/sim_evoked.py
@@ -11,9 +11,49 @@ import copy
from ..fiff.pick import pick_channels_cov
from ..minimum_norm.inverse import _make_stc
from ..utils import check_random_state
+from ..forward import apply_forward
-def generate_noise_evoked(evoked, noise_cov, n_samples, fir_filter=None,
+def generate_evoked(fwd, stc, evoked, cov, snr=3, tmin=None, tmax=None,
+ fir_filter=None, random_state=None):
+ """Generate noisy evoked data
+
+ Parameters
+ ----------
+ fwd : dict
+ a forward solution
+ stc : SourceEstimate object
+ The source time courses
+ evoked : Evoked object
+ An instance of evoked used as template
+ cov : Covariance object
+ The noise covariance
+ snr : float
+ signal to noise ratio in dB. It corresponds to
+ 10 * log10( var(signal) / var(noise) )
+ tmin : float | None
+ start of time interval to estimate SNR. If None first time point
+ is used.
+ tmax : float
+ start of time interval to estimate SNR. If None last time point
+ is used.
+ fir_filter : None | array
+ FIR filter coefficients e.g. [1, -1, 0.2]
+ random_state : None | int | np.random.RandomState
+ To specify the random generator state.
+
+ Returns
+ -------
+ evoked : Evoked object
+ The simulated evoked data
+ """
+ evoked = apply_forward(fwd, stc, evoked)
+ noise = generate_noise_evoked(evoked, cov, fir_filter)
+ evoked_noise = add_noise_evoked(evoked, noise, snr, tmin=tmin, tmax=tmax)
+ return evoked_noise
+
+
+def generate_noise_evoked(evoked, noise_cov, fir_filter=None,
random_state=None):
"""Creates noise as a multivariate Gaussian
@@ -23,10 +63,8 @@ def generate_noise_evoked(evoked, noise_cov, n_samples, fir_filter=None,
----------
evoked : evoked object
an instance of evoked used as template
- noise_cov : cov object
- an instance of cov
- n_samples : int
- number of time samples to generate
+ cov : Covariance object
+ The noise covariance
fir_filter : None | array
FIR filter coefficients
random_state : None | int | np.random.RandomState
@@ -41,6 +79,7 @@ def generate_noise_evoked(evoked, noise_cov, n_samples, fir_filter=None,
noise_cov = pick_channels_cov(noise_cov, include=noise.info['ch_names'])
rng = check_random_state(random_state)
n_channels = np.zeros(noise.info['nchan'])
+ n_samples = evoked.data.shape[1]
noise.data = rng.multivariate_normal(n_channels, noise_cov.data,
n_samples).T
if fir_filter is not None:
@@ -48,22 +87,20 @@ def generate_noise_evoked(evoked, noise_cov, n_samples, fir_filter=None,
return noise
-def add_noise_evoked(evoked, noise, snr, times, tmin=None, tmax=None):
+def add_noise_evoked(evoked, noise, snr, tmin=None, tmax=None):
"""Adds noise to evoked object with specified SNR.
SNR is computed in the interval from tmin to tmax.
Parameters
----------
- evoked : evoked object
- an instance of evoked with signal
- noise : evoked object
- an instance of evoked with noise
+ evoked : Evoked object
+ An instance of evoked with signal
+ noise : Evoked object
+ An instance of evoked with noise
snr : float
signal to noise ratio in dB. It corresponds to
10 * log10( var(signal) / var(noise) )
- timesamples : array
- samples in seconds
tmin : float
start time before event
tmax : float
@@ -71,10 +108,11 @@ def add_noise_evoked(evoked, noise, snr, times, tmin=None, tmax=None):
Returns
-------
- evoked : evoked object
+ evoked_noise : Evoked object
An instance of evoked corrupted by noise
"""
evoked = copy.deepcopy(evoked)
+ times = evoked.times
if tmin is None:
tmin = np.min(times)
if tmax is None:
@@ -125,6 +163,28 @@ def select_source_in_label(fwd, label, random_state=None):
def generate_stc(fwd, labels, stc_data, tmin, tstep, random_state=0):
+ """Generate sources time courses from waveforms and labels
+
+ Parameters
+ ----------
+ fwd : dict
+ a forward solution
+ labels : list of dict
+ The labels
+ stc_data : array
+ The waveforms
+ tmin : float
+ The beginning of the timeseries
+ tstep : float
+ The time step (1 / sampling frequency)
+ random_state : None | int | np.random.RandomState
+ To specify the random generator state.
+
+ Returns
+ -------
+ stc : SourceEstimate
+ The generated source time courses.
+ """
rng = check_random_state(random_state)
vertno = [[], []]
for label in labels:
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git
More information about the debian-med-commit
mailing list