-
Notifications
You must be signed in to change notification settings - Fork 105
[ENH] - Add support for converting model results, including to DFs #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a10cb48
add tresults & update check_dep
TomDonoghue 673adee
add converter funcs for data stores
TomDonoghue 6a7ca58
add tests for data conversions
TomDonoghue 7d00c36
alias conversion functions to model objects
TomDonoghue b8afe54
fix typo that was running wrong func in tests
TomDonoghue ec8c0e7
add pandas as a listed optional requirement
TomDonoghue 4b4caf6
Merge branch 'main' into df
TomDonoghue 5a835bd
fix merge
TomDonoghue 7ba9834
add pandas to readme list of optional dependencies
TomDonoghue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"""Conversion functions for organizing model results into alternate representations.""" | ||
|
||
import numpy as np | ||
|
||
from fooof import Bands | ||
from fooof.core.funcs import infer_ap_func | ||
from fooof.core.info import get_ap_indices, get_peak_indices | ||
from fooof.core.modutils import safe_import, check_dependency | ||
from fooof.analysis.periodic import get_band_peak | ||
|
||
pd = safe_import('pandas') | ||
|
||
################################################################################################### | ||
################################################################################################### | ||
|
||
def model_to_dict(fit_results, peak_org): | ||
"""Convert model fit results to a dictionary. | ||
|
||
Parameters | ||
---------- | ||
fit_results : FOOOFResults | ||
Results of a model fit. | ||
peak_org : int or Bands | ||
How to organize peaks. | ||
If int, extracts the first n peaks. | ||
If Bands, extracts peaks based on band definitions. | ||
|
||
Returns | ||
------- | ||
dict | ||
Model results organized into a dictionary. | ||
""" | ||
|
||
fr_dict = {} | ||
|
||
# aperiodic parameters | ||
for label, param in zip(get_ap_indices(infer_ap_func(fit_results.aperiodic_params)), | ||
fit_results.aperiodic_params): | ||
fr_dict[label] = param | ||
|
||
# periodic parameters | ||
peaks = fit_results.peak_params | ||
|
||
if isinstance(peak_org, int): | ||
|
||
if len(peaks) < peak_org: | ||
nans = [np.array([np.nan] * 3) for ind in range(peak_org-len(peaks))] | ||
peaks = np.vstack((peaks, nans)) | ||
|
||
for ind, peak in enumerate(peaks[:peak_org, :]): | ||
for pe_label, pe_param in zip(get_peak_indices(), peak): | ||
ryanhammonds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fr_dict[pe_label.lower() + '_' + str(ind)] = pe_param | ||
|
||
elif isinstance(peak_org, Bands): | ||
for band, f_range in peak_org: | ||
for label, param in zip(get_peak_indices(), get_band_peak(peaks, f_range)): | ||
fr_dict[band + '_' + label.lower()] = param | ||
|
||
# goodness-of-fit metrics | ||
fr_dict['error'] = fit_results.error | ||
fr_dict['r_squared'] = fit_results.r_squared | ||
|
||
return fr_dict | ||
|
||
@check_dependency(pd, 'pandas') | ||
def model_to_dataframe(fit_results, peak_org): | ||
"""Convert model fit results to a dataframe. | ||
|
||
Parameters | ||
---------- | ||
fit_results : FOOOFResults | ||
Results of a model fit. | ||
peak_org : int or Bands | ||
How to organize peaks. | ||
If int, extracts the first n peaks. | ||
If Bands, extracts peaks based on band definitions. | ||
|
||
Returns | ||
------- | ||
pd.Series | ||
Model results organized into a dataframe. | ||
""" | ||
|
||
return pd.Series(model_to_dict(fit_results, peak_org)) | ||
|
||
|
||
@check_dependency(pd, 'pandas') | ||
def group_to_dataframe(fit_results, peak_org): | ||
"""Convert a group of model fit results into a dataframe. | ||
|
||
Parameters | ||
---------- | ||
fit_results : list of FOOOFResults | ||
List of FOOOFResults objects. | ||
peak_org : int or Bands | ||
How to organize peaks. | ||
If int, extracts the first n peaks. | ||
If Bands, extracts peaks based on band definitions. | ||
|
||
Returns | ||
------- | ||
pd.DataFrame | ||
Model results organized into a dataframe. | ||
""" | ||
|
||
return pd.DataFrame([model_to_dataframe(f_res, peak_org) for f_res in fit_results]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Tests for the fooof.data.conversions.""" | ||
|
||
from copy import deepcopy | ||
|
||
import numpy as np | ||
|
||
from fooof.core.modutils import safe_import | ||
pd = safe_import('pandas') | ||
|
||
from fooof.data.conversions import * | ||
|
||
################################################################################################### | ||
################################################################################################### | ||
|
||
def test_model_to_dict(tresults, tbands): | ||
|
||
out = model_to_dict(tresults, peak_org=1) | ||
assert isinstance(out, dict) | ||
assert 'cf_0' in out | ||
assert out['cf_0'] == tresults.peak_params[0, 0] | ||
assert not 'cf_1' in out | ||
|
||
out = model_to_dict(tresults, peak_org=2) | ||
assert 'cf_0' in out | ||
assert 'cf_1' in out | ||
assert out['cf_1'] == tresults.peak_params[1, 0] | ||
|
||
out = model_to_dict(tresults, peak_org=3) | ||
assert 'cf_2' in out | ||
assert np.isnan(out['cf_2']) | ||
|
||
out = model_to_dict(tresults, peak_org=tbands) | ||
assert 'alpha_cf' in out | ||
|
||
def test_model_to_dataframe(tresults, tbands, skip_if_no_pandas): | ||
|
||
for peak_org in [1, 2, 3]: | ||
out = model_to_dataframe(tresults, peak_org=peak_org) | ||
assert isinstance(out, pd.Series) | ||
|
||
out = model_to_dataframe(tresults, peak_org=tbands) | ||
assert isinstance(out, pd.Series) | ||
|
||
def test_group_to_dataframe(tresults, tbands, skip_if_no_pandas): | ||
|
||
fit_results = [deepcopy(tresults), deepcopy(tresults), deepcopy(tresults)] | ||
|
||
for peak_org in [1, 2, 3]: | ||
out = group_to_dataframe(fit_results, peak_org=peak_org) | ||
assert isinstance(out, pd.DataFrame) | ||
|
||
out = group_to_dataframe(fit_results, peak_org=tbands) | ||
assert isinstance(out, pd.DataFrame) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
matplotlib | ||
tqdm | ||
tqdm | ||
pandas |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.