Skip to content

Commit

Permalink
Merge pull request #20 from denehoffman/acceptance-correction
Browse files Browse the repository at this point in the history
Acceptance corrections and saving fits
  • Loading branch information
denehoffman authored Nov 20, 2024
2 parents f026533 + 644750b commit f7e5ae5
Show file tree
Hide file tree
Showing 35 changed files with 7,188 additions and 1,782 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,29 @@ def main():

s0p = manager.register(ld.Scalar("s0p", parameter("s0p")))
s0n = manager.register(ld.Scalar("s0n", parameter("s0n")))
d2p = manager.register(ld.ComplexScalar("d2", parameter("d2 re"), parameter("d2 im")))
d2p = manager.register(ld.ComplexScalar("d2p", parameter("d2 re"), parameter("d2 im")))

pos_re = (s0p * z00p.real() + d2p * z22p.real()).norm_sqr()
pos_im = (s0p * z00p.imag() + d2p * z22p.imag()).norm_sqr()
neg_re = (s0n * z00n.real()).norm_sqr()
neg_im = (s0n * z00n.imag()).norm_sqr()
model = pos_re + pos_im + neg_re + neg_im

nll = ld.NLL(manager, ds_data, ds_mc)
status = nll.minimize(model, [1.0] * len(nll.parameters))
nll = ld.NLL(manager, model, ds_data, ds_mc)
status = nll.minimize([1.0] * len(nll.parameters))
print(status)
fit_weights = nll.project(status.x, model)
fit_weights = nll.project(status.x)
s0p_weights = nll.project_with(status.x, ["z00p", "s0p"])
s0n_weights = nll.project_with(status.x, ["z00n", "s0n"])
d2p_weights = nll.project_with(status.x, ["z22p", "d2p"])
masses_mc = res_mass.value_on(ds_mc)
masses_data = res_mass.value_on(ds_data)
weights_data = ds_data.weights
plt.hist(masses_data, weights=weights_data, bins=80, range=(1.0, 2.0), label="Data", histtype="step")
plt.hist(masses_mc, weights=fit_weights, bins=80, range=(1.0, 2.0), label="Fit", histtype="step")
plt.hist(masses_mc, weights=s0p_weights, bins=80, range=(1.0, 2.0), label="$S_0^+$", histtype="step")
plt.hist(masses_mc, weights=s0n_weights, bins=80, range=(1.0, 2.0), label="$S_0^-$", histtype="step")
plt.hist(masses_mc, weights=d2p_weights, bins=80, range=(1.0, 2.0), label="$D_2^+$", histtype="step")
plt.legend()
plt.savefig("demo.svg")

Expand Down
2 changes: 1 addition & 1 deletion benches/kmatrix_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn kmatrix_nll_benchmark(c: &mut Criterion) {
let neg_re = (&s0n * z00n.real()).norm_sqr();
let neg_im = (&s0n * z00n.imag()).norm_sqr();
let model = pos_re + pos_im + neg_re + neg_im;
let nll = NLL::new(&manager, &ds_data, &ds_mc, &model);
let nll = NLL::new(&manager, &model, &ds_data, &ds_mc);
let mut rng = rand::thread_rng();
let range = Uniform::new(-100.0, 100.0);
c.bench_function("kmatrix benchmark (nll)", |b| {
Expand Down
3 changes: 0 additions & 3 deletions docs/requirements.txt

This file was deleted.

2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ["sphinx.ext.autodoc", "sphinx.ext.autosummary", "sphinx.ext.napoleon"]
extensions = ["sphinx.ext.autodoc", "sphinx.ext.autosummary", "sphinx.ext.napoleon", "sphinx_copybutton"]

autodoc_default_options = {
"members": True,
Expand Down
10 changes: 5 additions & 5 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
laddu
=====

``laddu`` is an library for analyzing particle physics data. It is written in Rust with bindings to Python which are documented here.
`laddu <https://github.com/denehoffman/laddu>`_ is an library for analyzing particle physics data. It is written in Rust with bindings to Python which are documented here.

Right now, this page just documents the basic Python API of the library, but the plan is to eventually write a tutorial here for general use. See the `GitHub Repository <https://github.com/denehoffman/laddu>`_ for more details.

.. toctree::
:caption: API Reference
:maxdepth: 2
:caption: Contents
:maxdepth: 1
:name: sec-api


tutorials/index
modules/index
6 changes: 4 additions & 2 deletions docs/source/modules/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Submodules
==========
API Reference
=============

This is the main API documentation for ``laddu``.

.. toctree::
:maxdepth: 1
Expand Down
2 changes: 1 addition & 1 deletion docs/source/modules/laddu/amplitudes/index.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Amplitudes
amplitudes
==========

.. automodule:: laddu.amplitudes
Expand Down
2 changes: 1 addition & 1 deletion docs/source/modules/laddu/data.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Data
data
====

.. automodule:: laddu.data
Expand Down
2 changes: 1 addition & 1 deletion docs/source/modules/laddu/likelihoods.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Likelihoods
likelihoods
===========

.. automodule:: laddu.likelihoods
Expand Down
2 changes: 1 addition & 1 deletion docs/source/modules/laddu/utils/index.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Utilities
utils
=========

.. toctree::
Expand Down
4 changes: 4 additions & 0 deletions docs/source/tutorials/binned_fit.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Binned Fitting Tutorial
=======================

.. note:: Tutorial under construction!
11 changes: 11 additions & 0 deletions docs/source/tutorials/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Tutorials
=========

To get started with ``laddu``, please refer to the following tutorials for the basic operation of the library and the API reference for more details.

.. toctree::
:maxdepth: 1

unbinned_fit
binned_fit
live_plotting
4 changes: 4 additions & 0 deletions docs/source/tutorials/live_plotting.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Live Plotting
=============

.. note:: Tutorial under construction!
325 changes: 325 additions & 0 deletions docs/source/tutorials/unbinned_fit.rst

Large diffs are not rendered by default.

Binary file added docs/source/tutorials/unbinned_fit_efficiency.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/tutorials/unbinned_fit_result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ amptools-to-laddu = "laddu:convert.run"

[project.optional-dependencies]
tests = ["pytest"]
docs = ["sphinx", "sphinx-rtd-theme"]
docs = ["sphinx", "sphinx-rtd-theme", "sphinx-copybutton"]

[tool.maturin]
python-source = "python"
Expand Down
2 changes: 1 addition & 1 deletion python/laddu/amplitudes/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Amplitude: ...
class Manager:
def __init__(self) -> None: ...
def register(self, amplitude: Amplitude) -> AmplitudeID: ...
def load(self, dataset: Dataset, expression: Expression) -> Evaluator: ...
def load(self, expression: Expression, dataset: Dataset) -> Evaluator: ...

class Evaluator:
parameters: list[str]
Expand Down
22 changes: 17 additions & 5 deletions python/laddu/likelihoods/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from typing import Any, Literal
import numpy as np
import numpy.typing as npt

from laddu.amplitudes import Expression, Manager
from laddu.amplitudes import Evaluator, Expression, Manager
from laddu.data import Dataset

class LikelihoodID:
Expand Down Expand Up @@ -43,18 +43,30 @@ class LikelihoodEvaluator:
class NLL:
parameters: list[str]
data: Dataset
mc: Dataset
def __init__(self, manager: Manager, ds_data: Dataset, ds_mc: Dataset, expression: Expression) -> None: ...
accmc: Dataset
def __init__(
self,
manager: Manager,
expression: Expression,
ds_data: Dataset,
ds_accmc: Dataset,
) -> None: ...
def as_term(self) -> LikelihoodTerm: ...
def activate(self, name: str | list[str]) -> None: ...
def activate_all(self) -> None: ...
def deactivate(self, name: str | list[str]) -> None: ...
def deactivate_all(self) -> None: ...
def isolate(self, name: str | list[str]) -> None: ...
def evaluate(self, parameters: list[float] | npt.NDArray[np.float64]) -> float: ...
def project(self, parameters: list[float] | npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: ...
def project(
self, parameters: list[float] | npt.NDArray[np.float64], *, mc_evaluator: Evaluator | None = None
) -> npt.NDArray[np.float64]: ...
def project_with(
self, parameters: list[float] | npt.NDArray[np.float64], name: str | list[str]
self,
parameters: list[float] | npt.NDArray[np.float64],
name: str | list[str],
*,
mc_evaluator: Evaluator | None = None,
) -> npt.NDArray[np.float64]: ...
def minimize(
self,
Expand Down
Binary file added python_examples/example_1/accmc_1.parquet
Binary file not shown.
Loading

0 comments on commit f7e5ae5

Please sign in to comment.