Skip to content

Commit f18442d

Browse files
committed
update tutorials for code updaets
1 parent a6aaa3b commit f18442d

9 files changed

+67
-58
lines changed

tutorials/plot_01-ModelDescription.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
set_random_seed(21)
5151

5252
# Simulate example power spectra
53-
freqs1, powers1 = sim_power_spectrum([3, 40], [1, 1],
54-
[[10, 0.2, 1.25], [30, 0.15, 2]])
55-
freqs2, powers2 = sim_power_spectrum([1, 150], [1, 125, 1.25],
56-
[[8, 0.15, 1.], [30, 0.1, 2]])
53+
freqs1, powers1 = sim_power_spectrum([3, 40], {'fixed' : [1, 1]},
54+
{'gaussian' : [[10, 0.2, 1.25], [30, 0.15, 2]]})
55+
freqs2, powers2 = sim_power_spectrum([1, 150], {'knee' : [1, 125, 1.25]},
56+
{'gaussian' : [[8, 0.15, 1.], [30, 0.1, 2]]})
5757

5858
###################################################################################################
5959

tutorials/plot_02-PSDModel.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@
125125

126126
# Goodness of fit measures
127127
print('Goodness of fit:')
128-
print(' Error - ', fm.results.error_)
129-
print(' R^2 - ', fm.results.r_squared_, '\n')
128+
print(' Error - ', fm.results.metrics.results['error_mae'])
129+
print(' R^2 - ', fm.results.metrics.results['gof_rsquared'], '\n')
130130

131131
# Check how many peaks were fit
132132
print('Number of fit peaks: \n', fm.results.n_peaks_)
@@ -142,7 +142,7 @@
142142
###################################################################################################
143143

144144
# Extract a model parameter with `get_params`
145-
err = fm.get_params('error')
145+
err = fm.get_params('metrics', 'error_mae')
146146

147147
# Extract parameters, indicating sub-selections of parameters
148148
exp = fm.get_params('aperiodic_params', 'exponent')
@@ -227,7 +227,7 @@
227227
fres = fm.results.get_results()
228228

229229
# You can also unpack all fit parameters when using `get_results`
230-
ap_params, peak_params, r_squared, fit_error, gauss_params = fm.results.get_results()
230+
ap_params, peak_params, metrics, gauss_params = fm.results.get_results()
231231

232232
###################################################################################################
233233

@@ -238,8 +238,8 @@
238238
print('Aperiodic Parameters: \n', fres.aperiodic_params)
239239

240240
# Check the R^2 and error of the model fit
241-
print('R-squared: \n {:5.4f}'.format(fres.r_squared))
242-
print('Fit error: \n {:5.4f}'.format(fres.error))
241+
print('R-squared: \n {:5.4f}'.format(fres.metrics['gof_rsquared']))
242+
print('Fit error: \n {:5.4f}'.format(fres.metrics['error_mae']))
243243

244244
###################################################################################################
245245
# Conclusion

tutorials/plot_03-Algorithm.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
# Do an initial aperiodic fit - a robust fit, that excludes outliers
108108
# This recreates an initial fit that isn't ultimately stored in the model object
109109
init_ap_fit = gen_aperiodic(\
110-
fm.data.freqs, fm.algorithm._robust_ap_fit(fm.data.freqs, fm.data.power_spectrum))
110+
fm.data.freqs, fm.modes.aperiodic,
111+
fm.algorithm._robust_ap_fit(fm.data.freqs, fm.data.power_spectrum))
111112

112113
# Plot the initial aperiodic fit
113114
_, ax = plt.subplots(figsize=(12, 10))

tutorials/plot_04-ModelObject.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,13 @@
230230

231231
###################################################################################################
232232

233-
# Print out model fit results
233+
# Print out model fit results parameters
234234
print('aperiodic params: \t', fm.results.aperiodic_params_)
235235
print('peak params: \t', fm.results.peak_params_)
236-
print('r-squared: \t', fm.results.r_squared_)
237-
print('fit error: \t', fm.results.error_)
238-
print('modeled spectrum: \t', fm.results.modeled_spectrum_[0:5])
236+
237+
# Print out metrics model fit results parameters
238+
print('fit error: \t', fm.results.metrics.results['error_mae'])
239+
print('r-squared: \t', fm.results.metrics.results['gof_rsquared'])
239240

240241
###################################################################################################
241242
# 4) Methods

tutorials/plot_06-GroupFits.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@
149149
cfs = fg.get_params('peak_params', 'CF')
150150

151151
# Extract goodness-of-fit metrics
152-
errors = fg.get_params('error')
153-
r2s = fg.get_params('r_squared')
152+
errors = fg.get_params('metrics', 'error_mae')
153+
r2s = fg.get_params('metrics', 'gof_rsquared')
154154

155155
###################################################################################################
156156

tutorials/plot_07-TimeModels.py

+30-28
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from specparam.sim import sim_spectrogram
2020
from specparam.plts.spectra import plot_spectrogram
2121

22-
2322
###################################################################################################
2423
# Parameterizing Spectrograms
2524
# ---------------------------
@@ -42,38 +41,20 @@
4241

4342
###################################################################################################
4443

45-
# Create & plot an example spectrogram
44+
# Define simulation parameters for a spectrogram
4645
n_pre_post = 50
4746
freq_range = [3, 25]
48-
ap_params = [[1, 1.5]] * n_pre_post + [[1, 1]] * n_pre_post
49-
pe_params = [[10, 1.5, 2.5]] * n_pre_post + [[10, 0.5, 2.]] * n_pre_post
47+
ap_params = {'fixed' : [[1, 1.5]] * n_pre_post + [[1, 1]] * n_pre_post}
48+
pe_params = {'gaussian' : [[10, 1.5, 2.5]] * n_pre_post + [[10, 0.5, 2.]] * n_pre_post}
49+
50+
# Simulate spectrogram
5051
freqs, spectrogram = sim_spectrogram(n_pre_post * 2, freq_range, ap_params, pe_params, nlvs=0.1)
5152

5253
###################################################################################################
5354

5455
# Plot our simulated spectrogram
5556
plot_spectrogram(freqs, spectrogram)
5657

57-
###################################################################################################
58-
# SpectralTimeModel
59-
# -----------------
60-
#
61-
# The :class:`~specparam.SpectralTimeModel` object is an extension of the SpectralModel objects
62-
# to support parameterizing neural power spectra that are organized across time (spectrograms).
63-
#
64-
# In practice, this object is very similar to the previously introduced spectral model objects,
65-
# especially the Group model object. The time object is a mildly updated Group object.
66-
#
67-
# The main differences with the SpectralTimeModel from previous model objects are that the
68-
# data it accepts and parameterizes should be organized as as array of power spectra over
69-
# time windows - basically as a spectrogram.
70-
#
71-
72-
###################################################################################################
73-
74-
# Initialize a SpectralTimeModel model, which accepts all the same settings as SpectralModel
75-
ft = SpectralTimeModel()
76-
7758
###################################################################################################
7859
# Defining Oscillation Bands
7960
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -93,6 +74,26 @@
9374
# Define a bands object to organize peak parameters
9475
bands = Bands({'alpha' : [7, 14]})
9576

77+
###################################################################################################
78+
# SpectralTimeModel
79+
# -----------------
80+
#
81+
# The :class:`~specparam.SpectralTimeModel` object is an extension of the SpectralModel objects
82+
# to support parameterizing neural power spectra that are organized across time (spectrograms).
83+
#
84+
# In practice, this object is very similar to the previously introduced spectral model objects,
85+
# especially the Group model object. The time object is a mildly updated Group object.
86+
#
87+
# The main differences with the SpectralTimeModel from previous model objects are that the
88+
# data it accepts and parameterizes should be organized as as array of power spectra over
89+
# time windows - basically as a spectrogram.
90+
#
91+
92+
###################################################################################################
93+
94+
# Initialize a SpectralTimeModel model, which accepts all the same settings as SpectralModel
95+
ft = SpectralTimeModel(bands=bands)
96+
9697
###################################################################################################
9798
#
9899
# Now we are ready to fit our spectrogram! As with all model objects, we can fit the models
@@ -102,7 +103,7 @@
102103
###################################################################################################
103104

104105
# Fit the spectrogram and print out report
105-
ft.report(freqs, spectrogram, peak_org=bands)
106+
ft.report(freqs, spectrogram)
106107

107108
###################################################################################################
108109
#
@@ -129,7 +130,8 @@
129130
n_events = 3
130131
spectrograms = []
131132
for ind in range(n_events):
132-
freqs, cur_spect = sim_spectrogram(n_pre_post * 2, freq_range, ap_params, pe_params, nlvs=0.1)
133+
freqs, cur_spect = sim_spectrogram(\
134+
n_pre_post * 2, freq_range, ap_params, pe_params, nlvs=0.1)
133135
spectrograms.append(cur_spect)
134136

135137
###################################################################################################
@@ -157,12 +159,12 @@
157159
###################################################################################################
158160

159161
# Initialize the spectral event model
160-
fe = SpectralTimeEventModel()
162+
fe = SpectralTimeEventModel(bands=bands)
161163

162164
###################################################################################################
163165

164166
# Fit the spectrograms and print out report
165-
fe.report(freqs, spectrograms, peak_org=bands)
167+
fe.report(freqs, spectrograms)
166168

167169
###################################################################################################
168170
#

tutorials/plot_08-TroubleShooting.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@
137137
set_random_seed(21)
138138

139139
# Create a simulated power spectrum
140-
freqs, spectrum = sim_power_spectrum(f_range, ap_params, gauss_params, nlv)
140+
freqs, spectrum = sim_power_spectrum(\
141+
f_range, {'fixed' : ap_params}, {'gaussian' : gauss_params}, nlv)
141142

142143
###################################################################################################
143144

@@ -222,7 +223,8 @@
222223
nlv = 0.025
223224

224225
# Create a simulated power spectrum
225-
freqs, spectrum = sim_power_spectrum([1, 50], ap_params, gauss_params, nlv=nlv)
226+
freqs, spectrum = sim_power_spectrum(\
227+
[1, 50], {'fixed' : ap_params}, {'gaussian' : gauss_params}, nlv=nlv)
226228

227229
###################################################################################################
228230

@@ -276,8 +278,8 @@
276278
gauss_opts = param_sampler([[], [10, 0.5, 2], [10, 0.5, 2, 20, 0.3, 4]])
277279

278280
# Simulate a group of power spectra
279-
freqs, power_spectra = sim_group_power_spectra(n_spectra, sim_freq_range,
280-
ap_opts, gauss_opts, nlv)
281+
freqs, power_spectra = sim_group_power_spectra(\
282+
n_spectra, sim_freq_range, {'fixed' : ap_opts}, {'gaussian' : gauss_opts}, nlv)
281283

282284
###################################################################################################
283285

@@ -306,7 +308,7 @@
306308
###################################################################################################
307309

308310
# Find the index of the worst model fit from the group
309-
worst_fit_ind = np.argmax(fg.get_params('error'))
311+
worst_fit_ind = np.argmax(fg.get_params('metrics', 'error_mae'))
310312

311313
# Extract this model fit from the group
312314
fm = fg.get_model(worst_fit_ind, regenerate=True)
@@ -335,11 +337,12 @@
335337
error_threshold = 0.010
336338
to_check = []
337339
for ind, res in enumerate(fg.results):
338-
if res.error > error_threshold:
340+
if res.metrics['error_mae'] > error_threshold:
339341
to_check.append(fg.get_model(ind, regenerate=True))
340342

341343
# A more condensed version of the procedure above can be written like this:
342-
#to_check = [fg.get_model(ind, True) for ind, res in enumerate(fg) if res.error > error_threshold]
344+
#to_check = [fg.get_model(ind, True) for ind, res in enumerate(fg) \
345+
# if res.metrics['error_mae'] > error_threshold]
343346

344347
###################################################################################################
345348

tutorials/plot_09-FurtherAnalysis.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@
7878
set_random_seed(21)
7979

8080
# Create some simulated power spectra
81-
freqs, spectra = sim_group_power_spectra(n_spectra=10,
82-
freq_range=[3, 40],
83-
aperiodic_params=param_sampler([[20, 2], [35, 1.5]]),
84-
periodic_params=param_sampler([[], [10, 0.5, 2]]))
81+
freqs, spectra = sim_group_power_spectra(\
82+
n_spectra=10, freq_range=[3, 40],
83+
aperiodic_params={'fixed' : param_sampler([[20, 2], [35, 1.5]])},
84+
periodic_params={'gaussian' : param_sampler([[], [10, 0.5, 2]])})
8585

8686
###################################################################################################
8787

tutorials/plot_10-Reporting.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
###################################################################################################
106106

107107
# Simulate an example power spectrum
108-
freqs, powers = sim_power_spectrum([1, 50], [0, 10, 1], [10, 0.25, 2], freq_res=0.25)
108+
freqs, powers = sim_power_spectrum(\
109+
[1, 50], {'knee' : [0, 10, 1]}, {'gaussian' : [10, 0.25, 2]}, freq_res=0.25)
109110

110111
# Initialize model object
111112
fm = SpectralModel(min_peak_height=0.1, peak_width_limits=[1, 6], aperiodic_mode='knee')
@@ -141,7 +142,8 @@
141142
###################################################################################################
142143

143144
# Simulate an example group of power spectra
144-
freqs, powers = sim_group_power_spectra(10, [1, 75], [0, 1], [10, 0.25, 2])
145+
freqs, powers = sim_group_power_spectra(\
146+
10, [1, 75], {'fixed' : [0, 1]}, {'gaussian' : [10, 0.25, 2]})
145147

146148
###################################################################################################
147149

0 commit comments

Comments
 (0)