Skip to content

Commit

Permalink
[FIX] collateral effects when scaling of data in _compute_covariance_…
Browse files Browse the repository at this point in the history
…auto (mne-tools#5698)
  • Loading branch information
DavidSabbagh authored and massich committed Nov 7, 2018
1 parent ae38e1b commit 8886f42
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ Changelog

Bug
~~~

- Fix: bug with scaling of data in func:`mne.cov._compute_covariance_auto` by `David Sabbagh `_

- Fix :func:`mne.io.Raw.plot_projs_topomap` by `Joan Massich`_

- Fix bug in :func:`mne.minimum_norm.compute_source_psd` where the ``stc.times`` output was scaled by 1000, by `Eric Larson`_
Expand Down Expand Up @@ -2980,3 +2983,5 @@ of commits):
.. _Rasmus Zetter: https://people.aalto.fi/rasmus.zetter

.. _Marcin Koculak: https://github.com/mkoculak

.. _David Sabbagh: https://github.com/DavidSabbagh
6 changes: 3 additions & 3 deletions examples/decoding/plot_decoding_spoc_CMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from sklearn.linear_model import Ridge
from sklearn.model_selection import KFold, cross_val_predict

# define parameters
# Define parameters
fname = data_path() + '/SubjectCMC.ds'
raw = mne.io.read_raw_ctf(fname)
raw.crop(50., 250.).load_data() # crop for memory purposes
Expand All @@ -45,7 +45,7 @@
emg = raw.copy().pick_channels(['EMGlft'])
emg.filter(20., None, fir_design='firwin')

# Filter MEG data to focus on alpha band
# Filter MEG data to focus on beta band
raw.pick_types(meg=True, ref_meg=True, eeg=False, eog=False)
raw.filter(15., 30., fir_design='firwin')

Expand All @@ -70,7 +70,7 @@
# Run cross validaton
y_preds = cross_val_predict(clf, X, y, cv=cv)

# plot the True EMG power and the EMG power predicted from MEG data
# Plot the True EMG power and the EMG power predicted from MEG data
fig, ax = plt.subplots(1, 1, figsize=[10, 4])
times = raw.times[meg_epochs.events[:, 0] - raw.first_samp]
ax.plot(times, y_preds, color='b', label='Predicted EMG')
Expand Down
6 changes: 4 additions & 2 deletions mne/cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ def _compute_covariance_auto(data, method, info, method_params, cv,
logliks = [None]

# undo scaling
_undo_scaling_array(data.T, picks_list=picks_list, scalings=scalings)
for c in estimator_cov_info:
_undo_scaling_cov(c[1], picks_list, scalings)

Expand All @@ -997,8 +998,9 @@ def _compute_covariance_auto(data, method, info, method_params, cv,
cov_methods = [c.__name__ if callable(c) else c for c in method]
runtime_infos, covs = list(runtime_infos), list(covs)
my_zip = zip(cov_methods, runtime_infos, logliks, covs, estimators)
for this_method, runtime_info, loglik, data, est in my_zip:
out[this_method] = {'loglik': loglik, 'data': data, 'estimator': est}
for this_method, runtime_info, loglik, cov_data, est in my_zip:
out[this_method] = {
'loglik': loglik, 'data': cov_data, 'estimator': est}
if runtime_info is not None:
out[this_method].update(runtime_info)

Expand Down
7 changes: 6 additions & 1 deletion mne/tests/test_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from mne.cov import (regularize, whiten_evoked, _estimate_rank_meeg_cov,
_auto_low_rank_model, _apply_scaling_cov,
_undo_scaling_cov, prepare_noise_cov, compute_whitener,
_apply_scaling_array, _undo_scaling_array)
_apply_scaling_array, _undo_scaling_array,
_regularized_covariance)

from mne import (read_cov, write_cov, Epochs, merge_events,
find_events, compute_raw_covariance,
Expand Down Expand Up @@ -496,6 +497,10 @@ def test_cov_scaling():
_undo_scaling_array(data, picks_list, scalings=scalings)
assert_allclose(data, evoked.data, atol=1e-20)

# check that input data remain unchanged. gh-5698
_regularized_covariance(data)
assert_array_almost_equal(data, evoked.data)


@requires_version('sklearn', '0.15')
def test_auto_low_rank():
Expand Down

0 comments on commit 8886f42

Please sign in to comment.