Python client for the xoptics remote simulation API
Project description
xoptics-client
Python client for the xoptics remote simulation API.
Install
For now: drop xoptics_client.py next to your notebook. (PyPI package will come later.)
Dependencies:
pip install httpx h5py numpy
# Optional, for to_dataframe():
pip install pandas
Usage
import os
from xoptics_client import Client
# Pass api_key=..., or set XOPTICS_API_KEY env var
c = Client(api_key=os.environ["XOPTICS_API_KEY"])
# Submit, stream stdout live, wait for completion
result = c.run(
xml=open("scene.xml").read(),
cores=64,
label="my_design_v3",
on_log=print, # any callable taking a single str line
)
# Inspect detectors
print(result.detectors())
arr = result.detector("eyebox_g").to_array() # numpy
df = result.detector("eyebox_g").to_dataframe() # pandas (optional)
# Save h5 locally
result.save("./my_result.h5")
Async / fire-and-forget
job = c.submit(xml=..., cores=64, label="long_run")
print(job.id) # save this somewhere
# ... come back later, possibly from a different process ...
job = c.get_job("abc123def456")
job.wait(on_log=print)
result = job.result()
List your jobs
for j in c.jobs(limit=20):
print(f"{j.id} {j.status:7s} {j.elapsed_sec or 0:.1f}s {j.label}")
Monitor + cancel
# Your own usage: cores in use, lifetime CPU-hours, running jobs with progress %
c.print_usage()
# demo (Demo user) [sk_test_qkML...EWEU]
# cores in use: 32
# lifetime CPU-hrs: 1.905
# running jobs: 1
# 457fbec87414 cores= 32 59.5% elapsed=00:00:30 remaining=00:00:20 'my run'
# Cancel a specific job
c.cancel_job("457fbec87414")
# or:
job.cancel()
Admin: see all users' usage
If your key has role: admin:
c.print_fleet_usage()
# server cores: 384 total | 32 in use | 352 free
# ────────────────────────────────────────────────
# admin (Admin) [sk_live__mcZ...3_LI] role=admin
# cores in use: 0
# lifetime CPU-hrs: 0.456
# running jobs: (none)
#
# demo (Demo user) [sk_test_qkML...EWEU]
# cores in use: 32
# lifetime CPU-hrs: 1.905
# running jobs: 1
# 457fbec87414 cores= 32 59.5% elapsed=00:00:30 remaining=00:00:20 'my run'
Admin can also cancel any user's job through c._http.delete("/admin/jobs/{id}").
Error handling
from xoptics_client import Client, AuthError, JobFailedError
try:
result = c.run(xml=..., cores=64)
except AuthError:
# bad API key, or hitting an admin-only endpoint as a regular user
...
except JobFailedError as e:
# xTracerCL ran but exited non-zero (bad XML, runtime error, etc.)
print(e)
Configuration
Client(api_key=..., base_url="https://api.xoptics.org", timeout=30.0)
api_key— string, or setXOPTICS_API_KEYenv varbase_url— override only if you're testing against a different deploymenttimeout— for individual HTTP requests; streaming (run,wait(on_log=...)) is not capped
How it works under the hood
client server (api.xoptics.org)
────── ────────────────────────
c.submit(...) POST /v1/jobs ──► server writes input.xml,
launches xTracerCL,
returns job_id immediately
c.run(...) POST /v1/jobs
GET /v1/jobs/{id}/stream (SSE) ──► live stdout pushed
as text/event-stream
GET /v1/jobs/{id}/result ──► h5 file binary
(cached locally)
The on_log= callback is invoked once per line as it's emitted by xTracerCL, including in-progress messages like 4.5M/21.6M rays processed, 21%.
Examples
See examples/demo.py.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xoptics_client-0.3.0.tar.gz.
File metadata
- Download URL: xoptics_client-0.3.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd40dae856083f8ecfc46c471b419df32033350e9f812c5906d6c63cebdee884
|
|
| MD5 |
fd044029243482124876a18f0f208355
|
|
| BLAKE2b-256 |
d3100e4e793a1b5b20f6b90b2cb86ba511b137c242593663b4a6f2fea2cb8401
|
File details
Details for the file xoptics_client-0.3.0-py3-none-any.whl.
File metadata
- Download URL: xoptics_client-0.3.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6be732ad09f26ed897c8efc1e8cc3c492c78aaf3edfb7f4f33e307082d909685
|
|
| MD5 |
be7ce1625f17266d997e667b9c8e80e0
|
|
| BLAKE2b-256 |
a3d6369f3ea9da72df67a7937894608db647b31a87fd8213a19ab29061c49454
|