大型3D阵列上的快速1D线性np.NaN插值

时间:2015-06-18 09:02:42

标签: python numpy pandas scipy interpolation

我有一个带有(z, y, x)的3D数组shape=(92, 4800, 4800),其中axis 0的每个值代表不同的时间点。在少数情况下,时域中值的获取失败,导致某些值为np.NaN。在其他情况下,未获取任何值,并且z中的所有值均为np.NaN

使用线性插值来填充np.NaN axis 0以及忽略所有值为np.NaN的实例的最有效方法是什么?

以下是我使用pandas包装到scipy.interpolate.interp1d所做的工作示例。在原始数据集上每个切片大约需要2秒,这意味着整个数组在2.6小时内处理完毕。缩小大小的示例数据集大约需要9.5秒。

import numpy as np
import pandas as pd

# create example data, original is (92, 4800, 4800)
test_arr = np.random.randint(low=-10000, high=10000, size=(92, 480, 480))
test_arr[1:90:7, :, :] = -32768  # NaN fill value in original data
test_arr[:, 1:90:6, 1:90:8] = -32768

def interpolate_nan(arr, method="linear", limit=3):
    """return array interpolated along time-axis to fill missing values"""
    result = np.zeros_like(arr, dtype=np.int16)

    for i in range(arr.shape[1]):
        # slice along y axis, interpolate with pandas wrapper to interp1d
        line_stack = pd.DataFrame(data=arr[:,i,:], dtype=np.float32)
        line_stack.replace(to_replace=-37268, value=np.NaN, inplace=True)
        line_stack.interpolate(method=method, axis=0, inplace=True, limit=limit)
        line_stack.replace(to_replace=np.NaN, value=-37268, inplace=True)
        result[:, i, :] = line_stack.values.astype(np.int16)
    return result

使用示例数据集在我的机器上的性能:

%timeit interpolate_nan(test_arr)
1 loops, best of 3: 9.51 s per loop

编辑:

我应该澄清代码正在产生我预期的结果。问题是 - 我该如何优化这个过程?

3 个答案:

答案 0 :(得分:3)

我最近在 numba 的帮助下针对我的特定用例解决了这个问题,并且还a little writeup on it

from numba import jit

@jit(nopython=True)
def interpolate_numba(arr, no_data=-32768):
    """return array interpolated along time-axis to fill missing values"""
    result = np.zeros_like(arr, dtype=np.int16)

    for x in range(arr.shape[2]):
        # slice along x axis
        for y in range(arr.shape[1]):
            # slice along y axis
            for z in range(arr.shape[0]):
                value = arr[z,y,x]
                if z == 0:  # don't interpolate first value
                    new_value = value
                elif z == len(arr[:,0,0])-1:  # don't interpolate last value
                    new_value = value

                elif value == no_data:  # interpolate

                    left = arr[z-1,y,x]
                    right = arr[z+1,y,x]
                    # look for valid neighbours
                    if left != no_data and right != no_data:  # left and right are valid
                        new_value = (left + right) / 2

                    elif left == no_data and z == 1:  # boundary condition left
                        new_value = value
                    elif right == no_data and z == len(arr[:,0,0])-2:  # boundary condition right
                        new_value = value

                    elif left == no_data and right != no_data:  # take second neighbour to the left
                        more_left = arr[z-2,y,x]
                        if more_left == no_data:
                            new_value = value
                        else:
                            new_value = (more_left + right) / 2

                    elif left != no_data and right == no_data:  # take second neighbour to the right
                        more_right = arr[z+2,y,x]
                        if more_right == no_data:
                            new_value = value
                        else:
                            new_value = (more_right + left) / 2

                    elif left == no_data and right == no_data:  # take second neighbour on both sides
                        more_left = arr[z-2,y,x]
                        more_right = arr[z+2,y,x]
                        if more_left != no_data and more_right != no_data:
                            new_value = (more_left + more_right) / 2
                        else:
                            new_value = value
                    else:
                        new_value = value
                else:
                    new_value = value
                result[z,y,x] = int(new_value)
    return result

这比我的初始代码快20倍

答案 1 :(得分:2)

这取决于;你必须拿出一张纸并计算你的整体统计数据如果你没有进行插值而只是零填充这些 NaN 的错误。< / p>

除此之外,我认为你的插值超过顶部。 只需找到每个NaN,并线性插值到相邻的四个值(即,将(y + - 1,x + - 1)处的值相加) - 这将严重限制您的误差(自己计算!),以及您没有使用您的案例中使用的任何复杂方法进行插值(您没有定义packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' } )。

您可以尝试预先计算一个&#34;平均值&#34;每z值4800x4800矩阵 - 这不应该花费很长时间 - 通过在矩阵上应用十字形内核(这里所有非常像图像处理)。在NaN的情况下,一些平均值将是NaN(NaN在邻域中的每个平均像素),但是你不在乎 - 除非有两个相邻的NaN,NaN细胞你想在原始矩阵中替换的都是实值的。

然后您只需用平均矩阵中的值替换所有NaN。

将速度与&#34;手册&#34;的速度进行比较。计算每个NaN的邻域平均值。

答案 2 :(得分:1)

发问者利用numba给了很好的答案。我真的很感激,但是我不能完全同意interpolate_numba函数中的内容。我认为在特定点上进行线性插值的逻辑不是要找到其左,右邻居的平均值。为了举例说明,假设我们有一个数组[1,nan,nan,4,nan,6],上面的interpolate_numba函数可能会返回[1,2.5,2.5,4,5,6](仅理论上推论),而pandas包装器肯定会返回[1,2,3,4,5,6]。相反,我认为在特定点上进行线性插值的逻辑是找到其左和右邻居,使用它们的值确定一条线(即斜率和截距),最后计算出插值。下面显示了我的代码。为了简化起见,我假设输入数据是一个包含nan值的3-D数组。我规定第一个元素和最后一个元素与它们的最右边和最左边的邻居等效(即limit_direction='both'中的pandas)。我没有指定连续插值的最大数量(即limit中没有pandas)。

import numpy as np
from numba import jit
@jit(nopython=True)
def f(arr_3d):
    result=np.zeros_like(arr_3d)
    for i in range(arr_3d.shape[1]):
        for j in range(arr_3d.shape[2]):
            arr=arr_3d[:,i,j]
            # If all elements are nan then cannot conduct linear interpolation.
            if np.sum(np.isnan(arr))==arr.shape[0]:
                result[:,i,j]=arr
            else:
                # If the first elemet is nan, then assign the value of its right nearest neighbor to it.
                if np.isnan(arr[0]):
                    arr[0]=arr[~np.isnan(arr)][0]
                # If the last element is nan, then assign the value of its left nearest neighbor to it.
                if np.isnan(arr[-1]):
                    arr[-1]=arr[~np.isnan(arr)][-1]
                # If the element is in the middle and its value is nan, do linear interpolation using neighbor values.
                for k in range(arr.shape[0]):
                    if np.isnan(arr[k]):
                        x=k
                        x1=x-1
                        x2=x+1
                        # Find left neighbor whose value is not nan.
                        while x1>=0:
                            if np.isnan(arr[x1]):
                                x1=x1-1
                            else:
                                y1=arr[x1]
                                break
                        # Find right neighbor whose value is not nan.
                        while x2<arr.shape[0]:
                            if np.isnan(arr[x2]):
                                x2=x2+1
                            else:
                                y2=arr[x2]
                                break
                        # Calculate the slope and intercept determined by the left and right neighbors.
                        slope=(y2-y1)/(x2-x1)
                        intercept=y1-slope*x1
                        # Linear interpolation and assignment.
                        y=slope*x+intercept
                        arr[x]=y
                result[:,i,j]=arr
    return result

初始化一个包含一些Nan的3-D数组,我检查了我的代码,该代码可以提供与pandas包装程序相同的答案。通过pandas包装程序代码会有点混乱,因为熊猫只能处理二维数据。

使用我的代码

y1=np.ones((2,2))
y2=y1+1
y3=y2+np.nan
y4=y2+2
y5=y1+np.nan
y6=y4+2
y1[1,1]=np.nan
y2[0,0]=np.nan
y4[1,1]=np.nan
y6[1,1]=np.nan
y=np.stack((y1,y2,y3,y4,y5,y6),axis=0)
print(y)
print("="*10)
f(y)

使用熊猫包装纸

import pandas as pd
y1=np.ones((2,2)).flatten()
y2=y1+1
y3=y2+np.nan
y4=y2+2
y5=y1+np.nan
y6=y4+2
y1[3]=np.nan
y2[0]=np.nan
y4[3]=np.nan
y6[3]=np.nan
y=pd.DataFrame(np.stack([y1,y2,y3,y4,y5,y6],axis=0))
y=y.interpolate(method='linear', limit_direction='both', axis=0)
y_numpy=y.to_numpy()
y_numpy.shape=((6,2,2))
print(np.stack([y1,y2,y3,y4,y5,y6],axis=0).reshape(6,2,2))
print("="*10)
print(y_numpy)

输出将相同

[[[ 1.  1.]
  [ 1. nan]]

 [[nan  2.]
  [ 2.  2.]]

 [[nan nan]
  [nan nan]]

 [[ 4.  4.]
  [ 4. nan]]

 [[nan nan]
  [nan nan]]

 [[ 6.  6.]
  [ 6. nan]]]
==========
[[[1. 1.]
  [1. 2.]]

 [[2. 2.]
  [2. 2.]]

 [[3. 3.]
  [3. 2.]]

 [[4. 4.]
  [4. 2.]]

 [[5. 5.]
  [5. 2.]]

 [[6. 6.]
  [6. 2.]]]

使用test_arr数据将其大小增加到(92,4800,4800)作为输入,我发现只需大约40 s即可完成插值!

test_arr = np.random.randint(low=-10000, high=10000, size=(92, 4800, 4800))
test_arr[1:90:7, :, :] = np.nan  # NaN fill value in original data
test_arr[2,:,:] = np.nan
test_arr[:, 1:479:6, 1:479:8] = np.nan
%time f(test_arr)

输出

CPU times: user 32.5 s, sys: 9.13 s, total: 41.6 s
Wall time: 41.6 s