# @tensorleap\_custom\_visualizer

The `tensorleap_custom_visualizer` decorates a [**Visualizer Function**](/tensorleap-integration/writing-integration-code/visualizer-function.md).

```python
@tensorleap_custom_visualizer(name='image_visualizer',
                              visualizer_type=LeapDataType.Image,
                              heatmap_function=None)
def image_visualizer(image: np.ndarray) -> LeapImage:
    pass
```

<table><thead><tr><th width="158.46928201888204">Args</th><th></th></tr></thead><tbody><tr><td><code>name</code></td><td><em>(str)</em> with the given name of the <strong>visualization,</strong> e.g. image_visualzier.</td></tr><tr><td><code>visualizer_type</code></td><td><em>(</em><a href="/pages/XVWuIgRFJgiODi1OYyUs"><em><strong>LeapDataType</strong></em></a><em>)</em> This property sets the type of the data to be visualized by the visualizer.</td></tr><tr><td>heatmap_function</td><td><p><em>(optional)</em></p><p><em>Callable[[npt.NDArray[np.float32]], npt.NDArray[np.float32]]</em><br>This parameter points to a <strong>function</strong> that modifies the heatmap data before visualization.</p></td></tr></tbody></table>

#### Visualizer inputs:

np.ndarray tensors with a batch dimension. This batch would always be 1.

#### Visualizers outputs:

One of the [visualizer objects](/tensorleap-integration/python-api/code_loader/visualizer_classes.md) contraining a squeezed (no-batch) object.

### Examples

#### Basic Usage

```python
import numpy as np
from code_loader.contract.visualizer_classes import LeapImage
from code_loader.contract.enums import LeapDataType

...

@tensorleap_custom_visualizer(name='image_visualizer', visualizer_type=LeapDataType.Image)
def image_visualizer(image: np.ndarray) -> LeapImage:
    image = image / image.max()
    image = image.squeeze(0)
    return LeapImage(image)
```

The [**decoder\_classes**](/tensorleap-integration/python-api/code_loader/visualizer_classes.md) pages contain additional, decoder specific, examples. Moreover, full script usage can be found in [**Integration Script**](/tensorleap-integration/writing-integration-code.md#dataset-script).

#### Resize Image and Heatmap

When changing the original data shape in the *visualizer function,* we need to reshape the heatmap data that is projected. `heatmap_visualizer` points to a function that modifies the heatmap data respectively. Consider the following example:

```python
from code_loader.contract.visualizer_classes import LeapImage
import numpy.typing as npt

def resized_image_visualizer_heatmap(data: npt.NDArray[np.float32]) -> npt.NDArray[np.float32]:
    # data is the heatmap with original size (origin_W, origin_H)
    return np.resize(data, (256, 512))    # we reshape to the resized shape
    
@tensorleap_custom_visualizer(name='image_visualizer',
                              visualizer_type=LeapDataType.Image,
                              heatmap_function=resized_image_visualizer_heatmap)
def resized_image_visualizer(data: npt.NDArray[np.float32]) -> LeapImage:
    return LeapImage(np.resize(data, (256, 512, 3)))

```

#### Guides

Full examples can be found in the **Dataset Integration** section of the following guides:

* [**MNIST Guide**](/guides/full-guides/mnist-guide.md)
* [**IMDB Guide**](/guides/full-guides/imdb-guide.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_custom_visualizer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
