Skip to content

[WIP/ENH] - Update object organization and documentation #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2878076
move model components to sub-object
TomDonoghue Apr 12, 2025
0057188
update BaseModel docs
TomDonoghue Apr 12, 2025
38b6734
modeled_spectrum_ -> modeled_spectrum
TomDonoghue Apr 12, 2025
9bb2331
move ModelComponents
TomDonoghue Apr 12, 2025
3b2c2c0
add ModelParameters object
TomDonoghue Apr 12, 2025
114d20a
update to use results.params
TomDonoghue Apr 13, 2025
c5b8f96
add non-default settings to test objects
TomDonoghue Apr 13, 2025
bb3cee9
extend compare_model_objs
TomDonoghue Apr 13, 2025
9804529
udpate IO tests to be more stringent
TomDonoghue Apr 13, 2025
d9101d4
update add_from_dict to clean up & fix algo settings
TomDonoghue Apr 13, 2025
e0cb026
finish removing _check_loaded_results
TomDonoghue Apr 13, 2025
ab9d696
deprecate results fields / add as property to params
TomDonoghue Apr 13, 2025
e4656d9
bands repr -> str
TomDonoghue Apr 13, 2025
d0b8831
drop trailing underscores
TomDonoghue Apr 13, 2025
23c1346
remove trailing underscores - null
TomDonoghue Apr 13, 2025
d161992
clean up data checks
TomDonoghue Apr 13, 2025
08c8961
sweep for docstrings content
TomDonoghue Apr 13, 2025
eb8d39c
tweak / update SettingsDefinitions
TomDonoghue Apr 14, 2025
e8f83d1
tweak Algorithm object for settings
TomDonoghue Apr 14, 2025
91720a3
add SettingsValues
TomDonoghue Apr 14, 2025
4418e1f
use SettingsValues
TomDonoghue Apr 14, 2025
9803c2f
udpate across code to use algorithm.settings
TomDonoghue Apr 14, 2025
47fef97
update spectral fit settings def dict name
TomDonoghue Apr 14, 2025
ad6c0e0
add private_settings to Algorithm
TomDonoghue Apr 15, 2025
cabf8af
use private settings on spectral fit algorithm object
TomDonoghue Apr 15, 2025
71cd252
update process for peak_width_limits -> gaussian std
TomDonoghue Apr 15, 2025
f751262
drop reset internal settings - no longer used
TomDonoghue Apr 15, 2025
7aaeb42
add clear method to settings values
TomDonoghue Apr 15, 2025
944e812
drop _check_loaded_settings
TomDonoghue Apr 15, 2025
4564872
update algo to use cf settings
TomDonoghue Apr 15, 2025
104128c
lints
TomDonoghue Apr 15, 2025
9b51f6d
udpate line spacing and settings docs
TomDonoghue Apr 15, 2025
edce4ec
add AlgorithmCF object
TomDonoghue Apr 17, 2025
e743691
add algo_cf initialize methods
TomDonoghue Apr 17, 2025
33d0eb0
use algocf initialization for ap
TomDonoghue Apr 17, 2025
6df8eb1
update _get_pe_bounds
TomDonoghue Apr 17, 2025
d96ed69
udpate _fit_peaks for modes
TomDonoghue Apr 17, 2025
6048b8c
update peak tuning for modes
TomDonoghue Apr 17, 2025
736a0be
udpate param conversion to use modes
TomDonoghue Apr 17, 2025
c488888
update / clean up descriptions
TomDonoghue Apr 17, 2025
394a0eb
Merge branch 'main' into resobj
TomDonoghue May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
udpate line spacing and settings docs
  • Loading branch information
TomDonoghue committed Apr 15, 2025
commit 9b51f6dc150ea4ef89e0b14c045a1b74b33dcc3f
79 changes: 50 additions & 29 deletions specparam/algorithms/spectral_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
'min_peak_height' : {
'type' : 'float, optional, default: 0',
'description' : \
'Absolute threshold for detecting peaks.\n ' \
'Absolute threshold for detecting peaks.'
'\n '
'This threshold is defined in absolute units of the power spectrum (log power).',
},
'peak_threshold' : {
'type' : 'float, optional, default: 2.0',
'description' : \
'Relative threshold for detecting peaks.\n ' \
'Relative threshold for detecting peaks.'
'\n '
'Threshold is defined in relative units of the power spectrum (standard deviation).',
},
})
Expand All @@ -44,38 +46,47 @@
'ap_percentile_thresh' : {
'type' : 'float',
'description' : \
'Percentile threshold, to select points from a flat spectrum for an initial aperiodic fit.\n '
'Points are selected at a low percentile value to restrict to non-peak points.'
'Percentile threshold to select data from flat spectrum for an initial aperiodic fit.'
'\n '
'Points are selected at a low percentile value to restrict to non-peak points.',
},
'ap_guess' : {
'type' : 'list of float',
'description' : \
'Guess parameters for fitting the aperiodic component.\n '
'The length of the guess parameters should match the number & order of the aperiodic parameters.\n '
'If \'offset\' is a parameter & guess is None, the first value of the power spectrum is used as the guess.\n '
'If \'exponent\' is a parameter & guess is None, the abs(log-log slope) of first & last points is used.'
'Guess parameters for fitting the aperiodic component.'
'\n '
'The guess parameters should match the length and order of the aperiodic parameters.'
'\n '
'If \'offset\' is a parameter, default guess is the first value of the power spectrum.'
'\n '
'If \'exponent\' is a parameter, '
'default guess is the abs(log-log slope) of first & last points.'
},
'ap_bounds' : {
'type' : 'tuple of tuple of float',
'description' : \
'Bounds for aperiodic fitting, as ((param1_low_bound, ...) (param1_high_bound, ...)).\n '
'By default, aperiodic fitting is unbound, but can be restricted here.'
'Bounds for aperiodic fitting, as ((param1_low_bound, ...) (param1_high_bound, ...)).'
'\n '
'By default, aperiodic fitting is unbound, but can be restricted here.',
},
'cf_bound' : {
'type' : 'float',
'description' : 'Parameter bounds for center frequency when fitting gaussians, in terms of +/- std dev.'
'description' : \
'Parameter bounds for center frequency when fitting gaussians, as +/- std dev.',
},
'bw_std_edge' : {
'type' : 'float',
'description' : \
'Threshold for how far a peak has to be from edge to keep.\n '
'This is defined in units of gaussian standard deviation.'
'Threshold for how far a peak has to be from edge to keep.'
'\n '
'This is defined in units of gaussian standard deviation.',
},
'gauss_overlap_thresh' : {
'type' : 'float',
'description' : \
'Degree of overlap between gaussian guesses for one to be dropped.\n '
'This is defined in units of gaussian standard deviation.'
'Degree of overlap between gaussian guesses for one to be dropped.'
'\n '
'This is defined in units of gaussian standard deviation.',
},
})

Expand Down Expand Up @@ -157,7 +168,8 @@
self.data.freqs, *np.ndarray.flatten(self.results.params.gaussian))

# Create peak-removed (but not flattened) power spectrum
self.results.model._spectrum_peak_rm = self.data.power_spectrum - self.results.model._peak_fit
self.results.model._spectrum_peak_rm = \
self.data.power_spectrum - self.results.model._peak_fit

# Run final aperiodic fit on peak-removed power spectrum
self.results.params.aperiodic = self._simple_ap_fit(\
Expand All @@ -167,7 +179,8 @@

# Create remaining model components: flatspec & full power_spectrum model fit
self.results.model._spectrum_flat = self.data.power_spectrum - self.results.model._ap_fit
self.results.model.modeled_spectrum = self.results.model._peak_fit + self.results.model._ap_fit
self.results.model.modeled_spectrum = \
self.results.model._peak_fit + self.results.model._ap_fit

## PARAMETER UPDATES

Expand Down Expand Up @@ -219,7 +232,7 @@

if ap_bounds:
msg = 'Provided aperiodic bounds do not have right length for fit function.'
assert len(ap_bounds[0]) == len(ap_bounds[1]) == self.modes.aperiodic.n_params, msg

Check warning on line 235 in specparam/algorithms/spectral_fit.py

View check run for this annotation

Codecov / codecov/patch

specparam/algorithms/spectral_fit.py#L235

Added line #L235 was not covered by tests
else:
ap_bounds = (tuple([-np.inf] * self.modes.aperiodic.n_params),
tuple([np.inf] * self.modes.aperiodic.n_params))
Expand Down Expand Up @@ -255,8 +268,10 @@
warnings.simplefilter("ignore")
aperiodic_params, _ = curve_fit(self.modes.aperiodic.func, freqs, power_spectrum,
p0=ap_guess, bounds=self._settings.ap_bounds,
maxfev=self._cf_settings.maxfev, check_finite=False,
ftol=self._cf_settings.tol, xtol=self._cf_settings.tol,
maxfev=self._cf_settings.maxfev,
check_finite=False,
ftol=self._cf_settings.tol,
xtol=self._cf_settings.tol,
gtol=self._cf_settings.tol)
except RuntimeError as excp:
error_msg = ("Model fitting failed due to not finding parameters in "
Expand Down Expand Up @@ -311,8 +326,10 @@
aperiodic_params, _ = curve_fit(self.modes.aperiodic.func,
freqs_ignore, spectrum_ignore,
p0=popt, bounds=self._settings.ap_bounds,
maxfev=self._cf_settings.maxfev, check_finite=False,
ftol=self._cf_settings.tol, xtol=self._cf_settings.tol,
maxfev=self._cf_settings.maxfev,
check_finite=False,
ftol=self._cf_settings.tol,
xtol=self._cf_settings.tol,
gtol=self._cf_settings.tol)
except RuntimeError as excp:
error_msg = ("Model fitting failed due to not finding "
Expand Down Expand Up @@ -391,7 +408,7 @@
except ValueError:
# This procedure can fail (very rarely), if both left & right inds end up as None
# In this case, default the guess to the average of the peak width limits
guess_std = np.mean(self.settings.peak_width_limits)

Check warning on line 411 in specparam/algorithms/spectral_fit.py

View check run for this annotation

Codecov / codecov/patch

specparam/algorithms/spectral_fit.py#L411

Added line #L411 was not covered by tests

# Check that guess value isn't outside preset limits - restrict if so
# This also converts the peak_width_limits from 2-sided BW to 1-sided gaussian std
Expand Down Expand Up @@ -480,8 +497,10 @@
p0=np.ndarray.flatten(guess),
bounds=self._get_pe_bounds(guess),
jac=self.modes.periodic.jacobian,
maxfev=self._cf_settings.maxfev, check_finite=False,
ftol=self._cf_settings.tol, xtol=self._cf_settings.tol,
maxfev=self._cf_settings.maxfev,
check_finite=False,
ftol=self._cf_settings.tol,
xtol=self._cf_settings.tol,
gtol=self._cf_settings.tol)

except RuntimeError as excp:
Expand Down Expand Up @@ -614,14 +633,16 @@

# Collect peak parameter data
if self.modes.periodic.name == 'gaussian': ## TEMP
peak_params[ii] = [peak[0],
self.results.model.modeled_spectrum[ind] - self.results.model._ap_fit[ind],
peak[2] * 2]
peak_params[ii] = [\
peak[0],
self.results.model.modeled_spectrum[ind] - self.results.model._ap_fit[ind],
peak[2] * 2]

## TEMP:
if self.modes.periodic.name == 'skewnorm':
peak_params[ii] = [peak[0],
self.results.model.modeled_spectrum[ind] - self.results.model._ap_fit[ind],
peak[2] * 2, peak[3]]
peak_params[ii] = [\

Check warning on line 643 in specparam/algorithms/spectral_fit.py

View check run for this annotation

Codecov / codecov/patch

specparam/algorithms/spectral_fit.py#L643

Added line #L643 was not covered by tests
peak[0],
self.results.model.modeled_spectrum[ind] - self.results.model._ap_fit[ind],
peak[2] * 2, peak[3]]

return peak_params
Loading