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 8dc00424edc7a8d004d7d6f48ae9a41eb51b6d0d Author: Martin Luessi <[email protected]> Date: Mon Jul 16 14:39:55 2012 -0400 evoked in separate file --- mne/simulation/__init__.py | 4 +- mne/simulation/evoked.py | 127 +++++++++++++++++++++++++++++++++++++++++++ mne/simulation/source.py | 132 +++------------------------------------------ 3 files changed, 138 insertions(+), 125 deletions(-) diff --git a/mne/simulation/__init__.py b/mne/simulation/__init__.py index e0d887b..70c5eba 100644 --- a/mne/simulation/__init__.py +++ b/mne/simulation/__init__.py @@ -1,6 +1,8 @@ """Data simulation code """ +from .evoked import generate_evoked + from .source import select_source_in_label, generate_sparse_stc, \ - generate_evoked, circular_source_labels + circular_source_labels diff --git a/mne/simulation/evoked.py b/mne/simulation/evoked.py new file mode 100644 index 0000000..cdecb32 --- /dev/null +++ b/mne/simulation/evoked.py @@ -0,0 +1,127 @@ +# Authors: Alexandre Gramfort <[email protected]> +# Martin Luessi <[email protected]> +# Matti Hamalainen <[email protected]> +# +# License: BSD (3-clause) +import copy + +import numpy as np +from scipy import signal + +from ..fiff.pick import pick_channels_cov +from ..utils import check_random_state +from ..forward import apply_forward + + +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 + + The spatial covariance of the noise is given from the cov matrix. + + Parameters + ---------- + evoked : evoked object + an instance of evoked used as template + cov : Covariance object + The noise covariance + fir_filter : None | array + FIR filter coefficients + random_state : None | int | np.random.RandomState + To specify the random generator state. + + Returns + ------- + noise : evoked object + an instance of evoked + """ + noise = copy.deepcopy(evoked) + 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: + noise.data = signal.lfilter([1], fir_filter, noise.data, axis=-1) + return noise + + +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 + snr : float + signal to noise ratio in dB. It corresponds to + 10 * log10( var(signal) / var(noise) ) + tmin : float + start time before event + tmax : float + end time after event + + Returns + ------- + 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: + tmax = np.max(times) + tmask = (times >= tmin) & (times <= tmax) + tmp = np.mean((evoked.data[:, tmask] ** 2).ravel()) / \ + np.mean((noise.data ** 2).ravel()) + tmp = 10 * np.log10(tmp) + noise.data = 10 ** ((tmp - float(snr)) / 20) * noise.data + evoked.data += noise.data + return evoked + + diff --git a/mne/simulation/source.py b/mne/simulation/source.py index de416b4..f0d1b9c 100644 --- a/mne/simulation/source.py +++ b/mne/simulation/source.py @@ -4,132 +4,16 @@ # # License: BSD (3-clause) import os -import copy import numpy as np -from scipy import signal from scipy.sparse import csr_matrix -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 from ..surface import read_surface from ..source_estimate import mesh_edges -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 - - The spatial covariance of the noise is given from the cov matrix. - - Parameters - ---------- - evoked : evoked object - an instance of evoked used as template - cov : Covariance object - The noise covariance - fir_filter : None | array - FIR filter coefficients - random_state : None | int | np.random.RandomState - To specify the random generator state. - - Returns - ------- - noise : evoked object - an instance of evoked - """ - noise = copy.deepcopy(evoked) - 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: - noise.data = signal.lfilter([1], fir_filter, noise.data, axis=-1) - return noise - - -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 - snr : float - signal to noise ratio in dB. It corresponds to - 10 * log10( var(signal) / var(noise) ) - tmin : float - start time before event - tmax : float - end time after event - - Returns - ------- - 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: - tmax = np.max(times) - tmask = (times >= tmin) & (times <= tmax) - tmp = np.mean((evoked.data[:, tmask] ** 2).ravel()) / \ - np.mean((noise.data ** 2).ravel()) - tmp = 10 * np.log10(tmp) - noise.data = 10 ** ((tmp - float(snr)) / 20) * noise.data - evoked.data += noise.data - return evoked - - def select_source_in_label(fwd, label, random_state=None): """Select source positions using a label @@ -278,15 +162,15 @@ def _verts_within_dist(graph, source, max_dist): def circular_source_labels(subject, seeds, extents, hemis, subjects_dir=None): """ Generate circular labels in source space - This function generates a number of labels in source space by growing regions - starting from the vertices defined in "seeds". For each seed, a label is generated - containing all vertices within a maximum geodesic distance on the white matter - surface from the seed. + This function generates a number of labels in source space by growing + regions starting from the vertices defined in "seeds". For each seed, a + label is generated containing all vertices within a maximum geodesic + distance on the white matter surface from the seed. - Note: "extents" and "hemis" can either be arrays with the same length as seeds, - which allows using a different extent and hemisphere for each label, or - integers, in which case the same extent and hemisphere is used for each - label. + Note: "extents" and "hemis" can either be arrays with the same length as + seeds, which allows using a different extent and hemisphere for each + label, or integers, in which case the same extent and hemisphere is + used for each label. Parameters: ---------- -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git _______________________________________________ debian-med-commit mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/debian-med-commit
