Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/github_actions/pypa/cibuildwhe…
Browse files Browse the repository at this point in the history
…el-2.16.1
  • Loading branch information
HDembinski authored Dec 7, 2023
2 parents 18aed0d + 86c5201 commit 9df7ad3
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
- uses: hendrikmuhs/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
- uses: hendrikmuhs/[email protected]
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
outputs:
tag: ${{ steps.tag.outputs.tag }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
Expand Down Expand Up @@ -56,12 +56,12 @@ jobs:
CIBW_BUILD: ${{ matrix.py }}-*
CIBW_ARCHS_LINUX: ${{ matrix.arch }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true

- if: ${{ matrix.arch == 'aarch64' }}
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3

- uses: pypa/[email protected]
env:
Expand All @@ -77,7 +77,7 @@ jobs:
name: source package
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true

Expand Down Expand Up @@ -111,7 +111,7 @@ jobs:
runs-on: ubuntu-latest
if: ${{ github.ref == 'refs/heads/main' }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: softprops/action-gh-release@v1
with:
name: v${{ needs.release_check.outputs.tag }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
python-version: "pypy-3.8"
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
# must come after checkout
Expand All @@ -51,7 +51,7 @@ jobs:
# py: /opt/python/cp309-cp309/bin/python
# img: quay.io/pypa/manylinux2014_aarch64
# steps:
# - uses: actions/checkout@v3
# - uses: actions/checkout@v4
# with:
# submodules: true
# - uses: docker/setup-qemu-action@v2
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ __pycache__

pip-wheel-metadata

bench/*.svg

.benchmarks
.DS_Store
.idea
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
repos:
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-case-conflict
- id: check-docstring-first
Expand All @@ -34,20 +34,20 @@ repos:

# Python formatting
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.7.0
rev: 23.11.0
hooks:
- id: black

# Ruff linter, replacement for flake8, pydocstyle, isort
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.0.286'
rev: 'v0.1.6'
hooks:
- id: ruff
args: [--fix, --show-fixes]

# C++ formatting
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v16.0.6
rev: v17.0.6
hooks:
- id: clang-format

Expand All @@ -62,7 +62,7 @@ repos:

# Python type checking
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.5.1'
rev: 'v1.7.1'
hooks:
- id: mypy
additional_dependencies: [numpy]
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ else()
endif()
target_include_directories(_core PRIVATE extern/root/math/minuit2/inc)
set_target_properties(_core PROPERTIES VISIBILITY_INLINES_HIDDEN ON)
target_compile_definitions(_core PUBLIC PYBIND11_DETAILED_ERROR_MESSAGES=1)

install(TARGETS _core DESTINATION iminuit)
110 changes: 92 additions & 18 deletions bench/plot.ipynb

Large diffs are not rendered by default.

104 changes: 75 additions & 29 deletions bench/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ def logsumexp(a, b):
r = np.empty_like(a)
for i in nb.prange(len(r)):
if a[i] > b[i]:
r[i] = a[i] + np.log(1 + np.exp(b[i] - a[i]))
c = a[i]
d = b[i]
else:
r[i] = b[i] + np.log(1 + np.exp(a[i] - b[i]))
c = b[i]
d = a[i]
r[i] = c + np.log1p(np.exp(d - c))
return r


Expand Down Expand Up @@ -210,34 +213,77 @@ def run():
assert_allclose(m.values[:-1], ARGS[:-1], atol=2 / n**0.5)


@pytest.mark.parametrize("n", N)
@pytest.mark.parametrize("BatchMode", [False, True])
@pytest.mark.parametrize("NumCPU", [0, nb.get_num_threads()])
def test_RooFit(benchmark, n, BatchMode, NumCPU):
try:
import ROOT as R

x = R.RooRealVar("x", "x", 0, 1)
z = R.RooRealVar("z", "z", 0.5, 0, 1)
mu = R.RooRealVar("mu", "mu", 0.5, 0, 1)
sigma = R.RooRealVar("sigma", "sigma", 0.1, 0, 10)
slope = R.RooRealVar("slope", "slope", 1.0, 0, 10)
pdf1 = R.RooGaussian("gauss", "gauss", x, mu, sigma)
pdf2 = R.RooExponential("expon", "expon", x, slope)
pdf = R.RooAddPdf("pdf", "pdf", [pdf1, pdf2], [z])
if R.__version__ >= "6.30":

@pytest.mark.parametrize("n", N)
@pytest.mark.parametrize("NumCPU", [0, nb.get_num_threads()])
@pytest.mark.parametrize(
"EvalBackend", ["legacy", "cpu", "codegen", "codegen_no_grad"]
)
def test_RooFit(benchmark, n, NumCPU, EvalBackend):
x = R.RooRealVar("x", "x", 0, 1)
z = R.RooRealVar("z", "z", 0.5, 0, 1)
mu = R.RooRealVar("mu", "mu", 0.5, 0, 1)
sigma = R.RooRealVar("sigma", "sigma", 0.1, 0, 10)
slope = R.RooRealVar("slope", "slope", 1.0, 0, 10)
pdf1 = R.RooGaussian("gauss", "gauss", x, mu, sigma)
pdf2 = R.RooExponential("expon", "expon", x, slope)
pdf = R.RooAddPdf("pdf", "pdf", [pdf1, pdf2], [z])

data = pdf.generate(x, n)

args = [R.RooFit.PrintLevel(-1), R.RooFit.EvalBackend(EvalBackend)]
if NumCPU:
args.append(R.RooFit.NumCPU(NumCPU))

def run():
mu.setVal(0.5)
sigma.setVal(0.1)
slope.setVal(1)
z.setVal(0.5)
pdf.fitTo(data, *args)

benchmark(run)
assert_allclose(z.getVal(), 0.5, atol=5 / n**0.5)
assert_allclose(mu.getVal(), 0.5, atol=5 / n**0.5)
assert_allclose(sigma.getVal(), 0.1, atol=5 / n**0.5)

data = pdf.generate(x, n)
else:

def run():
mu.setVal(0.5)
sigma.setVal(0.1)
slope.setVal(1)
z.setVal(0.5)
args = [R.RooFit.PrintLevel(-1), R.RooFit.BatchMode(BatchMode)]
if NumCPU:
args.append(R.RooFit.NumCPU(NumCPU))
pdf.fitTo(data, *args)

benchmark(run)
assert_allclose(z.getVal(), 0.5, atol=5 / n**0.5)
assert_allclose(mu.getVal(), 0.5, atol=5 / n**0.5)
assert_allclose(sigma.getVal(), 0.1, atol=5 / n**0.5)
@pytest.mark.parametrize("n", N)
@pytest.mark.parametrize("NumCPU", [0, nb.get_num_threads()])
@pytest.mark.parametrize("BatchMode", [False, True])
def test_RooFit(benchmark, n, NumCPU, BatchMode):
x = R.RooRealVar("x", "x", 0, 1)
z = R.RooRealVar("z", "z", 0.5, 0, 1)
mu = R.RooRealVar("mu", "mu", 0.5, 0, 1)
sigma = R.RooRealVar("sigma", "sigma", 0.1, 0, 10)
slope = R.RooRealVar("slope", "slope", 1.0, 0, 10)
pdf1 = R.RooGaussian("gauss", "gauss", x, mu, sigma)
pdf2 = R.RooExponential("expon", "expon", x, slope)
pdf = R.RooAddPdf("pdf", "pdf", [pdf1, pdf2], [z])

data = pdf.generate(x, n)

args = [R.RooFit.PrintLevel(-1), R.RooFit.BatchMode(BatchMode)]
if NumCPU:
args.append(R.RooFit.NumCPU(NumCPU, 1))
args.append(R.RooFit.Parallelize(NumCPU))

def run():
mu.setVal(0.5)
sigma.setVal(0.1)
slope.setVal(1)
z.setVal(0.5)
pdf.fitTo(data, *args)

benchmark(run)
assert_allclose(z.getVal(), 0.5, atol=5 / n**0.5)
assert_allclose(mu.getVal(), 0.5, atol=5 / n**0.5)
assert_allclose(sigma.getVal(), 0.1, atol=5 / n**0.5)

except ModuleNotFoundError:
pass
2 changes: 1 addition & 1 deletion src/iminuit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _set(self, i: int, arg: UserBound) -> None:
state.set_error(i, err)


def _normalize_limit(lim: UserBound) -> Tuple[float, float]:
def _normalize_limit(lim: Optional[Iterable]) -> Tuple[float, float]:
if lim is None:
return (-np.inf, np.inf)
a, b = lim
Expand Down
12 changes: 6 additions & 6 deletions tests/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ def grad2(x, b, a):
return g

def model3(x, c):
return c
return c * np.ones_like(x)

def grad3(x, c):
return np.ones((1, len(x)))
Expand Down Expand Up @@ -1201,7 +1201,7 @@ def grad3(x, c):
a = 2
b = 3
ref = np.zeros(2)
ref[0] += lsq1.grad(a)
ref[0] += lsq1.grad(a)[0]
ref[[1, 0]] += lsq2.grad(b, a)
assert_allclose(lsq12.grad(a, b), ref)

Expand All @@ -1214,9 +1214,9 @@ def grad3(x, c):
a = 2
b = 3
ref = np.zeros(2)
ref[0] += lsq1.grad(a)
ref[0] += lsq1.grad(a)[0]
ref[[1, 0]] += lsq2.grad(b, a)
ref[0] += lsq1.grad(a)
ref[0] += lsq1.grad(a)[0]
assert_allclose(lsq121.grad(a, b), ref)

lsq312 = lsq3 + lsq12
Expand All @@ -1229,8 +1229,8 @@ def grad3(x, c):
b = 3
c = 4
ref = np.zeros(3)
ref[0] += lsq3.grad(c)
ref[1] += lsq1.grad(a)
ref[0] += lsq3.grad(c)[0]
ref[1] += lsq1.grad(a)[0]
ref[[2, 1]] += lsq2.grad(b, a)
assert_allclose(lsq312.grad(c, a, b), ref)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def rosen(par):


def test_disp(capsys):
minimize(lambda x: x**2, 0)
minimize(lambda x: np.sum(x**2), 0)
assert capsys.readouterr()[0] == ""
minimize(lambda x: x**2, 0, options={"disp": True})
minimize(lambda x: np.sum(x**2), 0, options={"disp": True})
assert capsys.readouterr()[0] != ""


Expand Down Expand Up @@ -115,7 +115,7 @@ class Fcn:

def __call__(self, x):
self.n += 1
return x**2 + 1e-2 * (self.n % 3)
return np.sum(x**2 + 1e-2 * (self.n % 3))

r = minimize(Fcn(), [1], options={"maxfun": 100000000})
assert not r.success
Expand Down

0 comments on commit 9df7ad3

Please sign in to comment.