Skip to main content

GitHub Project GitHub Discussion

Docs from latest Docs from main

PyPI version Conda-forge version Supported Python versions PyPI platforms

Code Coverage CodeFactor pre-commit.ci status Code style: black

Documentation Status GitHub Actions Status GitHub Actions Status: CI GitHub Actions Status: Docs GitHub Actions Status: Publish

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.axes import DomainAxis
>>> from pyhs3.domains import ProductDomain
>>> 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=[
...         dict(name="x", min=-5.0, max=5.0),
...         dict(name="mu", min=-2.0, max=2.0),
...         dict(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(0)
<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(0)
<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(0)
<BLANKLINE>
>>> print("Round-trip model:", model_roundtrip.parameterset.name)
Round-trip model: default_values

Release files for pyhs3 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyhs3 0.4.2
File Size Uploaded
pyhs3-0.4.2.tar.gz 92.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyhs3 0.4.2
File Interpreter ABI Platform
pyhs3-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 203.9 kB

Release files / pyhs3-0.4.2.tar.gz

Download URL pyhs3-0.4.2.tar.gz
Size 92.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1a2eb45bd2c4b028902918b692ea7333b39359c688f3252ea9170b16d967de1f
BLAKE2b-256 checksum
How to use checksums
51369a361b7f54f774d0548d85db7afeada879a5421e023bca0cef5195577d5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 23, 2026.

Transparency log

Release files / pyhs3-0.4.2-py3-none-any.whl

Download URL pyhs3-0.4.2-py3-none-any.whl
Size 111.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ce312fd0cc27784d23b1eb4babdb9895941f81ea5e4893f77595dbd10d50381
BLAKE2b-256 checksum
How to use checksums
e9d4a40df2c994d5b4b03833f424c3a838d2ce929ad102e437c69b3f41374c0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 23, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page