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

Add mypy #291

Merged
merged 29 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bb523b4
Add mypy
felixhekhorn Jul 21, 2023
103567c
Restrict mypy and start fixing
felixhekhorn Jul 21, 2023
e7b6e93
Start fixing tests for num_flavs_ref=None
felixhekhorn Jul 21, 2023
bbfc166
Apply more mypy fixes
felixhekhorn Jul 21, 2023
2d8d8bf
Apply more mypy fixes 2
felixhekhorn Jul 21, 2023
494da3c
Rename OpMembers
felixhekhorn Jul 21, 2023
48ac90e
Fix mypy in io/legacy
felixhekhorn Jul 21, 2023
bab8cf2
Fix mypy in box/{genpdf,apply}
felixhekhorn Jul 21, 2023
e61c8e6
Fix ekomark/plots
felixhekhorn Jul 24, 2023
e40a718
Fix ekobox/cli
felixhekhorn Jul 24, 2023
9271ebc
Make managers class
felixhekhorn Jul 24, 2023
80b85f1
Use backported return in apply
felixhekhorn Jul 24, 2023
34c68a5
Fix eko/io/legacy masses usage
felixhekhorn Jul 24, 2023
d6d1734
Fix remaining mypy errors
felixhekhorn Jul 24, 2023
6e0f434
Remove init from benchmarks
felixhekhorn Jul 25, 2023
e8c218e
Drop new_op_key variable in grid
felixhekhorn Aug 17, 2023
54d9e53
Define couplings cache key type
felixhekhorn Aug 17, 2023
9b0d804
Define label type in apply
felixhekhorn Aug 17, 2023
3b57600
Cast labels in evop/Operator
felixhekhorn Aug 17, 2023
bbe955c
Update src/eko/runner/operators.py
felixhekhorn Aug 17, 2023
16e3809
Introduce OperatorLabel type
felixhekhorn Aug 17, 2023
c9cdc72
Remove list comprension in msbar
felixhekhorn Aug 17, 2023
c946b3f
Upgrade banana
felixhekhorn Aug 17, 2023
0451697
Merge branch 'master' into mypy
felixhekhorn Jan 12, 2024
a49fca6
Fix evol_pdf
felixhekhorn Jan 12, 2024
4be6d44
Merge branch 'master' into mypy
felixhekhorn Jul 15, 2024
0d13e35
Merge branch 'master' into mypy
felixhekhorn Aug 8, 2024
b651ebe
Fix Rust ev_op patch
felixhekhorn Aug 8, 2024
deb41cd
Merge branch 'master' into mypy
felixhekhorn Aug 8, 2024
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
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ repos:
args: ["--add-ignore=D107,D105"]
additional_dependencies:
- toml
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
hooks:
- id: mypy
additional_dependencies: [types-PyYAML]
pass_filenames: false
args: ["--ignore-missing-imports", "src/"]
- repo: https://github.com/pre-commit/pre-commit
rev: v3.3.3
hooks:
Expand Down
Empty file added benchmarks/__init__.py
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
Empty file added benchmarks/ekobox/__init__.py
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
5 changes: 3 additions & 2 deletions src/eko/couplings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

"""
import logging
from typing import Iterable, List
from typing import Dict, Iterable, List, Tuple

import numba as nb
import numpy as np
import numpy.typing as npt
import scipy

from . import constants, matchings
Expand Down Expand Up @@ -474,7 +475,7 @@ def assert_positive(name, var):
self.decoupled_running,
)
# cache
self.cache = {}
self.cache: Dict[Tuple[float, float, int, float, float], npt.NDArray] = {}
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved

@property
def mu2_ref(self):
Expand Down
10 changes: 7 additions & 3 deletions src/eko/evolution_operator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import time
from multiprocessing import Pool
from typing import Dict, Tuple

import numba as nb
import numpy as np
Expand Down Expand Up @@ -578,6 +579,9 @@ def quad_ker_qed(
return ker


OPMEMBERS = Dict[Tuple[int, int], OpMember]
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved


class Operator(sv.ModeMixin):
"""Internal representation of a single EKO.

Expand All @@ -603,8 +607,8 @@ class Operator(sv.ModeMixin):

log_label = "Evolution"
# complete list of possible evolution operators labels
full_labels = br.full_labels
full_labels_qed = br.full_unified_labels
full_labels = list(br.full_labels)
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
full_labels_qed = list(br.full_unified_labels)

def __init__(
self, config, managers, segment: Segment, mellin_cut=5e-2, is_threshold=False
Expand All @@ -617,7 +621,7 @@ def __init__(
# TODO make 'cut' external parameter?
self._mellin_cut = mellin_cut
self.is_threshold = is_threshold
self.op_members = {}
self.op_members: OPMEMBERS = {}
self.order = tuple(config["order"])
self.alphaem_running = self.managers["couplings"].alphaem_running
if self.log_label == "Evolution":
Expand Down
15 changes: 7 additions & 8 deletions src/eko/evolution_operator/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

"""
import logging
from dataclasses import astuple
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional

import numpy as np
import numpy.typing as npt
Expand All @@ -17,9 +16,9 @@
from ..interpolation import InterpolatorDispatcher
from ..io.runcards import Configs, Debug
from ..io.types import EvolutionPoint as EPoint
from ..io.types import Order
from ..io.types import Order, SquaredScale
from ..matchings import Atlas, Segment, flavor_shift, is_downward_path
from . import Operator, flavors, matching_condition, physical
from . import OPMEMBERS, Operator, flavors, matching_condition, physical
from .operator_matrix_element import OperatorMatrixElement

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,7 +60,7 @@ def __init__(
interpol_dispatcher: InterpolatorDispatcher,
):
# check
config = {}
config: Dict[str, Any] = {}
config["order"] = order
config["intrinsic_range"] = intrinsic_flavors
config["xif2"] = xif**2
Expand Down Expand Up @@ -95,8 +94,8 @@ def __init__(
couplings=couplings,
interpol_dispatcher=interpol_dispatcher,
)
self._threshold_operators = {}
self._matching_operators = {}
self._threshold_operators: Dict[Segment, Operator] = {}
self._matching_operators: Dict[SquaredScale, OPMEMBERS] = {}

def get_threshold_operators(self, path: List[Segment]) -> List[Operator]:
"""Generate the threshold operators.
Expand All @@ -118,7 +117,7 @@ def get_threshold_operators(self, path: List[Segment]) -> List[Operator]:
is_downward = is_downward_path(path)
shift = flavor_shift(is_downward)
for seg in path[:-1]:
new_op_key = astuple(seg)
new_op_key = seg
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
kthr = self.config["thresholds_ratios"][seg.nf - shift]
ome = OperatorMatrixElement(
self.config,
Expand Down
3 changes: 2 additions & 1 deletion src/eko/evolution_operator/operator_matrix_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import functools
import logging
from typing import List, Tuple

import numba as nb
import numpy as np
Expand Down Expand Up @@ -208,7 +209,7 @@ class OperatorMatrixElement(Operator):
(br.matching_hminus_pid, br.matching_hminus_pid),
]
# still valid in QED since Sdelta and Vdelta matchings are diagonal
full_labels_qed = copy.deepcopy(full_labels)
full_labels_qed: List[Tuple[int, int]] = copy.deepcopy(full_labels)
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, config, managers, nf, q2, is_backward, L, is_msbar):
super().__init__(config, managers, Segment(q2, q2, nf))
Expand Down
4 changes: 2 additions & 2 deletions src/eko/io/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import base64
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Dict, Generic, Optional, Type, TypeVar
from typing import Dict, Generic, Literal, Optional, Type, TypeVar

import yaml

from .access import AccessConfigs
from .items import Header, Operator

NBYTES = 8
ENDIANNESS = "little"
ENDIANNESS: Literal["little", "big"] = "little"

HEADER_EXT = ".yaml"
ARRAY_EXT = [".npy", ".npz"]
Expand Down
22 changes: 11 additions & 11 deletions src/eko/io/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,17 @@ def open(cls, path: os.PathLike, mode="r"):
raise ValueError(f"Unknown file mode: {mode}")

tmpdir = pathlib.Path(tempfile.mkdtemp(prefix=TEMP_PREFIX))
if load:
cls.load(path, tmpdir)
metadata = Metadata.load(tmpdir)
opened = cls(
**inventories(tmpdir, access),
metadata=metadata,
access=access,
)
opened.operators.sync()
else:
opened = Builder(path=tmpdir, access=access)
if not load:
return Builder(path=tmpdir, access=access)
# load existing instead
cls.load(path, tmpdir)
metadata = Metadata.load(tmpdir)
opened: EKO = cls(
**inventories(tmpdir, access),
metadata=metadata,
access=access,
)
opened.operators.sync()

return opened

Expand Down
2 changes: 1 addition & 1 deletion src/eko/msbar_masses.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def sc(thr_masses):
heavy_quarks = quark_names[3:]
hq_idxs = np.arange(0, 3)
if nf_ref > 4:
heavy_quarks = reversed(heavy_quarks)
heavy_quarks = "".join([e for e in reversed(heavy_quarks)])
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
hq_idxs = reversed(hq_idxs)

# loop on heavy quarks and compute the msbar masses
Expand Down
3 changes: 1 addition & 2 deletions src/eko/quantities/couplings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Types and quantities related to theory couplings."""
import dataclasses
import enum
from typing import Optional

from ..io.dictlike import DictLike
from ..io.types import FlavorsNumber, LinearScale, ReferenceRunning, Scalar
Expand All @@ -22,7 +21,7 @@ class CouplingsInfo(DictLike):
alphaem: Coupling
scale: LinearScale
max_num_flavs: FlavorsNumber
alecandido marked this conversation as resolved.
Show resolved Hide resolved
num_flavs_ref: Optional[FlavorsNumber]
num_flavs_ref: FlavorsNumber
r"""Number of active flavors at strong coupling reference scale.

I.e. :math:`n_{f,\text{ref}}(\mu^2_{\text{ref}})`, formerly called
Expand Down
2 changes: 1 addition & 1 deletion tests/eko/quantities/test_couplings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def test_couplings_ref():
scale = 90.0
d = dict(alphas=0.1, alphaem=0.01, scale=scale, max_num_flavs=6, num_flavs_ref=None)
d = dict(alphas=0.1, alphaem=0.01, scale=scale, max_num_flavs=6, num_flavs_ref=5)
couplings = CouplingsInfo.from_dict(d)
assert couplings.scale == scale
assert not couplings.em_running
28 changes: 15 additions & 13 deletions tests/eko/test_couplings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_init(self):
alphas=alpharef[0],
alphaem=alpharef[1],
scale=muref,
num_flavs_ref=None,
num_flavs_ref=5,
max_num_flavs=6,
)
)
Expand Down Expand Up @@ -152,18 +152,19 @@ def test_ref(self):
(0, np.inf, np.inf),
(2, 4, 175),
]
nfrefs = (3, 4, 5)
alpharef = (0.118, 0.00781)
muref = 91.0
couplings = CouplingsInfo.from_dict(
dict(
alphas=alpharef[0],
alphaem=alpharef[1],
scale=muref,
num_flavs_ref=None,
max_num_flavs=6,
for thresh_setup, nfref in zip(thresh_setups, nfrefs):
couplings = CouplingsInfo.from_dict(
dict(
alphas=alpharef[0],
alphaem=alpharef[1],
scale=muref,
num_flavs_ref=nfref,
max_num_flavs=6,
)
)
)
for thresh_setup in thresh_setups:
for order_s in [1, 2, 3, 4]:
for order_em in [0, 1, 2]:
for evmod in CouplingEvolutionMethod:
Expand Down Expand Up @@ -220,9 +221,10 @@ def test_exact(self):
(0, np.inf, np.inf),
(2, 4, 175),
]
nfrefs = (3, 4, 5)
alpharef = np.array([0.118, 0.00781])
muref = 91.0
for thresh_setup in thresh_setups:
for thresh_setup, nfref in zip(thresh_setups, nfrefs):
for qcd in range(1, 4 + 1):
for qed in range(2 + 1):
for em_running in [
Expand All @@ -235,7 +237,7 @@ def test_exact(self):
alphas=alpharef[0],
alphaem=alpharef[1],
scale=muref,
num_flavs_ref=None,
num_flavs_ref=nfref,
max_num_flavs=6,
em_running=em_running,
)
Expand Down Expand Up @@ -298,7 +300,7 @@ def benchmark_expanded_n3lo(self):
alphas=alpharef[0],
alphaem=alpharef[1],
scale=muref,
num_flavs_ref=None,
num_flavs_ref=5,
max_num_flavs=6,
)
m2c = 2
Expand Down