Skip to content

Commit

Permalink
Merge 595b487 into 9a9cdd0
Browse files Browse the repository at this point in the history
  • Loading branch information
jvansanten committed Oct 30, 2020
2 parents 9a9cdd0 + 595b487 commit a5e00dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
19 changes: 8 additions & 11 deletions sncosmo/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import warnings
from collections import OrderedDict
from packaging.version import parse as version_parse

import numpy as np

Expand Down Expand Up @@ -50,15 +51,6 @@ def generate_chisq(data, model, spectra, signature='iminuit', modelcov=False):
# parameters)
if signature == 'iminuit':
def chisq(*parameters):
# When a fit fails, iminuit sometimes calls the chisq function with
# the parameters set to nan. This sometimes leads to a segfault in
# sncosmo because the internal functions (specifically
# BicubicInterpolator) aren't designed to handle that. See
# https://github.com/sncosmo/sncosmo/issues/266 for details. For
# now, return nan to minuit if it tries to set any parameter to
# nan.
if np.any(np.isnan(parameters)):
return np.nan
model.parameters = parameters

full_chisq = 0.
Expand Down Expand Up @@ -662,6 +654,11 @@ def fit_lc(data=None, model=None, vparam_names=[], bounds=None, spectra=None,
_print_iminuit_params(vparam_names, kwargs)
print()

# Set up keyword arguments to Migrad.minuit
migrad_kwargs = {"ncall": maxcall}
if version_parse(iminuit.__version__) >= version_parse("1.5.2"):
migrad_kwargs["iterate"] = 1

# run once with no model covariance, regardless of whether
# modelcov=True
fitchisq = generate_chisq(fitdata, model, spectra, signature='iminuit',
Expand All @@ -680,7 +677,7 @@ def fit_lc(data=None, model=None, vparam_names=[], bounds=None, spectra=None,
forced_parameters=model.param_names,
print_level=(1 if verbose >= 2 else 0),
throw_nan=True, **kwargs)
d, l = m.migrad(ncall=maxcall)
d, l = m.migrad(**migrad_kwargs)
if verbose:
print("{} function calls; {} dof.".format(d.nfcn, ndof))

Expand Down Expand Up @@ -737,7 +734,7 @@ def fit_lc(data=None, model=None, vparam_names=[], bounds=None, spectra=None,
forced_parameters=model.param_names,
print_level=(1 if verbose >= 2 else 0),
throw_nan=True, **kwargs)
d, l = m.migrad(ncall=maxcall)
d, l = m.migrad(**migrad_kwargs)

if verbose:
print("{} function calls; {} dof.".format(d.nfcn, ndof))
Expand Down
4 changes: 2 additions & 2 deletions sncosmo/salt2utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ cdef class BicubicInterpolator(object):
y_j = yc[j]

# if y is out of range, we won't be using the value at all
if (y_j < self.ymin or y_j > self.ymax):
if (not (y_j >= self.ymin and y_j <= self.ymax)):
yflagvec[j] = -1
else:
iy = find_index_unsafe(self.yval, self.ny, y_j, iy)
Expand Down Expand Up @@ -243,7 +243,7 @@ cdef class BicubicInterpolator(object):
x_i = xc[i]

# precompute some stuff for x
if (x_i < self.xmin or x_i > self.xmax):
if (not (x_i >= self.xmin and x_i <= self.xmax)):
xflag = -1
else:
ix = find_index_unsafe(self.xval, self.nx, x_i, ix)
Expand Down

0 comments on commit a5e00dd

Please sign in to comment.