Skip to main content

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 set XOPTICS_API_KEY env var
  • base_url — override only if you're testing against a different deployment
  • timeout — 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xoptics_client-0.2.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xoptics_client-0.2.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file xoptics_client-0.2.0.tar.gz.

File metadata

  • Download URL: xoptics_client-0.2.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for xoptics_client-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6f701e0c0b64cb83dde653c44353d8c95e5f26f573158a3ce5510e1b10bf5604
MD5 ca32ce31b9e34e9c6c49261d206858d1
BLAKE2b-256 aea078234afbfff5deeb2212481ad94e337f0681d031a535f8f7657bb1974589

See more details on using hashes here.

File details

Details for the file xoptics_client-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: xoptics_client-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for xoptics_client-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0313f06fedd3c2c0193329ad3295a0b33fb2b0691dd8eda7b51ea01d06e42886
MD5 9f2bdb8457fcf41cab235fc187d51beb
BLAKE2b-256 07e8ce7946d39381d01c169871500fc76dc85e9414a69111c6f15e9cc420111a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page