How usnistgov/afl-automation works
1. Overall shape
- This is a Python automation platform for the NIST Autonomous Formulation Laboratory.
- The repository is organized around a reusable HTTP-facing server framework plus many device- and protocol-specific modules.
2. Core runtime: DeviceServer / APIServer
- The architectural center is AFL/automation/APIServer.
- It provides the common server pattern: expose Python objects over HTTP, manage request/response handling, queue work, render outputs, and support automatic UI generation.
- shared.launcher is the entry point layer that starts these servers, typically with waitress in production or Flask’s built-in server for local use.
3. Driver layer: hardware and service adapters
- APIServer/Driver.py and related mixins define the contract for concrete devices.
- Drivers encapsulate hardware operations and present them through the common API.
- There are dummy drivers and virtual drivers alongside real ones, so the same API works for production hardware and offline simulation.
4. Protocol and workflow subsystems
- prepare, mixcalc, and orchestrator sit above individual devices and assemble them into lab workflows.
- mixcalc handles formulation logic and mass-balance calculations.
- prepare focuses on execution planning for preparation robots like OT-2.
- orchestrator coordinates multi-step closed-loop workflows across devices.
5. Hardware domains
- instrument covers measurement devices such as scattering instruments, spectrometers, cameras, and beamline-specific adapters.
- loading covers fluid handling and sample loading hardware such as pumps, valves, relays, pressure controllers, sensors, and sample cells.
- manipulate includes robot/arm-style physical manipulation.
- sample_env models environmental control around samples, such as temperature decks.
6. Data flow and persistence
- The APIServer/data package defines how server results are packaged and stored.
- Data can be represented as JSON, structured packets, trash/discard placeholders, or sent to Tiled-backed storage.
- This creates a boundary between live device control and durable experiment/event recording.
7. Tiled integration and browser tooling
- The repo includes Tiled client adapters and a tiled config, indicating Tiled is a key external data service.
- apps/tiled_browser and APIServer/data/TiledClients provide user-facing browsing and catalog access to stored experimental data.
- This is the main persistence/query plane separate from the live control plane.
8. User interface layer
- AFL/automation/apps contains browser-facing HTML/JS/CSS apps for server control, configuration, status, mix doctor workflows, OT-2 deck management, and Tiled browsing.
- APIServer/static/jl also embeds a JupyterLite environment with notebooks and extensions, giving an in-browser analysis/scripting surface.
- UI assets are mostly static and talk to the HTTP APIs exposed by the servers.
9. Shared infrastructure
- AFL/automation/shared holds cross-cutting utilities: serialization, units, queue wrappers, persistent config, server discovery, widgets, warnings, and helper clients.
- This layer is the glue between drivers, UIs, and data backends.
- It also suggests that configuration is partly user-local, with ~/.afl/config.json used by virtual server setup.
10. Offline/virtual operation
- The repo deliberately supports a full software-only stack under server_scripts/virtual_instrument and dummy modules in prepare, APIServer, and instrument.
- The same HTTP interfaces are preserved, so workflows can be tested without hardware.
- This is an important architectural boundary: hardware implementation is swappable behind the same API.
11. Deployment and environment
- There are Dockerfiles for major service groups (instrument, mixcalc, orchestrator, tiled) and a docker-compose.yml for multi-service deployment.
- launchers/ and deploy/ contain operational scripts and platform-specific setup, including vendored packages for constrained environments.
- The architecture is therefore service-oriented even though the code is packaged as one repository.
12. Quality, docs, and CI
- Tests are organized with pytest and cover drivers, orchestration, Tiled integration, web app behavior, and math/model logic.
- GitHub Actions runs test, integration, docs, and publish workflows.
- Sphinx docs under docs/ describe architecture, data model, launcher behavior, and UI usage, reinforcing that the repo is intended to be deployed as a coordinated automation platform rather than a single application.
13. Primary end-to-end flow
- A user or launcher starts one or more DeviceServer instances.
- UI or client code sends HTTP requests to those servers.
- Drivers execute hardware or virtual actions.
- Results are queued, rendered, and optionally persisted to JSON/Tiled.
- Higher-level workflow services like prepare and orchestrator compose multiple device actions into complete lab protocols.
Main entry points: APIServer.py APIServer · Driver.py Driver · DummySampleServer.sh Virtual scripts · OrchestratorDriver.py Orchestrator
Components 23
Core runtime
HTTP server framework and driver contract.
- APIServer · Exposes Python objects over HTTP, queues work, renders responses, and supports UI generation.
AFL/automation/APIServer/APIServer.py - Driver · Base interface for concrete hardware and service adapters.
AFL/automation/APIServer/Driver.py - WebApp mixin · Adds browser-facing behaviors and searchable server actions.
AFL/automation/APIServer/DriverWebAppsMixin.py - Queue daemon · Background execution and request queue management for server tasks.
AFL/automation/APIServer/QueueDaemon.py
Workflows
Higher-level automation and formulation logic.
- Mixcalc · Mass-balance calculations, solution models, and formulation planning.
AFL/automation/mixcalc/MassBalance.py - Prepare · OT-2 and preparation-robot planning and execution helpers.
AFL/automation/prepare/PrepareDriver.py - Orchestrator · Coordinates multi-step workflows across devices and services.
AFL/automation/orchestrator/OrchestratorDriver.py
Hardware domains
Device adapters grouped by lab function.
- Instrument · Scattering, spectroscopy, imaging, and beamline-facing instrument drivers.
AFL/automation/instrument/ScatteringInstrument.py - Loading · Pumps, valves, sensors, relays, pressure controllers, and sample-cell hardware.
AFL/automation/loading/SyringePump.py - Manipulate · Physical manipulation adapters such as arm-style motion control.
AFL/automation/manipulate/XArmManipulator.py - Sample env · Environmental control around samples, such as temperature decks.
AFL/automation/sample_env/TemperatureDeck.py
Data plane
Result packaging, storage, and Tiled integration.
- JSON data · Structured in-memory and serialized server results.
AFL/automation/APIServer/data/DataJSON.py - Data packet · Typed packet representation for server outputs and records.
AFL/automation/APIServer/data/DataPacket.py - Tiled data · Stores or forwards experiment data into Tiled-backed storage.
AFL/automation/APIServer/data/DataTiled.py - Tiled clients · Client wrappers for event catalogs and driver-task access.
AFL/automation/APIServer/data/TiledClients/CatalogOfAFLEvents.py
User interface
Browser apps and embedded JupyterLite surface.
- AFL app · Main browser control surface for servers, queues, and status.
AFL/automation/apps/afl_app/webapp.html - Tiled browser · Browser tooling for querying, plotting, and inspecting stored datasets.
AFL/automation/apps/tiled_browser/tiled_browser.html - JupyterLite · Embedded notebook and REPL environment served from static assets.
AFL/automation/APIServer/static/jl/index.html
Shared infra
Cross-cutting utilities, launchers, and deployment.
- Launcher · Starts server processes locally or in production.
AFL/automation/shared/launcher.py - Shared utils · Serialization, units, queues, configs, discovery, warnings, and helper clients.
AFL/automation/shared/utilities.py - Config store · Persistent user-local configuration used by virtual and deployed server setups.
AFL/automation/shared/PersistentConfig.py - Deploy · Docker, compose, environment, and launcher assets for multi-service deployment.
docker-compose.yml - Virtual scripts · Standalone virtual instrument and dummy server entrypoints for software-only operation.
server_scripts/virtual_instrument/DummySampleServer.sh
Connections 27
- Launcher → APIServer: starts · Launches HTTP server instances.
- APIServer → Driver: hosts · Wraps device implementations behind the common API.
- Driver → WebApp mixin: extends · Drivers can expose richer browser-facing actions and views.
- APIServer → Queue daemon: queues work · Delegates long-running actions and async execution.
- APIServer → Shared utils: uses · Shares serialization, discovery, config, and helper utilities.
- Shared utils → Config store: persists · Reads and writes local configuration state.
- Driver → Instrument: implements · Instrument adapters satisfy the driver contract.
- Driver → Loading: implements · Loading hardware is exposed through the same driver abstraction.
- Driver → Manipulate: implements · Robot/manipulator control is wrapped as drivers.
- Driver → Sample env: implements · Environmental hardware is exposed via drivers.
- Mixcalc → Prepare: feeds · Formulation outputs inform preparation planning.
- Prepare → Orchestrator: coordinates · Preparation steps participate in broader workflows.
- Orchestrator → Instrument: controls · Coordinates measurements and acquisitions.
- Orchestrator → Loading: controls · Coordinates pumps, valves, and sample loading.
- Orchestrator → Sample env: controls · Coordinates environmental conditions during workflows.
- APIServer → JSON data: returns · Packages responses and server results.
- APIServer → Data packet: emits · Converts actions into structured records.
- Data packet → Tiled data: stores · Persistent experiment/event storage path.
- Tiled data → Tiled clients: queries · Uses client adapters to access Tiled catalogs.
- AFL app → APIServer: calls · Control UI talks to the HTTP server APIs.
- Tiled browser → Tiled data: browses · Data browser queries persisted datasets.
- JupyterLite → Tiled data: analyzes · Notebook tooling can inspect stored data.
- Deploy → Launcher: packages · Deployment assets wire together server startup and services.
- Virtual scripts → APIServer: mirrors · Offline entrypoints preserve the same server shape without hardware.
- Virtual scripts → Instrument: simulates · Virtual instrument entrypoints stand in for real hardware.
- Virtual scripts → Loading: simulates · Dummy loaders and sample servers model loading hardware.
- Virtual scripts → Prepare: simulates · Virtual OT-2 and prep flows support offline testing.