# Array Libraries Interoperability!
<!-- Put the link to this slide here so people can follow -->
link: https://hackmd.io/@anirudh/scipy-interop
---
## SciPy FFT
```python
import numpy as np
import scipy.fft
# Basic
y_numpy = scipy.fft.fft(np.arange(10))
```
---
## CuPy FFT
```python
import cupy as cp
import cupyx.scipy.fft as cu_fft
# Basic
y_cupy = cu_fft.fft(cp.arange(10))
```
---
## uarray
* backend / dispatch mechanism
* separately define an API, along with backends containing separate implementations of that API.
* user can register an implementation
---
## SciPy & CuPy FFT
```python
# SciPy with CuPy array
x = cp.arange(10)
with scipy.fft.set_backend(cu_fft):
# Calls into cupyx and returns a CuPy array
y_cupy = scipy.fft.fft(x)
```
---
## Auto-Dispatch FFT
```python
import cupy as cp
import scipy.fft
x = cp.arange(4)
# without using `with set_backend(...)
y_cupy = scipy.fft.fft(x)
# this will now raise a `TypeError`, but should be made to work (auto-dispatch)
y_cupy, type(y_cupy)
```
---
## uarray backend for ndimage
```python
# Basic SciPy Land
y_numpy = ndimage.correlate1d(np.arange(10), [1, 2.5])
y_numpy, type(y_numpy)
```
```python
# Basic CuPy Land. Works!!! :)
y_cupy = cupy_image.correlate1d(cp.arange(10),
cp.array([1, 2.5]))
y_cupy, type(y_cupy)
```
```python
# Basic CuPy Land. Fails!!!
y_cupy = cupy_image.correlate1d(cp.arange(10),
[1, 2.5])
>>> AttributeError: 'list' object has no attribute 'ndim'
y_cupy, type(y_cupy)
```
---
## uarray backend for ndimage
```python
# SciPy ndimage method with CuPy array should be made to work
with scipy.ndimage.set_backend(cupy_ndimage):
y_cupy = ndimage.correlate1d(cp.arange(10),
cp.array([1, 2.5]))
y_cupy, type(y_cupy)
```
---
## Thank you!
---
{"metaMigratedAt":"2023-06-16T03:41:56.173Z","metaMigratedFrom":"YAML","title":"SciPy Interoperability","breaks":true,"description":"Make array libraries and SciPy/scikit-image/scikit-learn work together better.","contributors":"[{\"id\":\"c2527c0b-2aca-4972-850f-f12531e5a6fe\",\"add\":5365,\"del\":3326}]"}