prodigy.analysis.session¶
Analysis sessions¶
An AnalysisSession bundles everything a single analysis run needs to know up
front: the program, the input distribution, the configuration and one decision per
while loop (which loop strategy to use, with
which parameters). Instruction handlers no longer call input() themselves; they ask
the session’s DecisionProvider:
InteractiveDecisionsasks on the terminal with the historic prompts. The CLI uses it, soprodigy mainbehaves as before.FixedDecisionsanswers from a dictionary (e.g. parsed from JSON). The web GUI, a language server orprodigy analyzeuse it. A missing decision raisesDecisionRequiredinstead of blocking.
While a session runs, it also records what happened: query results with sound intervals
(see prodigy.analysis.result), what each loop strategy produced and certified,
warnings, progress and optionally the distribution after each top-level instruction.
AnalysisSession.run() returns an AnalysisResult
whose to_json() is the exchange format for
front ends.
Code that calls compute_discrete_distribution() or the
handlers directly (tests, scripts, older code) does not need a session: if none is active,
current_session() hands out a detached interactive one, which reproduces the old
behaviour.
- token_positions(source, pattern)[source]¶
1-based (line, column) of every match of
patternoutside of comments.
- class LoopInfo(id, cond, depth, function=None, line=None, column=None)[source]¶
Bases:
objectA while loop of the analysed program, identified by its position in the text.
- Parameters:
- class LoopRegistry(program, source=None)[source]¶
Bases:
objectAssigns stable ids to the while loops of a program (textual pre-order).
- query_instructions(program)[source]¶
- Parameters:
program (
Program)- Return type:
List[SkipInstr|AbortInstr|WhileInstr|IfInstr|AsgnInstr|ChoiceInstr|LoopInstr|TickInstr|ObserveInstr|QueryInstr|ProbabilityQueryInstr|ExpectationInstr|PlotInstr|PrintInstr|OptimizationQuery]
- class LoopDecision(strategy, params=dict())[source]¶
Bases:
objectWhich strategy to use for a loop, and its parameters (raw, not yet parsed).
- static parse_spec(spec)[source]¶
Parses the command-line form
STRATEGY[:name=value,name=value].Example:
unroll:iterations=100orinvariant:invariant_file=inv.pgcl.- Parameters:
spec (
str)- Return type:
- DEFAULT_KEY = '*'¶
Key for the decision that applies to every loop without an own decision.
- validate_decisions(decisions, loops=None)[source]¶
Problems with
decisions(unknown strategies, unknown loops, bad parameters).- Parameters:
decisions (
Mapping[int|str,LoopDecision])loops (
LoopRegistry|None)
- Return type:
- exception DecisionRequired(message, *, loop=None, strategy=None, param=None)[source]¶
Bases:
ExceptionA non-interactive analysis reached a choice that was not decided up front.
- class DecisionProvider(decisions=None)[source]¶
Bases:
objectAnswers the questions an analysis may have while it runs.
- decisions: Dict[int | str, LoopDecision]¶
- loop_decision(session, loop, instruction)[source]¶
- Parameters:
session (
AnalysisSession)instruction (
WhileInstr)
- Return type:
- ask_param(session, task, spec)[source]¶
- Parameters:
session (
AnalysisSession)task (
LoopTask)spec (
ParamSpec)
- Return type:
- class InteractiveDecisions(decisions=None)[source]¶
Bases:
DecisionProviderAsks on the terminal, with the prompts the CLI has always used.
Decisions given up front (e.g.
prodigy main --loop 0=unroll:iterations=10) are used without asking.
- class FixedDecisions(decisions=None)[source]¶
Bases:
DecisionProviderAnswers only from decisions given up front; never blocks.
- class LoopRecord(loop, cond, strategy, params, invocations=0, kind='exact', residual=0, time=0.0, certificates=list(), messages=list(), progress=None, failed=None, inside_checks=0, suppressed_messages=0)[source]¶
Bases:
objectEverything one loop produced during a run (a loop may be entered several times).
- Parameters:
- inside_checks: int = 0¶
How many of the invocations happened inside the check of another loop’s invariant.
- MAX_MESSAGES = 4¶
- current_session()[source]¶
The session of the running analysis, or a detached interactive one.
- Return type:
- class AnalysisSession(program, *, source=None, config=None, provider=None, decisions=None, input_distribution=None, allow_files=True, capture_output=False, output_callback=None, progress_callback=None, record_steps=False, coefficient_limit=30)[source]¶
Bases:
objectOne analysis run with explicit decisions; see the module documentation.
- Parameters:
- loop_records: Dict[Any, LoopRecord]¶
- classmethod detached()[source]¶
Session for code that runs handlers without setting up a session.
- Return type:
- activate()[source]¶
Makes this the current session (see
current_session()).- Return type:
- resolve_alias(task, alias, raw)[source]¶
Turns an alternative parameter (e.g. a file name) into the main parameter.
- property scope: _Scope¶
- sub_analysis(reason)[source]¶
Runs nested analyses whose distributions are not the program’s (second-order test distributions, invariant candidates, …). Their loops are still decided as usual, but neither their queries nor their residual mass count for the program. The yielded scope tells afterwards whether everything in it was exact.
- on_normalize(error_prob)[source]¶
Called right before the distribution is conditioned on the observations.
- Return type:
- property collect_details: bool¶
Whether to compute extra data for front ends (coefficient tables etc.).
- run_loop(task)[source]¶
Runs
task.strategyand books its outcome (used by the while handler).- Parameters:
task (
LoopTask)- Return type:
- on_instruction(instruction)[source]¶
Called for every instruction before it is executed (see
compute_semantics).
- loop_query_bounds(kind, expression)[source]¶
Bounds that the strategy of the loop right before this query computed for it.
- record_query(query)[source]¶
Books a
QueryResult(ignored in sub-analyses).- Return type:
- record_step(instruction, distribution, error_prob)[source]¶
- Parameters:
instruction (
SkipInstr|AbortInstr|WhileInstr|IfInstr|AsgnInstr|ChoiceInstr|LoopInstr|TickInstr|ObserveInstr|QueryInstr|ProbabilityQueryInstr|ExpectationInstr|PlotInstr|PrintInstr|OptimizationQuery)- Return type: