From b1261428576190c08b648dd1bfe43d9bc0fc6f70 Mon Sep 17 00:00:00 2001 From: Arthur Sloan Date: Fri, 23 Oct 2026 21:44:04 -0400 Subject: [PATCH 1/3] Created an agents.md file for working in or with the pyares library --- AGENTS.md | 350 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 350 insertions(+) create mode 100644 AGENTS.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..5ecbacb --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,350 @@ +# AGENTS.md — Repository Navigation Context for PyAres + +Context for AI agents and human contributors working **in** (maintaining/extending) +or **with** (consuming/implementing services for) the **PyAres** library. + +--- + +## 1. What This Project Is + +**PyAres** is the official Python SDK and gRPC service framework for the **ARES** +(Autonomous Research System) self-driving laboratory ecosystem. It bridges native Python +code with the centralized [ARES OS 2](https://github.com/AFRL-ARES/ARES) orchestrator using +Protocol Buffers and gRPC via [`ares-datamodel`](https://github.com/AFRL-ARES/ares-datamodel). + +PyAres abstracts away raw Protobuf messages and gRPC boilerplate, exposing clean, +idiomatic Python interfaces to implement four core service archetypes: + +1. **Device Services (`AresDeviceService`)** — Control laboratory hardware, actuators, + and sensors; expose executable commands, schemas, and live telemetry; implement safety + interlocks and safe-mode procedures. +2. **Planning Services (`AresPlannerService`)** — Drive experimental campaigns using + optimization algorithms, active learning, Bayesian optimization, heuristic search, etc. + to suggest the next parameter values to evaluate. +3. **Analysis Services (`AresAnalyzerService`)** — Extract quantitative objectives and + metrics from raw experimental outputs, passing structured results to planners and ARES OS. +4. **Visualizer Services (`AresVisualizerService`)** — Render charts and dashboard widgets + (e.g., Plotly figures) for display in the ARES OS web interface. + +--- + +## 2. Python Environment & Build System + +- **Python Version**: `>= 3.10` +- **Build Backend**: [`hatchling`](https://hatch.pypa.io/) (configured in `pyproject.toml`) +- **Core Dependency**: `ares-datamodel >= 0.36.0` (provides Protobuf/gRPC message stubs) +- **Optional Dependencies**: `visualization = ["plotly >= 5.0"]` +- **Dynamic Versioning**: `hatchling` reads the package version dynamically from the + `PYARES_PACKAGE_VERSION` environment variable (required during `build` or `pip install -e .`): + ```bash + export PYARES_PACKAGE_VERSION="0.0.1" # or matching release tag + ``` + +### Development & Testing Setup + +```bash +# Clone and enter repo +cd PyAres + +# Set version for hatchling +export PYARES_PACKAGE_VERSION="0.0.1" + +# Install in editable mode with test dependencies +pip install -e . +pip install grpcio grpcio-tools protobuf pytest + +# Run full test suite +pytest -v tests/ +``` + +--- + +## 3. Repository Layout & Code Map + +``` +PyAres/ +├── pyproject.toml # Hatchling build specification & dependencies +├── README.md # Upstream package summary & centralized docs link +├── LICENSE # MIT License +├── .github/workflows/ # CI/CD: unit tests (ubuntu, py3.10) & PyPI publishing +├── tests/ # Unit tests & MockGrpcContext +└── PyAres/ # Root package directory + ├── __init__.py # Public top-level API exports + ├── Device/ # Hardware control service framework + │ ├── device_service.py # AresDeviceService & AresDeviceServiceWrapper (gRPC servicer) + │ ├── device_models.py # DeviceCommandDescriptor, DeviceCommandResponse, StatusCode, etc. + │ └── device_warnings.py # Warning categories for device services + ├── Planning/ # Campaign & experiment planning framework + │ ├── planning_service.py # AresPlannerService & AresPlannerServiceWrapper (gRPC servicer) + │ └── planner_models.py # PlanRequest, PlanResponse, PlanningParameter, Plan, etc. + ├── Analyzing/ # Experiment evaluation & objective calculation framework + │ ├── analysis_service.py # AresAnalyzerService & AresAnalyzerServiceWrapper (gRPC servicer) + │ └── analyzer_models.py # AnalysisRequest, AnalysisResponse, Objective, ObjectiveSchema + ├── Visualizers/ # UI Visualization rendering framework + │ ├── visualizer_service.py # AresVisualizerService & AresVisualizerServiceWrapper + │ └── visualizing_models.py # VisualizationOption, VisualizationRequest, DataPoint + ├── Models/ # Shared data models & enums + │ └── ares_data_models.py # AresDataType, Outcome, PlanStatusCode, RequestMetadata, Limits + ├── Utils/ # Protobuf ↔ Python conversion utilities & base infrastructure + │ ├── grpc_base.py # AresGrpcServiceBase: server lifecycle, thread pool, message size + │ ├── ares_service_base.py# AresBaseService & AresServiceWrapperBase: common RPCs & async resolver + │ ├── metadata_interceptors.py # DatamodelVersionServerInterceptor: handshake version header + │ ├── ares_value_utils.py # Primitive/AresValue protobuf bi-directional conversion + │ ├── ares_struct_utils.py# Dict/AresStruct conversion helpers + │ ├── ares_data_schema_utils.py # Schema builder helpers for inputs, settings, and outputs + │ ├── ares_data_type_utils.py # AresDataType enum translation + │ ├── device_status_code_utils.py # Status code mapping & success evaluation + │ ├── ares_outcome_utils.py # Outcome enum translation + │ ├── plan_request_utils.py # Protobuf PlanRequest unpacking + │ └── plan_response_utils.py # Protobuf PlanResponse packaging + ├── test_tools/ # Standalone test clients + │ ├── analyzer_tester.py # AnalyzerTestClient: test Analyzer services without ARES OS + │ └── planner_tester.py # PlannerTestClient: test Planner services without ARES OS + └── Demo/ # Working reference implementations + ├── Devices/ # Example devices (hotplate, rotary mixer, random generator, etc.) + ├── Planners/ # Example planners (grid, random, test-tools integration) + └── Analyzers/ # Example analyzers (basic, Ax analyzer, test-tools integration) +``` + +--- + +## 4. Architectural Patterns & Core Invariants + +### 4.1 Base Service Inheritance Architecture +Every service in PyAres (`Device`, `Planning`, `Analyzing`, `Visualizer`) is split into two components: +1. **The User-Facing Service (`AresBaseService` subclass)**: + - Manages the gRPC server lifecycle (`start()`, `stop()`). + - Configures networking (`port`, `use_localhost`, `max_message_size`). + - Manages service settings and capability schema declarations. +2. **The Internal gRPC Servicer Wrapper (`AresServiceWrapperBase` subclass)**: + - Implements generated gRPC Servicer stubs from `ares-datamodel`. + - Exposes standard diagnostic RPCs: `GetInfo`, `GetConnectionStatus`, `GetState`. + - Intercepts requests, translates protobuf messages to native Python data structures, executes registered callbacks, and packs Python returns back into protobuf responses. + +### 4.2 Version Handshake & Metadata Interception +All services run with `DatamodelVersionServerInterceptor` (`PyAres/Utils/metadata_interceptors.py`). +Every RPC attaches the `datamodel-version` header corresponding to the installed `ares_datamodel` +version. This ensures ARES OS and external orchestrators can verify protocol compatibility. + +### 4.3 Async & Sync Callback Support +User-supplied logic (e.g., `enter_safe_mode`, `plan_logic`, `custom_analysis_logic`, commands) +can be written as standard synchronous functions or `async` coroutines. The wrapper automatically +resolves awaitables via `_resolve_awaitable()` (`PyAres/Utils/ares_service_base.py`). + +### 4.4 Default Port Allocations +- **Device Service**: `7100` (configurable via `port`) +- **Planner Service**: `7082` (configurable via `port`) +- **Analyzer Service**: `7083` (configurable via `port`) + +--- + +## 5. Best Practices: Working WITH PyAres (Developing Services) + +### 5.1 Implementing Device Services (`AresDeviceService`) + +A device service wraps real or simulated hardware to expose actuators and sensors to ARES OS. + +```python +from PyAres import AresDeviceService, DeviceCommandDescriptor, DeviceSchemaEntry, AresDataType, StatusCode +from PyAres.Device.device_models import DeviceCommandResponse + +# 1. Mandatory Safe Mode Callback (must be robust, non-blocking or fast, and idempotent) +def enter_safe_mode(): + heater.set_power(0.0) + motors.stop() + +# 2. Mandatory State Polling Callback (returns telemetry dictionary) +def get_device_state() -> dict: + return { + "temperature": sensor.read_temp(), + "is_moving": motors.is_active(), + } + +service = AresDeviceService( + enter_safe_mode_logic=enter_safe_mode, + get_device_state_logic=get_device_state, + device_name="Tubular Furnace", + description="High temperature furnace driver", + version="1.0.0", + port=7100, +) + +# 3. Register Commands +set_temp_descriptor = DeviceCommandDescriptor( + name="set_temperature", + description="Sets the target furnace temperature in Celsius.", + input_schema={ + "target_celsius": DeviceSchemaEntry( + type=AresDataType.FLOAT, + description="Target temperature in deg C", + min_number_value=20.0, + max_number_value=1200.0, + ) + }, + output_schema={ + "achieved_target": DeviceSchemaEntry(type=AresDataType.FLOAT) + } +) + +def set_temperature(target_celsius: float) -> DeviceCommandResponse: + try: + hardware.set_temp(target_celsius) + return DeviceCommandResponse( + status_code=StatusCode.COMMAND_SUCCESS, + response={"achieved_target": target_celsius} + ) + except Exception as e: + return DeviceCommandResponse( + status_code=StatusCode.HARDWARE_FAULT, + error_string=str(e), + response={} + ) + +service.add_new_command(set_temp_descriptor, set_temperature) +service.start() +``` + +#### Invariants & Rules for Device Services: +- **Always return `DeviceCommandResponse`**: Returning bare values or raw dictionaries is deprecated and triggers a `FutureWarning`. Always encapsulate return values in `DeviceCommandResponse(status_code=..., response=...)`. +- **Match Parameter Signatures**: The parameter names and count of your Python callback **must match exactly** with the `input_schema` keys in `DeviceCommandDescriptor`. Mismatches emit warnings or fail at execution. +- **Fail Gracefully via `StatusCode`**: Do not let unhandled hardware exceptions crash the service. Catch exceptions and return appropriate status codes (`StatusCode.HARDWARE_FAULT`, `StatusCode.COMMAND_FAILED`, `StatusCode.OUT_OF_RANGE`, etc.). +- **Always Keep `enter_safe_mode` Safe**: The `enter_safe_mode` callback must never throw an unhandled exception; wrap internal calls in `try/except` and ensure it powers down active heating, radiation, lasers, or mechanical stages. + +--- + +### 5.2 Implementing Planner Services (`AresPlannerService`) + +A planner receives parameter histories and analysis feedback from past experiments and computes +the next set of parameters to test. + +```python +from PyAres import AresPlannerService, PlanRequest, PlanResponse, Outcome, ObjectiveStatus + +def my_planning_logic(request: PlanRequest) -> PlanResponse: + # 1. Access parameter definitions & histories + for param in request.parameters: + print(f"Param: {param.name}, Min: {param.minimum_value}, Max: {param.maximum_value}") + for item in param.param_history: + print(f" Planned: {item.planned_value}, Achieved: {item.achieved_value}") + + # 2. Access analysis objectives from previous runs + for entry in request.analysis_data: + for obj in entry.analysis_objectives: + print(f" Objective {obj.objective_name} = {obj.objective_value}") + + # 3. Calculate next suggestion + next_temp = 350.0 + next_speed = 12.5 + + # 4. Return PlanResponse + return PlanResponse( + parameter_data={"temperature": next_temp, "speed": next_speed}, + outcome=Outcome.SUCCESS, + objective_status=ObjectiveStatus.OBJECTIVE_UNACHIEVED, + ) + +service = AresPlannerService( + plan_logic=my_planning_logic, + service_name="BayesianOptimizer", + description="Gaussian Process planner for synthesis optimization", + version="0.1.0", + port=7082, +) +service.start() +``` + +#### Invariants & Rules for Planner Services: +- **Use `request.analysis_data` (Not `request.analysis_results`)**: `PlanRequest.analysis_results` is deprecated. Use `request.analysis_data` or the convenience helper `request.analysis_objectives` to access `Objective` instances. +- **Provide `parameter_data` Dict or Matched Lists**: Either pass `parameter_data={"key": value}` or pass two lists of equal length: `parameter_names=[...]` and `parameter_values=[...]`. +- **Stop Campaigns cleanly**: When optimization converges or target thresholds are reached, return `objective_status=ObjectiveStatus.OBJECTIVE_ACHIEVED`. + +--- + +### 5.3 Implementing Analyzer Services (`AresAnalyzerService`) + +An analyzer ingests raw sensor or device measurements and produces structured objective values. + +```python +from PyAres import AresAnalyzerService, AnalysisRequest, AnalysisResponse, Objective, Outcome, AresDataType + +def my_analysis_logic(request: AnalysisRequest) -> AnalysisResponse: + raw_resistance = request.inputs.get("ldr_resistance") + if raw_resistance is None: + return AnalysisResponse( + objectives=[], + outcome=Outcome.FAILURE, + error_string="Missing input 'ldr_resistance'" + ) + + # Compute metric + normalized_intensity = 1.0 / (raw_resistance + 1e-6) + + return AnalysisResponse( + objectives=[ + Objective( + objective_name="optical_transmission", + objective_value=normalized_intensity, + objective_metadata={"units": "a.u.", "confidence": 0.98} + ) + ], + outcome=Outcome.SUCCESS + ) + +service = AresAnalyzerService( + custom_analysis_logic=my_analysis_logic, + name="PhotodiodeAnalyzer", + version="1.0.0", + description="Computes optical transmission from resistance readings", + port=7083, +) + +service.add_analysis_parameter(name="ldr_resistance", data_type=AresDataType.FLOAT, optional=False) +service.add_objective_output(name="optical_transmission", data_type=AresDataType.FLOAT) +service.start() +``` + +#### Invariants & Rules for Analyzer Services: +- **Return explicit `Objective` instances**: Constructing `AnalysisResponse(result=...)` is deprecated. Always construct `AnalysisResponse(objectives=[Objective(...)], outcome=...)`. +- **Declare schemas via `add_analysis_parameter` and `add_objective_output`**: Declaring input and output schemas allows ARES OS to validate compatibility before starting campaigns. + +--- + +### 5.4 Standalone Testing with `test_tools` + +PyAres provides built-in standalone test clients in `PyAres.test_tools` so you can verify services +without running the entire ARES OS orchestrator: +- `AnalyzerTestClient(port=7083, host='localhost')`: Test `get_info()`, `check_status()`, and `run_analysis(inputs, settings)`. +- `PlannerTestClient(port=7082, host='localhost')`: Test `get_info()`, `check_status()`, and `run_planning(request)`. + +--- + +## 6. Best Practices: Working IN PyAres (Maintaining the Library) + +### 6.1 Protobuf & Datamodel Compatibility +- PyAres depends strictly on `ares-datamodel`. Never import raw gRPC files from custom paths; always import from `ares_datamodel...`. +- If a Protobuf schema in `ares-datamodel` changes, update the translation layers in `PyAres/Utils/` (`ares_value_utils.py`, `ares_struct_utils.py`, `ares_data_schema_utils.py`, etc.) and ensure both forward and backwards compatibility. + +### 6.2 Backwards Compatibility & Deprecation Policy +- Many existing laboratory devices run older PyAres conventions. +- When introducing a new model or schema structure, provide an adapter or backwards compatibility fallback (e.g., how `ExecuteCommand` still accepts legacy raw dict returns and emits a `FutureWarning`, or how `AnalysisResponse` handles legacy `result`). +- Use Python's `warnings.warn` with `FutureWarning` or `DeprecationWarning` rather than throwing errors on older API patterns. + +### 6.3 Network Binding & Host Configuration +- `AresGrpcServiceBase` defaults `use_localhost=True`, binding to `localhost:{port}`. +- Setting `use_localhost=False` binds to `[::]:{port}` (IPv6 wildcard, allowing LAN access from other host machines). +- Never hardcode IP addresses or assume external network access. + +### 6.4 Unit Testing Practices +- Place unit tests under `tests/`. +- Name test files `test_*.py`. +- Use `unittest.TestCase` or `pytest`. +- Use `MockGrpcContext` (from `tests/mock_grpc_context.py` or inline) to test gRPC servicer logic without needing live socket bindings. +- When spinning up a real service in a test, always ensure `service.stop()` is invoked in `tearDown()` to release the port. + +--- + +## 7. Licensing & Clearance + +- **License**: MIT License (see [`LICENSE`](LICENSE)). +- **Clearance**: AFRL Clearance: *Distribution A. Approved for public release: distribution unlimited. AFRL-2025-5332.* +- Keep all first-party code MIT licensed. Do not vendor or introduce copyleft/GPL code into PyAres. From cccfc0615f813fc8df1b74ae9c2d69372eb00ce0 Mon Sep 17 00:00:00 2001 From: Arthur Sloan Date: Thu, 29 Oct 2026 07:47:40 -0400 Subject: [PATCH 2/3] Working on spliting out directions for work on vs with ares into separate file to avoid unecessary context. --- AGENTS.md | 276 +++++++--------------------------- PyAres/Demo/BEST_PRACTICES.md | 225 +++++++++++++++++++++++++++ 2 files changed, 276 insertions(+), 225 deletions(-) create mode 100644 PyAres/Demo/BEST_PRACTICES.md diff --git a/AGENTS.md b/AGENTS.md index 5ecbacb..10a5c30 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,10 +7,11 @@ or **with** (consuming/implementing services for) the **PyAres** library. ## 1. What This Project Is -**PyAres** is the official Python SDK and gRPC service framework for the **ARES** -(Autonomous Research System) self-driving laboratory ecosystem. It bridges native Python +**PyAres** is the official Python SDK and gRPC service framework for the **ARES** +self-driving laboratory ecosystem. It bridges native Python code with the centralized [ARES OS 2](https://github.com/AFRL-ARES/ARES) orchestrator using Protocol Buffers and gRPC via [`ares-datamodel`](https://github.com/AFRL-ARES/ares-datamodel). +Official documentation for all aspects of the ARES ecosystem is located on the [Docs Page](https://afrl-ares.github.io/). PyAres abstracts away raw Protobuf messages and gRPC boilerplate, exposing clean, idiomatic Python interfaces to implement four core service archetypes: @@ -30,12 +31,10 @@ idiomatic Python interfaces to implement four core service archetypes: ## 2. Python Environment & Build System -- **Python Version**: `>= 3.10` -- **Build Backend**: [`hatchling`](https://hatch.pypa.io/) (configured in `pyproject.toml`) -- **Core Dependency**: `ares-datamodel >= 0.36.0` (provides Protobuf/gRPC message stubs) -- **Optional Dependencies**: `visualization = ["plotly >= 5.0"]` -- **Dynamic Versioning**: `hatchling` reads the package version dynamically from the - `PYARES_PACKAGE_VERSION` environment variable (required during `build` or `pip install -e .`): +- **Consult TOML for Dependencies and Build Info** The `pyproject.toml` file in the repository root file serves + as the single source of truth for dependencies and build information. +- **Dynamic Versioning**: The [`hatchling`](https://hatch.pypa.io/) build back ends reads the package version + dynamically from the `PYARES_PACKAGE_VERSION` environment variable (required during `build` or `pip install -e .`): ```bash export PYARES_PACKAGE_VERSION="0.0.1" # or matching release tag ``` @@ -63,47 +62,49 @@ pytest -v tests/ ``` PyAres/ -├── pyproject.toml # Hatchling build specification & dependencies -├── README.md # Upstream package summary & centralized docs link -├── LICENSE # MIT License -├── .github/workflows/ # CI/CD: unit tests (ubuntu, py3.10) & PyPI publishing -├── tests/ # Unit tests & MockGrpcContext -└── PyAres/ # Root package directory - ├── __init__.py # Public top-level API exports - ├── Device/ # Hardware control service framework - │ ├── device_service.py # AresDeviceService & AresDeviceServiceWrapper (gRPC servicer) - │ ├── device_models.py # DeviceCommandDescriptor, DeviceCommandResponse, StatusCode, etc. - │ └── device_warnings.py # Warning categories for device services - ├── Planning/ # Campaign & experiment planning framework - │ ├── planning_service.py # AresPlannerService & AresPlannerServiceWrapper (gRPC servicer) - │ └── planner_models.py # PlanRequest, PlanResponse, PlanningParameter, Plan, etc. - ├── Analyzing/ # Experiment evaluation & objective calculation framework - │ ├── analysis_service.py # AresAnalyzerService & AresAnalyzerServiceWrapper (gRPC servicer) - │ └── analyzer_models.py # AnalysisRequest, AnalysisResponse, Objective, ObjectiveSchema - ├── Visualizers/ # UI Visualization rendering framework - │ ├── visualizer_service.py # AresVisualizerService & AresVisualizerServiceWrapper - │ └── visualizing_models.py # VisualizationOption, VisualizationRequest, DataPoint - ├── Models/ # Shared data models & enums - │ └── ares_data_models.py # AresDataType, Outcome, PlanStatusCode, RequestMetadata, Limits - ├── Utils/ # Protobuf ↔ Python conversion utilities & base infrastructure - │ ├── grpc_base.py # AresGrpcServiceBase: server lifecycle, thread pool, message size - │ ├── ares_service_base.py# AresBaseService & AresServiceWrapperBase: common RPCs & async resolver - │ ├── metadata_interceptors.py # DatamodelVersionServerInterceptor: handshake version header - │ ├── ares_value_utils.py # Primitive/AresValue protobuf bi-directional conversion - │ ├── ares_struct_utils.py# Dict/AresStruct conversion helpers - │ ├── ares_data_schema_utils.py # Schema builder helpers for inputs, settings, and outputs - │ ├── ares_data_type_utils.py # AresDataType enum translation - │ ├── device_status_code_utils.py # Status code mapping & success evaluation - │ ├── ares_outcome_utils.py # Outcome enum translation - │ ├── plan_request_utils.py # Protobuf PlanRequest unpacking - │ └── plan_response_utils.py # Protobuf PlanResponse packaging - ├── test_tools/ # Standalone test clients - │ ├── analyzer_tester.py # AnalyzerTestClient: test Analyzer services without ARES OS - │ └── planner_tester.py # PlannerTestClient: test Planner services without ARES OS - └── Demo/ # Working reference implementations - ├── Devices/ # Example devices (hotplate, rotary mixer, random generator, etc.) - ├── Planners/ # Example planners (grid, random, test-tools integration) - └── Analyzers/ # Example analyzers (basic, Ax analyzer, test-tools integration) +├── pyproject.toml # Hatchling build specification & dependencies +├── README.md # Upstream package summary & centralized docs link +├── AGENTS.md # This File +├── LICENSE # MIT License +├── .github/workflows/ # CI/CD: unit tests (ubuntu, py3.10) & PyPI publishing +├── tests/ # Unit tests & MockGrpcContext +└── PyAres/ # Root package directory + ├── __init__.py # Public top-level API exports + ├── Device/ # Hardware control service framework + │ ├── device_service.py # AresDeviceService & AresDeviceServiceWrapper (gRPC servicer) + │ ├── device_models.py # DeviceCommandDescriptor, DeviceCommandResponse, StatusCode, etc. + │ └── device_warnings.py # Warning categories for device services + ├── Planning/ # Campaign & experiment planning framework + │ ├── planning_service.py # AresPlannerService & AresPlannerServiceWrapper (gRPC servicer) + │ └── planner_models.py # PlanRequest, PlanResponse, PlanningParameter, Plan, etc. + ├── Analyzing/ # Experiment evaluation & objective calculation framework + │ ├── analysis_service.py # AresAnalyzerService & AresAnalyzerServiceWrapper (gRPC servicer) + │ └── analyzer_models.py # AnalysisRequest, AnalysisResponse, Objective, ObjectiveSchema + ├── Visualizers/ # UI Visualization rendering framework + │ ├── visualizer_service.py # AresVisualizerService & AresVisualizerServiceWrapper + │ └── visualizing_models.py # VisualizationOption, VisualizationRequest, DataPoint + ├── Models/ # Shared data models & enums + │ └── ares_data_models.py # AresDataType, Outcome, PlanStatusCode, RequestMetadata, Limits + ├── Utils/ # Protobuf ↔ Python conversion utilities & base infrastructure + │ ├── grpc_base.py # AresGrpcServiceBase: server lifecycle, thread pool, message size + │ ├── ares_service_base.py # AresBaseService & AresServiceWrapperBase: common RPCs & async resolver + │ ├── metadata_interceptors.py # DatamodelVersionServerInterceptor: handshake version header + │ ├── ares_value_utils.py # Primitive/AresValue protobuf bi-directional conversion + │ ├── ares_struct_utils.py # Dict/AresStruct conversion helpers + │ ├── ares_data_schema_utils.py # Schema builder helpers for inputs, settings, and outputs + │ ├── ares_data_type_utils.py # AresDataType enum translation + │ ├── device_status_code_utils.py # Status code mapping & success evaluation + │ ├── ares_outcome_utils.py # Outcome enum translation + │ ├── plan_request_utils.py # Protobuf PlanRequest unpacking + │ └── plan_response_utils.py # Protobuf PlanResponse packaging + ├── test_tools/ # Standalone test clients + │ ├── analyzer_tester.py # AnalyzerTestClient: test Analyzer services without ARES OS + │ └── planner_tester.py # PlannerTestClient: test Planner services without ARES OS + └── Demo/ # Working reference implementations + ├── BEST_PRACTICES.md # Best Practcies for PyAres Service Implementations + ├── Devices/ # Example devices (hotplate, rotary mixer, random generator, etc.) + ├── Planners/ # Example planners (grid, random, test-tools integration) + └── Analyzers/ # Example analyzers (basic, Ax analyzer, test-tools integration) ``` --- @@ -139,182 +140,7 @@ resolves awaitables via `_resolve_awaitable()` (`PyAres/Utils/ares_service_base. --- ## 5. Best Practices: Working WITH PyAres (Developing Services) - -### 5.1 Implementing Device Services (`AresDeviceService`) - -A device service wraps real or simulated hardware to expose actuators and sensors to ARES OS. - -```python -from PyAres import AresDeviceService, DeviceCommandDescriptor, DeviceSchemaEntry, AresDataType, StatusCode -from PyAres.Device.device_models import DeviceCommandResponse - -# 1. Mandatory Safe Mode Callback (must be robust, non-blocking or fast, and idempotent) -def enter_safe_mode(): - heater.set_power(0.0) - motors.stop() - -# 2. Mandatory State Polling Callback (returns telemetry dictionary) -def get_device_state() -> dict: - return { - "temperature": sensor.read_temp(), - "is_moving": motors.is_active(), - } - -service = AresDeviceService( - enter_safe_mode_logic=enter_safe_mode, - get_device_state_logic=get_device_state, - device_name="Tubular Furnace", - description="High temperature furnace driver", - version="1.0.0", - port=7100, -) - -# 3. Register Commands -set_temp_descriptor = DeviceCommandDescriptor( - name="set_temperature", - description="Sets the target furnace temperature in Celsius.", - input_schema={ - "target_celsius": DeviceSchemaEntry( - type=AresDataType.FLOAT, - description="Target temperature in deg C", - min_number_value=20.0, - max_number_value=1200.0, - ) - }, - output_schema={ - "achieved_target": DeviceSchemaEntry(type=AresDataType.FLOAT) - } -) - -def set_temperature(target_celsius: float) -> DeviceCommandResponse: - try: - hardware.set_temp(target_celsius) - return DeviceCommandResponse( - status_code=StatusCode.COMMAND_SUCCESS, - response={"achieved_target": target_celsius} - ) - except Exception as e: - return DeviceCommandResponse( - status_code=StatusCode.HARDWARE_FAULT, - error_string=str(e), - response={} - ) - -service.add_new_command(set_temp_descriptor, set_temperature) -service.start() -``` - -#### Invariants & Rules for Device Services: -- **Always return `DeviceCommandResponse`**: Returning bare values or raw dictionaries is deprecated and triggers a `FutureWarning`. Always encapsulate return values in `DeviceCommandResponse(status_code=..., response=...)`. -- **Match Parameter Signatures**: The parameter names and count of your Python callback **must match exactly** with the `input_schema` keys in `DeviceCommandDescriptor`. Mismatches emit warnings or fail at execution. -- **Fail Gracefully via `StatusCode`**: Do not let unhandled hardware exceptions crash the service. Catch exceptions and return appropriate status codes (`StatusCode.HARDWARE_FAULT`, `StatusCode.COMMAND_FAILED`, `StatusCode.OUT_OF_RANGE`, etc.). -- **Always Keep `enter_safe_mode` Safe**: The `enter_safe_mode` callback must never throw an unhandled exception; wrap internal calls in `try/except` and ensure it powers down active heating, radiation, lasers, or mechanical stages. - ---- - -### 5.2 Implementing Planner Services (`AresPlannerService`) - -A planner receives parameter histories and analysis feedback from past experiments and computes -the next set of parameters to test. - -```python -from PyAres import AresPlannerService, PlanRequest, PlanResponse, Outcome, ObjectiveStatus - -def my_planning_logic(request: PlanRequest) -> PlanResponse: - # 1. Access parameter definitions & histories - for param in request.parameters: - print(f"Param: {param.name}, Min: {param.minimum_value}, Max: {param.maximum_value}") - for item in param.param_history: - print(f" Planned: {item.planned_value}, Achieved: {item.achieved_value}") - - # 2. Access analysis objectives from previous runs - for entry in request.analysis_data: - for obj in entry.analysis_objectives: - print(f" Objective {obj.objective_name} = {obj.objective_value}") - - # 3. Calculate next suggestion - next_temp = 350.0 - next_speed = 12.5 - - # 4. Return PlanResponse - return PlanResponse( - parameter_data={"temperature": next_temp, "speed": next_speed}, - outcome=Outcome.SUCCESS, - objective_status=ObjectiveStatus.OBJECTIVE_UNACHIEVED, - ) - -service = AresPlannerService( - plan_logic=my_planning_logic, - service_name="BayesianOptimizer", - description="Gaussian Process planner for synthesis optimization", - version="0.1.0", - port=7082, -) -service.start() -``` - -#### Invariants & Rules for Planner Services: -- **Use `request.analysis_data` (Not `request.analysis_results`)**: `PlanRequest.analysis_results` is deprecated. Use `request.analysis_data` or the convenience helper `request.analysis_objectives` to access `Objective` instances. -- **Provide `parameter_data` Dict or Matched Lists**: Either pass `parameter_data={"key": value}` or pass two lists of equal length: `parameter_names=[...]` and `parameter_values=[...]`. -- **Stop Campaigns cleanly**: When optimization converges or target thresholds are reached, return `objective_status=ObjectiveStatus.OBJECTIVE_ACHIEVED`. - ---- - -### 5.3 Implementing Analyzer Services (`AresAnalyzerService`) - -An analyzer ingests raw sensor or device measurements and produces structured objective values. - -```python -from PyAres import AresAnalyzerService, AnalysisRequest, AnalysisResponse, Objective, Outcome, AresDataType - -def my_analysis_logic(request: AnalysisRequest) -> AnalysisResponse: - raw_resistance = request.inputs.get("ldr_resistance") - if raw_resistance is None: - return AnalysisResponse( - objectives=[], - outcome=Outcome.FAILURE, - error_string="Missing input 'ldr_resistance'" - ) - - # Compute metric - normalized_intensity = 1.0 / (raw_resistance + 1e-6) - - return AnalysisResponse( - objectives=[ - Objective( - objective_name="optical_transmission", - objective_value=normalized_intensity, - objective_metadata={"units": "a.u.", "confidence": 0.98} - ) - ], - outcome=Outcome.SUCCESS - ) - -service = AresAnalyzerService( - custom_analysis_logic=my_analysis_logic, - name="PhotodiodeAnalyzer", - version="1.0.0", - description="Computes optical transmission from resistance readings", - port=7083, -) - -service.add_analysis_parameter(name="ldr_resistance", data_type=AresDataType.FLOAT, optional=False) -service.add_objective_output(name="optical_transmission", data_type=AresDataType.FLOAT) -service.start() -``` - -#### Invariants & Rules for Analyzer Services: -- **Return explicit `Objective` instances**: Constructing `AnalysisResponse(result=...)` is deprecated. Always construct `AnalysisResponse(objectives=[Objective(...)], outcome=...)`. -- **Declare schemas via `add_analysis_parameter` and `add_objective_output`**: Declaring input and output schemas allows ARES OS to validate compatibility before starting campaigns. - ---- - -### 5.4 Standalone Testing with `test_tools` - -PyAres provides built-in standalone test clients in `PyAres.test_tools` so you can verify services -without running the entire ARES OS orchestrator: -- `AnalyzerTestClient(port=7083, host='localhost')`: Test `get_info()`, `check_status()`, and `run_analysis(inputs, settings)`. -- `PlannerTestClient(port=7082, host='localhost')`: Test `get_info()`, `check_status()`, and `run_planning(request)`. +- Examples and guidelines for implementation best bractices are located in `PyAres/Demos/`. --- diff --git a/PyAres/Demo/BEST_PRACTICES.md b/PyAres/Demo/BEST_PRACTICES.md new file mode 100644 index 0000000..3c2cfac --- /dev/null +++ b/PyAres/Demo/BEST_PRACTICES.md @@ -0,0 +1,225 @@ +# Best Practices: Working WITH PyAres (Developing Services) + +Context for AI agents and human contributors working **with** +(consuming/implementing services for) the **PyAres** library. + +## 0. Directory Structure + +The `PyAres/Demo/` directory contains reference implementations and test clients for PyAres services: + +``` +PyAres/Demo/ +├── BEST_PRACTICES.md # This File. Service implementation guidelines and best practices +├── Analyzers/ # Example analyzer services and test scripts +│ ├── analyzer_test.py # Basic analyzer implementation example +│ ├── analyzer_test_tools_demo.py # Standalone analyzer testing with AnalyzerTestClient +│ ├── analyzer_wiki.py # Reference analyzer returning structured Objective metrics +│ └── ax_analyzer_test.py # Ground-truth evaluation service for Ax optimization +├── Devices/ # Example device services and hardware wrappers +│ ├── device_test.py # Simulated device with temperature control and state +│ ├── failure_test_device.py # Device demonstrating intentional failures & error handling +│ ├── hotplate.py # Virtual hotplate with safe mode, polling, & command schemas +│ ├── random_number_device.py # Virtual sensor generating random numbers with schemas +│ └── rotary_mixer.py # Virtual rotary mixer with RPM speed control +└── Planners/ # Example planning services and test scripts + ├── planner_test.py # Planner example processing parameter histories & objectives + ├── planner_test_tools_demo.py # Standalone planner testing with PlannerTestClient + └── planner_wiki.py # Batch random parameter generation planner +``` + +## 1. General Guidance + +- **Soft Preference for Stateless Services** - When possible, services should be designed to be stateless, treating each request independently, + and retaining no data between calls. Sometimes, a purely stateless desgin is not possible or may result in large time delays due to computational overhead. For example a service with a persistent connection to hardware or software, a streamed data source, a large database that must be loaded into ram etc. In these cases the execution logic should be contained within a class that initializes and manages the persistent resources either on class instantiation or on the receipt of the first request from ARES +- **Graceful Closeout and Exception Handling** - Services should always take care to exit gracefully in the case of an exception or manual + closeout of the service by using try-except-finally control structure. If an exception occured while procesing a request from ARES, this exception should be conveyed back to ARES as part of error handling. When the service involves a connection to an extneral hardware interface, network, or software instance logic should be included to safely close down those connections. Ensure that all child threads or sub-processes spawned by the service are terminated upon closeout. +- **Object-Oriented Design for more complex services** - Any service logic that excedes more that 4 or 5 subsidiary functions or more than a few dozen + lines of code should be implmented as a class. +- **Service Portability** - When implemnting a service use a project structure that is suitable for redistribution and installation with package managers + such as `pip`. Every service should have documented requirements in a `requirements.txt` or `pyproject.toml` file. + + +## 2 Implementing Device Services (`AresDeviceService`) + +A device service wraps real or simulated hardware to expose actuators and sensors to ARES OS. +They are also the most flexible class of servce and can be used execute arbitrary logic that is needed +within an ARES Experimental. For example: + +- **Calculations Outside of Analyzers** - When derived values are needed with the experimental loop (e.g., setting a sample acquistion time based on a the system flow + rate). +- **Execution of External Scripts** - When an experimental campaign needs to trigger external actions on the host PC (e.g., compiling software, cleaning up temporary files). +- **Interactions With External Software** - Call external software APIs (e.g., Initialize or post-process computer simulations). + +```python +from PyAres import AresDeviceService, DeviceCommandDescriptor, DeviceSchemaEntry, AresDataType, StatusCode +from PyAres.Device.device_models import DeviceCommandResponse + +# 1. Mandatory Safe Mode Callback (must be robust, non-blocking or fast, and idempotent) +def enter_safe_mode(): + heater.set_power(0.0) + motors.stop() + +# 2. Mandatory State Polling Callback (returns telemetry dictionary) +def get_device_state() -> dict: + return { + "temperature": sensor.read_temp(), + "is_moving": motors.is_active(), + } + +service = AresDeviceService( + enter_safe_mode_logic=enter_safe_mode, + get_device_state_logic=get_device_state, + device_name="Tubular Furnace", + description="High temperature furnace driver", + version="1.0.0", + port=7100, +) + +# 3. Register Commands +set_temp_descriptor = DeviceCommandDescriptor( + name="set_temperature", + description="Sets the target furnace temperature in Celsius.", + input_schema={ + "target_celsius": DeviceSchemaEntry( + type=AresDataType.FLOAT, + description="Target temperature in deg C", + min_number_value=20.0, + max_number_value=1200.0, + ) + }, + output_schema={ + "achieved_target": DeviceSchemaEntry(type=AresDataType.FLOAT) + } +) + +def set_temperature(target_celsius: float) -> DeviceCommandResponse: + try: + hardware.set_temp(target_celsius) + return DeviceCommandResponse( + status_code=StatusCode.COMMAND_SUCCESS, + response={"achieved_target": target_celsius} + ) + except Exception as e: + return DeviceCommandResponse( + status_code=StatusCode.HARDWARE_FAULT, + error_string=str(e), + response={} + ) + +service.add_new_command(set_temp_descriptor, set_temperature) +service.start() +``` + +### Invariants & Rules for Device Services: +- **Always return `DeviceCommandResponse`**: Returning bare values or raw dictionaries is deprecated and triggers a `FutureWarning`. Always encapsulate return values in `DeviceCommandResponse(status_code=..., response=...)`. +- **Match Parameter Signatures**: The parameter names and count of your Python callback **must match exactly** with the `input_schema` keys in `DeviceCommandDescriptor`. Mismatches emit warnings or fail at execution. +- **Fail Gracefully via `StatusCode`**: Do not let unhandled hardware exceptions crash the service. Catch exceptions and return appropriate status codes (`StatusCode.HARDWARE_FAULT`, `StatusCode.COMMAND_FAILED`, `StatusCode.OUT_OF_RANGE`, etc.). +- **Always Keep `enter_safe_mode` Safe**: The `enter_safe_mode` callback must never throw an unhandled exception; wrap internal calls in `try/except` and ensure it powers down active heating, radiation, lasers, or mechanical stages. + +--- + +## 2 Implementing Planner Services (`AresPlannerService`) + +A planner receives parameter histories and analysis feedback from past experiments and computes +the next set of parameters to test. + +```python +from PyAres import AresPlannerService, PlanRequest, PlanResponse, Outcome, ObjectiveStatus + +def my_planning_logic(request: PlanRequest) -> PlanResponse: + # 1. Access parameter definitions & histories + for param in request.parameters: + print(f"Param: {param.name}, Min: {param.minimum_value}, Max: {param.maximum_value}") + for item in param.param_history: + print(f" Planned: {item.planned_value}, Achieved: {item.achieved_value}") + + # 2. Access analysis objectives from previous runs + for entry in request.analysis_data: + for obj in entry.analysis_objectives: + print(f" Objective {obj.objective_name} = {obj.objective_value}") + + # 3. Calculate next suggestion + next_temp = 350.0 + next_speed = 12.5 + + # 4. Return PlanResponse + return PlanResponse( + parameter_data={"temperature": next_temp, "speed": next_speed}, + outcome=Outcome.SUCCESS, + objective_status=ObjectiveStatus.OBJECTIVE_UNACHIEVED, + ) + +service = AresPlannerService( + plan_logic=my_planning_logic, + service_name="BayesianOptimizer", + description="Gaussian Process planner for synthesis optimization", + version="0.1.0", + port=7082, +) +service.start() +``` + +### Invariants & Rules for Planner Services: +- **Use `request.analysis_data` (Not `request.analysis_results`)**: `PlanRequest.analysis_results` is deprecated. Use `request.analysis_data` or the convenience helper `request.analysis_objectives` to access `Objective` instances. +- **Provide `parameter_data` Dict or Matched Lists**: Either pass `parameter_data={"key": value}` or pass two lists of equal length: `parameter_names=[...]` and `parameter_values=[...]`. +- **Stop Campaigns cleanly**: When optimization converges or target thresholds are reached, return `objective_status=ObjectiveStatus.OBJECTIVE_ACHIEVED`. + +--- + +## 3 Implementing Analyzer Services (`AresAnalyzerService`) + +An analyzer ingests raw sensor or device measurements and produces structured objective values. + +```python +from PyAres import AresAnalyzerService, AnalysisRequest, AnalysisResponse, Objective, Outcome, AresDataType + +def my_analysis_logic(request: AnalysisRequest) -> AnalysisResponse: + raw_resistance = request.inputs.get("ldr_resistance") + if raw_resistance is None: + return AnalysisResponse( + objectives=[], + outcome=Outcome.FAILURE, + error_string="Missing input 'ldr_resistance'" + ) + + # Compute metric + normalized_intensity = 1.0 / (raw_resistance + 1e-6) + + return AnalysisResponse( + objectives=[ + Objective( + objective_name="optical_transmission", + objective_value=normalized_intensity, + objective_metadata={"units": "a.u.", "confidence": 0.98} + ) + ], + outcome=Outcome.SUCCESS + ) + +service = AresAnalyzerService( + custom_analysis_logic=my_analysis_logic, + name="PhotodiodeAnalyzer", + version="1.0.0", + description="Computes optical transmission from resistance readings", + port=7083, +) + +service.add_analysis_parameter(name="ldr_resistance", data_type=AresDataType.FLOAT, optional=False) +service.add_objective_output(name="optical_transmission", data_type=AresDataType.FLOAT) +service.start() +``` + +### Invariants & Rules for Analyzer Services: +- **Return explicit `Objective` instances**: Constructing `AnalysisResponse(result=...)` is deprecated. Always construct `AnalysisResponse(objectives=[Objective(...)], outcome=...)`. +- **Declare schemas via `add_analysis_parameter` and `add_objective_output`**: Declaring input and output schemas allows ARES OS to validate compatibility before starting campaigns. + +--- + +## 4 Standalone Testing with `test_tools` + +PyAres provides built-in standalone test clients in `PyAres.test_tools` so you can verify services +without running the entire ARES OS orchestrator: +- `AnalyzerTestClient(port=7083, host='localhost')`: Test `get_info()`, `check_status()`, and `run_analysis(inputs, settings)`. +- `PlannerTestClient(port=7082, host='localhost')`: Test `get_info()`, `check_status()`, and `run_planning(request)`. + +--- From 5e8fb27608b5bf62e4ea1a45c548602d2f98cf8d Mon Sep 17 00:00:00 2001 From: Arthur Sloan Date: Fri, 30 Oct 2026 14:14:06 -0400 Subject: [PATCH 3/3] finished best practices md --- PyAres/Demo/BEST_PRACTICES.md | 174 +++++----------------------------- 1 file changed, 23 insertions(+), 151 deletions(-) diff --git a/PyAres/Demo/BEST_PRACTICES.md b/PyAres/Demo/BEST_PRACTICES.md index 3c2cfac..d261cc4 100644 --- a/PyAres/Demo/BEST_PRACTICES.md +++ b/PyAres/Demo/BEST_PRACTICES.md @@ -7,7 +7,7 @@ Context for AI agents and human contributors working **with** The `PyAres/Demo/` directory contains reference implementations and test clients for PyAres services: -``` +```text PyAres/Demo/ ├── BEST_PRACTICES.md # This File. Service implementation guidelines and best practices ├── Analyzers/ # Example analyzer services and test scripts @@ -31,90 +31,29 @@ PyAres/Demo/ - **Soft Preference for Stateless Services** - When possible, services should be designed to be stateless, treating each request independently, and retaining no data between calls. Sometimes, a purely stateless desgin is not possible or may result in large time delays due to computational overhead. For example a service with a persistent connection to hardware or software, a streamed data source, a large database that must be loaded into ram etc. In these cases the execution logic should be contained within a class that initializes and manages the persistent resources either on class instantiation or on the receipt of the first request from ARES -- **Graceful Closeout and Exception Handling** - Services should always take care to exit gracefully in the case of an exception or manual - closeout of the service by using try-except-finally control structure. If an exception occured while procesing a request from ARES, this exception should be conveyed back to ARES as part of error handling. When the service involves a connection to an extneral hardware interface, network, or software instance logic should be included to safely close down those connections. Ensure that all child threads or sub-processes spawned by the service are terminated upon closeout. -- **Object-Oriented Design for more complex services** - Any service logic that excedes more that 4 or 5 subsidiary functions or more than a few dozen - lines of code should be implmented as a class. -- **Service Portability** - When implemnting a service use a project structure that is suitable for redistribution and installation with package managers - such as `pip`. Every service should have documented requirements in a `requirements.txt` or `pyproject.toml` file. +- **Graceful Closeout and Exception Handling** - Services should always take care to exit gracefully in the case of an exception or manual (ctrl+c)closeout of the service by using try-except-finally control structure. If an exception occured while procesing a request from ARES, this exception should be conveyed back to ARES as part of error handling. When the service involves a connection to an extneral hardware interface, network, or software instance logic should be included to safely close down those connections. Ensure that all child threads or sub-processes spawned by the service are terminated upon closeout. +- **Object-Oriented Design for More Complex Services** - Any service logic that needs more that 4 or 5 functions or more + than a few dozen lines of code should be implmented as a class. +- **Service Portability** - When implemnting a service use a project structure that is suitable for redistribution and installation with package managers such as `pip`. Every service should have documented requirements in a `requirements.txt` or `pyproject.toml` file. If a servce requires proprietary elements that cannot be freely distributed (e.g., an API key, a manufacturer SDK), the service should be structured so that it can interact with user-provided proprietary elements without incorporating them into the redistributable core of the service. - ## 2 Implementing Device Services (`AresDeviceService`) -A device service wraps real or simulated hardware to expose actuators and sensors to ARES OS. -They are also the most flexible class of servce and can be used execute arbitrary logic that is needed -within an ARES Experimental. For example: +A device service wraps real or simulated hardware to expose actuators and sensors to ARES OS. They are also the most flexible class of servce and can be used execute arbitrary logic that is needed within an ARES Experimental. For example: -- **Calculations Outside of Analyzers** - When derived values are needed with the experimental loop (e.g., setting a sample acquistion time based on a the system flow - rate). +- **Calculations Outside of Analyzers** - When derived values are needed with the experimental loop (e.g., setting a sample acquistion time based on a the system flowrate). - **Execution of External Scripts** - When an experimental campaign needs to trigger external actions on the host PC (e.g., compiling software, cleaning up temporary files). - **Interactions With External Software** - Call external software APIs (e.g., Initialize or post-process computer simulations). -```python -from PyAres import AresDeviceService, DeviceCommandDescriptor, DeviceSchemaEntry, AresDataType, StatusCode -from PyAres.Device.device_models import DeviceCommandResponse - -# 1. Mandatory Safe Mode Callback (must be robust, non-blocking or fast, and idempotent) -def enter_safe_mode(): - heater.set_power(0.0) - motors.stop() - -# 2. Mandatory State Polling Callback (returns telemetry dictionary) -def get_device_state() -> dict: - return { - "temperature": sensor.read_temp(), - "is_moving": motors.is_active(), - } - -service = AresDeviceService( - enter_safe_mode_logic=enter_safe_mode, - get_device_state_logic=get_device_state, - device_name="Tubular Furnace", - description="High temperature furnace driver", - version="1.0.0", - port=7100, -) - -# 3. Register Commands -set_temp_descriptor = DeviceCommandDescriptor( - name="set_temperature", - description="Sets the target furnace temperature in Celsius.", - input_schema={ - "target_celsius": DeviceSchemaEntry( - type=AresDataType.FLOAT, - description="Target temperature in deg C", - min_number_value=20.0, - max_number_value=1200.0, - ) - }, - output_schema={ - "achieved_target": DeviceSchemaEntry(type=AresDataType.FLOAT) - } -) - -def set_temperature(target_celsius: float) -> DeviceCommandResponse: - try: - hardware.set_temp(target_celsius) - return DeviceCommandResponse( - status_code=StatusCode.COMMAND_SUCCESS, - response={"achieved_target": target_celsius} - ) - except Exception as e: - return DeviceCommandResponse( - status_code=StatusCode.HARDWARE_FAULT, - error_string=str(e), - response={} - ) - -service.add_new_command(set_temp_descriptor, set_temperature) -service.start() -``` +Example device implementations can be found in `PyAres/Demo/Devices` -### Invariants & Rules for Device Services: +### Invariants & Rules for Device Services + +- **Wrap Generic Device Drivers** - When a python compatible device driver is not provided, it is preferable to implement a separate python interface class that handles device communication and top-level commands and then wrap the device in a PyAres compatibility layer used by the device service. This ensures the device is useable with or without PyAres. +- **Mandatory Safe Mode and Get State Callbacks** - Safe mode and get state callbacks are mandatory on every PyAres device service. Safe mode callbacks must be robust, non-blocking or fast, and idempotent. - **Always return `DeviceCommandResponse`**: Returning bare values or raw dictionaries is deprecated and triggers a `FutureWarning`. Always encapsulate return values in `DeviceCommandResponse(status_code=..., response=...)`. - **Match Parameter Signatures**: The parameter names and count of your Python callback **must match exactly** with the `input_schema` keys in `DeviceCommandDescriptor`. Mismatches emit warnings or fail at execution. - **Fail Gracefully via `StatusCode`**: Do not let unhandled hardware exceptions crash the service. Catch exceptions and return appropriate status codes (`StatusCode.HARDWARE_FAULT`, `StatusCode.COMMAND_FAILED`, `StatusCode.OUT_OF_RANGE`, etc.). -- **Always Keep `enter_safe_mode` Safe**: The `enter_safe_mode` callback must never throw an unhandled exception; wrap internal calls in `try/except` and ensure it powers down active heating, radiation, lasers, or mechanical stages. +- **Always Keep `enter_safe_mode` Safe**: The `enter_safe_mode` callback must never throw an unhandled exception; wrap internal calls in `try/except` and ensure it sets physical hardware to a safe state. --- @@ -123,43 +62,11 @@ service.start() A planner receives parameter histories and analysis feedback from past experiments and computes the next set of parameters to test. -```python -from PyAres import AresPlannerService, PlanRequest, PlanResponse, Outcome, ObjectiveStatus - -def my_planning_logic(request: PlanRequest) -> PlanResponse: - # 1. Access parameter definitions & histories - for param in request.parameters: - print(f"Param: {param.name}, Min: {param.minimum_value}, Max: {param.maximum_value}") - for item in param.param_history: - print(f" Planned: {item.planned_value}, Achieved: {item.achieved_value}") - - # 2. Access analysis objectives from previous runs - for entry in request.analysis_data: - for obj in entry.analysis_objectives: - print(f" Objective {obj.objective_name} = {obj.objective_value}") - - # 3. Calculate next suggestion - next_temp = 350.0 - next_speed = 12.5 - - # 4. Return PlanResponse - return PlanResponse( - parameter_data={"temperature": next_temp, "speed": next_speed}, - outcome=Outcome.SUCCESS, - objective_status=ObjectiveStatus.OBJECTIVE_UNACHIEVED, - ) - -service = AresPlannerService( - plan_logic=my_planning_logic, - service_name="BayesianOptimizer", - description="Gaussian Process planner for synthesis optimization", - version="0.1.0", - port=7082, -) -service.start() -``` +Example planner implementations can be found in `PyAres/Demo/Planners` -### Invariants & Rules for Planner Services: +### Invariants & Rules for Planner Services + +- **Batch Planning Support**: Planners should check for the number of requested experiments in `PlanRequest.batch_size` to determine how many sets of parameter values to generate and return in the `Plan` object. - **Use `request.analysis_data` (Not `request.analysis_results`)**: `PlanRequest.analysis_results` is deprecated. Use `request.analysis_data` or the convenience helper `request.analysis_objectives` to access `Objective` instances. - **Provide `parameter_data` Dict or Matched Lists**: Either pass `parameter_data={"key": value}` or pass two lists of equal length: `parameter_names=[...]` and `parameter_values=[...]`. - **Stop Campaigns cleanly**: When optimization converges or target thresholds are reached, return `objective_status=ObjectiveStatus.OBJECTIVE_ACHIEVED`. @@ -168,48 +75,12 @@ service.start() ## 3 Implementing Analyzer Services (`AresAnalyzerService`) -An analyzer ingests raw sensor or device measurements and produces structured objective values. - -```python -from PyAres import AresAnalyzerService, AnalysisRequest, AnalysisResponse, Objective, Outcome, AresDataType - -def my_analysis_logic(request: AnalysisRequest) -> AnalysisResponse: - raw_resistance = request.inputs.get("ldr_resistance") - if raw_resistance is None: - return AnalysisResponse( - objectives=[], - outcome=Outcome.FAILURE, - error_string="Missing input 'ldr_resistance'" - ) - - # Compute metric - normalized_intensity = 1.0 / (raw_resistance + 1e-6) - - return AnalysisResponse( - objectives=[ - Objective( - objective_name="optical_transmission", - objective_value=normalized_intensity, - objective_metadata={"units": "a.u.", "confidence": 0.98} - ) - ], - outcome=Outcome.SUCCESS - ) - -service = AresAnalyzerService( - custom_analysis_logic=my_analysis_logic, - name="PhotodiodeAnalyzer", - version="1.0.0", - description="Computes optical transmission from resistance readings", - port=7083, -) - -service.add_analysis_parameter(name="ldr_resistance", data_type=AresDataType.FLOAT, optional=False) -service.add_objective_output(name="optical_transmission", data_type=AresDataType.FLOAT) -service.start() -``` +An analyzer ingests data from an experimental source and produces structured objective values that are used as feedback for experimental planning. + +Example planner implementations can be found in `PyAres/Demo/Analyzers` + +### Invariants & Rules for Analyzer Services -### Invariants & Rules for Analyzer Services: - **Return explicit `Objective` instances**: Constructing `AnalysisResponse(result=...)` is deprecated. Always construct `AnalysisResponse(objectives=[Objective(...)], outcome=...)`. - **Declare schemas via `add_analysis_parameter` and `add_objective_output`**: Declaring input and output schemas allows ARES OS to validate compatibility before starting campaigns. @@ -219,6 +90,7 @@ service.start() PyAres provides built-in standalone test clients in `PyAres.test_tools` so you can verify services without running the entire ARES OS orchestrator: + - `AnalyzerTestClient(port=7083, host='localhost')`: Test `get_info()`, `check_status()`, and `run_analysis(inputs, settings)`. - `PlannerTestClient(port=7082, host='localhost')`: Test `get_info()`, `check_status()`, and `run_planning(request)`.