File: test_snr.py

package info (click to toggle)
python-mne 0.19.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 100,440 kB
  • sloc: python: 120,243; pascal: 1,861; makefile: 225; sh: 15
file content (43 lines) | stat: -rw-r--r-- 1,419 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
# Authors: Eric Larson <larson.eric.d@gmail.com>
#          Matti Hamalainen <msh@nmr.mgh.harvard.edu>
#
# License: BSD (3-clause)

import os
from os import path as op
import numpy as np
from numpy.testing import assert_allclose

from mne import read_evokeds
from mne.datasets import testing
from mne.minimum_norm import read_inverse_operator, estimate_snr

from mne.utils import _TempDir, requires_mne, run_subprocess

s_path = op.join(testing.data_path(download=False), 'MEG', 'sample')
fname_inv = op.join(s_path, 'sample_audvis_trunc-meg-eeg-oct-6-meg-inv.fif')
fname_evoked = op.join(s_path, 'sample_audvis-ave.fif')


@testing.requires_testing_data
@requires_mne
def test_snr():
    """Test SNR calculation."""
    tempdir = _TempDir()
    inv = read_inverse_operator(fname_inv)
    evoked = read_evokeds(fname_evoked, baseline=(None, 0))[0]
    snr = estimate_snr(evoked, inv)[0]
    orig_dir = os.getcwd()
    os.chdir(tempdir)
    try:
        cmd = ['mne_compute_mne', '--inv', fname_inv, '--meas', fname_evoked,
               '--snronly', '--bmin', '-200', '--bmax', '0']
        run_subprocess(cmd)
    except Exception:
        pass  # this returns 1 for some reason
    finally:
        os.chdir(orig_dir)
    times, snr_c, _ = np.loadtxt(op.join(tempdir, 'SNR')).T
    assert_allclose(times / 1000., evoked.times, atol=1e-2)
    assert_allclose(snr, snr_c, atol=1e-2, rtol=1e-2)