Skip to content

Commit 0e589f9

Browse files
JaLuka98Copilot
andauthored
First stable version (#1)
* First version of the code * Changing the starting value makes the fits converge, good, need to adjust that slightly * Improvement in minimisation routine now also allows C=0 starting value * Add two sensible physical test * Fix formatting and lints * Update docs/source/conf.py Residual name change Co-authored-by: Copilot <[email protected]> * Ruffing tests * Fix mypy requirements and pipeline --------- Co-authored-by: Copilot <[email protected]>
1 parent 17a58c0 commit 0e589f9

23 files changed

+804
-254
lines changed

.flake8

Lines changed: 0 additions & 7 deletions
This file was deleted.

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
run: ruff format --check .
3939

4040
- name: mypy
41-
run: mypy .
41+
run: mypy ./src
4242

4343
# ---- Tests ----
4444
- name: Run tests

README.md

Lines changed: 1 addition & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,158 +1 @@
1-
# template-python-lib
2-
3-
A minimal, modern **Python library template** designed to jumpstart your project with best practices and tooling.
4-
5-
---
6-
7-
## 🚀 Quick start: using this template
8-
9-
### 1. Create a new repository
10-
- Via GitHub UI:
11-
Click **Use this template** → create `my-awesome-lib`
12-
- Or via CLI:
13-
```bash
14-
gh repo create your-org/my-awesome-lib --template JaLuka98/template-python-lib --public
15-
```
16-
17-
### 2. Clone your new repo and enter it
18-
```bash
19-
git clone https://github.com/your-org/my-awesome-lib.git
20-
cd my-awesome-lib
21-
```
22-
23-
### 3. Apply required customisations
24-
Before you start developing, update the following items in your new project:
25-
26-
| Item | Where to change | Notes |
27-
|-----------------------------|-----------------------------------------------------|-------------------------------------------------|
28-
| Package name | `src/template_python_lib/``src/your_package/` | Rename the directory and update all references |
29-
| Import paths | All source and test files | Replace `template_python_lib` with your package |
30-
| Package metadata | `pyproject.toml`, `setup.cfg` | Update name, author, description, etc. |
31-
| Version | `pyproject.toml`, `setup.cfg` | Optionally set initial version |
32-
| License | `pyproject.toml`, `setup.cfg`, LICENSE | Choose and update license as needed |
33-
| README | `README.md` | Update project name and description |
34-
| Documentation | `docs/` | Update Sphinx config and docstrings |
35-
36-
### 4. Set up your environment
37-
Set up the necessary packages in an environment of your choice:
38-
```bash
39-
<activate_your_environment>
40-
pip install -e .[dev]
41-
```
42-
43-
### 5. Run tests to verify setup
44-
```bash
45-
pytest
46-
```
47-
48-
---
49-
50-
## 📂 Project structure
51-
52-
```plaintext
53-
.
54-
├── src/
55-
│ └── template_python_lib/ # Your package source code
56-
├── tests/ # Test suite
57-
├── docs/ # Sphinx documentation source
58-
├── .github/workflows/ # GitHub Actions CI workflows
59-
├── .pre-commit-config.yaml # Pre-commit hooks configuration
60-
├── pyproject.toml # Build system and tool configurations
61-
├── setup.cfg # Package metadata and settings
62-
├── requirements-dev.txt # Development dependencies
63-
└── README.md # This file
64-
```
65-
66-
---
67-
68-
## 🛠️ Local development
69-
70-
- Use the `src` layout to avoid import conflicts.
71-
- Install dev dependencies with `pip install -e .[dev]`.
72-
- Run tests with `pytest`.
73-
- Type-check your code with `mypy`.
74-
- Lint your code with `ruff`.
75-
76-
---
77-
78-
## 🎨 Linting & formatting
79-
80-
- **Ruff** handles linting and formatting checks.
81-
- Run lint checks manually with:
82-
```bash
83-
ruff check src tests
84-
```
85-
86-
---
87-
88-
## 🔧 Pre-commit hooks
89-
90-
- Pre-commit hooks run automatically before each commit to catch issues early.
91-
- Set up pre-commit by running:
92-
```bash
93-
pre-commit install
94-
```
95-
- Hooks include:
96-
- Ruff linting
97-
- Adding trailing whitespaces
98-
99-
---
100-
101-
## 📦 Versioning
102-
103-
- Uses **CalVer** (Calendar Versioning) format: `YYYY.MM.PATCH` (e.g., `2025.09.0`).
104-
- Version is managed with [bump-my-version](https://github.com/callowayproject/bump-my-version).
105-
- Bump versions with:
106-
```bash
107-
bump-my-version patch
108-
```
109-
or with the dedicated GitHub action.
110-
111-
---
112-
113-
## 📚 Documentation
114-
115-
- Documentation is built with **Sphinx**.
116-
- Source files are in the `docs/` directory.
117-
- Ready for hosting on Read the Docs.
118-
- Build docs locally with:
119-
```bash
120-
cd docs
121-
make html
122-
```
123-
124-
---
125-
126-
## ⚙️ Continuous Integration (CI)
127-
128-
- GitHub Actions workflows automate:
129-
- Linting and type-checking.
130-
- Running tests on multiple Python versions.
131-
- Ensures code quality and consistency on every push and pull request.
132-
133-
---
134-
135-
## 🤔 Rationale
136-
137-
This template aims to provide a minimal yet modern starting point for Python libraries, incorporating:
138-
139-
- Best practices like the `src` layout to avoid import pitfalls.
140-
- Automated tooling for testing, linting, formatting, and type checking.
141-
- Pre-commit hooks to maintain code quality from the start.
142-
- Clear versioning strategy with CalVer.
143-
- Documentation setup ready for expansion and publishing.
144-
145-
---
146-
147-
## 🤝 Contributing
148-
149-
Contributions are welcome! Please:
150-
151-
- Follow the existing code style and conventions.
152-
- Run tests and linting before submitting PRs.
153-
- Use the pre-commit hooks to catch issues early.
154-
- Update this README as needed.
155-
156-
---
157-
158-
Thank you for using this template — happy coding!
1+
# NPKit

docs/source/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
import sys
1111
import importlib
1212

13-
project = "template-python-lib"
13+
project = "NPKit"
1414
try:
15-
__version__ = importlib.import_module("template_python_lib").__version__
15+
__version__ = importlib.import_module("npkit").__version__
1616
except Exception:
1717
__version__ = "0.0.0"
1818
sys.path.insert(0, os.path.abspath("../../src"))
1919

20-
project = "template-python-lib"
20+
project = "NPKit"
2121
copyright = "2025, Jan Lukas Späh"
2222
author = "Jan Lukas Späh"
2323
release = __version__

docs/source/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
.. CPax documentation master file, created by
1+
.. NPkit documentation master file, created by
22
sphinx-quickstart on Mon Mar 31 23:52:35 2025.
33
You can adapt this file completely to your liking, but it should at least
44
contain the root `toctree` directive.
55
6-
CPax documentation
6+
NPKit documentation
77
==================
88

99
Add your content using ``reStructuredText`` syntax. See the

examples/minimal_example.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import numpy as np
2+
from npkit import (
3+
Observable,
4+
ObservableSet,
5+
GaussianModel,
6+
GaussianLikelihood,
7+
)
8+
from npkit import build_belt, invert_belt
9+
import matplotlib.pyplot as plt
10+
from npkit.plot import plot_belt
11+
12+
# Model: two observables
13+
obs = ObservableSet(
14+
[
15+
Observable("xsec1", lambda p: 100 + 10 * p["C"] ** 2),
16+
]
17+
)
18+
19+
V = np.array([100])
20+
21+
model = GaussianModel(obs=obs, covariance=V)
22+
23+
# Observed data (example)
24+
y_obs = 100
25+
# data = Combination(names=obs.names, values=y_obs, covariance=V)
26+
27+
28+
def like_builder():
29+
return GaussianLikelihood(obs, y_obs, V)
30+
31+
32+
start = {"C": 0.0}
33+
# bounds = {"C": (-1e6, 1e6)}
34+
bounds = None
35+
rng = np.random.default_rng(123)
36+
37+
# Build a 68% belt
38+
grid = np.linspace(-2, 2, 51)
39+
belt = build_belt(
40+
param="C",
41+
model=model,
42+
like_builder=like_builder,
43+
grid=grid,
44+
n_toys=3000,
45+
alpha=1 - 0.6827,
46+
rng=rng,
47+
start=start,
48+
bounds=bounds,
49+
)
50+
51+
print(belt)
52+
53+
# Invert for observed interval
54+
interval = invert_belt(belt, like_builder=like_builder, start=start, bounds=bounds)
55+
print("Neyman CI:", interval)
56+
57+
# Visualize the Neyman belt
58+
ax = plot_belt(belt)
59+
ax.set_title("Neyman Belt Visualization")
60+
plt.show()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import numpy as np
2+
from npkit import (
3+
Observable,
4+
ObservableSet,
5+
Combination,
6+
GaussianModel,
7+
GaussianLikelihood,
8+
)
9+
from npkit import build_belt, invert_belt
10+
11+
# Model: two observables
12+
obs = ObservableSet(
13+
[
14+
Observable("xsec1", lambda p: 1.0 + 0.4 * p["C"]),
15+
Observable("xsec2", lambda p: 0.8 - 0.2 * p["C"]),
16+
]
17+
)
18+
19+
V = np.array([[0.04**2, 0.5 * 0.04 * 0.05], [0.5 * 0.04 * 0.05, 0.05**2]])
20+
21+
model = GaussianModel(obs=obs, covariance=V)
22+
23+
# Observed data (example)
24+
y_obs = np.array([1.05, 0.78])
25+
data = Combination(names=obs.names, values=y_obs, covariance=V)
26+
27+
28+
def like_builder():
29+
return GaussianLikelihood(obs, y_obs, V)
30+
31+
32+
start = {"C": 0.0}
33+
bounds = {"C": (-2.0, 2.0)}
34+
rng = np.random.default_rng(123)
35+
36+
# Build a 68% belt
37+
grid = np.linspace(-2, 2, 161)
38+
belt = build_belt(
39+
param="C",
40+
model=model,
41+
like_builder=like_builder,
42+
grid=grid,
43+
n_toys=1000,
44+
alpha=1 - 0.6827,
45+
rng=rng,
46+
start=start,
47+
bounds=bounds,
48+
)
49+
50+
# Invert for observed interval
51+
interval = invert_belt(belt, like_builder=like_builder, start=start, bounds=bounds)
52+
print("Neyman CI:", interval)

examples/root_finding_example.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

pyproject.toml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ requires = ["setuptools>=65.5", "wheel"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
6-
name = "template-python-lib"
6+
name = "NPKit"
77
version = "2025.09.0"
8-
description = "template-python-lib: A template repository for modern python packages."
8+
description = "NPKit: Toolkit for frequentist interpretations and inference."
99
authors = [
1010
{ name = "Jan Lukas Späh", email = "[email protected]" }
1111
]
@@ -19,7 +19,7 @@ classifiers = [
1919
]
2020

2121
dependencies = [
22-
"numpy>=2.0",
22+
"numpy>=2.0", "scipy>=1.16"
2323
]
2424

2525
[project.optional-dependencies]
@@ -28,12 +28,28 @@ dev = ["bump-my-version>=1.2", "ruff>=0.6", "pre-commit>=3.8", "mypy>=1.11"]
2828
docs = ["sphinx>=7", "sphinx-rtd-theme", "sphinx-autodoc-typehints", "sphinx-autoapi", "myst-parser"]
2929

3030
[project.urls]
31-
Homepage = "https://github.com/JaLuka98/template-python-lib"
32-
Issues = "https://github.com/JaLuka98/template-python-lib/issues"
33-
Source = "https://github.com/JaLuka98/template-python-lib"
31+
Homepage = "https://github.com/JaLuka98/NPKit"
32+
Issues = "https://github.com/JaLuka98/NPKit/issues"
33+
Source = "https://github.com/JaLuka98/NPKit"
3434

3535
[tool.setuptools]
3636
package-dir = {"" = "src"}
3737

3838
[tool.setuptools.packages.find]
3939
where = ["src"]
40+
41+
[tool.mypy]
42+
python_version = "3.12"
43+
strict = true
44+
exclude = ["^docs/", "^examples/"]
45+
46+
warn_unused_ignores = true
47+
warn_redundant_casts = true
48+
warn_unreachable = true
49+
50+
[[tool.mypy.overrides]]
51+
module = [
52+
"scipy.*",
53+
"matplotlib.*",
54+
]
55+
ignore_missing_imports = true

0 commit comments

Comments
 (0)