Repository: mne-tools/mne-python

Issue:
ENH: save cov method + loglik to disk 
ok @agramfort ?



Discussion:
Ok

> On 17 janv. 2015, at 18:45, "Denis A. Engemann" notifications@github.com wrote:
> 
> ok @agramfort ?
> 
> —
> Reply to this email directly or view it on GitHub.


Resolving patch:
diff --git a/mne/cov.py b/mne/cov.py
--- a/mne/cov.py
+++ b/mne/cov.py
@@ -26,7 +26,7 @@
 from .io.tag import find_tag
 from .io.tree import dir_tree_find
 from .io.write import (start_block, end_block, write_int, write_name_list,
-                       write_double, write_float_matrix)
+                       write_double, write_float_matrix, write_string)
 from .epochs import _is_good
 from .utils import (check_fname, logger, verbose, estimate_rank,
                     _compute_row_norms, requires_sklearn, check_sklearn_version )
@@ -1350,6 +1350,18 @@ def _read_cov(fid, node, cov_kind, verbose=None):
             else:
                 nfree = int(tag.data)
 
+            tag = find_tag(fid, this, FIFF.FIFF_MNE_COV_METHOD)
+            if tag is None:
+                method = None
+            else:
+                method = tag.data
+
+            tag = find_tag(fid, this, FIFF.FIFF_MNE_COV_SCORE)
+            if tag is None:
+                score = None
+            else:
+                score = tag.data[0]
+
             tag = find_tag(fid, this, FIFF.FIFF_MNE_ROW_NAMES)
             if tag is None:
                 names = []
@@ -1409,6 +1421,11 @@ def _read_cov(fid, node, cov_kind, verbose=None):
             cov = dict(kind=cov_kind, diag=diagmat, dim=dim, names=names,
                        data=data, projs=projs, bads=bads, nfree=nfree, eig=eig,
                        eigvec=eigvec)
+            if score is not None:
+                cov['loglik'] = score
+            if method is not None:
+                cov['method'] = method
+
             return cov
 
     logger.info('    Did not find the desired covariance matrix (kind = %d)'
@@ -1456,6 +1473,15 @@ def _write_cov(fid, cov):
         write_name_list(fid, FIFF.FIFF_MNE_CH_NAME_LIST, cov['bads'])
         end_block(fid, FIFF.FIFFB_MNE_BAD_CHANNELS)
 
+    # estimator method
+    if 'method' in cov:
+        write_string(fid, FIFF.FIFF_MNE_COV_METHOD, cov['method'])
+
+    # negative log-likelihood score
+    if 'loglik' in cov:
+        write_double(
+            fid, FIFF.FIFF_MNE_COV_SCORE, np.array(cov['loglik']))
+
     #   Done!
     end_block(fid, FIFF.FIFFB_MNE_COV)
 
diff --git a/mne/io/constants.py b/mne/io/constants.py
--- a/mne/io/constants.py
+++ b/mne/io/constants.py
@@ -445,6 +445,9 @@ def __setattr__(self, attr, val):
 FIFF.FIFF_MNE_COV_EIGENVALUES        = 3534  # Eigenvalues and eigenvectors of the above
 FIFF.FIFF_MNE_COV_EIGENVECTORS       = 3535
 FIFF.FIFF_MNE_COV_NFREE              = 3536  # Number of degrees of freedom
+FIFF.FIFF_MNE_COV_METHOD             = 3537  # The estimator used
+FIFF.FIFF_MNE_COV_SCORE              = 3538  # Negative log-likelihood
+
 #
 # 3540... Inverse operator
 #


Test patch:
diff --git a/mne/tests/test_cov.py b/mne/tests/test_cov.py
--- a/mne/tests/test_cov.py
+++ b/mne/tests/test_cov.py
@@ -56,9 +56,13 @@ def test_io_cov():
     """
     tempdir = _TempDir()
     cov = read_cov(cov_fname)
+    cov['method'] = 'empirical'
+    cov['loglik'] = -np.inf
     cov.save(op.join(tempdir, 'test-cov.fif'))
     cov2 = read_cov(op.join(tempdir, 'test-cov.fif'))
     assert_array_almost_equal(cov.data, cov2.data)
+    assert_equal(cov['method'], cov2['method'])
+    assert_equal(cov['loglik'], cov2['loglik'])
 
     cov2 = read_cov(cov_gz_fname)
     assert_array_almost_equal(cov.data, cov2.data)