@@ -116,6 +116,44 @@ def average_fg(fg, bands, avg_method='mean', regenerate=True):
116116 return fm
117117
118118
119+ def average_reconstructions (fg , avg_method = 'mean' ):
120+ """Average across model reconstructions for a group of power spectra.
121+
122+ Parameters
123+ ----------
124+ fg : FOOOFGroup
125+ Object with model fit results to average across.
126+ avg : {'mean', 'median'}
127+ Averaging function to use.
128+
129+ Returns
130+ -------
131+ freqs : 1d array
132+ Frequency values for the average model reconstruction.
133+ avg_model : 1d array
134+ Power values for the average model reconstruction.
135+ Note that power values are in log10 space.
136+ """
137+
138+ if avg_method not in ['mean' , 'median' ]:
139+ raise ValueError ("Requested average method not understood." )
140+ if not fg .has_model :
141+ raise NoModelError ("No model fit results are available, can not proceed." )
142+
143+ if avg_method == 'mean' :
144+ avg_func = np .nanmean
145+ elif avg_method == 'median' :
146+ avg_func = np .nanmedian
147+
148+ models = np .zeros (shape = fg .power_spectra .shape )
149+ for ind in range (len (fg )):
150+ models [ind , :] = fg .get_fooof (ind , regenerate = True ).fooofed_spectrum_
151+
152+ avg_model = avg_func (models , 0 )
153+
154+ return fg .freqs , avg_model
155+
156+
119157def combine_fooofs (fooofs ):
120158 """Combine a group of FOOOF and/or FOOOFGroup objects into a single FOOOFGroup object.
121159
0 commit comments