SequenceContext#

Module: exa.core.sequence.context

class SequenceContext(data, tracker_class=<class 'exa.core.sequence.experiment_tracker.ExperimentTracker'>, observation_stash=None, attribute_options=None, sequence_executor_class=<class 'exa.core.sequence.sequence_executor.SequenceExecutor'>, sequence_run_id=None)#

Bases: object

A stateful object that provides the tools for Sequence execution and captures the inputs and outputs.

SequenceContext is a context object that captures the run IDs, analysis results, and analysis arguments for each Experiment or Sequence execution done within the context.

A user does not need to construct a SequenceContext. The intended way is to use it as a context manager, see Sequence.context().

If exceptions are caught in execute_sequence and execute_experiment, error summary and traceback are stored in results. Execution errors are stored in sequence result data which will be stored to persistence. On the contrary, analysis errors are stored only locally in analyze results.

Logging of errors is handled exclusively in __exit__.

Parameters:
  • data (SequenceData) – The serializable part of the Sequence that will be executed with this context.

  • tracker_class (type[ExperimentTracker]) – Optional subclass of ExperimentTracker. This class will be used when creating a tracker object with track().

  • observation_stash (ObservationStash | None) – The stash to save observations to instead of the DB.

  • sequence_executer_class – Will be used when executing a sequence.

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

  • sequence_executor_class (type[SequenceExecutor])

  • sequence_run_id (UUID | None)

Attributes

sub_stashes

Stores the observation stashes of any subsequences executed in this context (mapped to the sequence_run_id).

Methods

end_tracking

Get the captured data from an ExperimentTracker instance and store it to the context.

execute_experiment

Run a single experiment and its analysis.

execute_sequence

Execute and analyze another sequence in this context.

to_result

Converts the captured execution results into a SequenceExecuteResult data object.

track

Create a tracker object that wraps an Experiment instance and captures the actions done with it.

sub_stashes: dict[UUID, ObservationStash | None]#

Stores the observation stashes of any subsequences executed in this context (mapped to the sequence_run_id).

execute_experiment(experiment, custom_analysis=None, run_args=None, analyze_args=None, observation_stash=None)#

Run a single experiment and its analysis.

This includes:

  • running the experiment on hardware

  • analyzing the execution results with given arguments

  • tagging the observations from the analysis. Note: the tags entry of Sequence.options will be used for tagging, if any.

  • saving the observations.

Under the hood, the Experiment is wrapped with tracker_class, which means that a tracker class can be used to customize the behavior without calling track() explicitly.

Parameters:
  • experiment (Experiment | type[Experiment]) – The experiment to execute. Can be either an instance or a class, in which case a new instance is created.

  • custom_analysis (Callable[[...], AnalyzeResult] | None) – Custom analysis function for the experiment.

  • run_args (dict[str, Any] | None) – Arguments to pass to the experiment.run, most commonly at least the list of components.

  • analyze_args (dict[str, Any] | None) – Arguments to pass to the experiment.analyze.

  • observation_stash (ObservationStash | None) – The stash to save observations to instead of the DB. If not provided, will use the stash in self._observation_stash, and if that is empty as well, the observations will be saved to the DB.

Returns:

The result of the experiment.analyze.

Return type:

AnalyzeResult

execute_sequence(sequence, analyze_args=None)#

Execute and analyze another sequence in this context.

The analyze_results of each Experiment or sub-sequence within sequence are captured.

If sequence does not have a stash and the context has self._observation_stash populated, will copy the contents of self._observation_stash and run sequence on that stash. At the end of execution, the stash of sequence will be saved (i.e. either merged to self._observation_stash or if that does not exist, saved to the DB) based on the value of the attribute option save_stash.

Parameters:
  • sequence (_Sequence) – The sequence instance to execute.

  • analyze_args (dict[str, Any] | None) – Arguments to pass to the sequence.analyze.

Returns:

The analyzed result of executing sequence.

Return type:

SequenceAnalyzeResult

to_result()#

Converts the captured execution results into a SequenceExecuteResult data object.

As a side effect, the final result of this sequence is saved to persistence. The result is marked final after which further changes to it are prevented. Thus, this method is callable only once per object.

Should be called at the end of Sequence.execute().

Returns:

A new instance of SequenceExecuteResult.

Return type:

SequenceExecuteResult

track(experiment, observation_stash=None)#

Create a tracker object that wraps an Experiment instance and captures the actions done with it.

As an alternative to execute_experiment(), the tracker allows a more fluent workflow with the experiment instance. The tracker behaves like a normal Experiment. The use cases are, for example, tuning settings, sweep ranges, and options before running.

The tracker should only be used for one round of run & analyze. After this, end_tracking() should be called.

Parameters:
  • experiment (Experiment | type[Experiment] | ExperimentTracker) – An Experiment instance or an Experiment class. In case of class, a new instance is created.

  • observation_stash (ObservationStash | None) – The stash to save observations to instead of the DB. If not provided, the observations

  • DB. (will be saved to the)

Returns:

A new instance of ExperimentTracker. The created class is determined be tracker_class.

Return type:

ExperimentTracker

end_tracking(tracker)#

Get the captured data from an ExperimentTracker instance and store it to the context.

This should be called after performing the desired run and analyze actions with the tracker, so that the results and used arguments are stored.

Parameters:

tracker (ExperimentTracker) – the instance to get data from.

Returns:

The AnalyzeResult captured by the tracker, if any.

Return type:

AnalyzeResult

exception AnalyzeError#

Bases: Exception

Internal helper type for propagating analysis errors.

Analysis error can happen:

  • subsequence or experiment analysis in execute_sequence method inside sequence.execute.

  • in sequence analysis in execute_sequence method.

exception ChildSequenceExecuteError#

Bases: Exception

Internal helper type for propagating child sequence execution errors to upper level context.

exception InternalError#

Bases: Exception

Internal helper type for propagating internal sequence execution errors to upper level context.

Inheritance

Inheritance diagram of exa.core.sequence.context.SequenceContext