---
title: Spatial Distortion Index
description: Compute Spatial Distortion Index (SpatialDistortionIndex) also now as D_s.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/image/spatial_distortion_index'
---
# Spatial Distortion Index {#spatial-distortion-index}

## Module Interface {#module-interface}

### `class torchmetrics.image.SpatialDistortionIndex(norm_order=1, window_size=7, reduction='elementwise_mean', **kwargs)` {#torchmetrics.image.SpatialDistortionIndex}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/d_s.py#L35-L223)

Compute Spatial Distortion Index ([SpatialDistortionIndex](https://www.semanticscholar.org/paper/Multispectral-and-panchromatic-data-fusion-without-Alparone-Aiazzi/b6db12e3785326577cb95fd743fecbf5bc66c7c9)) also now as D\_s.

The metric is used to compare the spatial distortion between two images. A value of 0 indicates no distortion (optimal value) and corresponds to the case where the high resolution panchromatic image is equal to the low resolution panchromatic image. The metric is defined as:

```math
\begin{split}D_s = \\sqrt[q]{\frac{1}{L}\\sum_{l=1}^L|Q(\\hat{G_l}, P) - Q(\tilde{G}, \tilde{P})|^q}\end{split}
```

where $`Q`$ is the universal image quality index (see this [`UniversalImageQualityIndex`](/docs/torchmetrics/1.9.0/image/universal_image_quality_index.md#torchmetrics.image.UniversalImageQualityIndex "torchmetrics.image.UniversalImageQualityIndex") for more info), $`\\hat{G_l}`$ is the l-th band of the high resolution multispectral image, $`\tilde{G}`$ is the high resolution panchromatic image, $`P`$ is the high resolution panchromatic image, $`\tilde{P}`$ is the low resolution panchromatic image, $`L`$ is the number of bands and $`q`$ is the order of the norm applied on the difference.

As input to `forward` and `update` the metric accepts the following input

*   `preds` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): High resolution multispectral image of shape `(N,C,H,W)`.
    
*   `target` (`Dict`): A dictionary containing the following keys:
    
    *   `ms` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Low resolution multispectral image of shape `(N,C,H',W')`.
        
    *   `pan` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): High resolution panchromatic image of shape `(N,C,H,W)`.
        
    *   `pan_lr` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Low resolution panchromatic image of shape `(N,C,H',W')`.
        
    

where H and W must be multiple of H’ and W’.

As output of forward and compute the metric returns the following output

*   `sdi` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): if `reduction!='none'` returns float scalar tensor with average SDI value over sample else returns tensor of shape `(N,)` with SDI values per sample
    

Parameters:

*   **norm\_order** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Order of the norm applied on the difference.
    
*   **window\_size** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Window size of the filter applied to degrade the high resolution panchromatic image.
    
*   **reduction** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'elementwise_mean'`, `'sum'`, `'none'`\]) –
    
    a method to reduce metric score over labels.
    
    *   `'elementwise_mean'`: takes the mean (default)
        
    *   `'sum'`: takes the sum
        
    *   `'none'`: no reduction will be applied
        
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Example

```python
>>> from torch import rand
>>> from torchmetrics.image import SpatialDistortionIndex
>>> preds = rand([16, 3, 32, 32])
>>> target = {
...     'ms': rand([16, 3, 16, 16]),
...     'pan': rand([16, 3, 32, 32]),
... }
>>> sdi = SpatialDistortionIndex()
>>> sdi(preds, target)
tensor(0.0090)
```

#### `plot(val=None, ax=None)` {#torchmetrics.image.SpatialDistortionIndex.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/d_s.py#L173-L223)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> # Example plotting a single value
>>> from torch import rand
>>> from torchmetrics.image import SpatialDistortionIndex
>>> preds = rand([16, 3, 32, 32])
>>> target = {
...     'ms': rand([16, 3, 16, 16]),
...     'pan': rand([16, 3, 32, 32]),
... }
>>> metric = SpatialDistortionIndex()
>>> metric.update(preds, target)
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/spatial\_distortion\_index-1.png](/docs/torchmetrics/1.9.0/static/_images/spatial_distortion_index-1.png)](/docs/torchmetrics/1.9.0/static/_images/spatial_distortion_index-1.png)

```python
>>> # Example plotting multiple values
>>> from torch import rand
>>> from torchmetrics.image import SpatialDistortionIndex
>>> preds = rand([16, 3, 32, 32])
>>> target = {
...     'ms': rand([16, 3, 16, 16]),
...     'pan': rand([16, 3, 32, 32]),
... }
>>> metric = SpatialDistortionIndex()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(preds, target))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/spatial\_distortion\_index-2.png](/docs/torchmetrics/1.9.0/static/_images/spatial_distortion_index-2.png)](/docs/torchmetrics/1.9.0/static/_images/spatial_distortion_index-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.image.spatial_distortion_index(preds, ms, pan, pan_lr=None, norm_order=1, window_size=7, reduction='elementwise_mean')` {#torchmetrics.functional.image.spatial_distortion_index}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/image/d_s.py#L205-L267)

Calculate [Spatial Distortion Index](#spatial-distortion-index) ([SpatialDistortionIndex](https://www.semanticscholar.org/paper/Multispectral-and-panchromatic-data-fusion-without-Alparone-Aiazzi/b6db12e3785326577cb95fd743fecbf5bc66c7c9)) also known as D\_s.

Metric is used to compare the spatial distortion between two images.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – High resolution multispectral image.
    
*   **ms** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – Low resolution multispectral image.
    
*   **pan** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – High resolution panchromatic image.
    
*   **pan\_lr** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\]) – Low resolution panchromatic image.
    
*   **norm\_order** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Order of the norm applied on the difference.
    
*   **window\_size** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Window size of the filter applied to degrade the high resolution panchromatic image.
    
*   **reduction** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'elementwise_mean'`, `'sum'`, `'none'`\]) –
    
    A method to reduce metric score over labels.
    
    *   `'elementwise_mean'`: takes the mean (default)
        
    *   `'sum'`: takes the sum
        
    *   `'none'`: no reduction will be applied
        
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

Returns:

Tensor with SpatialDistortionIndex score

Raises:

*   [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError "(in Python v3.15)") – If `preds`, `ms`, `pan` and `pan_lr` don’t have the same data type.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `preds`, `ms`, `pan` and `pan_lr` don’t have `BxCxHxW shape`.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `preds`, `ms`, `pan` and `pan_lr` don’t have the same batch and channel sizes.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `preds` and `pan` don’t have the same dimension.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `ms` and `pan_lr` don’t have the same dimension.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `preds` and `pan` don’t have dimension which is multiple of that of `ms`.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `norm_order` is not a positive integer.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `window_size` is not a positive integer.
    

Example

```python
>>> from torch import rand
>>> from torchmetrics.functional.image import spatial_distortion_index
>>> preds = rand([16, 3, 32, 32])
>>> ms = rand([16, 3, 16, 16])
>>> pan = rand([16, 3, 32, 32])
>>> spatial_distortion_index(preds, ms, pan)
tensor(0.0090)
```

---

**Navigation**

- Parent: [Image](/docs/torchmetrics/1.9.0/image.md)
