pure-Python HS3 implementation with tensors and autodiff
Project description
Hello World
This section shows two ways to build the same statistical model and evaluate it: using PyHS3 Python objects directly, and using HS3 JSON-like dictionaries.
Hello World (Python)
This is how you use the pyhs3 Python API to build a statistical model directly with objects:
>>> import pyhs3
>>> import scipy
>>> import math
>>> import numpy as np
>>> from pyhs3.distributions import GaussianDist
>>> from pyhs3.parameter_points import ParameterPoint, ParameterSet
>>> from pyhs3.domains import ProductDomain, Axis
>>> from pyhs3.metadata import Metadata
>>>
>>> # Create metadata
>>> metadata = Metadata(hs3_version="0.2")
>>>
>>> # Create a Gaussian distribution
>>> gaussian_dist = GaussianDist(name="model", x="x", mean="mu", sigma="sigma")
>>>
>>> # Create parameter points with values
>>> param_points = ParameterSet(
... name="default_values",
... parameters=[
... ParameterPoint(name="x", value=0.0),
... ParameterPoint(name="mu", value=0.0),
... ParameterPoint(name="sigma", value=1.0),
... ],
... )
>>>
>>> # Create domain with parameter bounds
>>> domain = ProductDomain(
... name="default_domain",
... axes=[
... Axis(name="x", min=-5.0, max=5.0),
... Axis(name="mu", min=-2.0, max=2.0),
... Axis(name="sigma", min=0.1, max=3.0),
... ],
... )
>>>
>>> # Build workspace from objects
>>> ws = pyhs3.Workspace(
... metadata=metadata,
... distributions=[gaussian_dist],
... parameter_points=[param_points],
... domains=[domain],
... )
>>> model = ws.model()
<BLANKLINE>
>>> print(model)
Model(
mode: FAST_RUN
parameters: 3 (sigma, mu, x)
distributions: 1 (model)
functions: 0 ()
)
>>> parameters = {par.name: np.array(par.value) for par in model.parameterset}
>>> result = -2 * model.logpdf("model", **parameters)
>>> print(f"parameters: {parameters}")
parameters: {'x': array(0.), 'mu': array(0.), 'sigma': array(1.)}
>>> print(f"nll: {result:.8f}")
nll: 1.83787707
>>>
>>> # Serialize workspace back to dictionary for saving/sharing
>>> workspace_dict = ws.model_dump()
>>> print("Serialized workspace keys:", list(workspace_dict.keys()))
Serialized workspace keys: ['metadata', 'distributions', 'functions', 'domains', 'parameter_points', 'data', 'likelihoods', 'analyses', 'misc']
Hello World (HS3)
This is the same model built using HS3 JSON-like dictionary format:
>>> import pyhs3
>>> import scipy
>>> import math
>>> import numpy as np
>>> workspace_data = {
... "metadata": {"hs3_version": "0.2"},
... "distributions": [
... {
... "name": "model",
... "type": "gaussian_dist",
... "x": "x",
... "mean": "mu",
... "sigma": "sigma",
... }
... ],
... "parameter_points": [
... {
... "name": "default_values",
... "parameters": [
... {"name": "x", "value": 0.0},
... {"name": "mu", "value": 0.0},
... {"name": "sigma", "value": 1.0},
... ],
... }
... ],
... "domains": [
... {
... "name": "default_domain",
... "type": "product_domain",
... "axes": [
... {"name": "x", "min": -5.0, "max": 5.0},
... {"name": "mu", "min": -2.0, "max": 2.0},
... {"name": "sigma", "min": 0.1, "max": 3.0},
... ],
... }
... ],
... }
>>> ws = pyhs3.Workspace(**workspace_data)
>>> model = ws.model()
<BLANKLINE>
>>> print(model)
Model(
mode: FAST_RUN
parameters: 3 (sigma, mu, x)
distributions: 1 (model)
functions: 0 ()
)
>>> parameters = {par.name: np.array(par.value) for par in model.parameterset}
>>> result = -2 * model.logpdf("model", **parameters)
>>> print(f"parameters: {parameters}")
parameters: {'x': array(0.), 'mu': array(0.), 'sigma': array(1.)}
>>> print(f"nll: {result:.8f}")
nll: 1.83787707
>>> result_scipy = -2 * math.log(scipy.stats.norm.pdf(0, loc=0, scale=1))
>>> print(f"nll: {result_scipy:.8f}")
nll: 1.83787707
>>>
>>> # Round-trip: serialize workspace back to dictionary
>>> serialized_dict = ws.model_dump()
>>> print("Round-trip successful:", serialized_dict["metadata"]["hs3_version"])
Round-trip successful: 0.2
>>>
>>> # Can recreate workspace from serialized dictionary
>>> ws_roundtrip = pyhs3.Workspace(**serialized_dict)
>>> model_roundtrip = ws_roundtrip.model()
<BLANKLINE>
>>> print("Round-trip model:", model_roundtrip.parameterset.name)
Round-trip model: default_values
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyhs3-0.4.0.tar.gz.
File metadata
- Download URL: pyhs3-0.4.0.tar.gz
- Upload date:
- Size: 178.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9000370d61959f01e8f5fbd5dcec2dca82a5d475c41b2f879527dc3135d9cfad
|
|
| MD5 |
ee2d81021b24e88b1e270720602a26ab
|
|
| BLAKE2b-256 |
618c4d694b78b195a8870059a86f0b9f9466d96a36a38fc3a07532484fb19973
|
Provenance
The following attestation bundles were made for pyhs3-0.4.0.tar.gz:
Publisher:
cd.yml on scipp-atlas/pyhs3
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyhs3-0.4.0.tar.gz -
Subject digest:
9000370d61959f01e8f5fbd5dcec2dca82a5d475c41b2f879527dc3135d9cfad - Sigstore transparency entry: 628996049
- Sigstore integration time:
-
Permalink:
scipp-atlas/pyhs3@66cfe8a7d96067fea474bb2ca77cefaa3ac48dda -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/scipp-atlas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@66cfe8a7d96067fea474bb2ca77cefaa3ac48dda -
Trigger Event:
release
-
Statement type:
File details
Details for the file pyhs3-0.4.0-py3-none-any.whl.
File metadata
- Download URL: pyhs3-0.4.0-py3-none-any.whl
- Upload date:
- Size: 84.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6de7e8ef56286ada664beca9c42960b4cd9e965eb2031623ec1f5ead5ff2a5c
|
|
| MD5 |
5df7b6111edac8eac3da550f1c6023f5
|
|
| BLAKE2b-256 |
2711df32969498c3a79d4ba623a8974307ed15c09a067221dce97c92cf760657
|
Provenance
The following attestation bundles were made for pyhs3-0.4.0-py3-none-any.whl:
Publisher:
cd.yml on scipp-atlas/pyhs3
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyhs3-0.4.0-py3-none-any.whl -
Subject digest:
e6de7e8ef56286ada664beca9c42960b4cd9e965eb2031623ec1f5ead5ff2a5c - Sigstore transparency entry: 628996055
- Sigstore integration time:
-
Permalink:
scipp-atlas/pyhs3@66cfe8a7d96067fea474bb2ca77cefaa3ac48dda -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/scipp-atlas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@66cfe8a7d96067fea474bb2ca77cefaa3ac48dda -
Trigger Event:
release
-
Statement type: