2016-12-16 29 views
3

我有一些数据,我尝试插入使用scipy.interpolate.griddata。在我的使用情况我打上一些,只读的numpy的阵列,这显然打破了插值:为什么`scipy.interpolate.griddata`为只读数组失败?

import numpy as np 
from scipy import interpolate 

x0 = 10 * np.random.randn(100, 2) 
y0 = np.random.randn(100) 
x1 = np.random.randn(3, 2) 

x0.flags.writeable = False 
# x1.flags.writeable = False 

interpolate.griddata(x0, y0, x1) 

产生以下异常:

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-14-a6e09dbdd371> in <module>() 
     6 # x1.flags.writeable = False 
     7 
----> 8 interpolate.griddata(x0, y0, x1) 

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/interpolate/ndgriddata.pyc in griddata(points, values, xi, method, fill_value, rescale) 
    216   ip = LinearNDInterpolator(points, values, fill_value=fill_value, 
    217         rescale=rescale) 
--> 218   return ip(xi) 
    219  elif method == 'cubic' and ndim == 2: 
    220   ip = CloughTocher2DInterpolator(points, values, fill_value=fill_value, 

scipy/interpolate/interpnd.pyx in scipy.interpolate.interpnd.NDInterpolatorBase.__call__ (scipy/interpolate/interpnd.c:3930)() 

scipy/interpolate/interpnd.pyx in scipy.interpolate.interpnd.LinearNDInterpolator._evaluate_double (scipy/interpolate/interpnd.c:5267)() 

scipy/interpolate/interpnd.pyx in scipy.interpolate.interpnd.LinearNDInterpolator._do_evaluate (scipy/interpolate/interpnd.c:6006)() 

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/interpolate/interpnd.so in View.MemoryView.memoryview_cwrapper (scipy/interpolate/interpnd.c:17829)() 

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/interpolate/interpnd.so in View.MemoryView.memoryview.__cinit__ (scipy/interpolate/interpnd.c:14104)() 

ValueError: buffer source array is read-only 

显然,插值功能不喜欢数组是写保护的。但是,我不明白他们为什么要改变这一点 - 我当然不希望我的输入被插值函数的调用所突变,这一点在文档中也没有提及,据我所知。为什么函数的行为如此?

请注意,设置x1只读而不是x0会导致类似的错误。

回答

2

relevant code是用Cython编写的,当Cython请求输入数组的内存视图it always asks for a writeable one时,即使您不需要它。

由于被标记为不可写入的数组将拒绝提供可写入的内存视图,因此即使代码不需要先写入数组,也会失败。

+0

我看到了 - 这解释了错误,但我仍然认为这是一个scipy中的错误,不能自己创建副本,但会抛出错误。最终,cython可能应该改为允许只读内存视图。我为scipy打开了一个bug报告(https://github.com/scipy/scipy/issues/6864)。让我们看看那些人在那里说什么 –