> ## 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.inference.writers

Streaming writers for persisting tile-level predictions.

Provides a [`PredictionWriter`](https://docs.bioptimus.com/sdk-reference/inference/writers#predictionwriter) protocol and three concrete implementations ([`ZarrWriter`](https://docs.bioptimus.com/sdk-reference/inference/writers#zarrwriter), [`HDF5Writer`](https://docs.bioptimus.com/sdk-reference/inference/writers#hdf5writer), [`NPZWriter`](https://docs.bioptimus.com/sdk-reference/inference/writers#npzwriter)) that save per-tile outputs and metadata to disk in a memory-efficient way.

Zarr and HDF5 writers stream results row-by-row so that the full output tensor never needs to reside in memory.  The NPZ writer accumulates results in Python lists and flushes on close.

## PredictionWriter

```python theme={null}
class PredictionWriter(path: Path)
```

Abstract base for streaming prediction writers.

***

#### open

```python theme={null}
@abstractmethod
def open(num_tiles: int, embedding_dim: int) -> None
```

Pre-allocates storage once the embedding dim is known.

**num\_tiles**

Total number of tiles to be written.

**embedding\_dim**

Dimensionality of each prediction vector.

***

#### write

```python theme={null}
@abstractmethod
def write(response: ModelResponse) -> None
```

Writes a single tile's model response.

**response**

The model response to persist.

***

#### set\_thumbnail

```python theme={null}
@abstractmethod
def set_thumbnail(thumbnail: NDArray) -> None
```

Stores a slide thumbnail image.

**thumbnail**

RGB image array of shape `(H, W, 3)`.

***

#### set\_metadata

```python theme={null}
@abstractmethod
def set_metadata(metadata: dict[str, Any]) -> None
```

Stores slide-level metadata alongside the arrays.

**metadata**

Key-value pairs (slide\_name, tile\_size, etc.).

***

#### set\_tissue\_mask

```python theme={null}
@abstractmethod
def set_tissue_mask(mask: NDArray) -> None
```

Stores the tissue mask used during extraction.

**mask**

Binary mask array of shape `(H, W)`, dtype `uint8`.

***

#### set\_gene\_names

```python theme={null}
def set_gene_names(input_genes: list[str] | None = None,
                   output_genes: list[str] | None = None) -> None
```

Stores input and output gene name lists.

Default implementation is a no-op. Subclasses that support gene annotation should override this.

**input\_genes**

Ordered Ensembl IDs for bulk RNA input.

**output\_genes**

Ordered Ensembl IDs for predicted output.

***

#### close

```python theme={null}
@abstractmethod
def close() -> None
```

Flushes and finalises the output file.

***

#### abort

```python theme={null}
def abort() -> None
```

Releases resources and removes any partial output on failure.

Called when inference fails before completion so that no misleading, unfinalized output file is left on disk. Safe to call whether or not [`open`](https://docs.bioptimus.com/sdk-reference/inference/writers#open) succeeded, and independent of how many tiles were written.

## ZarrWriter

```python theme={null}
class ZarrWriter(PredictionWriter)
```

Streams tile predictions into a Zarr directory store.

***

#### open

```python theme={null}
def open(num_tiles: int, embedding_dim: int) -> None
```

Pre-allocates Zarr datasets for outputs, coordinates, and tissue ratios.

**num\_tiles**

Total number of tiles to be written.

**embedding\_dim**

Dimensionality of each prediction vector.

***

#### write

```python theme={null}
def write(response: ModelResponse) -> None
```

Writes a single tile response into the Zarr datasets.

**response**

The model response to persist.

***

#### set\_thumbnail

```python theme={null}
def set_thumbnail(thumbnail: NDArray) -> None
```

Stores a slide thumbnail as a Zarr dataset.

**thumbnail**

RGB image array of shape `(H, W, 3)`.

***

#### set\_metadata

```python theme={null}
def set_metadata(metadata: dict[str, Any]) -> None
```

Stores metadata as Zarr root attributes.

**metadata**

Key-value pairs (slide\_name, tile\_size, etc.).

***

#### set\_tissue\_mask

```python theme={null}
def set_tissue_mask(mask: NDArray) -> None
```

Stores the tissue mask as a Zarr dataset.

**mask**

Binary mask array of shape `(H, W)`, dtype `uint8`.

***

#### set\_gene\_names

```python theme={null}
def set_gene_names(input_genes: list[str] | None = None,
                   output_genes: list[str] | None = None) -> None
```

Stores gene name arrays as Zarr string datasets.

**input\_genes**

Ordered Ensembl IDs for bulk RNA input.

**output\_genes**

Ordered Ensembl IDs for predicted output.

***

#### close

```python theme={null}
def close() -> None
```

Logs output path (Zarr stores are flushed on write).

## HDF5Writer

```python theme={null}
class HDF5Writer(PredictionWriter)
```

Streams tile predictions into an HDF5 file.

***

#### open

```python theme={null}
def open(num_tiles: int, embedding_dim: int) -> None
```

Opens the HDF5 file and pre-allocates datasets.

**num\_tiles**

Total number of tiles to be written.

**embedding\_dim**

Dimensionality of each prediction vector.

***

#### write

```python theme={null}
def write(response: ModelResponse) -> None
```

Writes a single tile response into the HDF5 datasets.

**response**

The model response to persist.

***

#### set\_thumbnail

```python theme={null}
def set_thumbnail(thumbnail: NDArray) -> None
```

Stores a slide thumbnail as an HDF5 dataset.

**thumbnail**

RGB image array of shape `(H, W, 3)`.

***

#### set\_metadata

```python theme={null}
def set_metadata(metadata: dict[str, Any]) -> None
```

Stores metadata as HDF5 file-level attributes.

**metadata**

Key-value pairs (slide\_name, tile\_size, etc.).

***

#### set\_tissue\_mask

```python theme={null}
def set_tissue_mask(mask: NDArray) -> None
```

Stores the tissue mask as an HDF5 dataset.

**mask**

Binary mask array of shape `(H, W)`, dtype `uint8`.

***

#### set\_gene\_names

```python theme={null}
def set_gene_names(input_genes: list[str] | None = None,
                   output_genes: list[str] | None = None) -> None
```

Stores gene name arrays as HDF5 string datasets.

**input\_genes**

Ordered Ensembl IDs for bulk RNA input.

**output\_genes**

Ordered Ensembl IDs for predicted output.

***

#### close

```python theme={null}
def close() -> None
```

Closes the HDF5 file handle and flushes to disk.

***

#### abort

```python theme={null}
def abort() -> None
```

Closes the open HDF5 handle before removing the partial file.

## NPZWriter

```python theme={null}
class NPZWriter(PredictionWriter)
```

Accumulates tile predictions in memory, saves as compressed npz.

***

#### open

```python theme={null}
def open(num_tiles: int, embedding_dim: int) -> None
```

Allocates in-memory arrays for accumulating results.

**num\_tiles**

Total number of tiles to be written.

**embedding\_dim**

Dimensionality of each prediction vector.

***

#### write

```python theme={null}
def write(response: ModelResponse) -> None
```

Writes a single tile response into the in-memory arrays.

**response**

The model response to persist.

***

#### set\_thumbnail

```python theme={null}
def set_thumbnail(thumbnail: NDArray) -> None
```

Stores the slide thumbnail for later NPZ serialization.

**thumbnail**

RGB image array of shape `(H, W, 3)`.

***

#### set\_metadata

```python theme={null}
def set_metadata(metadata: dict[str, Any]) -> None
```

Stores metadata for later NPZ serialization.

**metadata**

Key-value pairs (slide\_name, tile\_size, etc.).

***

#### set\_tissue\_mask

```python theme={null}
def set_tissue_mask(mask: NDArray) -> None
```

Stores the tissue mask for later NPZ serialization.

**mask**

Binary mask array of shape `(H, W)`, dtype `uint8`.

***

#### set\_gene\_names

```python theme={null}
def set_gene_names(input_genes: list[str] | None = None,
                   output_genes: list[str] | None = None) -> None
```

Stores gene name lists for later NPZ serialization.

**input\_genes**

Ordered Ensembl IDs for bulk RNA input.

**output\_genes**

Ordered Ensembl IDs for predicted output.

***

#### close

```python theme={null}
def close() -> None
```

Flushes all accumulated arrays to a compressed `.npz` file.

***

#### create\_writer

```python theme={null}
def create_writer(fmt: OutputFormat, path: Path) -> PredictionWriter
```

Creates a writer for the requested format.

**fmt**

Output format.

**path**

Destination file or directory path.

**Returns**:

An initialised (but not yet opened) [`PredictionWriter`](https://docs.bioptimus.com/sdk-reference/inference/writers#predictionwriter).


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