Skip to content

Commit

Permalink
fix internal bug on fit re-tries in HillCurve (#51)
Browse files Browse the repository at this point in the history
Internally, when a fit fails in the first try, `HillCurve` is supposed to retry
with alternative optimization strategies. However, there was a bug in this
code that threw an error, now is fixed.
  • Loading branch information
jbloom authored Jan 1, 2024
1 parent 2f2c2eb commit 77434b9
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 187 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.

The format is based on `Keep a Changelog <https://keepachangelog.com>`_.

1.1.1
-----

Fixed
+++++
- Fix internal bug in ``HillCurve`` that led to failure to try alternative fitting methods if initial fitting failed as it sometimes does for problematic curves.

1.1.0
-----

Expand Down
2 changes: 1 addition & 1 deletion neutcurve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

__author__ = "Jesse Bloom"
__email__ = "[email protected]"
__version__ = "1.1.0"
__version__ = "1.1.1"
__url__ = "https://github.com/jbloomlab/neutcurve"

from neutcurve.curvefits import CurveFits # noqa: F401
Expand Down
16 changes: 11 additions & 5 deletions neutcurve/hillcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,22 @@ def __init__(
# first try to fit using curve_fit
try:
if fix_slope_first:
init_tup, _ = self._fit_curve(
fix_first_init_tup, _ = self._fit_curve(
fixtop=fixtop,
fixbottom=fixbottom,
fitlogc=fitlogc,
use_stderr_for_fit=use_stderr_for_fit,
init_tup=init_tup,
fix_slope=True,
)
else:
fix_first_init_tup = init_tup
fit_tup, self.params_stdev = self._fit_curve(
fixtop=fixtop,
fixbottom=fixbottom,
fitlogc=fitlogc,
use_stderr_for_fit=use_stderr_for_fit,
init_tup=init_tup,
init_tup=fix_first_init_tup,
fix_slope=False,
)
# A RuntimeError is raised by scipy if curve_fit fails:
Expand All @@ -436,7 +438,7 @@ def __init__(
# curve_fit failed, try using minimize
for method in ["TNC", "L-BFGS-B", "SLSQP", "Powell"]:
if fix_slope_first:
init_tup = self._minimize_fit(
fix_first_init_tup = self._minimize_fit(
fixtop=fixtop,
fixbottom=fixbottom,
fitlogc=fitlogc,
Expand All @@ -445,13 +447,17 @@ def __init__(
init_tup=init_tup,
fix_slope=True,
)
if fix_first_init_tup is False:
continue
else:
fix_first_init_tup = init_tup
fit_tup = self._minimize_fit(
fixtop=fixtop,
fixbottom=fixbottom,
fitlogc=fitlogc,
use_stderr_for_fit=use_stderr_for_fit,
method=method,
init_tup=init_tup,
init_tup=fix_first_init_tup,
fix_slope=False,
)
self.params_stdev = None # can't estimate errors
Expand Down Expand Up @@ -648,7 +654,7 @@ def _minimize_fit(
init_tup,
fix_slope,
):
"""Fit via minimization, return `(midpoint, slope, bottom, top)`."""
"""Return `(midpoint, slope, bottom, top)` if succeeds or `False` if fails."""

midpoint, slope, bottom, top = init_tup

Expand Down
Loading

0 comments on commit 77434b9

Please sign in to comment.