Metadata-Version: 2.5
Name: venusrpc
Version: 0.1.0
Summary: Fast HTTP-based bridge from Python to the Hamilton Venus HSL runtime.
Project-URL: Repository, https://github.com/BIIE-DeepIR/venusrpc
Project-URL: Issues, https://github.com/BIIE-DeepIR/venusrpc/issues
Author: Valerijs Korotkovs
License-Expression: LGPL-3.0-or-later
License-File: COPYING
License-File: LICENSE
Keywords: automation,hamilton,hsl,liquid handling,venus
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: Microsoft :: Windows
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Requires-Python: >=3.10
Requires-Dist: jinja2>=3.1
Requires-Dist: openpyxl>=3.1
Requires-Dist: pandas>=2.0
Requires-Dist: pydantic>=2.0
Requires-Dist: pyodbc>=5.3.0
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Description-Content-Type: text/markdown

# venusrpc

Fast HTTP-based Python bridge to the Hamilton Venus HSL runtime.

`venusrpc` lets you drive a Hamilton STAR liquid handler from Python by calling
HSL submethods over a loopback HTTP connection to the Venus run-time
environment (`HxRun.exe`). The HSL host script that runs inside Venus is
shipped with the package.

## Credit

`venusrpc` is a separate implementation of the idea pioneered by
[PyVenus](https://github.com/sniprbiome/PyVenus) by SNIPR Biome — driving
Venus from Python via an HSL host script. The codegen approach (parsing
`.lay` / `.smt` / `.hsi` to emit typed Python wrappers) and the HSL helpers
shipped here are directly inspired by their work. `venusrpc` swaps PyVenus's
file-polling IPC for a loopback HTTP socket so per-call latency drops from
~150–300 ms to ~1–5 ms.

## Status

Alpha. APIs may change between minor versions. Requires Windows with
Hamilton Venus installed, for driving an instrument and for codegen (Venus's
converter reads the binary `.lay`/`.smt` files).
Tested with Venus 4.5 and Venus 6.

HSL libraries venusrpc needs from Venus's `Library`
folder (`C:\Program Files (x86)\HAMILTON\Library`):

| Library | Include | Needed for |
|--|--|--|
| HSLHttp | `HSLHttp\HSLHttp.hsl` | always: the HTTP transport to Python |
| HSLStrLib | `HSLStrLib.hsl` | always: string handling in the host script |
| HSLLabwrAccess | `HSLLabwrAccess\HSLLabwrAccess.hsl` | only `Labware` pose nudges; include it yourself via `con.execute(definitions=...)` |

If one is missing, `Connection` fails at startup with Venus's
`cannot open include file` error naming it.

## Install

```bash
pip install venusrpc
```

## Quickstart

```bash
venusrpc init my_method && cd my_method
# copy your deck to lay/deck.lay (+ .res) and submethod libraries to smt/
venusrpc generate
```

`venusrpc generate` turns the deck layout and submethod libraries into typed
Python wrappers under `venus_resources/`. A method then reads like this:

```python
from venusrpc import Connection, Device, Sequence
from venus_resources.__layout__deck import Deck
from venus_resources.__smt__channels1ml_8 import Channels1ml_8
from venus_resources.__smt__ml_star import Ml_star

with Connection(layout_file=Deck.file, simulation=True) as con:
    star = Device.from_existing(con)
    channels = Channels1ml_8(con)
    tips = Sequence(con, Deck.Sequences.Tips_50ul, deck_sequence=True)
    source = Sequence(con, Deck.Sequences.Source_tubes, deck_sequence=True)
    target = Sequence(con, Deck.Sequences.Target_plate, deck_sequence=True)

    Ml_star(con).initialize(star)
    channels.tip_pickup(ML_STAR=star, positions=tips)
    channels.aspirate(
        ML_STAR=star, positions=source, volume=10,
        liquid_class="Tip_50ulFilter_Water_DispenseSurface_Empty",
    )
    channels.dispense(ML_STAR=star, positions=target, volume=10)
    channels.tip_eject(ML_STAR=star, positions=tips, default_waste=1)
```

## How it works

```
Python (HTTP server)                   Venus (HTTP client)
─────────────────                      ─────────────────
HTTPServer on 127.0.0.1:0  ◄── GET ─── HxRun.exe -t venusrpc_host.hsl
write venusrpc.port
─── HTTP response (JSON) ─────────►   __EvalExpr__()
◄── next GET with result    ──────
... loop ...
```

Python is the server because it is the only side that knows its ephemeral
port. The HSL host reads `venusrpc.port` from the same directory as the host
script and connects back.

## Sequence pull semantics

`Sequence.pull()` (called automatically at the end of every generated SMT
wrapper) returns only the cursor — `current` and `end`. Labware and position
entries are **not** round-tripped: Python pushed them, Venus's
`increment_sequence` only walks the cursor along them, so re-serializing the
arrays on every call wastes bandwidth and exposes an HSL `sequence` quirk
where each `push()` appends to `GetTotal()` without truncating, so the JSON
payload doubles per call (~30 bytes → MB after a dozen iterations, killing
HxRun via stack/memory pressure).

| Call | Returns | Use |
|--|--|--|
| `seq.pull()` | `current`, `end` | default; safe for any SMT op |
| `seq.pull_full()` | `current`, `end`, `labware`, `position` | rare: Venus populated the sequence itself (e.g. a barcode scan SMT) |
| `seq.pull_with_xyz()` | + per-position xyz/angle | inspecting taught coordinates |

Automatic sequence counting still works — Venus's `increment_sequence`
advances `GetCurrentPosition()`, which `pull()` reads back. The only thing
you lose is the ability to see labware/position changes Venus made
server-side, which no current SMT does. If you ever write one that mutates a
sequence, call `pull_full()` at that one site.

## Codegen

`venusrpc.Codegen` parses Venus deck layouts (`.lay`), submethod libraries
(`.smt` + `.hsi`), labware definitions (`.rck`/`.tml`/`.lid` + linked `.ctr`),
and (optionally) the Hamilton liquid class Access DB. It emits typed Python
wrappers into `./venus_resources/` that import from `venusrpc`.

```python
from venusrpc import Codegen
Codegen.read_layout("example_layout.lay")
Codegen.read_submethods("./smt")
Codegen.read_liquid_classes()  # Windows + pyodbc
Codegen.read_labware("example_layout.lay")  # vendors + parses referenced labware
```

`read_labware` resolves every placed labware in the `.lay` to its definition
file, copies (vendors) the referenced files under `resources/labware/` so the
build is repo-local, and emits `__labware__.py` — one **top-level class per
definition**, each subclassing `Rack`, `Carrier`, or `Lid` (from `venusrpc`) so
its kind is explicit. Every definition carries footprint `length`/`width`/
`height`; `Rack`s additionally expose `rows`/`columns`/pitch, well
`depth`/`max_volume`/`volume_eqn`, and the `segments_x`/`segments_y` profile
(with `width_at_x(h)`/`width_at_y(h)` helpers).

Placed instances live on the **deck layout class**, not in `__labware__`: when
labware codegen is enabled, the deck class gains an `Instances` nested class of
`LabwareInstance` objects — each carrying the **exact placed name** (`.name`,
e.g. `"Cos_96_HalfArea_PR_Source"`) and its shared `.definition`. Other
attributes proxy to the definition, so no data is duplicated:

```python
inst = Ngsstarv_deck.Instances.Cos_96_HalfArea_PR_Source
inst.name        # "Cos_96_HalfArea_PR_Source"  (exact deck Id)
inst.definition  # labware.Cos_96_HalfArea       (shared Rack subclass)
inst.model       # "Cos_96_HalfArea"             (proxied; the .rck stem)
inst.length      # 127.0                          (proxied)
```

The definition's own type name is `model` (the placed name owns `name`).

### Config-driven codegen

Instead of calling `Codegen` by hand on every change, declare a
`[tool.venusrpc]` block in `pyproject.toml` (or a standalone `venusrpc.toml`
in the project root) and use the `venusrpc` CLI.

```toml
# pyproject.toml
[tool.venusrpc]
layouts = ["lay/my_deck.lay"]   # str or list; paths relative to this file
submethods_dir = "smt"                 # default: ./smt
output_dir = "venus_resources"         # default: ./venus_resources

# Opt-in runtime guard. Set to `true` (strict, raises StaleResourcesError),
# `"warn"` (issue UserWarning), or omit/false to skip. See verify_resources().
verify_on_import = false

[tool.venusrpc.liquid_classes]
enabled = true                         # set false to skip the DB scan
include_1ml_channels = true
include_5ml_channels = false
include_96head = true
include_384head = false
include_washstations = false
include_default = true
include_custom = true
database = 'C:\Program Files (x86)\HAMILTON\Config\ML_STARLiquids.mdb'

[tool.venusrpc.labware]
enabled = true                         # set false to skip labware codegen
# Source root the deck's relative labware paths resolve against. Referenced
# definitions are vendored into vendor_dir (repo-local) and parsed from there.
base = 'C:\Program Files (x86)\HAMILTON\LabWare'
vendor_dir = "resources/labware"       # default: resources/labware (rel. to this file)
```

Commands (auto-detect config in cwd; override with `--config <file>`):

| Command | Behavior |
|---------|----------|
| `venusrpc generate` | Regenerate wrappers whose source hash differs. Up-to-date files print `Up-to-date: <path>`. Regenerated print `Generated: <path>`. |
| `venusrpc generate --force` | Regenerate every wrapper regardless of hash. Use after editing the codegen template. |
| `venusrpc check` | Verify every generated file's stored hash matches its source. Exits **0** if clean, **2** if any entry is stale or missing — suitable for CI / pre-commit. |
| `venusrpc check --warn` | Same diagnostic, but exits 0. |

### Opt-in runtime guard

For an early failure when a teammate forgets to run `venusrpc generate`,
import the helper at the top of your driver script:

```python
from venusrpc import verify_resources
verify_resources()   # honors `verify_on_import` from config

# or force a mode regardless of config:
verify_resources(mode="strict")  # raises StaleResourcesError
verify_resources(mode="warn")    # emits UserWarning
```

`mode="auto"` (default) reads `verify_on_import` from the resolved config:

| `verify_on_import` value | Effect |
|--------------------------|--------|
| `true` | strict — raises `StaleResourcesError` listing every stale entry |
| `"warn"` | issues a `UserWarning`, returns the stale list |
| `false` / unset | no-op |

## Differences from PyVenus

| | PyVenus | venusrpc |
|--|---------|----------|
| Transport | File polling (100 ms / 200 ms loops) | HTTP over loopback, blocking I/O |
| Per-call floor latency | ~150–300 ms | ~1–5 ms |
| `Connection.execute()` returns | JSON string | parsed `dict` |
| Re-parsing call sites | required | not needed |
| Liquid class DB driver | hard dependency on `pyodbc` | hard dependency on `pyodbc` |
| Python | 3.8+ | 3.10+ |

## Development

```bash
uv sync --extra dev
uv run pytest
```

## License

LGPL-3.0-or-later. See [LICENSE](LICENSE) and [COPYING](COPYING).

## AI disclosure

Most of this code was written with [Claude Code](https://claude.com/claude-code),
under human direction and review. Treat it as you would any alpha library: read what you run, and try
new methods in simulation first.
