question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change in behavior of spline prefiltering - expected?

See original GitHub issue

It appears that, since scipy/scipy@81d5e7a, the behavior of spline interpolation has changed in a way that we (over at NIPY) found surprising.

In particular, as of that commit (I believe - see https://github.com/nipy/nipy/issues/468), we get a different result from direct use of scipy.ndimage.map_coordinates compared to precalculating the spline filter, and sending that to scipy.ndimage.map_coordinates. Here’s a script that demonstrates the change:

# Test 2D interpolation for Scipy
import numpy as np

import scipy.ndimage as spnd

arr = np.arange(6).reshape((2, 3))

coord = np.array([[0, 3]]).T

# Interpolation without calculating filter beforehand.
v1 = spnd.map_coordinates(arr,
                          coord,
                          order=3,
                          mode='nearest',
                         )
print('Calculate filter with interpolation', v1)

# Calculate and pass filter.
filtered = spnd.spline_filter(arr, order=3, mode='nearest')
v2 =  spnd.map_coordinates(filtered,
                           coord,
                           order=3,
                           mode='nearest',
                           prefilter=False)
print('Calculate filter followed by interpolation', v2)

On Scipy 1.5.4 this gives [2.] and [2.]. On Scipy 1.6.0 this gives [2.] and [2.20261874] (macOS 11.2.1).

I was expecting precalculation to give the same results as no precalculation - as it used to in Scipy <= 1.5.4. Is that expectation not correct?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
grlee77commented, Mar 2, 2021

Hmm - testing now, I do not get the same with npad=12 and prefilter=False with non-integer voxel coordinates. Is the following surprising?

The difference here has to do with the dtype of the output. If unspecified, ndimage.interpolation functions use the dtype of the input array to determine the dtype of the output. In your example, when calculating v1, arr has an integer dtype, and so v1 is integer-valued as well. However, in the prefiltered case, filtered_and_padded.dtype is np.float64 so this gives a floating point result. This way of determining the output dtype has been present in ndimage for a long time and is not something new to the 1.6 release.

To get equivalent floating point results for both you would do:

When computing v1, specifyoutput=np.float64 in the map_coordinates call (or equivalently, cast arr to np.float64 before calling map_coordinates)

Or if the integer-valued output is the desired result

set output=np.int64 (or output=arr.dtype) in the computation of v2_padded

0reactions
matthew-brettcommented, Mar 2, 2021

Ouch - thank you! I’m so sorry to have accidentally made you do this debugging, rather than us - I’m afraid it was the classic problem of not enough tests on our side, then change of behavior (edges, ‘constant’) causing failures in the tests we did have, and then me writing more, new tests, without thinking to check what the results were previously.

Read more comments on GitHub >

github_iconTop Results From Across the Web

An Evaluation of Prefiltered B-Spline Reconstruction for Quasi ...
In this paper, we demonstrate that quasi-interpolation of orders two and four can be efficiently implemented on the Body-Centered Cubic (BCC) lattice by ......
Read more >
A Fresh Look at Generalized Sampling - Hugues Hoppe
The input f is prefiltered using the cubic B-spline basis β3. The resulting over-blurred image is then transformed with a digital filter p∨...
Read more >
Splines - A perfect fit for signal and image processing - EPFL
has changed significantly. This article attempts to fullfill three goals. The first is to provide a tutorial on splines that is geared to...
Read more >
Mailman 3 Re: [Numpy-discussion] nd_image.affine_transform edge ...
I would rather opt for changing the spline fitting algorithm than for padding with ... but these problems with the spline prefiltering are...
Read more >
Analyzing RNA-seq data with DESeq2 - Bioconductor
The DESeq2 model; Changes compared to DESeq; Methods changes since ... As input, the DESeq2 package expects count data as obtained, e.g., ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found