File tree Expand file tree Collapse file tree 4 files changed +63
-2
lines changed Expand file tree Collapse file tree 4 files changed +63
-2
lines changed Original file line number Diff line number Diff line change 7878from fooof .utils .data import trim_spectrum
7979from fooof .utils .params import compute_gauss_std
8080from fooof .data import FOOOFResults , FOOOFSettings , FOOOFMetaData
81+ from fooof .data .conversions import model_to_dataframe
8182from 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
Original file line number Diff line number Diff line change 2121from fooof .core .strings import gen_results_fg_str
2222from fooof .core .io import save_fg , load_jsonlines
2323from 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
Original file line number Diff line number Diff line change 1212from fooof .core .items import OBJ_DESC
1313from fooof .core .errors import FitError
1414from fooof .core .utils import group_three
15+ from fooof .core .modutils import safe_import
16+ from fooof .core .errors import DataError , NoDataError , InconsistentDataError
1517from fooof .sim import gen_freqs , gen_power_spectrum
1618from fooof .data import FOOOFSettings , FOOOFMetaData , FOOOFResults
17- from fooof .core .errors import DataError , NoDataError , InconsistentDataError
19+
20+ pd = safe_import ('pandas' )
1821
1922from fooof .tests .settings import TEST_DATA_PATH
2023from 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 )
Original file line number Diff line number Diff line change 99import numpy as np
1010from numpy .testing import assert_equal
1111
12- from fooof .data import FOOOFResults
1312from 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
1416from fooof .sim import gen_group_power_spectra
1517
18+ pd = safe_import ('pandas' )
19+
1620from fooof .tests .settings import TEST_DATA_PATH
1721from 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 )
You can’t perform that action at this time.
0 commit comments