pyhs3.Workspace

class pyhs3.Workspace(**data)[source]

Workspace for managing HS3 model specifications.

A workspace contains parameter points, distributions, domains, and functions that define a probabilistic model. It provides methods to construct Model objects with specific parameter values and domain constraints.

Variables:
  • metadata – Required metadata containing HS3 version and optional attribution

  • distributions – List of distribution configurations

  • functions – List of function configurations

  • domains – List of domain configurations

  • parameter_points – List of parameter point configurations

  • data – Data specifications for observations

  • likelihoods – Likelihood specifications mapping distributions to data

  • analyses – Analysis configurations for automated analyses

  • misc – Arbitrary user-created information

  • parameter_collection (ParameterPoints) – Named parameter sets.

  • distribution_set (Distributions) – Available distributions.

  • domain_collection (Domains) – Domain constraints for parameters.

  • function_set (Functions) – Available functions for parameter computation.

HS3 Reference

See HS3 file format specification for the complete workspace structure.

Parameters:

data (Any)

Fields

Field

Type

Required

Default

analyses

Analyses | None

No

factory

data

Data | None

No

factory

distributions

Distributions | None

No

factory

domains

Domains | None

No

factory

functions

Functions | None

No

factory

likelihoods

Likelihoods | None

No

factory

metadata

Metadata

Yes

misc

dict[str, Any] | None

No

factory

parameter_points

ParameterPoints | None

No

factory

__init__(**data)

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

Methods

format_validation_error(validation_error, ...)

Format a ValidationError into a readable error summary.

load(path, *[, verbose, suppress_traceback])

Load workspace from a JSON file.

model()

Constructs a Model rooted at target.

model_post_init(_Workspace__context, /)

Resolve foreign key references after workspace construction.

parameter_context()

Resolve the ParameterContext for target.

Attributes

classmethod Workspace.format_validation_error(validation_error, path, verbose)[source]

Format a ValidationError into a readable error summary.

Parameters:
  • validation_error (ValidationError) – The ValidationError to format

  • path (str | PathLike[str]) – Path to the file that caused the error

  • verbose (bool) – If True, show all errors. If False, show first 20 and summarize rest.

Return type:

str

Returns:

Formatted error message string

classmethod Workspace.load(path, *, verbose=False, suppress_traceback=True)[source]

Load workspace from a JSON file.

Parameters:
  • path (str | PathLike[str]) – Path to the JSON file containing the HS3 specification

  • verbose (bool) – If True, show all errors. If False, show first 20 and summarize rest.

  • suppress_traceback (bool) – If True, suppress traceback on validation errors (default True).

Returns:

The loaded workspace instance

Return type:

Workspace

Workspace.model(target, *, domain=None, parameter_set=None, progress=True, mode='CVM')[source]
Workspace.model(target, *, parameter_set=None, progress=True, mode='CVM')
Workspace.model(target, *, domain=None, parameter_set=None, progress=True, mode='CVM')
Workspace.model(target, *, domain=None, parameter_set=None, progress=True, mode='CVM')

Constructs a Model rooted at target.

Dispatch is based on the type of target:

  • Analysis — all context (domain, parameter set, observables) is derived from the analysis; gains access to log_prob, data, and free_params. Only the likelihood’s distributions and their transitive dependencies are built.

  • Likelihood — observable bounds are derived from the likelihood’s data; domain falls back to default_domain then index 0, and parameter_set falls back to workspace defaults unless overridden. Only the likelihood’s distributions and their transitive dependencies are built.

  • str — searches analyses then likelihoods by name; delegates to the appropriate registered path. Falls back to legacy domain indexing if the name is not found in either.

  • int — legacy path: target indexes into workspace domains and the complete workspace graph is built.

Parameters:
  • target (int) – Dispatch key. Pass an Analysis or Likelihood for the modern paths, a name string to search analyses/likelihoods, or an int domain index for the legacy path.

  • domain (int | str | Domain | None) – Override domain (legacy and Likelihood paths only).

  • parameter_set (int | str | ParameterSet | None) – Override parameter set (legacy and Likelihood paths only).

  • progress (bool) – Whether to show a progress bar during graph construction.

  • mode (str) – PyTensor compilation mode (default "CVM"; see Model).

Returns:

The constructed model.

Return type:

Model

Workspace.model_post_init(_Workspace__context, /)[source]

Resolve foreign key references after workspace construction.

Parameters:

_Workspace__context (Any)

Return type:

None

Workspace.parameter_context(target, *, parameter_set=None, domain=None)[source]
Workspace.parameter_context(target, *, parameter_set=None, domain=None)
Workspace.parameter_context(target, *, parameter_set=None, domain=None)
Workspace.parameter_context(target, *, parameter_set=None, domain=None)

Resolve the ParameterContext for target.

The context is what model() builds the model from: the referenced parameter point, the domain bounds and the parameters of interest. Dispatch is based on the type of target, as in model():

  • Analysis – the analysis domains, merged into one when there are several; its init parameter point (an empty one when it has none) unless parameter_set overrides it; and its parameters_of_interest. The analysis must satisfy HS3 section 2.7 with that parameter point. domain cannot be overridden.

  • Likelihood – domain falls back to default_domain then index 0, and parameter_set to the first parameter point; no parameters of interest.

  • str – searches analyses then likelihoods by name and resolves as above; otherwise the name selects a domain, as for an int.

  • int – target indexes into the workspace domains (unless domain overrides it) and parameter_set falls back to the first parameter point; no parameters of interest.

Parameters:
  • target (int) – Dispatch key, as for model().

  • parameter_set (int | str | ParameterSet | None) – Override parameter set: a ParameterSet, or an index or name into the workspace parameter points.

  • domain (int | str | Domain | None) – Override domain: a Domain, or an index or name into the workspace domains (not for analyses).

Returns:

The resolved context.

Return type:

ParameterContext

Raises:
  • UnlistedParameterError – If an analysis violates HS3 section 2.7.

  • MissingInitialValueError – If a free parameter of an analysis has no starting value in the parameter point.

  • UnknownParameterOfInterestError – If an analysis names a parameter of interest that its likelihood does not use.

  • UnconstrainedParameterError – If a parameter that gets no constraint term is not const in the parameter point.

  • ValueError – If an analysis references an unknown init, if domain is given for an analysis, or if an override is requested from a workspace without parameter points or domains.

Workspace.metadata
Workspace.distributions
Workspace.functions
Workspace.domains
Workspace.parameter_points
Workspace.data
Workspace.likelihoods
Workspace.analyses
Workspace.misc