Skip to content

Commit

Permalink
Merge pull request #154 from jiangyi15/pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
[pre-commit.ci] pre-commit autoupdate
  • Loading branch information
jiangyi15 authored Oct 26, 2024
2 parents dc0326f + d0ba406 commit 3a62e16
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[test]
pip install .[test] numpy==1.26
python -m pip install uproot3
- name: Test with pytest
run: |
Expand Down
23 changes: 13 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: check-useless-excludes

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
rev: v5.0.0
hooks:
- id: check-ast
- id: check-case-conflict
Expand All @@ -21,32 +21,32 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 22.3.0
rev: 24.10.0
hooks:
- id: black
language_version: python3 # Should be a command that runs python3.6+

- repo: https://github.com/PyCQA/doc8
rev: 0.8.1
rev: v1.1.2
hooks:
- id: doc8
args: ["--ignore=D001", "-q"]
additional_dependencies:
- importlib_metadata<5.0.0

- repo: https://github.com/kynan/nbstripout
rev: 0.3.8
rev: 0.7.1
hooks:
- id: nbstripout
files: ".ipynb"

- repo: https://github.com/prettier/pre-commit
rev: v2.1.2
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.8.0
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort

Expand All @@ -59,8 +59,11 @@ repos:
types: [python]
args: ["--rcfile=.pylintrc", "--score=no"]

- repo: https://github.com/myint/rstcheck
rev: 3f92957 # Use the sha / tag you want to point at
- repo: https://github.com/rstcheck/rstcheck
rev: v6.2.4 # Use the sha / tag you want to point at
hooks:
- id: rstcheck
files: \.(inc|rst)$

ci:
skip: [pylint]
40 changes: 34 additions & 6 deletions checks/resolution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ This is the toy distribution.
The detector model, the reconstructed value is the truch value plus a gauss
random number.

$$ m' = m + \delta ,\ \delta \sim \frac{1}{\sqrt{2\pi}\sigma} \exp(-\frac{(\delta-\mu)^2}{2\sigma^2})$$
<!-- prettier-ignore -->
$$
m' = m + \delta ,\ \delta \sim \frac{1}{\sqrt{2\pi}\sigma}
\exp(-\frac{(\delta-\mu)^2}{2\sigma^2})
$$

There is also a simple linear efficency with a mass range cut for reconstucted
value.

<!-- prettier-ignore -->
$$ \epsilon(m, m') = m \theta(m'-m_{min}) \theta(m_{max}-m') ,$$

$\theta(x)$ is the step function.
Expand All @@ -39,7 +44,11 @@ Analytical form of truth and reconstructed value distribution.

The function of detector.py is such transition funtion.

$$ T(m, m') = \frac{1}{\sqrt{2\pi}\sigma}\exp(-\frac{(m'-m-\mu)^2}{2\sigma^2}) \epsilon(m, m') \rho(m) .$$
<!-- prettier-ignore -->
$$
T(m, m') = \frac{1}{\sqrt{2\pi}\sigma}\exp(-\frac{(m'-m-\mu)^2}{2\sigma^2})
\epsilon(m, m') \rho(m) .
$$

$\rho(m)=pq$ is the phase space factor, $p$, $q$ is the breakup monmentum of
$A\rightarrow (BC) + D$ and $(BC) \rightarrow B + C$.
Expand All @@ -51,15 +60,21 @@ effect of efficency, we can set $\epsilon(m,m')=1$.

The projections of the truth and reconstucted value distrubution are

<!-- prettier-ignore -->
$$ f_{truth}(m) = \int T(m, m') dm',$$

<!-- prettier-ignore -->
$$ f_{rec}(m') = \int T(m, m') dm,$$

![img](./assets/m_rec.png)

and

$$ f_{delta}(\delta) = \int T(\frac{m_s - \delta}{2}, \frac{m_s + \delta}{2}) d m_s.$$
<!-- prettier-ignore -->
$$
f_{delta}(\delta) = \int T(\frac{m_s - \delta}{2}, \frac{m_s + \delta}{2})
d m_s.
$$

![img](./assets/m_diff.png)

Expand All @@ -69,6 +84,7 @@ It is a little different from gauss function because of the range cut.

Generate truth values for resolution. The resolution function is defined as

<!-- prettier-ignore -->
$$ R(m \rightarrow m') = \frac{T(m, m')}{\int T(m,m') d m} .$$

For every reconstructed values $m'$ (N events), we generate some $m_i$ (M
Expand All @@ -90,15 +106,27 @@ $x_i, A_i$ is sample points and weights for Gauss-Legendre quadrature.

The probobility of reconstructed value is

$$ P(m') = \int |A|^2 (m) T(m, m') dm \approx \epsilon_{rec}(m') \sum w_i' |A|^2(m_i). $$
<!-- prettier-ignore -->
$$
P(m') = \int |A|^2 (m) T(m, m') dm \approx \epsilon_{rec}(m') \sum w_i'
|A|^2(m_i).
$$

The normalised factor is

$$ \int P(m') d\Phi = \int |A|^2 (m) \int T(m, m') dm' dm \approx \sum_{m\sim f_{truth}(m)} |A|^2(m), $$
<!-- prettier-ignore -->
$$
\int P(m') d\Phi = \int |A|^2 (m) \int T(m, m') dm' dm \approx \sum_{m\sim
f_{truth}(m)} |A|^2(m),
$$

The negative log-likelihood (NLL) value is

$$ -\ln L = - \sum \ln P(m_j') + N \ln \int P(m_i') d \Phi \approx - \sum_{j} \ln \sum w_i' |A|(m_{ij}) + N \ln \int P(m_i') d \Phi + constant.$$
<!-- prettier-ignore -->
$$
-\ln L = - \sum \ln P(m_j') + N \ln \int P(m_i') d \Phi \approx - \sum_{j}
\ln \sum w_i' |A|(m_{ij}) + N \ln \int P(m_i') d \Phi + constant.
$$

This is fit result from one test.

Expand Down
7 changes: 5 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,8 @@ quiet=1

[rstcheck]
ignore_directives =
automodule
report=warning
automodule,
toctree,
seealso,
report_level=WARNING
ignore_roles=ref,file,doc,
7 changes: 4 additions & 3 deletions tf_pwa/adaptive_bins.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ def single_split_bound(data, n=2, base_bound=None):
"""split data in the order of data value
>>> data = np.array([1.0, 2.0, 1.4, 3.1])
>>> AdaptiveBound.single_split_bound(data)
[(1.0, 1.7...), (1.7..., 3.1...)]
>>> bound = AdaptiveBound.single_split_bound(data)
>>> [(float(i[0]+1e-6), float(i[1]+1e-6)) for i in bound]
[(1.0..., 1.7...), (1.7..., 3.1...)]
"""
if base_bound is None:
Expand All @@ -102,7 +103,7 @@ def multi_split_bound(datas, n, base_bound=None):
>>> data = np.array([[1.0, 2.0, 1.4, 3.1], [2.0, 1.0, 3.0, 1.0]])
>>> bound, _ = AdaptiveBound.multi_split_bound(data, [2, 1])
>>> [(i[0][0]+1e-6, i[1][0]+1e-6) for i in bound]
>>> [(float(i[0][0]+1e-6), float(i[1][0]+1e-6)) for i in bound]
[(1.0..., 1.7...), (1.7..., 3.1...)]
"""
Expand Down
1 change: 0 additions & 1 deletion tf_pwa/amp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Basic amplitude model
"""


import numpy as np

from tf_pwa.breit_wigner import (
Expand Down
2 changes: 2 additions & 0 deletions tf_pwa/amp/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ def get_matrix_interp1d3(x, xi):
N = len(xi) - 1
zeros = tf.zeros_like(x)
ones = tf.ones_like(x)

# @pysnooper.snoop()
def poly_i(i):
tmp = zeros
Expand Down Expand Up @@ -620,6 +621,7 @@ def get_matrix_interp1d3_v2(x, xi):
N = len(xi) - 1
zeros = tf.zeros_like(x)
ones = tf.ones_like(x)

# @pysnooper.snoop()
def poly_i(i):
tmp = zeros
Expand Down
1 change: 1 addition & 0 deletions tf_pwa/angle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module implements three classes **Vector3**, **LorentzVector**, **EulerAngle** .
"""

from .tensorflow_wrapper import numpy_cross, tf

_epsilon = 1.0e-14
Expand Down
1 change: 1 addition & 0 deletions tf_pwa/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
In general, users only need to import functions in this module to implement their physical analysis instead of
going into every modules. There are some example files where you can figure out how it is used.
"""

import os
import time
import warnings
Expand Down
1 change: 1 addition & 0 deletions tf_pwa/cal_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
Inner nodes are named as tuple of particles.
"""

import itertools

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions tf_pwa/dec_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
module for parsing decay card :code:`*.dec` file
"""

import re
import warnings

Expand Down
1 change: 1 addition & 0 deletions tf_pwa/experimental/factor_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
We can add some options to change the possible combination. (TODO)
"""

import contextlib
import itertools

Expand Down
3 changes: 1 addition & 2 deletions tf_pwa/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def fit_minuit(fcn, bounds_dict={}, hesse=True, minos=False, **kwargs):


def fit_minuit_v1(fcn, bounds_dict={}, hesse=True, minos=False, **kwargs):

"""
:param fcn:
Expand Down Expand Up @@ -91,7 +90,6 @@ def fit_minuit_v1(fcn, bounds_dict={}, hesse=True, minos=False, **kwargs):


def fit_minuit_v2(fcn, bounds_dict={}, hesse=True, minos=False, **kwargs):

"""
:param fcn:
Expand Down Expand Up @@ -226,6 +224,7 @@ def fit_scipy(
maxiter = max(100 * len(x0), 2000)
min_nll = 0.0
ndf = fcn.vm.get_all_val(True)

# maxiter = 0
def v_g2(x0):
f_g = fcn.vm.trans_fcn_grad(fcn.nll_grad)
Expand Down
2 changes: 1 addition & 1 deletion tf_pwa/fit_improve.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def fmin_bfgs_f(
Delta=10.0,
maxiter=None,
callback=None,
norm_ord=np.Inf,
norm_ord=np.inf,
**_kwargs
):
"""test BFGS with nonmonote line search"""
Expand Down
8 changes: 2 additions & 6 deletions tf_pwa/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def get_relative_p(ma, mb, mc):


def get_relative_p2(ma, mb, mc):
return (
((ma**2 - (mb + mc) ** 2) * (ma**2 - (mb - mc) ** 2)) / 4 / ma / ma
)
return ((ma**2 - (mb + mc) ** 2) * (ma**2 - (mb - mc) ** 2)) / 4 / ma / ma


def BW_dom(m, m0, g0):
Expand Down Expand Up @@ -63,9 +61,7 @@ def BWR_LS_dom(m, m0, g0, thetas, ls, m1, m2, d=3.0, fix_bug1=False):
p0 = get_relative_p2(m0, m1, m2)

def bf_f(l):
bf = Bprime_polynomial(l, p0 * d**2) / Bprime_polynomial(
l, p * d**2
)
bf = Bprime_polynomial(l, p0 * d**2) / Bprime_polynomial(l, p * d**2)
return (p / p0) ** l * bf

g_head = sympy.I * m0 * g0 * m / m0 * sympy.sqrt(p / p0)
Expand Down
3 changes: 1 addition & 2 deletions tf_pwa/generator/plane_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def _solve_2(a1, a2, x0, y):
from numpy import sqrt

return (
-a1
+ sqrt(a1**2 + 4 * a1 * a2 * x0 + 4 * a2**2 * x0**2 + 4 * a2 * y)
-a1 + sqrt(a1**2 + 4 * a1 * a2 * x0 + 4 * a2**2 * x0**2 + 4 * a2 * y)
) / (2 * a2)


Expand Down
1 change: 0 additions & 1 deletion tf_pwa/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,6 @@ def grad_hessp(self, x, p, batch=None):


class MixLogLikehoodFCN(CombineFCN):

"""
This class implements methods to calculate the NLL as well as its derivatives for a general function.
Expand Down
3 changes: 1 addition & 2 deletions tf_pwa/model/opt_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,7 @@ def grad_hessp_batch(self, p, data, mcdata, weight, mc_weight):

g_int_mc = np.array(g_int_mc)
hessp2 = sw * (
hessp_int_mc / int_mc
- g_int_mc * np.dot(p, g_int_mc) / int_mc**2
hessp_int_mc / int_mc - g_int_mc * np.dot(p, g_int_mc) / int_mc**2
)
# print("hessp2", hessp2)
# print("ret", g, hessp2 - hessp_ln_data)
Expand Down
1 change: 1 addition & 0 deletions tf_pwa/particle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module implements classes to describe particles and decays.
"""

import functools
from collections import UserList

Expand Down
1 change: 1 addition & 0 deletions tf_pwa/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module provides some functions that may be useful in other modules.
"""

import functools
import json
import math
Expand Down
1 change: 1 addition & 0 deletions tf_pwa/variable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module implements classes and methods to manage the variables in fitting.
"""

import contextlib
import warnings

Expand Down
4 changes: 1 addition & 3 deletions tutorials/examples/adaptive_chi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ def cal_chi2_config(
data_cut = np.array([data_index(data, idx) for idx in data_idx])
amp_weight = config.get_amplitude()(phsp).numpy()
phsp_cut = np.array([data_index(phsp, idx) for idx in data_idx])
phsp_slice = np.concatenate(
[np.array(phsp_cut**2), [amp_weight]], axis=0
)
phsp_slice = np.concatenate([np.array(phsp_cut**2), [amp_weight]], axis=0)
phsps = adapter.split_data(phsp_slice)
datas = adapter.split_data(data_cut**2)
bound = adapter.get_bounds()
Expand Down
1 change: 0 additions & 1 deletion tutorials/examples/check_interf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""


import numpy as np
import tensorflow as tf

Expand Down

0 comments on commit 3a62e16

Please sign in to comment.