mne-tools / mne-matlab

MNE scripting with Matlab
BSD 3-Clause "New" or "Revised" License
36 stars 36 forks source link

ENH - allow to read double precision data #24

Closed schoffelen closed 1 year ago

schoffelen commented 1 year ago

Hi all, I am working on more smooth fif-file writing in the FieldTrip fieldtrip2fiff function. I noticed that by default fif-files are written in single precision format, but that there's no principled objection against writing the numbers in double precision. At least I thought I read along those lines in the documentation of MNE-python. This PR is needed to allow FieldTrip to do a round trip (i.e. write a FieldTrip raw data structure as a fif-file, and read it back in without precision loss), since it is relying on mne-matlab for the reading and the writing.

I'd be happy to be given pointers to where additional code tweaks (for consistency) are also required (e.g. does fiff_start_writing_raw need to be extended with an optional precision input argument?), and to be pointed to required tests to be done before this PR is considered seriously.

britta-wstnr commented 1 year ago

@larsoner maybe?

larsoner commented 1 year ago

but that there's no principled objection against writing the numbers in double precision.

Well you have to be careful because any Neuromag/MEGIN/Elekta software (and possibly other commercial stuff) will probably not understand the double-precision data buffers. But MNE-Python will!

does fiff_start_writing_raw need to be extended with an optional precision input argument?

Based on what you had to modify for reading, plus following the chain in mne.io.Raw.save(..., fmt='double'):

Yes writing will probably need the same sort of triaging as you have already for reading

schoffelen commented 1 year ago

OK, thanks for the input. Then I think I will go ahead. I noticed that the MNE-python docs also mention the warning about non-MNE-P software having potential difficulties with handling double precision data files, but since the primary intention here is to not lose numeric precision while toggling back and forth between MNE-python and matlab, I think it's fine when I build in a well-placed warning about the precision somewhere (most likely in FT code)

schoffelen commented 1 year ago

@larsoner I think that this is good to go for the time being.

larsoner commented 1 year ago

Thanks @schoffelen !