From a863b84c4eb1252ad0996b439dd2d683363c870d Mon Sep 17 00:00:00 2001 From: Roy Hoitink Date: Mon, 18 Mar 2024 11:08:13 +0100 Subject: [PATCH] Added support for cupy convolution if installed --- src/simulatedmicroscopy/image.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/simulatedmicroscopy/image.py b/src/simulatedmicroscopy/image.py index e91aab0..762f554 100644 --- a/src/simulatedmicroscopy/image.py +++ b/src/simulatedmicroscopy/image.py @@ -6,7 +6,6 @@ import h5py import numpy as np -import scipy.signal import skimage.measure from .input import Coordinates @@ -398,9 +397,21 @@ def convolve(self, other: type[Image]) -> type[Image]: "Cannot convolve images with different pixel sizes" ) - self.image = scipy.signal.convolve( - self.image, other.image, mode="same" - ) + try: + import cupy as cp + from cupyx.scipy import signal as cusignal + + self.image = cusignal.fftconvolve( + cp.asarray(self.image), cp.asarray(other.image), mode="same" + ).get() + except ImportError: + # resort to scipy if cupy is not available + + import scipy.signal + + self.image = scipy.signal.convolve( + self.image, other.image, mode="same" + ) self.is_convolved = True self.metadata["is_convolved"] = True