You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, after calling set_eeg_reference() with a custom channel name, or invoking add_reference_channels() (which calls the former after adding all-zeros channels), to my knowledge it's impossible to retrieve which reference channels are being used in the current referencing scheme. The only thing that gets set in info is 'custom_ref_applied'.
importos.pathasopimportmnedata_path=mne.datasets.testing.data_path()
examples_dir=op.join(data_path, 'Brainvision')
vhdr_file=op.join(examples_dir, 'Analyzer_nV_Export.vhdr')
raw=mne.io.read_raw(vhdr_file, preload=True)
raw.set_eeg_reference(['F8'])
# do fancy stuff here# ...# Wait … WHICH reference did I apply again? Guess I'll have to find the channel# with all-zero data :(
Isn't there a way to keep track of which references were set?
Reacted by Stefan Appelhoff, Adam Li and Moritz Gerster
Maybe we can use this existing attribute, but instead of True/False we set it to the reference channel name(s)? I'd also include the average reference case here, which is currently also not stored anywhere.
I don't think we can simply write a different data type there…
At first I thought: Oh, cool, it's an int, so we simply write the index of the reference channel! But … obviously this wouldn't work for channels 0 to 2 ;) Also you wouldn't be able to specify more than just a single reference.
As for the avg reference:
It would be easy to add something like
FIFF.FIFFV_MNE_CUSTOM_REF_AVG=3
however this would break compatibility with existing code, as so far FIFF.FIFF_MNE_CUSTOM_REF has been set to 0 (i.e., False) when an average reference had been applied.
Yeah, that's the old FIFF format compatibility story which is really holding us back. If we can't change info["custom_ref_applied"], what about adding info["reference"]?
Reacted by Stefan Appelhoff and Richard Höchenberger
Yeah, that's the old FIFF format compatibility story which is really holding us back. If we can't change info["custom_ref_applied"], what about adding info["reference"]?
Can one "simply" add new entries to the FIFF files? I wasn't aware of that!
I was also thinking about abusing the new per-channel Annotations somehow… But this is really not my area of expertise. Let's wait for @larsoner to chime in here :)
No, we can't simply add new FIFF fields, but I thought that exporting to FIFF doesn't simply dump the whole Info object, but instead picks out the relevant fields. If this really works like this, we can add a new field which will be ignored. This field should be a string or list of strings, so any future references are automatically supported.
No, we can't simply add new FIFF fields, but I thought that exporting to FIFF doesn't simply dump the whole Info object, but instead picks out the relevant fields.
Yes, that seems to be the case
If this really works like this, we can add a new field which will be ignored.
👌 You mean ignored when read through software that doesn't know about this field, right?
I meant ignored by MNE when writing to FIFF - so this field shouldn't be written. However, it would be even better if we wrote that field and programs would just ignore it when reading if they don't know about it. But let's wait for @larsoner, he'll know for sure.
But then you'd lose the info during an I/O roundtrip, which is super confusing IMHO…
I know, but FIFF was never designed to hold all that information. If we can easily add optional fields, then everything is fine. Maybe we should think about a new simple format such as pickling raw?
If this really works like this, we can add a new field which will be ignored.
But then you'd lose the info during an I/O roundtrip, which is super confusing IMHO…
Agreed, Info should only hold things that will survive I/O round trip. If we need to store more information there we should improve the spec.
Maybe we should think about a new simple format such as pickling raw?
I don't think we plan to jump to a new format anytime soon and even if we do, we need to make FIF work as well as possible so I don't see it really helping with the problem.
We can add things to info the right way -- i.e., by enhancing the fiff-constants spec -- but we need to justify it and do the work of coming up with an appropriate spec.
I agree it would be nice to store what each channel's set of references is more flexibly. For example in sEEG if you have a ton if bipolar-reference pairs, it would be nice to store properly these references and be able to recover them. This case is technically possible now because you can store one ref location in info['chs']['loc'][3:6]. However, our system currently only works if each channel only has a single reference, or all channels use an average reference.
Is there a format that stores such arbitrary reference information well? We could look to that for guidance.
Just to give one proposal -- we recently spec'ed out and added support for an enhanced channel information block, mostly to get us past the 15-char limit for FIF. To make a concrete proposal, then, we could add a new tag there like FIFF_CH_REFS, which is a float32 ndarray of shape (N, 4) containing the x/y/z location (first 3 cols) and weight (last col) for the N references used for the channel. For standard Neuromag data and standard one-ref EEG acquisitions, this would just be (ref_x, ref_y, ref_z, 1.). If you did an average reference using a direct application instead of a projector, this would be all channel locations with a value of 1 / N in the last column. For non-EEG channels it's empty.
Thanks for the clarification, @larsoner! If you think the np.allclose() approach would work to construct location <> channel mappings, I'm +1 for your proposal!
REST is weird as it depends on passing a forward etc. We could think about adding a new constant for it or something but this seems like a far less critical case than standard channel-based references
Actually on second thought IIRC REST is also known as an infinity reference, so we can signal it's being used even in main just by setting locs[3:6] to np.inf, and we can also do this in the system I proposed. So no need for a chance just for that
What do you think, how should we go about moving forward here? Clearly I'm lacking a big deal of expertise, but if you think I could somehow contribute, please do let me know!
using locations to know how channels were referenced should not be the primary source of information. I anticipate that we have EEG data without channel locations but we some channel based references. See channel names you see in EEG world (eg saying that it's Cz-M1 referenced to M1). This suggests that the channel block idea (which I like) should contain a list of channels and not locations). Note that it means that it may contains channel names that don't exist in inst.ch_names or info['chs"] and it contains no data...
Note that it means that it may contains channel names that don't exist in inst.ch_names
This is exactly the case for data acquired by Neuromag systems currently for example. So I think just keeping track of channel names won't work for systems that do actually encode physical locations, just like you think keeping track based on locations won't work for systems that don't.
What if we keep track of name+location of each reference? This would just mean one more tag with is a list of names to match the list of locations.
What if we keep track of name+location of each reference? This would just mean one more tag with is a list of names to match the list of locations.
Sounds good to me. @agramfort's comment also reminded me of EEG recordings we'd done a few years back with reference electrodes placed on the tip of the nose. These would not have any proper location associated with them.
Whatever you decide is best to represent the reference internally, I would really love to see a public API that simply gives me the reference as a string (e.g. "C3","average", "REST", ...) or a list of strings (e.g. ["M1", "M2"]). Many EEG data sets do not specify the reference, so in that case we could simply set this to "unknown".
Come to think of it, maybe we shouldn't even use "average", because we could use a list of all channel names instead. This would be more explicit and would avoid ambiguities such as whether or not the original reference channel was added back to the data before the average reference was computed. If references consisting of multiple channels that are weighed differently are a thing, we could also store a list of weights in addition (which by default consists of all 1/N elements, where N is the number of reference electrodes).
but to me you will always have names even if they are ad-hoc names like EEG001,EEG002, etc.
It sounds like you're pushing to store only the names? To me this is still problematic because it's lossy. If we eventually want to do anything having to do with the physical arrangement of the recording setup (e.g., forward modeling), we need the locations of the references, which might not be proper channels (in the sense of having an entry in info['chs']) in the dataset, hence they could be missing/gone.
We have some recordings with just channels and no locations. We have other recordings with (at least) a single ref location and no channel name (Neuromag). And in theory it's not hard to imagine a recording with just reference locations stored in the dataset somehow (e.g., a recording with two EEG data channels, Cz and FCz, each of which was recorded referenced to the average of left and right mastoids), and in practice it's easy to get this after some picking/dropping.
To me I don't see a downside to storing names+locations rather than just names. In terms of implementation it's not much harder: if we set an EEG location based on a channel name (set_montage is really the only place we do this I think?) then we need to check the EEG reference names and set those locations, too.
Activity
cbrnr commentedon Feb 26, 2021
Maybe we can use this existing attribute, but instead of
True/Falsewe set it to the reference channel name(s)? I'd also include the average reference case here, which is currently also not stored anywhere.hoechenberger commentedon Feb 26, 2021
Currently, the following values are supported:
(CSD – current source density)
The correct value is then stored at the following location
In any case, when writing the measurement info, we do the following:
mne-python/mne/io/meas_info.py
Lines 1681 to 1682 in 8e3bcef
I don't think we can simply write a different data type there…
At first I thought: Oh, cool, it's an int, so we simply write the index of the reference channel! But … obviously this wouldn't work for channels 0 to 2 ;) Also you wouldn't be able to specify more than just a single reference.
As for the avg reference:
It would be easy to add something like
however this would break compatibility with existing code, as so far
FIFF.FIFF_MNE_CUSTOM_REFhas been set to0(i.e.,False) when an average reference had been applied.hoechenberger commentedon Feb 26, 2021
(heads-up, I've edited my above post several times)
cbrnr commentedon Feb 26, 2021
Yeah, that's the old FIFF format compatibility story which is really holding us back. If we can't change
info["custom_ref_applied"], what about addinginfo["reference"]?hoechenberger commentedon Feb 26, 2021
Can one "simply" add new entries to the FIFF files? I wasn't aware of that!
I was also thinking about abusing the new per-channel Annotations somehow… But this is really not my area of expertise. Let's wait for @larsoner to chime in here :)
hoechenberger commentedon Feb 26, 2021
Also we'd want to support the REST ref as well. And maybe future references which haven't been invented yet…
cbrnr commentedon Feb 26, 2021
No, we can't simply add new FIFF fields, but I thought that exporting to FIFF doesn't simply dump the whole
Infoobject, but instead picks out the relevant fields. If this really works like this, we can add a new field which will be ignored. This field should be a string or list of strings, so any future references are automatically supported.hoechenberger commentedon Feb 26, 2021
Yes, that seems to be the case
👌 You mean ignored when read through software that doesn't know about this field, right?
cbrnr commentedon Feb 26, 2021
I meant ignored by MNE when writing to FIFF - so this field shouldn't be written. However, it would be even better if we wrote that field and programs would just ignore it when reading if they don't know about it. But let's wait for @larsoner, he'll know for sure.
hoechenberger commentedon Feb 26, 2021
But then you'd lose the info during an I/O roundtrip, which is super confusing IMHO…
Let's wait for @larsoner ;)
cbrnr commentedon Feb 26, 2021
I know, but FIFF was never designed to hold all that information. If we can easily add optional fields, then everything is fine. Maybe we should think about a new simple format such as pickling raw?
hoechenberger commentedon Feb 26, 2021
I do love pickles (Spreewald 🥳) but I don't like pickled data :) It's not really portable and not suitable for long-term storage.
I'd actually prefer something like a simple JSON sidecar file or so…
cbrnr commentedon Feb 26, 2021
I don't like the pickle format either, but two files are also asking for trouble. Maybe we should just store as .mat (I'm only half-joking here)...
larsoner commentedon Feb 26, 2021
Agreed,
Infoshould only hold things that will survive I/O round trip. If we need to store more information there we should improve the spec.I don't think we plan to jump to a new format anytime soon and even if we do, we need to make FIF work as well as possible so I don't see it really helping with the problem.
We can add things to info the right way -- i.e., by enhancing the fiff-constants spec -- but we need to justify it and do the work of coming up with an appropriate spec.
I agree it would be nice to store what each channel's set of references is more flexibly. For example in sEEG if you have a ton if bipolar-reference pairs, it would be nice to store properly these references and be able to recover them. This case is technically possible now because you can store one ref location in
info['chs']['loc'][3:6]. However, our system currently only works if each channel only has a single reference, or all channels use an average reference.Is there a format that stores such arbitrary reference information well? We could look to that for guidance.
Just to give one proposal -- we recently spec'ed out and added support for an enhanced channel information block, mostly to get us past the 15-char limit for FIF. To make a concrete proposal, then, we could add a new tag there like
FIFF_CH_REFS, which is a float32 ndarray of shape(N, 4)containing the x/y/z location (first 3 cols) and weight (last col) for theNreferences used for the channel. For standard Neuromag data and standard one-ref EEG acquisitions, this would just be(ref_x, ref_y, ref_z, 1.). If you did an average reference using a direct application instead of a projector, this would be all channel locations with a value of1 / Nin the last column. For non-EEG channels it's empty.3 remaining items
hoechenberger commentedon Feb 26, 2021
Thanks for the clarification, @larsoner! If you think the
np.allclose()approach would work to construct location <> channel mappings, I'm +1 for your proposal!hoechenberger commentedon Feb 27, 2021
How would we encode the REST referencing scheme?
larsoner commentedon Feb 27, 2021
REST is weird as it depends on passing a forward etc. We could think about adding a new constant for it or something but this seems like a far less critical case than standard channel-based references
hoechenberger commentedon Feb 27, 2021
Yep. But a way to store a name – simply the string "REST" – could make sense, don't you think?
larsoner commentedon Feb 27, 2021
Actually on second thought IIRC REST is also known as an infinity reference, so we can signal it's being used even in main just by setting locs[3:6] to np.inf, and we can also do this in the system I proposed. So no need for a chance just for that
hoechenberger commentedon Feb 27, 2021
@larsoner Good idea!
What do you think, how should we go about moving forward here? Clearly I'm lacking a big deal of expertise, but if you think I could somehow contribute, please do let me know!
agramfort commentedon Feb 28, 2021
using locations to know how channels were referenced should not be the primary source of information. I anticipate that we have EEG data without channel locations but we some channel based references. See channel names you see in EEG world (eg saying that it's Cz-M1 referenced to M1). This suggests that the channel block idea (which I like) should contain a list of channels and not locations). Note that it means that it may contains channel names that don't exist in inst.ch_names or info['chs"] and it contains no data...
larsoner commentedon Feb 28, 2021
This is exactly the case for data acquired by Neuromag systems currently for example. So I think just keeping track of channel names won't work for systems that do actually encode physical locations, just like you think keeping track based on locations won't work for systems that don't.
What if we keep track of name+location of each reference? This would just mean one more tag with is a list of names to match the list of locations.
hoechenberger commentedon Feb 28, 2021
Sounds good to me. @agramfort's comment also reminded me of EEG recordings we'd done a few years back with reference electrodes placed on the tip of the nose. These would not have any proper location associated with them.
agramfort commentedon Mar 1, 2021
hoechenberger commentedon Mar 1, 2021
Yep!
cbrnr commentedon Mar 1, 2021
Whatever you decide is best to represent the reference internally, I would really love to see a public API that simply gives me the reference as a string (e.g.
"C3","average","REST", ...) or a list of strings (e.g.["M1", "M2"]). Many EEG data sets do not specify the reference, so in that case we could simply set this to"unknown".Come to think of it, maybe we shouldn't even use
"average", because we could use a list of all channel names instead. This would be more explicit and would avoid ambiguities such as whether or not the original reference channel was added back to the data before the average reference was computed. If references consisting of multiple channels that are weighed differently are a thing, we could also store a list of weights in addition (which by default consists of all 1/N elements, where N is the number of reference electrodes).larsoner commentedon Mar 1, 2021
It sounds like you're pushing to store only the names? To me this is still problematic because it's lossy. If we eventually want to do anything having to do with the physical arrangement of the recording setup (e.g., forward modeling), we need the locations of the references, which might not be proper channels (in the sense of having an entry in
info['chs']) in the dataset, hence they could be missing/gone.We have some recordings with just channels and no locations. We have other recordings with (at least) a single ref location and no channel name (Neuromag). And in theory it's not hard to imagine a recording with just reference locations stored in the dataset somehow (e.g., a recording with two EEG data channels, Cz and FCz, each of which was recorded referenced to the average of left and right mastoids), and in practice it's easy to get this after some picking/dropping.
To me I don't see a downside to storing names+locations rather than just names. In terms of implementation it's not much harder: if we set an EEG location based on a channel name (
set_montageis really the only place we do this I think?) then we need to check the EEG reference names and set those locations, too.agramfort commentedon Mar 2, 2021
hoechenberger commentedon Mar 2, 2021
I like that!
It's almost like eating the cake and having it too! 🍰