Skip to content

Commit b09f9db

Browse files
committed
add average_reconstructions helper function
1 parent f8ed771 commit b09f9db

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

doc/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Functions to manipulate, examine and analyze FOOOF objects, and related utilitie
5252

5353
compare_info
5454
average_fg
55+
average_reconstructions
5556
combine_fooofs
5657

5758
.. currentmodule:: fooof

fooof/objs/utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
119157
def combine_fooofs(fooofs):
120158
"""Combine a group of FOOOF and/or FOOOFGroup objects into a single FOOOFGroup object.
121159

fooof/tests/objs/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def test_average_fg(tfg, tbands):
4545
with raises(NoModelError):
4646
average_fg(ntfg, tbands)
4747

48+
def test_average_reconstructions(tfg):
49+
50+
freqs, avg_model = average_reconstructions(tfg)
51+
assert isinstance(freqs, np.ndarray)
52+
assert isinstance(avg_model, np.ndarray)
53+
assert freqs.shape == avg_model.shape
54+
4855
def test_combine_fooofs(tfm, tfg):
4956

5057
tfm2 = tfm.copy()

0 commit comments

Comments
 (0)