> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bioptimus.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Bioptimus SDK

> The Bioptimus Python SDK — installation and the two ways to use it.

The Bioptimus Python SDK runs whole-slide inference against either an [on-premise server](https://docs.bioptimus.com/deployment/platforms/on-premise.md) or a [SageMaker endpoint](https://docs.bioptimus.com/deployment/platforms/aws-sagemaker.md). It handles WSI reading, tiling, tissue masking, bulk-RNA alignment, and concurrent dispatch.

The Bioptimus SDK talks to whichever model is deployed at the endpoint you connect to. `Backbone.available_backbones()` lists the models the Bioptimus SDK *knows how to build* (`h1`, `m-optimus`, `tissue-seg`) — to see what a server actually has loaded, check its [`/ping`](https://docs.bioptimus.com/api-reference/introduction) response.

## Installation

> [tab] PyPI
>
> ```bash theme={null}
> pip install bioptimus-sdk
> ```

> [tab] Python Wheel
>
> ```bash theme={null}
> pip install bioptimus_sdk-<version>-py3-none-any.whl
> ```

## Two ways to use it

> [card] [Inference pipeline (recommended)](https://docs.bioptimus.com/guides/get-started/inference-pipeline.md)
>
> One object configures the whole pipeline. Caches tissue masks, organizes outputs into a workspace, and is reproducible. Best for most users and for cohorts.

> [card] [Core API (advanced)](https://docs.bioptimus.com/guides/workflows/spatial-transcriptomics.md)
>
> `Backbone` + `SlideInference` give explicit, per-slide control over the model client, mask provider, and writer.

## Connecting to a model

The `Backbone` factory is the low-level client used by both layers.

> [tab] On-premise
>
> ```python H-Optimus theme={null}
> from bioptimus.models.backbones import Backbone
> from bioptimus.models.types import Models
>
> print(Backbone.available_backbones())   # ['h1', 'm-optimus', 'tissue-seg']
> model = Backbone(Models.H1, backend="remote", base_url="http://localhost:8080")
> ```
>
> ```python M-Optimus theme={null}
> from bioptimus.models.backbones import Backbone
> from bioptimus.models.types import Models
>
> print(Backbone.available_backbones())   # ['h1', 'm-optimus', 'tissue-seg']
> model = Backbone(Models.M_OPTIMUS, backend="remote", base_url="http://localhost:8080")
> ```

> [tab] AWS SageMaker
>
> ```python H-Optimus theme={null}
> from bioptimus.models.backbones import Backbone
> from bioptimus.models.types import Models
>
> model = Backbone(
>     Models.H1, backend="aws",
>     endpoint_name="h-optimus", region_name="us-east-1",
> )
> ```
>
> ```python M-Optimus theme={null}
> from bioptimus.models.backbones import Backbone
> from bioptimus.models.types import Models
>
> model = Backbone(
>     Models.M_OPTIMUS, backend="aws",
>     endpoint_name="m-optimus", region_name="us-east-1",
> )
> ```

For M-Optimus, gene sets are fetched from the server automatically (`model.input_gene_names`, `model.output_gene_names`).

> [!TIP]
>
> Getting a connection error instead of a response? See [Common issues](https://docs.bioptimus.com/guides/get-started/troubleshooting.md).

## Recommended starting configurations

> [tab] Single slide, single GPU (fastest)
>
> ```python theme={null}
> from bioptimus.models.backbones import Backbone
> from bioptimus.inference.inference import SlideInference
> from bioptimus.inference.schemas import InferenceMode
>
> model = Backbone(
>     "h1", backend="local",
>     checkpoint=".../h1_optimus_cls_token_fp16.pt2",
>     device="cuda:0",
>     max_batch_size=128,
> )
> inferrer = SlideInference(
>     wsi_path=wsi_path, model=model,
>     mask_provider=tissue_mask_provider, mask_threshold=0.5,
> )
> inferrer.predict(output_path=out, mode=InferenceMode.EMBEDDING, max_concurrency=256)
> ```

> [tab] Many slides (cohort)
>
> Use the [`Inference`](https://docs.bioptimus.com/guides/workflows/cohort) facade. Set `workers` by backend: **1–2** on a single local GPU (higher is pointless — see [Performance tuning](https://docs.bioptimus.com/guides/get-started/performance-tuning.md#backend-specific-knobs)); a small multiple of the endpoint's GPU count for `remote`/`aws`; one process per GPU for multiple local GPUs.
>
> ```python theme={null}
> from bioptimus.inference import Inference
> from bioptimus.inference.schemas import InferenceMode
> from bioptimus.models.types import Models
>
> infer = Inference(
>     model_name=Models.H1, cohort=cohort,
>     api_url="http://localhost:8080",
>     tissue=True, mask_threshold=0.5,
>     output_path="/data/output", experiment="my_experiment", run=1,
>     workers=2,
> )
> infer.tissue()
> infer.run(mode=InferenceMode.EMBEDDING)
> ```

> [!TIP]
>
> Still too slow? Raise `mask_threshold` (fewer tiles), then move to a larger or additional GPU, or dig into the knobs on [Performance tuning](https://docs.bioptimus.com/guides/get-started/performance-tuning.md).

## Guides

> [card] [Inference pipeline](https://docs.bioptimus.com/guides/get-started/inference-pipeline.md)
>
> One-object pipeline, workspaces, reproducible config.

> [card] [Cohorts](https://docs.bioptimus.com/guides/workflows/cohort.md)
>
> Multi-slide cohorts and late-binding bulk RNA.

> [card] [Spatial transcriptomics](https://docs.bioptimus.com/guides/workflows/spatial-transcriptomics.md)
>
> M-Optimus gene-expression prediction end to end.

> [card] [Tile embeddings & PCA](https://docs.bioptimus.com/guides/workflows/embeddings-pca.md)
>
> Extract embeddings and visualize morphology.

> [card] [WSI processing](https://docs.bioptimus.com/guides/reference/wsi-processing.md)
>
> Read slides: levels, MPP, regions, thumbnails.

> [card] [Visualizing results](https://docs.bioptimus.com/guides/get-started/visualizing-results.md)
>
> Load Zarr/HDF5/NPZ and overlay genes and masks.

> [card] [Common issues](https://docs.bioptimus.com/guides/get-started/troubleshooting.md)
>
> Connection issues, and where to look for slow or stalled runs.

## Output formats

| Format | Extension | Notes |
| - | - | - |
| `OutputFormat.ZARR` | `.zarr` | Default. Directory store, memory-efficient |
| `OutputFormat.HDF5` | `.h5` | Single file, memory-efficient |
| `OutputFormat.NPZ` | `.npz` | Accumulates in memory, compressed on close |

Every output file contains the same contents:

* **Datasets:** `outputs`, `coords`, `tissue_ratios`, `thumbnail`, `tissue_mask` — plus `input_gene_names` and `output_gene_names` for M-Optimus.
* **Metadata attributes:** `slide_name`, `tile_size`, `stride`, `mpp`, `slide_dimensions`, `slide_dimensions_at_mpp`, `num_tiles`.

See [Visualizing results](https://docs.bioptimus.com/guides/get-started/visualizing-results.md) to load and plot them.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.