Skip to content

Commit

Permalink
Fix scipy import
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Jun 24, 2024
1 parent 30767e0 commit 5d54a81
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions glue/sample/src/sinter/_probability_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@

if TYPE_CHECKING:
import sinter
from scipy.stats._stats_mstats_common import LinregressResult

# Go on a magical journey looking for scipy's linear regression type.
try:
from scipy.stats._stats_py import LinregressResult
except ImportError:
try:
from scipy.stats._stats_mstats_common import LinregressResult
except ImportError:
from scipy.stats import linregress
LinregressResult = type(linregress([0, 1], [0, 1]))


def log_binomial(*, p: Union[float, np.ndarray], n: int, hits: int) -> np.ndarray:
Expand Down Expand Up @@ -150,7 +159,10 @@ def least_squares_cost(*, xs: np.ndarray, ys: np.ndarray, intercept: float, slop
def least_squares_through_point(*, xs: np.ndarray, ys: np.ndarray, required_x: float, required_y: float) -> 'LinregressResult':
# Local import to reduce initial cost of importing sinter.
from scipy.optimize import leastsq
from scipy.stats._stats_mstats_common import LinregressResult
from scipy.stats import linregress

# HACK: get scipy's linear regression result type
LinregressResult = type(linregress([0, 1], [0, 1]))

xs2 = xs - required_x
ys2 = ys - required_y
Expand All @@ -169,7 +181,11 @@ def err(intercept: float) -> float:

# Local import to reduce initial cost of importing sinter.
from scipy.optimize import leastsq
from scipy.stats._stats_mstats_common import LinregressResult

# HACK: get scipy's linear regression result type
from scipy.stats import linregress
LinregressResult = type(linregress([0, 1], [0, 1]))

(best_intercept,), _ = leastsq(func=err, x0=0.0)
return LinregressResult(required_slope, best_intercept, None, None, None, intercept_stderr=False)

Expand Down

0 comments on commit 5d54a81

Please sign in to comment.