|
| 1 | +# experiment |
| 2 | + |
| 3 | +Experiment orchestration engine for automated Ar/Ar geochronology analyses. |
| 4 | +Manages the full lifecycle of running samples through an extraction line and |
| 5 | +mass spectrometer -- from queue setup through data persistence. |
| 6 | + |
| 7 | +## What This Package Owns |
| 8 | + |
| 9 | +- **Experiment queues** -- tabular lists of analyses loaded from tab-delimited |
| 10 | + files with YAML metadata headers |
| 11 | +- **Automated runs** -- individual analysis executions with a formal state machine |
| 12 | + (NOT_RUN → EXTRACTION → MEASUREMENT → SUCCESS/FAILED/TRUNCATED/CANCELED) |
| 13 | +- **Python scripts** -- per-run PyScripts that program extraction, measurement, |
| 14 | + and post-measurement behavior |
| 15 | +- **Conditionals** -- runtime rules that can truncate, terminate, cancel, or |
| 16 | + modify the queue based on live data (isotope signals, pressures, ages, ratios) |
| 17 | +- **Overlap mode** -- concurrent execution where extraction of run N+1 starts |
| 18 | + in a thread while measurement of run N is still running |
| 19 | +- **Data persistence** -- saving results to database, DVC, and Excel |
| 20 | +- **Statistics** -- run timing, weighted means, MSWD, ETF tracking |
| 21 | +- **Scheduling** -- delayed start and scheduled stop |
| 22 | + |
| 23 | +This package **orchestrates** hardware subsystems; it does **not** own hardware |
| 24 | +communication (see `extraction_line/`, `spectrometer/`, `lasers/`) or data |
| 25 | +reduction (see `processing/`). |
| 26 | + |
| 27 | +## Entry Points |
| 28 | + |
| 29 | +| Class | File | Role | |
| 30 | +|-------|------|------| |
| 31 | +| `Experimentor` | `experimentor.py` | Top-level facade; owns factory, queue, executor | |
| 32 | +| `ExperimentFactory` | `factory.py` | Builds queues and run specs from UI input | |
| 33 | +| `ExperimentQueue` | `queue/experiment_queue.py` | Main queue class; manages run list | |
| 34 | +| `ExperimentExecutor` | `experiment_executor.py` (~2900 lines) | Core execution engine | |
| 35 | +| `AutomatedRun` | `automated_run/automated_run.py` (~3350 lines) | Executes a single analysis | |
| 36 | +| `AutomatedRunSpec` | `automated_run/spec.py` | Data model for a queued run | |
| 37 | +| `BaseScript` | `script/script.py` | Per-run PyScript management | |
| 38 | +| `Datahub` | `datahub.py` | Central data store manager (DVC, MassSpec DB) | |
| 39 | +| `ExperimentEditorTask` | `tasks/experiment_task.py` | Envisage task (UI integration) | |
| 40 | + |
| 41 | +## Critical Files |
| 42 | + |
| 43 | +``` |
| 44 | +experiment/ |
| 45 | +├── experimentor.py # Top-level manager / facade |
| 46 | +├── experiment_executor.py # Core execution engine (~2900 lines) |
| 47 | +├── factory.py # ExperimentFactory / AutomatedRunFactory |
| 48 | +├── experiment_queue/ |
| 49 | +│ ├── experiment_queue.py # Main queue class |
| 50 | +│ ├── base_queue.py # Load/dump tab-delimited files |
| 51 | +│ ├── run_block.py # Groups runs into blocks |
| 52 | +│ ├── parser.py # RunParser / UVRunParser |
| 53 | +│ └── factory.py # ExperimentQueueFactory |
| 54 | +├── automated_run/ |
| 55 | +│ ├── automated_run.py # Single analysis executor (~3350 lines) |
| 56 | +│ ├── spec.py # Run data model |
| 57 | +│ ├── state_machine.py # Formal state machine |
| 58 | +│ ├── multi_collector.py # Data collector (multi-collector) |
| 59 | +│ ├── peak_hop_collector.py # Data collector (peak-hopping) |
| 60 | +│ └── persistence.py # Save to DB / DVC / Excel |
| 61 | +├── conditional/ |
| 62 | +│ ├── conditional.py # All conditional types (~900 lines) |
| 63 | +│ └── utilities.py # Tokenizer for conditional expressions |
| 64 | +├── script/ |
| 65 | +│ └── script.py # PyScript loading and validation |
| 66 | +├── datahub.py # Central data store manager |
| 67 | +├── experiment_status.py # UI status display |
| 68 | +├── experiment_scheduler.py # Delayed start / scheduled stop |
| 69 | +├── conflict_resolver.py # Repository identifier conflicts |
| 70 | +├── events.py # Hook system (5 event levels) |
| 71 | +├── stats.py # Run timing and statistics |
| 72 | +├── utilities/ |
| 73 | +│ ├── identifier.py # Labnumber parsing and formatting |
| 74 | +│ ├── runid.py # Run ID manipulation |
| 75 | +│ ├── position_regex.py # Position expression parsing |
| 76 | +│ ├── conditionals.py # Conditional constants |
| 77 | +│ └── comment_template.py # Comment templating |
| 78 | +└── tasks/ |
| 79 | + └── experiment_task.py # Envisage task (UI panes) |
| 80 | +``` |
| 81 | + |
| 82 | +## Runtime Lifecycle |
| 83 | + |
| 84 | +### Phase 0: Setup |
| 85 | +User configures an `ExperimentQueue` via `ExperimentFactory` UI, adding |
| 86 | +`AutomatedRunSpec` entries. Each spec has: labnumber, aliquot, position, |
| 87 | +scripts (extraction, measurement, post-equilibration, post-measurement), |
| 88 | +and parameters (duration, extract value, cleanup times, etc.). |
| 89 | + |
| 90 | +### Phase 1: Execute |
| 91 | +`ExperimentExecutor.execute()` runs pre-execute checks (hardware connectivity, |
| 92 | +script existence), then starts a new thread `"Execute Queues"`. |
| 93 | + |
| 94 | +### Phase 2: Queue Loop |
| 95 | +Iterates over all open experiment queues. For each queue: pre-queue check, |
| 96 | +then `_execute_queue()`. |
| 97 | + |
| 98 | +### Phase 3: Run Loop |
| 99 | +Gets a run generator yielding `AutomatedRunSpec` objects. For each spec: |
| 100 | +pre-run check → `_make_run()` → execute run (sync or thread for overlap mode). |
| 101 | + |
| 102 | +### Phase 4: Individual Run |
| 103 | +Each run executes four steps sequentially: |
| 104 | +``` |
| 105 | +_start → _extraction → _measurement → _post_measurement |
| 106 | +``` |
| 107 | +- **`_start`**: Sets integration time, calls `run.start()` |
| 108 | +- **`_extraction`**: Runs extraction PyScript, monitors extraction line |
| 109 | +- **`_measurement`**: Runs measurement PyScript with data collection |
| 110 | + (multi-collector or peak-hop). Equilibration scripts run concurrently. |
| 111 | +- **`_post_measurement`**: Runs post-measurement PyScript |
| 112 | + |
| 113 | +After steps: `run.save()` → post-run check → `run.finish()` → `run.teardown()` |
| 114 | + |
| 115 | +### Phase 5: Completion |
| 116 | +`_end_runs()` stops stats timer, `END_QUEUE` event fires, `alive=False`. |
| 117 | + |
| 118 | +### Overlap Mode |
| 119 | +If a run's `overlap` flag is set, extraction runs in a separate thread while |
| 120 | +the next run's measurement can begin. Critical for throughput when laser |
| 121 | +heating takes longer than measurement setup. |
| 122 | + |
| 123 | +### Event System |
| 124 | +Five event levels allow external hooks: |
| 125 | +`START_QUEUE` → `START_RUN` → `SAVE_RUN` → `END_RUN` → `END_QUEUE` |
| 126 | + |
| 127 | +## Test Strategy |
| 128 | + |
| 129 | +Tests live in `pychron/experiment/tests/` (21 test files). Uses standard |
| 130 | +`unittest` framework. |
| 131 | + |
| 132 | +| Test File | Coverage | |
| 133 | +|-----------|----------| |
| 134 | +| `state_machine.py` | State machine transitions (nominal, terminal, abort, reset) | |
| 135 | +| `identifier.py` | `get_analysis_type()` for all special identifiers | |
| 136 | +| `conditionals.py` | Conditional parsing, tokenization, evaluation | |
| 137 | +| `conditionals_actions.py` | Conditional action execution | |
| 138 | +| `pyscript_integration.py` | Script name resolution, loading validation | |
| 139 | +| `frequency_test.py` | Frequency run generation | |
| 140 | +| `position_regex_test.py` | Position regex matching | |
| 141 | +| `analysis_grouping_test.py` | Analysis grouping logic | |
| 142 | +| `editor_executor_sync.py` | Editor-executor synchronization | |
| 143 | +| `repository_identifier.py` | Repository identifier handling | |
| 144 | + |
| 145 | +**Notable gaps**: No unit tests for `ExperimentExecutor`, `AutomatedRun`, |
| 146 | +`ExperimentQueue`, or `RunParser` -- too tightly coupled to hardware and Qt. |
| 147 | + |
| 148 | +## Common Failure Modes |
| 149 | + |
| 150 | +| Failure | Symptom | Where | |
| 151 | +|---------|---------|-------| |
| 152 | +| Pre-execute check failure | Warning dialog, `alive=False` | `experiment_executor.py` | |
| 153 | +| Pre-run check failure | Run skipped, logged | `experiment_executor.py` | |
| 154 | +| Step failure | Run transitions to FAILED, queue stops | `experiment_executor.py` | |
| 155 | +| Monitor fatal error | Run canceled and failed | `automated_run/` | |
| 156 | +| DVC save timeout (>5 min) | Run canceled, marked FAILED | `experiment_executor.py` | |
| 157 | +| Post-run check failure | `_err_message` set, queue stops | `experiment_executor.py` | |
| 158 | +| User cancel/stop/abort | Three levels of intervention | `experiment_executor.py` | |
| 159 | +| Repository conflicts | Same labnumber, different identifiers | `conflict_resolver.py` | |
| 160 | +| Database errors | `DatabaseError` caught, logged | `experiment_executor.py` | |
| 161 | + |
| 162 | +### Exception Classes |
| 163 | +- `ExtractionException` -- extraction hardware failures |
| 164 | +- `PreExecuteCheckException` -- pre-execute validation failures |
| 165 | +- `PreExtractionCheckException` -- pre-extraction check failures |
| 166 | +- `CheckException` -- base for check failures with tag |
| 167 | +- `MessageException` -- generic message+error exception |
| 168 | + |
| 169 | +### User Intervention Levels |
| 170 | +- **Stop**: Sets `alive=False`, waits for current run to finish |
| 171 | +- **Cancel**: Sets `_canceled=True`, cancels current run (30s confirmation timeout) |
| 172 | +- **Abort**: Sets `_aborted=True`, immediately aborts running extraction/measurement |
0 commit comments