Repeated image resampling can tempt a developer to disable a preprocessing step after a first successful test. For cubic interpolation, that change deserves a representation check. An array of sampled intensities and an array of spline coefficients can have exactly the same shape while carrying different numbers and different meanings.

Consider an offline robot-camera analysis tool that evaluates the same image at several coordinate sets. Reusing a properly prepared coefficient array may avoid repeating preparation for each set. Simply passing the original pixels with prefilter=False does something else. This article isolates the difference with seven synthetic samples, explicit boundary handling, and checks at both integer and fractional coordinates.

The documented distinction

SciPy's map_coordinates evaluates an input at requested coordinates using spline interpolation. Its default order is three. For orders greater than one, the default prefilter=True prepares filtered values before evaluation. The documentation warns that disabling this step on an unfiltered input changes the result, describing the output as blurred; an already prefiltered input is the intended alternative [1].

This is not an API prohibition on passing raw samples. Such a call runs. The question is whether it implements the intended interpolation procedure. The fixture below deliberately runs both paths and compares their numerical consequences, rather than using an exception as the test of correctness.

An original seven-sample profile

Use the floating-point sequence [0,0,0,6,0,0,0]. The sample at index 3 is 6; every other sample is zero. These values are invented dimensionless data, not measured brightness, contact force, or a robot event. Choose cubic interpolation and mode='mirror' throughout so that the experiment does not silently change boundary conventions between paths.

First request values at the original integer nodes. With automatic prefiltering, the result reproduces the original sequence within floating-point tolerance. Without prefiltering, the result is approximately [0,0,1,4,1,0,0]. The central value has become 4, and the two adjacent nodes have become 1.

That discrepancy is already visible before evaluating any fractional location. A visual preview at reduced resolution could miss it, especially if each image is automatically contrast-scaled. A direct assertion at index 3 makes the changed behavior explicit.

import numpy as np

from scipy.ndimage import map_coordinates, spline_filter

samples = np.array([0., 0., 0., 6., 0., 0., 0.])

nodes = np.arange(7, dtype=float)[None, :]

automatic_nodes = map_coordinates(

samples, nodes, order=3, mode='mirror', prefilter=True)

skipped_nodes = map_coordinates(

samples, nodes, order=3, mode='mirror', prefilter=False)

np.testing.assert_allclose(

automatic_nodes, samples, rtol=0, atol=1e-12)

np.testing.assert_allclose(

skipped_nodes, [0, 0, 1, 4, 1, 0, 0],

rtol=0, atol=1e-12)

Separate preparation from evaluation

SciPy's spline_filter produces a filtered array of the same shape. Its reference exposes interpolation order, output dtype, and boundary mode, and explains that multidimensional filtering is carried out as a sequence of one-dimensional filters. Intermediate precision depends on the output dtype [2]. Here the example explicitly requests float64 and the same cubic order and mirror mode as the evaluation path.

coefficients = spline_filter(

samples, order=3, output=np.float64, mode='mirror')

coordinates = np.array([[1.5, 2., 2.5, 3., 3.5, 4., 4.5]])

automatic = map_coordinates(

samples, coordinates, order=3,

mode='mirror', prefilter=True)

cached = map_coordinates(

coefficients, coordinates, order=3,

mode='mirror', prefilter=False)

np.testing.assert_allclose(

cached, automatic, rtol=0, atol=1e-12)

The coefficient vector for this example is approximately [-0.4,0.8,-2.8,10.4,-2.8,0.8,-0.4]. It is visibly different from the original sample vector. Calling the array coefficients, rather than image or filtered_image alone, helps preserve that distinction in a review.

At the listed query coordinates, both correctly prepared paths returned approximately [-0.75,0,3.6,6,3.6,0,-0.75]. Their maximum absolute difference was zero in the local execution. That run used SciPy 1.18.1, NumPy 2.5.2, and Python 3.14.7; the official reference pages consulted were labeled 1.18.0. The assertions use a tolerance rather than making exact equality a portability requirement.

Avoid preparing the coefficients twice

A second plausible mistake is to cache coefficients but leave prefilter=True in the later call. In the same fixture, that path returns about 10.4 at query coordinate 3, rather than the original sample value 6. It is a different calculation again. An array's shape cannot reveal whether preparation has already happened.

The cache interface should therefore carry a representation label. One input path accepts samples and performs preparation. Another accepts the prepared coefficients and explicitly disables it. Reviewers can then check the transition between those states without inferring meaning from a filename or remembering which caller last touched the array.

Do not interpret the coefficients as displayed intensities. Values outside the original range are part of this representation. The negative fractional results in the correctly interpolated fixture also show that this cubic calculation is not a range-preserving rule for arbitrary data. Clipping them would add a separate operation and should be identified as such in any comparison.

Design a cache check around the declared case

For a local cache, record the original sample identity, shape, dtype, interpolation order, boundary mode, preparation version, and coefficient identity. Changing the original image requires a new coefficient calculation. A new coordinate list alone can reuse the same coefficient array in this fixed mirror-mode example because the preparation was derived from the input samples, not that query list.

Do not turn this fixture into a claim that every boundary mode, padding scheme, or image pipeline has the same manual caching recipe. Extend the comparison to the actual configuration before using it. Include fractional positions, original sample locations, and coordinates near the selected boundaries so that an interior-only test does not conceal an edge mismatch.

Performance is a separate measurement. This article demonstrates numerical equivalence for one explicit cached path; it does not benchmark a speedup or estimate memory savings. A production experiment can measure preparation time, evaluation time, and cache storage after establishing that the two intended outputs agree.

The useful operational result is a small, reproducible contract: raw samples plus automatic preparation agree with matching cached coefficients plus disabled preparation. Keep that contract attached to the cache so that a future optimization preserves the computation the image analysis actually depends on.

Sources

[1] SciPy, map_coordinates: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html

[2] SciPy, spline_filter: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.spline_filter.html