From fa17477cc87752aba621ae9e97fcd843ba2ba5c9 Mon Sep 17 00:00:00 2001 From: Nicholas Smith Date: Wed, 28 Feb 2024 10:24:55 -0600 Subject: [PATCH] Fix PyROOT binding registration (#224) * Fix PyROOT binding registration * flake8 is falling behind other tools these days * no print --- .github/workflows/ci.yml | 31 ++++++++++++++++++++++++++- .pre-commit-config.yaml | 2 +- src/correctionlib/JSONEncoder.py | 2 +- src/correctionlib/binding.py | 4 ++-- tests/test_binding.py | 36 ++++++++++++++++++++++++++++++++ tests/test_core_valid.py | 2 +- 6 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 tests/test_binding.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b86270af..950f832e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,6 @@ jobs: - python-version: pypy-3.7 runs-on: ubuntu-latest - steps: - uses: actions/checkout@v4 with: @@ -52,3 +51,33 @@ jobs: - name: Test package run: python -m pytest -ra + + checkroot: + name: Check ROOT bindings + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Set up Conda + uses: conda-incubator/setup-miniconda@v2.2.0 + with: + python-version: 3.11 + miniforge-variant: Mambaforge + channels: conda-forge,defaults + channel-priority: true + + - name: Install ROOT + shell: bash -l {0} + run: | + mamba install root + + - name: Install package + shell: bash -l {0} + run: python -m pip install .[test] + + - name: Test package + shell: bash -l {0} + run: python -m pytest -ra diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d9ff6837..49f3ab49 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,7 +30,7 @@ repos: args: ["--py36-plus"] - repo: https://github.com/pycqa/flake8 - rev: 4.0.1 + rev: 7.0.0 hooks: - id: flake8 exclude: docs/conf.py diff --git a/src/correctionlib/JSONEncoder.py b/src/correctionlib/JSONEncoder.py index 5ff0d272..1e87c30f 100755 --- a/src/correctionlib/JSONEncoder.py +++ b/src/correctionlib/JSONEncoder.py @@ -11,7 +11,7 @@ import gzip import json import math -from typing import Any, List, Type +from typing import Any, List, Type # noqa: F401 import pydantic diff --git a/src/correctionlib/binding.py b/src/correctionlib/binding.py index aebda674..93195504 100644 --- a/src/correctionlib/binding.py +++ b/src/correctionlib/binding.py @@ -15,6 +15,6 @@ def register_pyroot_binding() -> None: lib = lib / "libcorrectionlib.dylib" else: lib = lib / "libcorrectionlib.so" - gbl.gSystem.Load(lib) - gbl.gInterpreter.AddIncludePath(base_path / "include") + gbl.gSystem.Load(str(lib)) + gbl.gInterpreter.AddIncludePath(str(base_path / "include")) gbl.gROOT.ProcessLine('#include "correction.h"') diff --git a/tests/test_binding.py b/tests/test_binding.py new file mode 100644 index 00000000..0c290709 --- /dev/null +++ b/tests/test_binding.py @@ -0,0 +1,36 @@ +import pytest + +import correctionlib +import correctionlib.schemav2 as cs + + +def test_pyroot_binding(): + ROOT = pytest.importorskip("ROOT") + correctionlib.register_pyroot_binding() + assert ROOT.correction.CorrectionSet + + ptweight = cs.Correction( + name="ptweight", + version=1, + inputs=[ + cs.Variable(name="pt", type="real", description="Muon transverse momentum") + ], + output=cs.Variable( + name="weight", type="real", description="Multiplicative event weight" + ), + data=cs.Binning( + nodetype="binning", + input="pt", + edges=[10, 20, 30, 40, 50, 80, 120], + content=[1.1, 1.08, 1.06, 1.04, 1.02, 1.0], + flow="clamp", + ), + ) + cset = cs.CorrectionSet(schema_version=2, corrections=[ptweight]) + csetstr = cset.model_dump_json().replace('"', r"\"") + + ROOT.gInterpreter.Declare( + f'auto cset = correction::CorrectionSet::from_string("{csetstr}");' # noqa: B907 + ) + ROOT.gInterpreter.Declare('auto corr = cset->at("ptweight");') + assert ROOT.corr.evaluate([1.2]) == 1.1 diff --git a/tests/test_core_valid.py b/tests/test_core_valid.py index 3a14bf34..3965d120 100644 --- a/tests/test_core_valid.py +++ b/tests/test_core_valid.py @@ -38,7 +38,7 @@ def test_evaluator_validation(): ] for json in bad_json: - with pytest.raises(ValidationError): + with pytest.raises(ValidationError): # noqa: B908 schema.CorrectionSet.model_validate_json(json) pytest.fail(f"{json} did not fail validation") with pytest.raises(RuntimeError):