# \[mne-python\] strange behavior of raw.save()

**URL:** <https://mne.discourse.group/t/mne-python-strange-behavior-of-raw-save/484>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [September 8, 2012, 8:04pm UTC](https://mne.discourse.group/t/mne-python-strange-behavior-of-raw-save/484 "2012-09-08T20:04:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [September 8, 2012, 8:04pm UTC](https://mne.discourse.group/t/mne-python-strange-behavior-of-raw-save/484/1 "2012-09-08T20:04:53Z")

</div>

Hi all,

on saving a raw file using the instance method save() I get the  
following error message.

[http://pastebin.com/g66H4svV](http://pastebin.com/g66H4svV)

After that the file is not readable anymore ( the same error message  
will appear as Raw.\_\_init\_\_() is called. )

If I use an alternative file name this does not happen.

a) what is the reason for that / what does it actually tell, and

b) shouldn't that rather throw an exception than messing up the original file?

Cheers,  
Denis

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [September 8, 2012, 9:39pm UTC](https://mne.discourse.group/t/mne-python-strange-behavior-of-raw-save/484/2 "2012-09-08T21:39:08Z")

</div>

Hi Denis,

Are you by any chance saving the raw file to the same file from where  
you read it? I.e., something like

raw = Raw('test\_raw.fif')  
..  
raw.save('test\_raw.fif')

Unless you use preload=True, this won't work since raw.save() will read  
from the file and at the same time overwrite it. We should add a check  
such that it throws and exception with a explanatory error message if  
one tries to do this.

Martin

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [September 8, 2012, 9:47pm UTC](https://mne.discourse.group/t/mne-python-strange-behavior-of-raw-save/484/3 "2012-09-08T21:47:27Z")

</div>

Thanks Martin,

that must be it.  
I actually I was testing a new command line tool of mine for mass riff  
export / augmentation / conversion  
And in one of the use cases I simply wanted to change the info  
structure without loading the data.  
So this means I can either preload the data or work around it by e.g.  
iterative renaming.

To prevent users from tnings like that one could add something like

raw.save(fname)

(...)

if fname == self.raw.info['filename'] and not self.preload:  
&nbsp;&nbsp;&nbsp;&nbsp;raise ValueError("Invalid operation "  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Either load data or use alternative file name")

Wdyt?

Cheers,  
Denis

2012/9/8 Martin Luessi \<mluessi at nmr.mgh.harvard.edu\>:

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [September 8, 2012, 10:01pm UTC](https://mne.discourse.group/t/mne-python-strange-behavior-of-raw-save/484/4 "2012-09-08T22:01:27Z")

</div>

Ok, I tried the following:

I added

if fname == self.info['filename'] and not hasattr(self,'\_data'):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise ValueError('Invalid operation. Either read data with preload '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'option or use alternative file name')

to Raw.save

Hower, in case the data is preloaded the file still breaks...

2012/9/8 Denis-Alexander Engemann \<d.engemann at [fz-juelich.de](http://fz-juelich.de)\>:

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [September 8, 2012, 10:03pm UTC](https://mne.discourse.group/t/mne-python-strange-behavior-of-raw-save/484/5 "2012-09-08T22:03:27Z")

</div>

Ok. self.\_preload

2012/9/9 Denis-Alexander Engemann \<d.engemann at [fz-juelich.de](http://fz-juelich.de)\>:

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [September 8, 2012, 10:23pm UTC](https://mne.discourse.group/t/mne-python-strange-behavior-of-raw-save/484/6 "2012-09-08T22:23:14Z")

</div>

Ok, self.\_data == True or self.\_preodaded == True does not make a difference.

Is it at all possible to write make a ras.save using the identical fid?  
Otherwise we should just add:

if fname == self.info['filename']:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise RuntimeError('Invalid operation. Use an alternative  
file name')

2012/9/9 Denis-Alexander Engemann \<d.engemann at [fz-juelich.de](http://fz-juelich.de)\>
