fooof
fooof copied to clipboard
[ENH] - Add support for converting model results, including to DFs
Adds fooof.data.conversions that allows for converting model results into alternate forms, including DataFrames.
A key component for doing this is to is to organize how to to organize peaks. Currently, this implementation supports organizing a set number of peaks, or using band definitions.
Note that this is an alternate approach to some other ideas explored in #194
Example usage:
from fooof import FOOOFGroup, Bands
from fooof.sim import gen_group_power_spectra
freqs, pows = gen_group_power_spectra(2, [3, 40], [1, 1], [[10, 1, 1], [20, 1, 1]])
fg = FOOOFGroup(verbose=False)
fg.fit(freqs, pows)
# Convert to DF, extracting 2 peaks per model
fg.to_df(2)

# Convert to DF, extracting peaks with a specified band definition
bands = Bands({'alpha' : [8, 12], 'beta' : [15, 30]})
fg.to_df(bands)

👍 !!
Hello,
Thanks for making FOOOF available, great tool!!
I'm trying to use this dataframe conversion utility and I can't seem to get it to work. When I run the example usage code exactly as above I get the following error.:

I also can't seem to import or access documentation for fooof.data.conversions. Are there some simple import steps or documentation I am missing?
@matteuler the branch containing the code for a data frame conversion is not merged yet into main and released, you probably installed an older version of the package that doesn't contain the method for conversion, I would recommend waiting until the authors make the new release
@matteuler, you may get this feature early via cloning, checking out the df branch, and running pip install -e . from within the cloned directory. Otherwise, as @danieltomasz mentioned, you can wait until this PR is merged and released.
@danieltomasz @ryanhammonds Thanks very much for these responses! I was able to use "get_params' to get the aperiodic and fit info directly (ignoring peaks for the time being) which made converting to a dataframe easy via pandas utilities.
@matteuler you may really easily join peaks into dataframe with aperiodic and periodic parameters, 'temp_df` is the dataframe with aperiodic parameters
temp_df.insert(0, 'ID', temp_df.index)
peaks = fg.get_params('peak_params') # prepare peaks dataframe
peaks_df = pd.DataFrame(peaks)
peaks_df.columns = ['CF', 'PW', 'BW', 'ID']
peaks_df['ID'] = peaks_df['ID'].astype(int)
peaks_df = peaks_df.join(temp_df.set_index('ID'), on='ID')