CLI and orchestration

CLI

Command-line interface for P2P.

p2p.cli.build_parser()[source]

Build the single command-line parser.

Return type:

ArgumentParser

p2p.cli.main(argv=None)[source]

Parse arguments and dispatch through the command registry.

Parameters:

argv (Sequence[str] | None)

Return type:

int

Commands

User-facing endowment-generation command composition.

p2p.commands.generate.run(config, *, run_dir, verbose=False)[source]

Validate and execute one configured generation strategy.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • verbose (bool)

Return type:

GenerationResult

User-facing elicitation from an existing endowment artifact.

p2p.commands.elicit.run(config, *, endowments_path, run_dir, verbose=False)[source]

Elicit responses from an existing endowments CSV.

Parameters:
  • config (Config)

  • endowments_path (str | PathLike[str])

  • run_dir (str | PathLike[str])

  • verbose (bool)

Return type:

ElicitationResult

Production full-pipeline command composition.

class p2p.commands.pipeline.PipelineResult(config_digest, run_dir, generation, elicitation, regression, report, usage=None, log_path=None)[source]

One completed in-memory full-pipeline execution.

Parameters:
  • config_digest (str)

  • run_dir (Path)

  • generation (Any)

  • elicitation (Any)

  • regression (Any)

  • report (Any)

  • usage (UsageArtifacts | None)

  • log_path (Path | None)

p2p.commands.pipeline.run(config, *, solver, run_dir, verbose=False)[source]

Generate, elicit, regress, and report one run.

Parameters:
  • config (Config)

  • solver (str)

  • run_dir (str | PathLike[str])

  • verbose (bool)

Return type:

PipelineResult

Production Stage-2 regression command composition.

class p2p.commands.regress.RegressionResult(data, fit, evaluation, config_digest=None, run_dir=None, report=None)[source]

One completed in-memory regression execution.

Parameters:
p2p.commands.regress.run(config, *, solver, verbose=False, basis=None, run_dir=None)[source]

Execute regression from config or generated basis artifacts.

Parameters:
  • config (Config)

  • solver (str)

  • verbose (bool)

  • basis (Any | None)

  • run_dir (str | PathLike[str] | None)

Return type:

RegressionResult

p2p.commands.regress.run_regression(data, model_config, *, metrics, solver, seed, test_split, distribution_prediction_policy, invalid_distribution='allow', verbose=False, fold_assignments=None, config_digest=None, distribution_route='model')[source]

Fit and evaluate one model on prepared regression data.

Parameters:
  • data (RegressionData)

  • model_config (Mapping[str, Any])

  • metrics (Sequence[str])

  • solver (str)

  • seed (int)

  • test_split (str)

  • distribution_prediction_policy (str)

  • invalid_distribution (str)

  • verbose (bool)

  • fold_assignments (Mapping[str, int] | None)

  • config_digest (str | None)

  • distribution_route (str)

Return type:

RegressionResult

Standalone evaluation over self-contained prediction artifacts.

class p2p.commands.evaluate.EvaluationCommandResult(config_digest, run_dir, input_kind, model_name, cleaning_policy, evaluations, report)[source]

One standalone evaluation execution and its artifacts.

Parameters:
  • config_digest (str)

  • run_dir (Path)

  • input_kind (str)

  • model_name (str)

  • cleaning_policy (str)

  • evaluations (tuple[EvaluationResult, ...])

  • report (ReportArtifacts)

p2p.commands.evaluate.run(config, *, run_dir, verbose=False)[source]

Evaluate declared outputs and write canonical artifacts.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • verbose (bool)

Return type:

EvaluationCommandResult

User-facing aggregation over canonical result directories.

class p2p.commands.aggregate.AggregateCommandResult(result, artifacts, output_dir)[source]

One completed aggregation and its written artifacts.

Parameters:
p2p.commands.aggregate.run(config, *, output_dir, runs=None, group_by=None, metrics=None, verbose=False)[source]

Validate, aggregate, and export configured or explicit runs.

Parameters:
  • config (Config | None)

  • output_dir (str | PathLike[str])

  • runs (Sequence[str | PathLike[str]] | None)

  • group_by (Sequence[str] | None)

  • metrics (Sequence[str] | None)

  • verbose (bool)

Return type:

AggregateCommandResult

Forecast and serve token/cost budgets without calling a backend.

class p2p.commands.budget.BudgetCommandResult(forecast, artifacts, dashboard)[source]

Artifacts emitted by one static budget build.

Parameters:
p2p.commands.budget.run(config, *, output_dir, history=None, target_command='pipeline', comparison_models=(), pricing_path=None, pilot_run=None, confidence=0.8, budget_usd=None, serve=False, host='127.0.0.1', port=8060, refresh_seconds=2.0, verbose=False)[source]

Build or serve a pre-run budget forecast.

Parameters:
  • config (Config)

  • output_dir (str | PathLike[str])

  • history (Sequence[str | PathLike[str]] | None)

  • target_command (str)

  • comparison_models (Sequence[str])

  • pricing_path (str | PathLike[str] | None)

  • pilot_run (str | PathLike[str] | None)

  • confidence (float)

  • budget_usd (float | None)

  • serve (bool)

  • host (str)

  • port (int)

  • refresh_seconds (float)

  • verbose (bool)

Return type:

BudgetCommandResult | None

Execute configured simulation sweeps with an explicit offline boundary.

p2p.commands.simulate.run(config, *, run_dir, solver='CLARABEL', verbose=False, registry=<p2p.registry.Registry object>, simulation_runner=<function run_declared_simulation>, aggregation_spec=None)[source]

Expand a simulation sweep and execute its declared target.

simulation.executor: scientific selects the built-in cached-basis executor. Other manifests retain the configuration-only boundary unless a caller injects a runner.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • solver (str)

  • verbose (bool)

  • registry (Registry[Callable[[...], Any]])

  • simulation_runner (Callable[[...], Any])

  • aggregation_spec (AggregateSpec | None)

Return type:

SweepCommandResult

Execute deterministic ablation sweeps over registered commands.

p2p.commands.ablate.run(config, *, run_dir, solver='CLARABEL', verbose=False, registry=<p2p.registry.Registry object>, aggregation_spec=None)[source]

Expand and execute the configured ablation target.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • solver (str)

  • verbose (bool)

  • registry (Registry[Callable[[...], Any]])

  • aggregation_spec (AggregateSpec | None)

Return type:

SweepCommandResult

Execute deterministic panel sweeps over registered commands.

p2p.commands.panel.run(config, *, run_dir, solver='CLARABEL', verbose=False, registry=<p2p.registry.Registry object>, aggregation_spec=None)[source]

Expand and execute the configured panel target.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • solver (str)

  • verbose (bool)

  • registry (Registry[Callable[[...], Any]])

  • aggregation_spec (AggregateSpec | None)

Return type:

SweepCommandResult

Deterministic offline clustering of existing endowment text.

class p2p.commands.cluster.ClusterAssignment(eid, cluster)[source]

One endowment’s canonical cluster assignment.

Parameters:
  • eid (str)

  • cluster (int)

class p2p.commands.cluster.ClusterResult(config_digest, run_dir, assignments, assignments_path, metadata_path, config_path)[source]

Artifacts and assignments from one clustering run.

Parameters:
  • config_digest (str)

  • run_dir (Path)

  • assignments (tuple[ClusterAssignment, ...])

  • assignments_path (Path)

  • metadata_path (Path)

  • config_path (Path)

p2p.commands.cluster.run(config, *, run_dir, verbose=False)[source]

Cluster endowment text and write deterministic assignments.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • verbose (bool)

Return type:

ClusterResult

Generate or serve the interactive run inspector.

p2p.commands.inspect.run(_config=None, *, run_dir, output_path=None, advanced=False, serve=False, host='127.0.0.1', port=8050, refresh_seconds=2.0, verbose=False)[source]

Build a static dashboard or serve a refreshing local view.

Parameters:
  • run_dir (str | PathLike[str])

  • output_path (str | PathLike[str] | None)

  • advanced (bool)

  • serve (bool)

  • host (str)

  • port (int)

  • refresh_seconds (float)

  • verbose (bool)

Return type:

DiagnosticDashboard | None

Thin command wrapper over verb-scoped configuration validation.

class p2p.commands.validate.ValidationResult(target_verb, config_digest, source_path)[source]

Identity of one successfully validated configuration.

Parameters:
  • target_verb (str)

  • config_digest (str)

  • source_path (Path)

p2p.commands.validate.run(config, *, target_verb, verbose=False)[source]

Validate one resolved config for the requested target verb.

Parameters:
  • config (Config)

  • target_verb (str)

  • verbose (bool)

Return type:

ValidationResult

Stages

Endowment-generation stage dispatch and artifact writing.

class p2p.stages.generate.GenerationResult(mode, survey, endowments, endowments_path, attribute_bank_snapshot=None, responses=None, responses_path=None, response_records=(), diagnostic_paths=<factory>, usage_tracker=None)[source]

In-memory generation result and its basis artifact.

Parameters:
  • mode (str)

  • survey (Survey)

  • endowments (Endowments)

  • endowments_path (Path)

  • attribute_bank_snapshot (Path | None)

  • responses (Responses | None)

  • responses_path (Path | None)

  • response_records (tuple[Mapping[str, Any], ...])

  • diagnostic_paths (Mapping[str, Path])

  • usage_tracker (UsageTracker | None)

p2p.stages.generate.load_survey(config)[source]

Load the survey referenced by a resolved generation config.

Parameters:

config (Config)

Return type:

Survey

p2p.stages.generate.run(config, *, run_dir, verbose=False, strategy_registry=<p2p.registry.Registry object>, usage_tracker=None)[source]

Run one registered generation strategy and save its basis.

Parameters:
Return type:

GenerationResult

Survey-elicitation stage and response artifact writing.

class p2p.stages.elicit.ElicitationResult(survey, endowments, responses, records, responses_path, reused_generation_responses=False, usage_tracker=None)[source]

In-memory elicitation result and its response artifact.

Parameters:
  • survey (Survey)

  • endowments (Endowments)

  • responses (Responses)

  • records (tuple[Mapping[str, Any], ...])

  • responses_path (Path)

  • reused_generation_responses (bool)

  • usage_tracker (UsageTracker | None)

p2p.stages.elicit.run(config, *, generation, run_dir, verbose=False, conductor_factory=<class 'p2p.agents.conductor.SurveyConductor'>, usage_tracker=None)[source]

Elicit responses from generated endowments and save a matrix CSV.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • verbose (bool)

  • usage_tracker (UsageTracker | None)

Return type:

ElicitationResult

Stage-2 data loading and split preparation.

class p2p.stages.split.RegressionData(survey, binary_survey, answer_responses, binary_responses, endowments, targets, splits, train_splits, test_split, feature_names, cleaning_policy, source_artifacts, source_digests, canonical_answers=True, reference=None)[source]

Prepared Stage-2 inputs shared by fitting and evaluation.

Parameters:
class p2p.stages.split.RegressionSplit(name, qids, X, y)[source]

One prepared regression split.

Parameters:
  • name (str)

  • qids (tuple[str, ...])

  • X (DataFrame)

  • y (ndarray)

p2p.stages.split.load_regression_data(*, survey_source, survey_schema, responses_path, endowments_path, target_path, target_subgroup=None, train_splits=('train', 'valid'), test_split='test', use_proxy_only=True, cleaning_policy='column_drop_v2', allow_missing_target=False, reference_respondent=False, renormalize_targets=False, canonical_answers=True, reference_distribution_path=None)[source]

Load one Stage-2 basis using explicit column-drop cleaning.

Parameters:
  • survey_source (str | Path)

  • survey_schema (str | Path)

  • responses_path (str | Path)

  • endowments_path (str | Path)

  • target_path (str | Path | None)

  • target_subgroup (str | None)

  • train_splits (Sequence[str])

  • test_split (str)

  • use_proxy_only (bool)

  • cleaning_policy (str)

  • allow_missing_target (bool)

  • reference_respondent (bool)

  • renormalize_targets (bool)

  • canonical_answers (bool)

  • reference_distribution_path (str | Path | None)

Return type:

RegressionData

Human-target loading for Stage-2 execution.

class p2p.stages.targets.TargetBundle(source_path, source_digest, selection_digest, subgroup, raw, binary, renormalized=(), degenerate=())[source]

Validated categorical targets and their binary-option expansion.

Parameters:
  • source_path (Path | None)

  • source_digest (str)

  • selection_digest (str)

  • subgroup (str | None)

  • raw (Mapping[str, Mapping[str, float]])

  • binary (Mapping[str, float])

  • renormalized (tuple[str, ...])

  • degenerate (tuple[str, ...])

p2p.stages.targets.load_targets(path, binary_survey, *, subgroup=None, renormalize=False)[source]

Load and validate human targets, optionally repairing their total.

renormalize divides each target vector by its own sum (first dividing by 100 when the sum exceeds 1.5, i.e. when it is given in percent), so a target whose shares do not quite sum to one can still be scored. It is opt-in: by default a target that is not a distribution is refused, so a corrupt file is caught rather than rescaled. Every repaired qid is recorded on TargetBundle.renormalized.

Under renormalize an all-zero target is left unchanged and recorded on TargetBundle.degenerate instead of raising. Such a question enters the design as a block of zero target mass; callers should skip those qids when scoring distributions, where the metric would be non-finite.

The repair applies to ``raw`` only, not to ``binary``. binary reaches the fit through split.py unrepaired, while raw reaches the metrics through evaluate.py repaired: the fit sees the target block as given and the scoring sees it rescaled. This is the deliberate contract of the option, kept so that runs made with it reproduce exactly.

Both renormalized and degenerate list every qid in the file, not only the modelled ones, so a caller that cares about its own question set intersects them with it.

Ungrouped files remain a flat {qid: {label: probability}} mapping. Grouped files use an explicit envelope:

{
  "schema_version": 1,
  "default_subgroup": "overall",
  "subgroups": {
    "overall": {"Q1": {"yes": 0.5, "no": 0.5}},
    "party.democrat": {"Q1": {"yes": 0.7, "no": 0.3}}
  }
}

subgroup is deliberately opt-in. A grouped file with multiple subgroups must either declare default_subgroup or be selected by the caller.

Parameters:
  • path (str | Path)

  • subgroup (str | None)

  • renormalize (bool)

Return type:

TargetBundle

p2p.stages.targets.target_renormalize(value)[source]

Read data.target.renormalize from a scalar or grouped target entry.

Kept separate from target_spec() so the existing two-tuple contract of that function is unchanged.

Parameters:

value (Any)

Return type:

bool

p2p.stages.targets.target_spec(value)[source]

Resolve a scalar or grouped target config entry.

Parameters:

value (Any)

Return type:

tuple[Path, str | None]

Registered Stage-2 model fitting.

class p2p.stages.fit.FitResult(method, model, solver, status, best_alpha, best_l1_ratio, diagnostics, folds=None)[source]

One fitted Stage-2 model and its selected hyperparameters.

Parameters:
  • method (str)

  • model (Any)

  • solver (str)

  • status (str)

  • best_alpha (float | None)

  • best_l1_ratio (float | None)

  • diagnostics (Mapping[str, Any])

  • folds (FoldAssignment | None)

p2p.stages.fit.fit_model(data, model_config, *, solver, seed, verbose=False, fold_assignments=None)[source]

Cross-validate and refit one registered constrained model.

Folds follow model.validation.folds (default: the hashed method seeded by seed). fold_assignments replays a given assignment instead; it must not be combined with folds.method: replay.

Parameters:
  • data (RegressionData)

  • model_config (Mapping[str, Any])

  • solver (str)

  • seed (int)

  • verbose (bool)

  • fold_assignments (FoldAssignment | Mapping[str, int] | None)

Return type:

FitResult

Single-run evaluation with explicit prediction policies.

class p2p.stages.evaluate.EvaluationResult(model_name, split, prediction_policy, values, coverage, predictions, diagnostics)[source]

In-memory result for one model and one split.

Parameters:
  • model_name (str)

  • split (str)

  • prediction_policy (str)

  • values (Mapping[str, float | None])

  • coverage (Mapping[str, MetricCoverage])

  • predictions (ndarray)

  • diagnostics (Mapping[str, Any])

class p2p.stages.evaluate.MetricCoverage(qids_requested, qids_used)[source]

Question coverage for one evaluated metric.

Parameters:
  • qids_requested (int)

  • qids_used (int)

p2p.stages.evaluate.evaluate_model(data, fit_result, *, split, metrics, distribution_prediction_policy, invalid_distribution='allow', distribution_route='model')[source]

Evaluate one fitted model on one configured split.

distribution_route chooses where the distributional metrics’ predicted distributions come from: "model" (the model’s own predictions, as MSE uses) or "answer_file" (the agents’ answer file re-aggregated by agent name and weight; "legacy_answer_file" is accepted as the same route).

Parameters:
  • data (RegressionData)

  • split (str)

  • metrics (Sequence[str])

  • distribution_prediction_policy (str)

  • invalid_distribution (str)

  • distribution_route (str)

Return type:

EvaluationResult

p2p.stages.evaluate.evaluate_predictions(survey, targets, predictions, *, model_name, split, metrics, distribution_prediction_policy, invalid_distribution)[source]

Evaluate self-contained option-level predictions for one split.

Parameters:
  • predictions (Mapping[str, Mapping[str, float]])

  • model_name (str)

  • split (str)

  • metrics (Sequence[str])

  • distribution_prediction_policy (str)

  • invalid_distribution (str)

Return type:

EvaluationResult

p2p.stages.evaluate.answer_file_predictions(data, model)[source]

The answer-file route: re-aggregate the answer file by agent name.

An alternative to scoring the model’s own predictions. It cannot see a feature that is not an answering agent, so such a model is refused rather than silently scored without it.

Parameters:
Return type:

dict[str, dict[str, float]]

p2p.stages.evaluate.model_distribution_predictions(data, model, *, split=None)[source]

Each question’s predicted option masses, from the model’s own predictions.

This is the model distribution route: model.predict on the design rows – the same predictions MSE scores – grouped by question and labelled with each option’s own text. Every model feature contributes through its own column, so a basis element that is not an answering agent (the reference respondent) is included. With split=None every split is covered.

Parameters:
Return type:

dict[str, dict[str, float]]

Canonical single-run report artifact writing.

class p2p.stages.report.ReportArtifacts(run_record, metric_artifact, run_json, metrics_json, metrics_csv, config_snapshot, attribute_bank_snapshot=None, diagnostics_json=None, folds_json=None)[source]

Canonical files and in-memory records for one run.

Parameters:
  • run_record (RunRecord)

  • metric_artifact (MetricArtifact)

  • run_json (Path)

  • metrics_json (Path)

  • metrics_csv (Path)

  • config_snapshot (Path)

  • attribute_bank_snapshot (Path | None)

  • diagnostics_json (Path | None)

  • folds_json (Path | None)

p2p.stages.report.write_evaluation_report(config, evaluations, *, source_artifacts, source_digests, cleaning_policy, run_dir)[source]

Write canonical artifacts for standalone evaluations.

Parameters:
  • config (Config)

  • evaluations (Sequence[Any])

  • source_artifacts (Mapping[str, Any])

  • source_digests (Mapping[str, Any])

  • cleaning_policy (str)

  • run_dir (str | PathLike[str])

Return type:

ReportArtifacts

p2p.stages.report.write_regression_report(config, regression, *, run_dir)[source]

Write canonical artifacts for one regression result.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

Return type:

ReportArtifacts

Simulation and sweep execution

Deterministic execution plumbing for configured sweeps and simulations.

class p2p.simulations.runner.SimulationArtifactResult(config_digest, run_dir, artifact_path, settings)[source]

A deterministic record of declared scalar simulation settings.

This fallback deliberately records configuration only. It does not claim to execute a scientific simulation or emit model/metric results.

Parameters:
  • config_digest (str)

  • run_dir (Path)

  • artifact_path (Path)

  • settings (Mapping[str, Any])

class p2p.simulations.runner.SweepChildResult(index, repeat, case, run_dir, config, snapshot_path, result)[source]

One expanded, snapshotted, and executed sweep child.

Parameters:
  • index (int)

  • repeat (int)

  • case (int)

  • run_dir (Path)

  • config (Config)

  • snapshot_path (Path)

  • result (Any)

class p2p.simulations.runner.SweepCommandResult(kind, target_verb, run_dir, children, aggregation=None, aggregation_artifacts=None, summary_csv=None, summary_json=None, aggregate_csv=None)[source]

Completed child runs and optional shared aggregation artifacts.

Parameters:
p2p.simulations.runner.run_declared_simulation(config, *, run_dir, verbose=False)[source]

Record scalar simulation settings without fabricating scientific output.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • verbose (bool)

Return type:

SimulationArtifactResult

p2p.simulations.runner.run_sweep(config, *, kind, run_dir, solver='CLARABEL', verbose=False, registry=<p2p.registry.Registry object>, same_verb_runner=None, aggregation_spec=None)[source]

Expand, snapshot, and execute one configured sweep.

Parameters:
  • config (Config)

  • kind (str)

  • run_dir (str | PathLike[str])

  • solver (str)

  • verbose (bool)

  • registry (Registry[Callable[[...], Any]])

  • same_verb_runner (Callable[[...], Any] | None)

  • aggregation_spec (AggregateSpec | None)

Return type:

SweepCommandResult

Faithful offline scientific simulations over cached P2P bases.

class p2p.simulations.scientific.ScientificSimulationResult(config_digest, run_dir, artifact_path, csv_path, data, fit, stats, entropy_sample=None)[source]

One fitted scientific simulation child and its artifact.

Parameters:
p2p.simulations.scientific.run_scientific_simulation(config, *, run_dir, solver='CLARABEL', verbose=False)[source]

Run one cached-basis simulation child and write auditable results.

Parameters:
  • config (Config)

  • run_dir (str | PathLike[str])

  • solver (str)

  • verbose (bool)

Return type:

ScientificSimulationResult

Interactive diagnostics

Self-contained interactive diagnostics for completed or running P2P runs.

class p2p.diagnostics.DiagnosticDashboard(run_dir, output_path, sections, warnings)[source]

One generated dashboard and the evidence it could load.

Parameters:
  • run_dir (Path)

  • output_path (Path)

  • sections (tuple[str, ...])

  • warnings (tuple[str, ...])

p2p.diagnostics.build_dashboard(run_dir, *, output_path=None, advanced=False, refresh_seconds=None)[source]

Build a standalone Plotly dashboard from a run directory.

Parameters:
  • run_dir (str | PathLike[str])

  • output_path (str | PathLike[str] | None)

  • advanced (bool)

  • refresh_seconds (float | None)

Return type:

DiagnosticDashboard

p2p.diagnostics.serve_dashboard(run_dir, *, output_path=None, advanced=False, host='127.0.0.1', port=8050, refresh_seconds=2.0)[source]

Serve a dashboard that rebuilds whenever the browser requests it.

Parameters:
  • run_dir (str | PathLike[str])

  • output_path (str | PathLike[str] | None)

  • advanced (bool)

  • host (str)

  • port (int)

  • refresh_seconds (float)

Return type:

None

Read-only adapters for runs written in the earlier result format.

class p2p.earlier_runs.EarlierRunBundle(detected, diagnostics, metrics, survey, responses, archived_images, notes)[source]

Canonical diagnostic inputs recovered from one legacy run directory.

Parameters:
  • detected (bool)

  • diagnostics (dict[str, Any] | None)

  • metrics (dict[str, Any] | None)

  • survey (Survey | None)

  • responses (Responses | None)

  • archived_images (tuple[Path, ...])

  • notes (tuple[str, ...])

p2p.earlier_runs.load_earlier_run(run_dir)[source]

Recover canonical diagnostics without importing or unpickling old code.

Parameters:

run_dir (str | Path)

Return type:

EarlierRunBundle