Closed weiglszonja closed 2 years ago
Will take a look towards the end of the week!
Yeah I suspect it's defaulting to an array rather then list of lists which isn't supported in HDF.
Thanks @adam2392 for looking into this :) I was thinking of a solution to just flatten the list lists, which could be done here:
What do you think about this?
if faverage:
# for each band we return the frequencies that were averaged
freqs = [np.mean(x) for x in freqs_bands]
if all(isinstance(freq_band, Iterable) for freq_band in freqs_bands):
freqs_used = [freq for freq_band in freqs_bands for freq in freq_band]
else:
freqs_used = freqs_bands
Sure that seems reasonable to me! Wanna try it out and PR with an additional test case? I can help guide where to do it if needed!
I don't think there's a case where Freqs used would be more then a 2 layer nested list.
Sure, thank you! I'll give it a try :)
Hi @weiglszonja how're things going w/ this? Need any assistance? No rush! Just wanted to make sure I can answer any questions.
Hey, I was testing that solution I proposed (and then went on vacation), and realised when faverage=True
then freqs_bands
is always a list of list, so the if/else branching is not necessary. And what I was trying to do here, to flatten the list is list is actually identical to what is originally in freqs
, so instead of all that I proposed above, now I would only have this:
freqs_used = freqs
if faverage:
# for each band we return the frequencies that were averaged
freqs = [np.mean(x) for x in freqs_bands]
I managed to write a unittest, and test that file writing/reading succeeds, however this change will break other tests, and I'm not sure how to proceed.
I can open a PR so you can look at it.
Ah I see, so the underlying problem after inspection is that h5netcdf
doesn't support saving list of lists with unequal lengths. Without faverage
, freqs_used
is equivalent to freqs
, because no post-processing is done on the frequency level. With faverage
, this raises the issue.
The question then is what should be saved inside freqs_used
in the faverage
case, which is just a metadata to remember which frequencies were used to compute the spectral connectivity.
I'm inclined to say that we need to store perhaps the endpoints of each frequency band(?) unfortunately losing the information of which frequency points were used in the computation. This would fix your problem, and make things a bit more future-proof.
You can modify the epochs.py
lines to look like this:
freqs_used = freqs
if faverage:
# for each band we return the frequencies that were averaged
freqs = [np.mean(x) for x in freqs_bands]
# make sure freq_bands is a list of equal-length lists
# XXX: we lose information on which frequency points went into the
# computation. If h5netcdf supports numpy objects in the future, then
# we can change the min/max to just make it a list of lists.
freqs_used = freqs_bands
freqs_used = [[np.min(band), np.max(band)] for band in freqs_used]
Then there are a few lines in the test test_spectral_connectivity
that we can alter. I can help you here on what to modify.
@adam2392 I'm checking this test_spectral_connectivity
and I'm not sure how to fix when this test tries to manually average connectivity, but we don't know the frequency points anymore:
Hi @weiglszonja if you open a PR, I can take a look at your explicit code. Doesn't need to work completely yet :p.
Also, I realized the issue becomes more involved since the frequency lists are not all of equal length, please take my code in https://github.com/mne-tools/mne-connectivity/pull/91 and adapt it for your PR.
Ah, sorry @adam2392, I didn't see #91 before opening my PR... I can close it :) In our use-case both are working!
@weiglszonja well you, the user, deserve credit for identifying the issue! Okay glad to hear it is working.
Then in that case I will add you via a co-author commit and you to the changelog entry. Thanks!
Thank you for helping with this issue! 👍
Describe the bug
Computing spectral connectivity for frequency bands gives TypeError when trying to save the object.
Steps to reproduce
Expected results
save the file
Actual results
Additional information
This error is triggered when the
freqs_used
attribute (conn.attrs['freqs_used']
) is not a plain list, but as the example code snippet shows a list of arrays, e.g.Any help would be much appreciated! Thank you :)