Repository navigation
Closed
Labels
Description
In reference.py we have the method perform_reference() which states the following:
# Phase 1: Estimate the true signal mean with robust referencing
self.robust_reference()
if self.noisy_channels["bad_all"]:
self.raw.info["bads"] = self.noisy_channels["bad_all"]
self.raw.interpolate_bads() <----------------------------------NOTICE THIS
self.reference_signal = (
np.nanmean(self.raw.get_data(picks=self.reference_channels), axis=0) * 1e6
)
rereferenced_index = [
self.ch_names_eeg.index(ch) for ch in self.rereferenced_channels
]
self.EEG = self.remove_reference(
self.EEG, self.reference_signal, rereferenced_index
)
Now in the original Matlab code this is equivalent to doRobustPost() inside performReference.m:
function [] = doRobustPost()
% Robust reference with interpolation afterwards
referenceOut = robustReference(signal, referenceOut);
noisy = referenceOut.badChannels.all;
if isempty(noisy) %No noisy channels -- ordinary ref
referenceOut.referenceSignal = ...
nanmean(signal.data(referenceOut.referenceChannels, :), 1);
else
sourceChannels = setdiff(referenceOut.evaluationChannels, noisy);
signalNew = interpolateChannels(signal, noisy, sourceChannels); <----- NOTICE THIS
referenceOut.referenceSignal = ...
nanmean(signalNew.data(referenceOut.referenceChannels, :), 1);
clear signalNew;
end
signal = removeReference(signal, referenceOut.referenceSignal, ...
referenceOut.rereferencedChannels);
referenceOut.noisyStatistics = ...
findNoisyChannels(removeTrend(signal, referenceOut), referenceOut);
So the basic difference is that robustReference produce a list of bad channels and this are interpolated in a dummy variable then averaged to produce a robust reference. In pyprep the interpolation is done on the raw object directly, not on a dummy copy of it.
I'm not so sure if this affects greatly or not but I would like your opinion on this.
If we wanted to make the code more alike the original one we would:
# Phase 1: Estimate the true signal mean with robust referencing
self.robust_reference()
if self.noisy_channels["bad_all"]:
dummy = self.raw.copy()
dummy.info["bads"] = self.noisy_channels["bad_all"]
dummy.interpolate_bads()
self.reference_signal = (
np.nanmean(dummy.get_data(picks=self.reference_channels), axis=0) * 1e6
)
del dummy
else:
self.reference_signal = (
np.nanmean(self.raw.get_data(picks=self.reference_channels), axis=0)
* 1e6
)
rereferenced_index = [
self.ch_names_eeg.index(ch) for ch in self.rereferenced_channels
]
self.EEG = self.remove_reference(
self.EEG, self.reference_signal, rereferenced_index
)
Activity
sappelhoff commentedon Apr 7, 2020
It seems to me that the only difference between the current pyprep implementation and the new implementation that you propose is:
self.rawhas all channels interpolated, while in the new implementation,self.rawis unchangeddummycopyso it is important what happens later in the code with
self.rawto know whether your new proposed implementation would have any effect at all 🤔yjmantilla commentedon Apr 7, 2020
Basically that self.raw will become the final signal of the prepPipeline. Overall after the first rereference (phase 1 in code, true signal mean):
In that regard, in the original prep the final signal is only interpolated once. In the 1st rereference it interpolates on a copy just to calculate the true signal mean using it.
Here is a copy of what follows exactly in our code:
yjmantilla commentedon Apr 7, 2020
Yeah but hypothetically if it is able to handle ransac (even in the channel wise implementation) then this will be negligible. And after all it would delete this new dummy signal once it calculates the true mean.
Regarding if the effect of this is high or not I do not know, but do take in mind the following:
In the current implementation we are actually **really ** interpolating more signals (as in affecting self.raw) than we say in self.interpolated_channels . This is because that variable only took into account the noisy_detector just before interpolation (and the self.unusable_channels flat and nan), not the noisy channels returned by robust_reference() (self.noisy_channels["bad_all"]),
sappelhoff commentedon Apr 8, 2020
okay, I see ... so it's really a deviation from MATLAB prep ... don't know if there was any reason for it to be implemented that way :-/
in that case I agree with you and we should change the behavior.
What makes me generally uncomfortable, that we have only few tests implemented in pyprep right now ... so every time we change something, there is a small chance that we break something without noticing :-/ but that's a separate issue.
reduced code redundancy, fixed sappelhoff#19 and another do_detrend p…
lesser ram ransac (#32)