fooof icon indicating copy to clipboard operation
fooof copied to clipboard

[ENH] - Add support for converting model results, including to DFs

Open TomDonoghue opened this issue 4 years ago • 6 comments

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)

Screen Shot 2021-02-23 at 1 57 14 PM

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

Screen Shot 2021-02-23 at 1 57 19 PM

TomDonoghue avatar Feb 23 '21 18:02 TomDonoghue

👍 !!

rdgao avatar Feb 24 '21 09:02 rdgao

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.:

image

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 avatar Jul 05 '21 20:07 matteuler

@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

danieltomasz avatar Jul 05 '21 22:07 danieltomasz

@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.

ryanhammonds avatar Jul 06 '21 19:07 ryanhammonds

@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 avatar Jul 06 '21 21:07 matteuler

@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')

danieltomasz avatar Jul 06 '21 21:07 danieltomasz