Skip to content

Commit

Permalink
ENH: pin computed values with assert statements (#352)
Browse files Browse the repository at this point in the history
* DX: allow concurrency on main branch
* DX: allow running `pytest --nbmake docs`
* DX: report call issues with `pyright` and fix
* FIX: address pyright issues
  • Loading branch information
redeboer authored Mar 19, 2024
1 parent 4d85cf1 commit 1000969
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 15 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: CI

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
cancel-in-progress: |-
${{ github.ref != format('refs/heads/{0}', github.event.repository.default_branch) }}
env:
PYTHONHASHSEED: "0"
Expand All @@ -28,15 +29,18 @@ on:
jobs:
doc:
uses: ./.github/workflows/ci-docs.yml
permissions:
pages: write
id-token: write
with:
specific-pip-packages: ${{ inputs.specific-pip-packages }}

pytest:
uses: ComPWA/actions/.github/workflows/pytest.yml@v1
with:
additional-extras: all
specific-pip-packages: ${{ inputs.specific-pip-packages }}

style:
if: inputs.specific-pip-packages == ''
secrets:
token: ${{ secrets.PAT }}
uses: ComPWA/actions/.github/workflows/pre-commit.yml@v1
38 changes: 38 additions & 0 deletions docs/appendix/ls-model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,44 @@
"Latex(src)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": true,
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": ""
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"from deepdiff import DeepDiff\n",
"\n",
"actual = {p.name: val for p, (val, err) in DECAY_RATES.items()}\n",
"expected = {\n",
" \"L(1405)\": 0.07778927301797013,\n",
" \"L(1520)\": 0.01914040908048272,\n",
" \"L(1600)\": 0.05156508524716874,\n",
" \"L(1670)\": 0.011527651764617479,\n",
" \"L(1690)\": 0.011620347822014534,\n",
" \"L(2000)\": 0.09545104884536759,\n",
" \"D(1232)\": 0.2873470262996363,\n",
" \"D(1600)\": 0.044995124448579754,\n",
" \"D(1700)\": 0.03886137420714012,\n",
" \"K(700)\": 0.029895286408557483,\n",
" \"K(892)\": 0.21953853300085302,\n",
" \"K(1430)\": 0.14700511274990097,\n",
"}\n",
"diff = DeepDiff(actual, expected, significant_digits=16)\n",
"assert not diff, diff.pretty()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/support_bibtex_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setup(app: Sphinx) -> None:

@classmethod
def _patch_from_latex(cls, latex: str) -> LaTeXMathParser:
return LaTeXMathParser(codecs.decode(latex, "ulatex")).parse()
return LaTeXMathParser(codecs.decode(latex, "ulatex")).parse() # pyright:ignore[reportCallIssue]


def _patch_format_math(self, text: list[Text]) -> list[docutils.nodes.math]:
Expand Down
42 changes: 42 additions & 0 deletions docs/intensity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,48 @@
"source": [
"np.testing.assert_almost_equal(compute_sum_over_decay_rates(sub_decay_rates), 1.0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": true,
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": ""
},
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"first_row = 100 * sub_decay_rates[0, ::-1]\n",
"expected = [\n",
" +26.63558177829345,\n",
" -65.25172205275672,\n",
" +106.83711625679399,\n",
" -72.49501131450914,\n",
" -9.84552389611183,\n",
" -18.67959609075919,\n",
" +22.71375508928650,\n",
" -33.99801046138805,\n",
" +26.47076007599460,\n",
" -69.45996359005044,\n",
" -13.91443000032757,\n",
" +65.45026432137945,\n",
"]\n",
"src = np.array2string(\n",
" first_row,\n",
" floatmode=\"unique\",\n",
" formatter={\"float_kind\": lambda x: f\"{x:+.14f}\"},\n",
" separator=\", \",\n",
")\n",
"print(src)\n",
"np.testing.assert_array_almost_equal(first_row, expected)"
]
}
],
"metadata": {
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dev = [
]
doc = [
"cairosvg",
"deepdiff",
"ipympl",
"ipywidgets",
"kaleido",
Expand Down Expand Up @@ -135,7 +136,7 @@ include = ["src", "tests"]
reportArgumentType = false
reportAssignmentType = false
reportAttributeAccessIssue = false
reportCallIssue = false
reportCallIssue = true
reportGeneralTypeIssues = false
reportImportCycles = true
reportMissingParameterType = false
Expand Down Expand Up @@ -165,6 +166,7 @@ addopts = [
"--doctest-continue-on-failure",
"--doctest-modules",
"--durations=3",
"--ignore=docs/appendix/widget.ipynb",
"--ignore=docs/conf.py",
"-m not slow",
]
Expand All @@ -176,6 +178,7 @@ markers = ["slow: marks tests as slow (select with '-m slow')"]
norecursedirs = [
".ipynb_checkpoints",
"_build",
"docs/extensions",
]
testpaths = [
"src",
Expand Down
4 changes: 2 additions & 2 deletions src/polarimetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def formulate_polarimetry(
ref = reference_subsystem
return tuple(
PoolSum(
builder.formulate_aligned_amplitude(λ0, *λ, ref)[0].conjugate()
builder.formulate_aligned_amplitude(λ0, *λ, ref)[0].conjugate() # pyright:ignore[reportCallIssue]
* pauli_matrix[_to_index(λ0), _to_index(λ0_prime)]
* builder.formulate_aligned_amplitude(λ0_prime, *λ, ref)[0],
* builder.formulate_aligned_amplitude(λ0_prime, *λ, ref)[0], # pyright:ignore[reportCallIssue]
(λ0, [-half, +half]),
(λ0_prime, [-half, +half]),
*λ.items(),
Expand Down
5 changes: 3 additions & 2 deletions src/polarimetry/amplitude/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ def formulate(
parameter_defaults = {}
for args in product(*allowed_helicities.values()):
for sub_system in [1, 2, 3]:
chain_model = self.formulate_subsystem_amplitude(*args, sub_system)
chain_model = self.formulate_subsystem_amplitude(*args, sub_system) # pyright:ignore[reportCallIssue]
amplitude_definitions.update(chain_model.amplitudes)
angle_definitions.update(chain_model.variables)
parameter_defaults.update(chain_model.parameter_defaults)
aligned_amp, zeta_defs = self.formulate_aligned_amplitude(
*helicity_symbols, reference_subsystem
*helicity_symbols,
reference_subsystem, # pyright:ignore[reportCallIssue]
)
angle_definitions.update(zeta_defs)
masses = create_mass_symbol_mapping(self.decay)
Expand Down
2 changes: 1 addition & 1 deletion src/polarimetry/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
def compute_sub_function(
func: ParametrizedFunction,
input_data: DataSample,
non_zero_couplings: list[Pattern],
non_zero_couplings: list[str],
):
old_parameters = dict(func.parameters)
pattern = rf"\\mathcal{{H}}.*\[(LS,)?(?!{'|'.join(non_zero_couplings)})"
Expand Down
8 changes: 4 additions & 4 deletions src/polarimetry/lhcb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def load_model(
imported_parameter_values = load_model_parameters(
model_file, builder.decay, model_id, particle_definitions
)
model.parameter_defaults.update(imported_parameter_values)
model.parameter_defaults.update(imported_parameter_values) # pyright:ignore[reportCallIssue]
return model


Expand Down Expand Up @@ -308,7 +308,7 @@ def load_model_parameters_with_uncertainties(
parameter_definitions, min_ls, particle_definitions
)
decay_couplings = compute_decay_couplings(decay)
parameters.update(decay_couplings)
parameters.update(decay_couplings) # pyright:ignore[reportCallIssue]
return parameters


Expand Down Expand Up @@ -359,12 +359,12 @@ def flip_production_coupling_signs(obj: _T, subsystem_names: Iterable[Pattern])
bootstrap._parameters = _flip_signs(bootstrap._parameters, subsystem_names) # type: ignore[reportPrivateUsage]
return bootstrap
if isinstance(obj, dict):
return _flip_signs(obj)
return _flip_signs(obj, subsystem_names)
raise NotImplementedError


def _flip_signs(
parameters: dict[_K, _V], subsystem_names: Iterable[Pattern]
parameters: dict[_K, _V], subsystem_names: Iterable[str]
) -> dict[_K, _V]:
pattern = rf".*\\mathrm{{production}}\[[{''.join(subsystem_names)}].*"
return {
Expand Down
3 changes: 2 additions & 1 deletion src/polarimetry/lhcb/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ def _get_mandelstam_s(decay: ThreeBodyDecayChain) -> sp.Symbol:
return s2
if decay_masses == {m1, m2}:
return s3
msg = f"Cannot find Mandelstam variable for {''.join(decay_masses)}"
decay_masses_str = "".join(str(s) for s in sorted(decay_masses, key=str))
msg = f"Cannot find Mandelstam variable for {''.join(decay_masses_str)}"
raise NotImplementedError(msg)


Expand Down

0 comments on commit 1000969

Please sign in to comment.