Cleanup and refactor eps_fit_lorentzian.py - #3253
Open
oskooi wants to merge 2 commits into
Open
Conversation
Collaborator
Author
|
Output of |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
nlopt →SciPy
nlopt.opt(...)+ the MMA/L-BFGS local-optimizer setup withscipy.optimize.least_squares(..., method="trf", bounds=(0, np.inf))— the idiomatic tool for bound-constrained nonlinear least squares, and the natural fit for this residual-minimization problem.gradidiom.lorentzerris replaced by a clean_residuals(real/imag-stacked residual vector) plus an analytic_jacobianpassed toleast_squares(verified against finite differences to ~1e-9).lorentzfitnow returns the optimal params and the residual sum of squares Σ|Δ|² (=2 * result.cost), preserving the original error metric.Cleanup / refactor
__main__block into focused functions:fit_medium(random-restart fitting → builds theMedium) andplot_fit(comparison plot), plusmain()/parse_args().argparseso the hard-coded fitting parameters are now CLI flags:csv,--eps-inf,--num-lorentzians,--wl-min,--wl-max,--num-repeat,--seed,--output(defaults preserve the original behavior;--wl-min/--wl-maxdefault to 400/700).--seedso restarts are reproducible (the original used unseedednp.random).lorentzfunc/lorentzfitnames (nothing in the repo imports them, but they're the recognizable API), added type hints and docstrings, and preserved the Drude special-case (ω = 0).Validation
materials.pyin a separate PR): γ→0 (lossless), and A/ω² gives σ = 2.6134 and 0.5625 — matching results from the unmodified script exactly, max |Δn| = 6.0×10⁻⁵nloptreferences remain,-hworks.