Skip to content
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

Fix issue #374 (Crash with numpy version 1.15 and above (function np.product removed)) #375

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 11 additions & 2 deletions symfit/core/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@

import inspect

from packaging.version import Version

numpy_version = Version(np.__version__)

if numpy_version >= Version("1.15.0"):
numpy_product_function = np.prod
else:
numpy_product_function = np.product

class TakesData(object):
"""
An base class for everything that takes data. Most importantly, it takes care
Expand All @@ -43,7 +52,7 @@ def __init__(self, model, *ordered_data, absolute_sigma=None, **named_data):
:param named_data: assign dependent, independent and sigma variables data by name.

Standard deviation can be provided to any variable. They have to be prefixed
with sigma\_. For example, let x be a Variable. Then sigma_x will give the
with sigma_. For example, let x be a Variable. Then sigma_x will give the
stdev in x.
"""
if isinstance(model, BaseModel):
Expand Down Expand Up @@ -253,7 +262,7 @@ def _covariance_matrix(self, best_fit_params, objective):
# Residual sum of squares
rss = 2 * objective(**key2str(best_fit_params))
# Degrees of freedom
raw_dof = np.sum([np.product(shape) for shape in self.data_shapes[1]])
raw_dof = np.sum([numpy_product_function(shape) for shape in self.data_shapes[1]])
dof = raw_dof - len(self.model.params)
if self.absolute_sigma:
# When interpreting as measurement error, we do not rescale.
Expand Down
16 changes: 14 additions & 2 deletions tests/test_constrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@

import numpy as np
import sympy
from scipy.integrate import simps

from packaging.version import Version
import pkg_resources

scipy_version = pkg_resources.get_distribution("scipy").version

if Version(scipy_version) >= Version("1.14.0"):
from scipy.integrate import simpson
scipy_simpson_function = simpson
else:
from scipy.integrate import simps
scipy_simpson_function = simps

from scipy.optimize import NonlinearConstraint, minimize

from symfit import (
Expand Down Expand Up @@ -763,7 +775,7 @@ def test_constrained_dependent_on_model():
constraint_model = Model.as_constraint(A - 1, model, constraint_type=Eq)
constraint_exact = Eq(A, 1)
constraint_num = CallableNumericalModel.as_constraint(
{Y: lambda x, y: simps(y, x) - 1}, # Integrate using simps
{Y: lambda x, y: scipy_simpson_function(y, x=x) - 1}, # Integrate using simps
model=model,
connectivity_mapping={Y: {x, y}},
constraint_type=Eq
Expand Down
10 changes: 9 additions & 1 deletion tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@

import inspect

from packaging.version import Version

numpy_version = Version(np.__version__)

if numpy_version >= Version("1.15.0"):
numpy_product_function = np.prod
else:
numpy_product_function = np.product

def setup_module():
np.random.seed(0)
Expand Down Expand Up @@ -529,7 +537,7 @@ def test_likelihood_fitting_exponential():
fit = Fit(pdf, xdata, objective=LogLikelihood)
fit_result = fit.execute()
pdf_i = fit.model(x=xdata, **fit_result.params).y # probabilities
likelihood = np.product(pdf_i)
likelihood = numpy_product_function(pdf_i)
loglikelihood = np.sum(np.log(pdf_i))

assert fit_result.value(b) == pytest.approx(mean, 1e-3)
Expand Down
Loading