<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/scipy/ticket/810" rel="nofollow">http://projects.scipy.org/scipy/ticket/810</a> on 2008-12-02 by trac user damian.eads, assigned to trac user damian.eads.</em></p>
<p dir="auto">The new scipy.spatial package combines my distance computation with Anne Archibald's kd-tree module. Some minor reworking is needed to make the kd-tree code use the distance module's distance functions. This reduces the duplication of functionality in SciPy and improves maintainability.</p>
<h3 dir="auto">Describe your issue.</h3>
<p dir="auto"><code class="notranslate">scipy.interpolate.RegularGridInterpolator.__call__()</code> raises an exception when one or more arrays of grid coordinates used to construct the interpolator are non-C-contiguous.</p>
<p dir="auto">This appears to be new behavior in v1.10.0. In previous versions, there was no such restriction that these arrays must be C-contiguous -- the interpolator would produce valid results instead of raising an exception.</p>
<h3 dir="auto">Reproducing Code Example</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from scipy.interpolate import RegularGridInterpolator
import numpy as np

def f(x, y, z):
    return 2 * x**3 + 3 * y**2 - z

x = np.linspace(1, 4, 11)
y = np.linspace(4, 7, 22)
z = np.linspace(7, 9, 33)
xg, yg ,zg = np.meshgrid(x, y[::2], z[::3], indexing='ij', sparse=True)
data = f(xg, yg, zg)

# Take a strided slice of `y` and `z` so that these input arrays are not
# C-contiguous.
interp = RegularGridInterpolator((x, y[::2], z[::3]), data)

pts = np.array([[2.1, 6.2, 8.3],
                [3.3, 5.2, 7.1]])
interp(pts)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">interpolate</span> <span class="pl-k">import</span> <span class="pl-v">RegularGridInterpolator</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-k">def</span> <span class="pl-en">f</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">z</span>):
    <span class="pl-k">return</span> <span class="pl-c1">2</span> <span class="pl-c1">*</span> <span class="pl-s1">x</span><span class="pl-c1">**</span><span class="pl-c1">3</span> <span class="pl-c1">+</span> <span class="pl-c1">3</span> <span class="pl-c1">*</span> <span class="pl-s1">y</span><span class="pl-c1">**</span><span class="pl-c1">2</span> <span class="pl-c1">-</span> <span class="pl-s1">z</span>

<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">1</span>, <span class="pl-c1">4</span>, <span class="pl-c1">11</span>)
<span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">4</span>, <span class="pl-c1">7</span>, <span class="pl-c1">22</span>)
<span class="pl-s1">z</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">7</span>, <span class="pl-c1">9</span>, <span class="pl-c1">33</span>)
<span class="pl-s1">xg</span>, <span class="pl-s1">yg</span> ,<span class="pl-s1">zg</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">meshgrid</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>[::<span class="pl-c1">2</span>], <span class="pl-s1">z</span>[::<span class="pl-c1">3</span>], <span class="pl-s1">indexing</span><span class="pl-c1">=</span><span class="pl-s">'ij'</span>, <span class="pl-s1">sparse</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-en">f</span>(<span class="pl-s1">xg</span>, <span class="pl-s1">yg</span>, <span class="pl-s1">zg</span>)

<span class="pl-c"># Take a strided slice of `y` and `z` so that these input arrays are not</span>
<span class="pl-c"># C-contiguous.</span>
<span class="pl-s1">interp</span> <span class="pl-c1">=</span> <span class="pl-v">RegularGridInterpolator</span>((<span class="pl-s1">x</span>, <span class="pl-s1">y</span>[::<span class="pl-c1">2</span>], <span class="pl-s1">z</span>[::<span class="pl-c1">3</span>]), <span class="pl-s1">data</span>)

<span class="pl-s1">pts</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([[<span class="pl-c1">2.1</span>, <span class="pl-c1">6.2</span>, <span class="pl-c1">8.3</span>],
                [<span class="pl-c1">3.3</span>, <span class="pl-c1">5.2</span>, <span class="pl-c1">7.1</span>]])
<span class="pl-en">interp</span>(<span class="pl-s1">pts</span>)</pre></div>
<h3 dir="auto">Error message</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/scratch/gunter/miniconda3/envs/tmp/lib/python3.9/site-packages/scipy/interpolate/_rgi.py&quot;, line 331, in __call__
    indices, norm_distances = self._find_indices(xi.T)
  File &quot;/scratch/gunter/miniconda3/envs/tmp/lib/python3.9/site-packages/scipy/interpolate/_rgi.py&quot;, line 499, in _find_indices
    return find_indices(self.grid, xi)
  File &quot;_rgi_cython.pyx&quot;, line 95, in scipy.interpolate._rgi_cython.find_indices
  File &quot;stringsource&quot;, line 660, in View.MemoryView.memoryview_cwrapper
  File &quot;stringsource&quot;, line 350, in View.MemoryView.memoryview.__cinit__
ValueError: ndarray is not C-contiguous"><pre class="notranslate">Traceback (most recent call last):
  File <span class="pl-s"><span class="pl-pds">"</span>&lt;stdin&gt;<span class="pl-pds">"</span></span>, line 1, <span class="pl-k">in</span> <span class="pl-k">&lt;</span>module<span class="pl-k">&gt;</span>
  File <span class="pl-s"><span class="pl-pds">"</span>/scratch/gunter/miniconda3/envs/tmp/lib/python3.9/site-packages/scipy/interpolate/_rgi.py<span class="pl-pds">"</span></span>, line 331, <span class="pl-k">in</span> __call__
    indices, norm_distances = self._find_indices(xi.T)
  File <span class="pl-s"><span class="pl-pds">"</span>/scratch/gunter/miniconda3/envs/tmp/lib/python3.9/site-packages/scipy/interpolate/_rgi.py<span class="pl-pds">"</span></span>, line 499, <span class="pl-k">in</span> _find_indices
    <span class="pl-k">return</span> find_indices(self.grid, xi)
  File <span class="pl-s"><span class="pl-pds">"</span>_rgi_cython.pyx<span class="pl-pds">"</span></span>, line 95, <span class="pl-k">in</span> scipy.interpolate._rgi_cython.find_indices
  File <span class="pl-s"><span class="pl-pds">"</span>stringsource<span class="pl-pds">"</span></span>, line 660, <span class="pl-k">in</span> View.MemoryView.memoryview_cwrapper
  File <span class="pl-s"><span class="pl-pds">"</span>stringsource<span class="pl-pds">"</span></span>, line 350, <span class="pl-k">in</span> View.MemoryView.memoryview.__cinit__
ValueError: ndarray is not C-contiguous</pre></div>
<h3 dir="auto">SciPy/NumPy/Python version information</h3>
<p dir="auto">1.10.0 1.24.1 sys.version_info(major=3, minor=9, micro=15, releaselevel='final', serial=0)</p>