Skip to content

fix: initial_peak_guesses refuses unknown models instead of falling back (v0.1.5) - #64

Merged
garrekstemo merged 1 commit into
mainfrom
fix/initial-guesses-unknown-model
Jul 29, 2026
Merged

fix: initial_peak_guesses refuses unknown models instead of falling back (v0.1.5)#64
garrekstemo merged 1 commit into
mainfrom
fix/initial-guesses-unknown-model

Conversation

@garrekstemo

Copy link
Copy Markdown
Owner

What

initial_peak_guesses carried two unknown-model fallbacks — npp = ... : 3 and positional p$i parameter names — that could never run. _peaks_to_p0 calls _n_peak_params, which throws "Unknown model. Provide n_peak_params manually." before either fallback is reached, so a custom lineshape always errored, just with a message naming a keyword that does not exist anywhere in the package.

The function now validates _is_known_model up front, names the five built-in lineshapes, and points custom-model callers at fit_peaks' explicit p0. Both dead branches are gone and the docstring states the constraint.

Failing fast is the right resolution rather than making the fallback work: automatic guesses need the model's parameter layout — the parameter count, which slot holds the width, and how a detected FWHM maps onto it via width_scale. None of that is inferable from an arbitrary function, and assuming a 3-parameter amplitude/center/width layout would silently build a wrong-length or wrong-order p0 for a 4-parameter custom model.

What is deliberately unchanged

The same-looking fallbacks in fit_peaks (npp, _model_name, param_names) are not dead, and are left alone. With an explicit p0, fit_peaks never calls _peaks_to_p0, so a custom lineshape fits normally and reports positional parameter names. Verified before touching anything:

custom_peak(p, x) = @. p[1] / (1 + ((x - p[2]) / p[3])^2)
f = fit_peaks(x, y; n_peaks=2, model=custom_peak, p0=[0.5, 2020.0, 8.0, 0.3, 2080.0, 6.0, 0.01, 0.0])
f[1].params      # [:p1, :p2, :p3]
f[1].model       # "custom_peak"
f.r_squared      # 1.0

Applying the same up-front check there would have removed a working feature.

Tests

Extends the initial_peak_guesses testset with both halves: the refusal (ArgumentError, message mentions "known lineshape") and the custom-model fit that still works through p0.

Full suite: 1940 passed, 0 failed.

Note, not addressed here

_n_peak_params' message still says "Provide n_peak_params manually", but no function in the package takes such a keyword. After this change its only remaining live caller is the TA peak-spec fit in fitting.jl:1037, so a custom peak function there is still pointed at a keyword that does not exist. Out of scope for this fix.

🤖 Generated with Claude Code

The unknown-model fallbacks in initial_peak_guesses (npp = 3 and the
positional p$i parameter names) were unreachable: _peaks_to_p0 calls
_n_peak_params, which throws "Unknown model" before either fallback is
used. Automatic guesses genuinely need the model's parameter layout —
the parameter count, which slot holds the width, and how a detected
FWHM maps onto it — and assuming a 3-parameter amplitude/center/width
layout would silently build a wrong-length or wrong-order p0 for a
4-parameter custom model.

The function now validates _is_known_model up front and names the five
built-in lineshapes in the error, pointing custom-model callers at
fit_peaks' explicit p0 instead of a nonexistent kwarg. Both dead
branches are gone, and the docstring states the constraint.

The equivalent fallbacks in fit_peaks are NOT dead and are left alone:
with an explicit p0, fit_peaks never calls _peaks_to_p0, so a custom
lineshape fits and reports positional parameter names. The new test
covers both halves — the refusal, and the custom-model fit that still
works via p0.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@garrekstemo
garrekstemo merged commit b01117a into main Jul 29, 2026
3 checks passed
@garrekstemo
garrekstemo deleted the fix/initial-guesses-unknown-model branch July 29, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant