Repository navigation
Description
On behalf of Brain Electrophysiology Laboratory, we would like to add support for all different flavors of MFF. The mne.io.egi module can be used to read in raw MFF files, but we would like to be able to read MFF segmented files (analogous to Epochs class) and MFF averaged files (analogous to Evoked class). We would also like to be able to convert BaseRaw, Epochs, or Evoked objects to continuous, segmented, and averaged MFF files respectively and write these as output files. Finally, we would like to be able to read and store a wider range of event track information from MFF files. MFF event track XML files contain several attributes for each event, such as label, duration, and response, that are not currently captured when reading in raw MFF files.
In order to address these requests, we propose using mffpy, a Python MFF reader/writer created by BEL for handling MFF inputs and outputs. This Python package is capable of reading and writing each of the different flavors of MFF as well as parsing individual XML files within an MFF directory. Our general proposal would be to make mffpy a dependency of MNE and then restructure the mne.io.egi module to read in MFF files with mffpy and then convert them to the appropriate object (Raw, Epochs, or Evoked). We also want to add a module that takes Raw, Epochs, and Evoked objects and converts them to an mffpy Writer object so that they can be output as MFF files. As far as event tracks, we are able to fully parse event track XML files with mffpy. We may need to create a new type of MFF events class to store this information, as it appears that event info is currently stored in a simple array with relative start time and event type.
We have developers at BEL that would be happy to implement these features. We are also happy to answer any questions about mffpy and how it could increase the functionality of MNE.
Activity
ephathaway commentedon Jul 21, 2020
@jusjusjus
drammock commentedon Jul 21, 2020
Have you looked at the MNE-Python
Annotationsclass? https://mne.tools/dev/auto_tutorials/raw/plot_30_annotate_raw.htmlagramfort commentedon Jul 22, 2020
ephathaway commentedon Jul 22, 2020
I actually hadn't looked at the
Annotationsclass until now. It looks a little more descriptive than what we get from themne.find_eventsfunction. It seems like it would be easy enough to parse an event track XMLs with mffpy and then convert them to anAnnotationsobject. How feasible would it be to add additional attributes to theAnnotationsclass for other aspects of MFF events, such as sourceDevice, response time, trial #, etc.? Could we maybe make a subclass of theAnnotationsclass specifically forRawEGIobjects?ephathaway commentedon Jul 22, 2020
Good idea to pilot on the
EpochsandEvokedsince that will be new code.drammock commentedon Jul 22, 2020
That is certainly possible in principle... can you say a bit more about what kind of metadata is stored in MFF events? Is it the same for continuous, epoched, and averaged data? The
Epochs.metadataproperty is also worth considering here (it's a restricted version of apandas.DataFrame) though it's not currently implemented forRaworEvokedobjects... here's a primer on Epochs metadataephathaway commentedon Jul 23, 2020
Below is an example of a single event from an event track XML. This is a good example of an event generated by a stimulus presentation device at recording. I think the
beginTime,duration,code,label,description, andsourceDeviceare common to all MFF events and then the number ofkeyscan vary.There are event tracks in continuous, epoched, and averaged MFF files, although I am not sure how events are preserved in averaged files, as the time scale is not preserved.
The
Epochs.metadatalooks pretty versatile. Seems like it could be adapted to store event info for aRawobject.agramfort commentedon Jul 23, 2020
drammock commentedon Jul 23, 2020
So @agramfort suggests 2 possibilities:
convert Event Track XML into an Annotations object, where the
descriptionfield of each annotation is a somewhat unwieldy string where all metadata fields are concatenated, separated by/characters. This has the advantage that/-separated strings are already parsed by downstream epoch-related code, so when the annotations are converted to aneventsarray and correspondingevent_iddict used in epoching, it will be straightforward to sub-select resulting epochs based on metadata fields.convert Event Track XML into a pandas DataFrame, and accept that it will be a separate object from the
Rawdata object. If this approach is taken, there should probably at least be a function to create MNE-Python-styleAnnotationsand/or aneventsarray from that DataFrame. I'm assuming one row per event, with the columns being the union of all keys present in the file, with some suitable NA value (np.nan, empty string, etc) wherever a key is not defined for the event in that row?Personally I am open to a third option which is attaching a dataframe-like metadata object to
Raw, similar toEpochs.metadata. But then how to make it useful... a lot of what makes epochs metadata useful is that there is exactly one metadata row per epoch, but the hypotheticalRaw.metadatawould be one row per event. I think there is considerable work necessary to figure out the API and supported use case(s) here.ephathaway commentedon Jul 23, 2020
Converting the event track XML to a DataFrame seems like the most attractive option as far as easy access to all the event attributes. However, we do want to be able to create
Epochsobjects based on the events and certain subsets of events. I think ideally, the events used to createEpochswould then become the contents ofEpochs.metadataas a description of each epoch.What if we store the events in a DataFrame separate from the
Rawfile and then add functionality to theEpochsclass to be able to create epochs around events from a DataFrame?I think it will be easiest to first focus on implementing reading of segmented and averaged MFFs with mffpy to see how well this works. From there we can look at reading of continuous MFF files and decide how we want to handle events.
drammock commentedon Jul 23, 2020
Agreed.
That is indeed an elegant-sounding approach.
hmm... perhaps an API like
Epochs.from_dataframe(raw, dataframe, ...)? I think this would be cleaner than trying to make the existingEpochsconstructor flexible enough to take in either a DataFrame or an events array + event_id dict. You'll probably end up with (private?) helper functions similar to the ones I mentioned above (functions to create an events array and event_id dict from the MFF-derived dataframe). But we should hear input from others before proceeding down that line of approach.Sounds good.
agramfort commentedon Jul 24, 2020
ephathaway commentedon Jul 24, 2020
I like this idea from a usability standpoint. This way someone processing an MFF could keep their events in the DataFrame format throughout the workflow.
drammock commentedon Jul 24, 2020
This was my earlier suggestion @agramfort, but it would not allow the (really nice) feature of the events dataframe automatically being used to populate the
Epochs.metadata. However, as I implied above, theevents_from_dataframefunction will probably need to get written under the hood anyway to makeEpochs.from_dataframe()work, so it's actually a nice sequential approach to implementevents_from_dataframe()first, and then add onEpochs.from_dataframe()as a wrapper of the main constructor in a subsequent step.But before all that, it sounds like the first steps are to simply read the already-epoched and already-averaged data formats, and come back to the event parsing later.
agramfort commentedon Jul 24, 2020
9 remaining items
larsoner commentedon Nov 30, 2020
Sure, would be good!
But actually I think the issue you're encountering with
info['dig']not being populated is actually separable -- when we add the EEG electrode locations ininfo['chs'][ii]['loc']we should also add it todig. So I think there should be two PRs: the first to add dig information and test that it's there and reasonable, then a second separate one to usemffpy. It will require some effort that will end up being discarded (to get the EEG + maybe fids in our native code) but hopefully this is actually a small effort. And it gives us time to move to mffpy, and do so with more complete (dig) tests.@christianbrodbeck let me know which of the two PRs you're interested in working on. If it's just the mffpy bit, I can add the native-reader bits + tests first.
christianbrodbeck commentedon Nov 30, 2020
They're potentially more interrelated in that the fiducials are currently never read (as far as I could tell), and I suspect implementing that would be more straight forward with
mffpythan with the current approach. This is why I would have suggested translating tomffpyfirst. But I might be wrong.ephathaway commentedon Nov 30, 2020
@christianbrodbeck I am not exactly sure what these digitizer points and fiducials are, but I suspect this info is contained in the
coordinates.xmlin an .mff file.mffpyhas a class to parse these.christianbrodbeck commentedon Dec 1, 2020
Thanks @ephathaway , I saw this in
coordinates.xml, do you know whether we can rely on theidentifierentries corresponding to nasion/LPA/RPA?ephathaway commentedon Dec 1, 2020
I'm not sure. I believe the
coordinates.xmlfile is determined by the recording device. So any number of MFFs recorded with a "HydroCel GSN 256 1.0" will have identicalcoordinates.xmlfiles.christianbrodbeck commentedon Dec 1, 2020
test_egi.mffin the MNE testing dataset has different net and also has the same identifiers, so maybe we can assume they're the same?ephathaway commentedon Dec 11, 2020
@christianbrodbeck you can view all of the possible
coordinates.xmlfiles here. It looks like theidentifierfields for all of these match except for theGeodesic Sensor Net 64 2.0.xmlcoordinates file. This does seem odd that it would only be different for this one recording device type, especially given that there are other 64-channel devices.ephathaway commentedon May 7, 2021
I would like to add a function to write one or more
Evokedobjects to an MFF file. MFFs can be of the "averaged" flavor and may contain multiple averages of different categories. I think theEvokedclass will translate nicely to this sort of MFF. To accomplish this, we could make use ofmffpy.Writerclass, which I think will make this function pretty slick.The question is where to put it? I noticed the
EpochsandBaseRawclasses both have anexportmethod, which exports these data structures to EEGLab, and maybe other formats in the future? Although, a method like this may not work in this case, since we want to be able to write multipleEvokedobjects to a single output file. Could we stick awrite_evokeds_mfffunction inmne/io/egi/egimff.py?larsoner commentedon May 10, 2021
To stick to the
exporttheme, how aboutmne.export_evokeds? Currently the only option would be MFF but eventually things likeEEGLABcould be added for example.ephathaway commentedon May 10, 2021
Sounds good! I will work on a PR in the next couple of weeks.
ephathaway commentedon May 11, 2021
@larsoner to avoid having a lot of MFF specific code in
evoked.py, I am thinking something like:larsoner commentedon May 11, 2021
Sounds good. If you need to avoid a circular import problem you can nest the import
ephathaway commentedon May 14, 2021
@larsoner mind taking a look at mne-tools/mne-testing-data#87? I'll need this to properly test the export function.
larsoner commentedon May 14, 2021
https://github.com/mne-tools/mne-testing-data/releases/tag/0.120
ephathaway commentedon May 17, 2021
Great! Thanks