Workspace Builder API
The workspace builder converts a human-readable YAML fit configuration into a
pyhf-like workspace dictionary that sbi_parametric_model
consumes directly. This is the bridge between your analysis definition and the
statistical model.
Typical usage
from nsbi_common_utils import workspace_builder, models, inference
# 1. Build workspace from fit config
ws = workspace_builder.WorkspaceBuilder(config_path="config_fit_nsbi.yml").build()
# 2. Optionally serialise / reload (avoids re-reading ROOT files)
builder.dump_workspace(ws, "workspace.json")
ws = workspace_builder.WorkspaceBuilder.load_workspace("workspace.json")
# 3. Pass to the statistical model
model = models.sbi_parametric_model(workspace=ws, measurement_to_fit="my_measurement")
For details on the YAML configuration format consumed by the builder, see Writing a Fit Configuration. For a hands-on walkthrough, see Model Building Example.
API Reference
- class WorkspaceBuilder(config_path)[source]
Bases:
objectBuild a pyhf-like JSON workspace from a YAML configuration file.
Reads sample definitions, region definitions, normalization factors, and systematic uncertainties from a YAML config (via
ConfigManager), loads the corresponding ROOT datasets and trained density-ratio models, and assembles them into a workspace dictionary that can be consumed bysbi_parametric_model.- Parameters:
config_path (
pathlib.Pathorstr) – Path to the YAML configuration file that defines samples, regions, normalization factors, and systematics. See Writing a Fit Configuration for the expected format.- Parameters:
See also
nsbi_common_utils.models.sbi_parametric_modelConsumes the workspace dictionary produced by
build().
Examples
>>> builder = WorkspaceBuilder("config_fit_nsbi.yml") >>> ws = builder.build() >>> builder.dump_workspace(ws, "workspace.json")
- build()[source]
Construct the full workspace dictionary.
Calls
channels()andmeasurements()and combines them with a version tag into the top-level workspace dict.- Returns:
dict– Workspace with keys"channels","measurements", and"version". Ready to be passed tosbi_parametric_modelor serialised viadump_workspace().- Return type:
- channels()[source]
Build the
"channels"list for the workspace.For every region in the configuration, loads datasets from ROOT files, computes nominal histograms, attaches density-ratio file paths (for unbinned regions), and collects all applicable normfactor and systematic modifiers per sample.
- dump_workspace(ws, outpath='workspace.json')[source]
Serialise a workspace dictionary to a JSON file.
- static load_workspace(path)[source]
Load a workspace dictionary from a JSON file.
- Parameters:
path (
str) – Path to the JSON workspace file written bydump_workspace().- Returns:
dict– The deserialised workspace dictionary.- Parameters:
path (str)
- Return type:
- measurements()[source]
Build the
"measurements"list for the workspace.Extracts parameter initial values and bounds from the
NormFactorsandSystematicssections of the config, filters to theParametersToFitsubset (if specified), and records the parameter of interest (POI).
- normfactor_modifiers(region_name, sample_name)[source]
Return normfactor modifiers that affect a given sample in a region.
Iterates over all
NormFactorsin the configuration and keeps only those whoseRegionandSampleslists include the requested region/sample (or are unset, meaning they apply everywhere).
- normplusshape_modifiers(dataset, region, sample, systematic_dict, nominal_data, type_of_fit)[source]
Build a NormPlusShape modifier for one systematic on one sample.
Histograms the up/down systematic variations, divides by the nominal to obtain variation ratios, and (for unbinned channels) attaches paths to the pre-computed density-ratio arrays.
- Parameters:
dataset (
dictofdictofDataFrame) – Nested dict keyed by"<syst>_Up"/"<syst>_Dn"then sample name, as returned bydatasets.filter_region_by_type().region (
dict) – Region (channel) configuration dictionary. Must contain"Name","Variable", and"Binning"keys.sample (
dict) – Sample configuration dictionary with at least"Name"and"SamplePath"keys.systematic_dict (
dict) – Single systematic entry from the YAML config (has"Name").nominal_data (
np.ndarray) – Nominal histogram bin counts used to normalise the variations.type_of_fit (:py:class:
``”binned”:py:class:``or :py:class:``”unbinned”:py:class:``) – Determines whether density-ratio file paths are included.
- Returns:
listofdict– A single-element list containing the modifier dictionary with keys"name","type": "normplusshape", and"data".- Parameters:
- Return type:
- sys_modifiers(dataset, region, sample, nominal_data, type_of_fit='binned')[source]
Collect all systematic modifiers for a sample in a region.
Loops over every systematic in the configuration, checks region/sample applicability, and delegates to
normplusshape_modifiers()forNormPlusShapetypes.- Parameters:
dataset (
dict) – Dataset dictionary as returned bydatasets.filter_region_by_type().region (
dict) – Region configuration dictionary.sample (
dict) – Sample configuration dictionary.nominal_data (
np.ndarray) – Nominal histogram used for ratio computation.type_of_fit (
str, optional) –"binned"(default) or"unbinned".
- Returns:
listofdict– Modifier dictionaries for all applicable systematics.- Raises:
NotImplementedError – If a systematic type other than
NormPlusShapeis encountered.- Parameters:
- Return type: