22
33import numpy as np
44
5- from specparam import Bands
5+ from specparam . bands . bands import Bands , check_bands
66from specparam .modutils .dependencies import safe_import , check_dependency
77from specparam .data .periodic import get_band_peak_arr
88from specparam .data .utils import flatten_results_dict
@@ -21,15 +21,18 @@ def model_to_dict(fit_results, modes, bands):
2121 Results of a model fit.
2222 modes : Modes
2323 Model modes definition.
24- bands : Bands or int
25- How to organize peaks, based on band definitions (Bands) or number of peaks (int).
24+ bands : Bands or dict or int
25+ How to organize peaks, based on band definitions.
26+ Can be Bands object or object that can be converted into a Bands object.
2627
2728 Returns
2829 -------
2930 dict
3031 Model results organized into a dictionary.
3132 """
3233
34+ bands = check_bands (bands )
35+
3336 fr_dict = {}
3437
3538 # aperiodic parameters
@@ -38,18 +41,18 @@ def model_to_dict(fit_results, modes, bands):
3841
3942 # periodic parameters
4043 peaks = fit_results .peak_params
44+ if not bands .bands and bands .n_bands :
4145
42- if isinstance (bands , int ):
43-
44- if len (peaks ) < bands :
45- nans = [np .array ([np .nan ] * 3 ) for ind in range (bands - len (peaks ))]
46+ # If bands if defined in terms of number of peaks
47+ if len (peaks ) < bands .n_bands :
48+ nans = [np .array ([np .nan ] * 3 ) for ind in range (bands .n_bands - len (peaks ))]
4649 peaks = np .vstack ((peaks , nans ))
4750
48- for ind , peak in enumerate (peaks [:bands , :]):
51+ for ind , peak in enumerate (peaks [:bands . n_bands , :]):
4952 for pe_label , pe_param in zip (modes .periodic .params .indices , peak ):
5053 fr_dict [pe_label + '_' + str (ind )] = pe_param
5154
52- elif isinstance ( bands , Bands ) :
55+ elif bands . bands :
5356 for band , f_range in bands :
5457 for label , param in zip (modes .periodic .params .indices ,
5558 get_band_peak_arr (peaks , f_range )):
@@ -72,16 +75,17 @@ def model_to_dataframe(fit_results, modes, bands):
7275 Results of a model fit.
7376 modes : Modes
7477 Model modes definition.
75- bands : Bands or int
76- How to organize peaks, based on band definitions (Bands) or number of peaks (int).
78+ bands : Bands or dict or int
79+ How to organize peaks, based on band definitions.
80+ Can be Bands object or object that can be converted into a Bands object.
7781
7882 Returns
7983 -------
8084 pd.Series
8185 Model results organized into a dataframe.
8286 """
8387
84- return pd .Series (model_to_dict (fit_results , modes , bands ))
88+ return pd .Series (model_to_dict (fit_results , modes , check_bands ( bands ) ))
8589
8690
8791def group_to_dict (group_results , modes , bands ):
@@ -93,15 +97,18 @@ def group_to_dict(group_results, modes, bands):
9397 List of FitResults objects, reflecting model results across a group of power spectra.
9498 modes : Modes
9599 Model modes definition.
96- bands : Bands or int
97- How to organize peaks, based on band definitions (Bands) or number of peaks (int).
100+ bands : Bands or dict or int
101+ How to organize peaks, based on band definitions.
102+ Can be Bands object or object that can be converted into a Bands object.
98103
99104 Returns
100105 -------
101106 dict
102107 Model results organized into a dictionary.
103108 """
104109
110+ bands = check_bands (bands )
111+
105112 nres = len (group_results )
106113 fr_dict = {ke : np .zeros (nres ) for ke in model_to_dict (group_results [0 ], modes , bands )}
107114 for ind , f_res in enumerate (group_results ):
@@ -121,16 +128,17 @@ def group_to_dataframe(group_results, modes, bands):
121128 List of FitResults objects.
122129 modes : Modes
123130 Model modes definition.
124- bands : Bands or int
125- How to organize peaks, based on band definitions (Bands) or number of peaks (int).
131+ bands : Bands or dict or int
132+ How to organize peaks, based on band definitions.
133+ Can be Bands object or object that can be converted into a Bands object.
126134
127135 Returns
128136 -------
129137 pd.DataFrame
130138 Model results organized into a dataframe.
131139 """
132140
133- return pd .DataFrame (group_to_dict (group_results , modes , bands ))
141+ return pd .DataFrame (group_to_dict (group_results , modes , check_bands ( bands ) ))
134142
135143
136144def event_group_to_dict (event_group_results , modes , bands ):
@@ -142,8 +150,9 @@ def event_group_to_dict(event_group_results, modes, bands):
142150 Model fit results from across a set of events.
143151 modes : Modes
144152 Model modes definition.
145- bands : Bands or int
146- How to organize peaks, based on band definitions (Bands) or number of peaks (int).
153+ bands : Bands or dict or int
154+ How to organize peaks, based on band definitions.
155+ Can be Bands object or object that can be converted into a Bands object.
147156
148157 Returns
149158 -------
@@ -152,6 +161,7 @@ def event_group_to_dict(event_group_results, modes, bands):
152161 """
153162
154163 event_time_results = {}
164+ bands = check_bands (bands )
155165
156166 for key in group_to_dict (event_group_results [0 ], modes , bands ):
157167 event_time_results [key ] = []
@@ -177,8 +187,9 @@ def event_group_to_dataframe(event_group_results, modes, bands):
177187 List of FitResults objects.
178188 modes : Modes
179189 Model modes definition.
180- bands : Bands or int
181- How to organize peaks, based on band definitions (Bands) or number of peaks (int).
190+ bands : Bands or dict or int
191+ How to organize peaks, based on band definitions.
192+ Can be Bands object or object that can be converted into a Bands object.
182193
183194 Returns
184195 -------
@@ -187,7 +198,7 @@ def event_group_to_dataframe(event_group_results, modes, bands):
187198 """
188199
189200 return pd .DataFrame (flatten_results_dict (\
190- event_group_to_dict (event_group_results , modes , bands )))
201+ event_group_to_dict (event_group_results , modes , check_bands ( bands ) )))
191202
192203
193204@check_dependency (pd , 'pandas' )
0 commit comments