Skip to content

Commit 7d00c36

Browse files
committed
alias conversion functions to model objects
1 parent 6a7ca58 commit 7d00c36

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

fooof/objs/fit.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
from fooof.utils.data import trim_spectrum
7979
from fooof.utils.params import compute_gauss_std
8080
from fooof.data import FOOOFResults, FOOOFSettings, FOOOFMetaData
81+
from fooof.data.conversions import model_to_dataframe
8182
from fooof.sim.gen import gen_freqs, gen_aperiodic, gen_periodic, gen_model
8283

8384
###################################################################################################
@@ -716,6 +717,25 @@ def set_check_data_mode(self, check_data):
716717
self._check_data = check_data
717718

718719

720+
def to_df(self, peak_org):
721+
"""Convert and extract the model results as a pandas object.
722+
723+
Parameters
724+
----------
725+
peak_org : int or Bands
726+
How to organize peaks.
727+
If int, extracts the first n peaks.
728+
If Bands, extracts peaks based on band definitions.
729+
730+
Returns
731+
-------
732+
pd.Series
733+
Model results organized into a pandas object.
734+
"""
735+
736+
return model_to_dataframe(self.get_results(), peak_org)
737+
738+
719739
def _check_width_limits(self):
720740
"""Check and warn about peak width limits / frequency resolution interaction."""
721741

fooof/objs/group.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from fooof.core.strings import gen_results_fg_str
2222
from fooof.core.io import save_fg, load_jsonlines
2323
from fooof.core.modutils import copy_doc_func_to_method, safe_import
24+
from fooof.data.conversions import group_to_dataframe
2425

2526
###################################################################################################
2627
###################################################################################################
@@ -543,6 +544,25 @@ def print_results(self, concise=False):
543544
print(gen_results_fg_str(self, concise))
544545

545546

547+
def to_df(self, peak_org):
548+
"""Convert and extract the model results as a pandas object.
549+
550+
Parameters
551+
----------
552+
peak_org : int or Bands
553+
How to organize peaks.
554+
If int, extracts the first n peaks.
555+
If Bands, extracts peaks based on band definitions.
556+
557+
Returns
558+
-------
559+
pd.DataFrame
560+
Model results organized into a pandas object.
561+
"""
562+
563+
return group_to_dataframe(self.get_results(), peak_org)
564+
565+
546566
def _fit(self, *args, **kwargs):
547567
"""Create an alias to FOOOF.fit for FOOOFGroup object, for internal use."""
548568

fooof/tests/objs/test_fit.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
from fooof.core.items import OBJ_DESC
1313
from fooof.core.errors import FitError
1414
from fooof.core.utils import group_three
15+
from fooof.core.modutils import safe_import
16+
from fooof.core.errors import DataError, NoDataError, InconsistentDataError
1517
from fooof.sim import gen_freqs, gen_power_spectrum
1618
from fooof.data import FOOOFSettings, FOOOFMetaData, FOOOFResults
17-
from fooof.core.errors import DataError, NoDataError, InconsistentDataError
19+
20+
pd = safe_import('pandas')
1821

1922
from fooof.tests.settings import TEST_DATA_PATH
2023
from fooof.tests.tutils import get_tfm, plot_test
@@ -425,3 +428,10 @@ def test_fooof_check_data():
425428
# Model fitting should execute, but return a null model fit, given the NaNs, without failing
426429
tfm.fit()
427430
assert not tfm.has_model
431+
432+
def test_fooof_to_df(tfm, tbands, skip_if_no_pandas):
433+
434+
df1 = tfm.to_df(2)
435+
assert isinstance(df1, pd.Series)
436+
df2 = tfm.to_df(tbands)
437+
assert isinstance(df2, pd.Series)

fooof/tests/objs/test_group.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
import numpy as np
1010
from numpy.testing import assert_equal
1111

12-
from fooof.data import FOOOFResults
1312
from fooof.core.items import OBJ_DESC
13+
from fooof.core.modutils import safe_import
14+
from fooof.core.errors import DataError, NoDataError, InconsistentDataError
15+
from fooof.data import FOOOFResults
1416
from fooof.sim import gen_group_power_spectra
1517

18+
pd = safe_import('pandas')
19+
1620
from fooof.tests.settings import TEST_DATA_PATH
1721
from fooof.tests.tutils import default_group_params, plot_test
1822

@@ -349,3 +353,10 @@ def test_fg_get_group(tfg):
349353
# Check that the correct results are extracted
350354
assert [tfg.group_results[ind] for ind in inds1] == nfg1.group_results
351355
assert [tfg.group_results[ind] for ind in inds2] == nfg2.group_results
356+
357+
def test_fg_to_df(tfg, tbands, skip_if_no_pandas):
358+
359+
df1 = tfg.to_df(2)
360+
assert isinstance(df1, pd.DataFrame)
361+
df2 = tfg.to_df(tbands)
362+
assert isinstance(df2, pd.DataFrame)

0 commit comments

Comments
 (0)