Skip to content

Commit

Permalink
Fix PyROOT binding registration (#224)
Browse files Browse the repository at this point in the history
* Fix PyROOT binding registration

* flake8 is falling behind other tools these days

* no print
  • Loading branch information
nsmith- authored Feb 28, 2024
1 parent 767029c commit fa17477
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
31 changes: 30 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ jobs:
- python-version: pypy-3.7
runs-on: ubuntu-latest


steps:
- uses: actions/checkout@v4
with:
Expand All @@ -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/[email protected]
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/correctionlib/JSONEncoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/correctionlib/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"')
36 changes: 36 additions & 0 deletions tests/test_binding.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/test_core_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit fa17477

Please sign in to comment.