Repository: mne-tools/mne-python

Issue:
Raw meas info saved incorrectly
There is something very wrong, when I modify the measurement info and save the raw file, it keeps the old value for some reason: 

```
import sys
import os
import numpy as np
import mne
from mne.datasets import sample
mne.set_log_level('WARNING')

data_path = sample.data_path()
raw = mne.fiff.Raw(data_path + '/MEG/sample/sample_audvis_raw.fif')
raw.info['dev_head_t']['trans'] = np.eye(4)
t1 =  raw.info['dev_head_t']['trans']
print t1
fname = '/tmp/raw.fif'
if os.path.exists(fname):
    os.remove(fname)
raw.save(fname, tmax=1.0)
raw2 = mne.fiff.Raw(fname)
t2 =  raw2.info['dev_head_t']['trans']
print t2
```

Output:

```
[[ 1.  0.  0.  0.]
[ 0.  1.  0.  0.]
[ 0.  0.  1.  0.]
[ 0.  0.  0.  1.]]
[[  9.91419971e-01  -3.99363637e-02  -1.24467291e-01  -6.12930907e-03]
[  6.06611520e-02   9.84011710e-01   1.67456239e-01   6.36056066e-05]
[  1.15789659e-01  -1.73569739e-01   9.77991164e-01   6.47415221e-02]
[  0.00000000e+00   0.00000000e+00   0.00000000e+00   1.00000000e+00]]
```

I used `git bisect` to track the first occurrence of this bug to bc28c1822f5beeaf19219dadf345fc1e21373380. However, I can't figure out what's wrong. The script I used to track down the bug is here: https://gist.github.com/mluessi/9693766. Then used

```
git checkout master
git bisect start
git bisect bad
git bisect good d474ff321ff87aed578cbbd8a922c61ff6286a68
git bisect run ./bisect_raw.py
```

Any ideas? Can anyone else reproduce this?



Discussion:
Is there a conceptually redundant inv_trans entry in that dict? I can't
remember, but maybe that is being used when saving.

Our maybe it's in info, head_dev_t? I seem to remember it being redundant
somewhere, but I can't check right now.

Actually, based in the bisect error it might happen for some files and not
others. There are certain fields we don't read and instead store in
StringIO. That string io might be overwriting it.
On Mar 21, 2014 12:57 PM, "Eric Larson" larson.eric.d@gmail.com wrote:

> Our maybe it's in info, head_dev_t? I seem to remember it being redundant
> somewhere, but I can't check right now.

outch... we should fix this. I am going on holidays next week so I doubt
I'll have the time to dig into it.

I can reproduce it locally ....

It's definitely the BytesIO and the orig_blocks.
With this line added to the script there is no bug.

`raw.info['orig_blocks'] = None`

And I can confirm that the bug occurs only with certain datasets 


Resolving patch:
diff --git a/mne/fiff/meas_info.py b/mne/fiff/meas_info.py
--- a/mne/fiff/meas_info.py
+++ b/mne/fiff/meas_info.py
@@ -289,13 +289,14 @@ def read_meas_info(fid, tree, verbose=None):
                 if kind == FIFF.FIFF_COORD_TRANS:
                     tag = read_tag(fid, pos)
                     cand = tag.data
-                    if cand['from'] == FIFF.FIFFV_COORD_DEVICE and \
-                                cand['to'] == FIFF.FIFFV_COORD_HEAD:
+                    if (cand['from'] == FIFF.FIFFV_COORD_DEVICE and
+                        cand['to'] == FIFF.FIFFV_COORD_HEAD and
+                        dev_head_t is None):
                         dev_head_t = cand
-                    elif cand['from'] == FIFF.FIFFV_MNE_COORD_CTF_HEAD and \
-                                cand['to'] == FIFF.FIFFV_COORD_HEAD:
+                    elif (cand['from'] == FIFF.FIFFV_MNE_COORD_CTF_HEAD and
+                          cand['to'] == FIFF.FIFFV_COORD_HEAD and
+                          ctf_head_t is None):
                         ctf_head_t = cand
-
     #   Locate the Polhemus data
     isotrak = dir_tree_find(meas_info, FIFF.FIFFB_ISOTRAK)
     dig = None


Test patch:
diff --git a/mne/fiff/tests/test_meas_info.py b/mne/fiff/tests/test_meas_info.py
--- a/mne/fiff/tests/test_meas_info.py
+++ b/mne/fiff/tests/test_meas_info.py
@@ -1,6 +1,7 @@
 import os.path as op
 
 from nose.tools import assert_true, assert_equal, assert_raises
+import numpy as np
 from numpy.testing import assert_array_equal
 
 from mne import fiff, Epochs, read_events
@@ -71,6 +72,12 @@ def test_read_write_info():
     """
     info = fiff.read_info(raw_fname)
     temp_file = op.join(tempdir, 'info.fif')
+    # check for bug `#1198`
+    info['dev_head_t']['trans'] = np.eye(4)
+    t1 =  info['dev_head_t']['trans']
     fiff.write_info(temp_file, info)
     info2 = fiff.read_info(temp_file)
+    t2 = info2['dev_head_t']['trans']
     assert_true(len(info['chs']) == len(info2['chs']))
+    assert_array_equal(t1, t2)
+