python中的2d卷积,缺少数据

时间:2016-07-12 00:51:17

标签: python numpy scipy convolution

我知道有scipy.signal.convolve2d函数来处理2d numpy数组的2维卷积,并且有numpy.ma模块来处理丢失的数据,但这两种方法似乎并不相互兼容(其中意味着即使你在numpy中屏蔽了一个2d数组,convolve2d中的进程也不会受到影响)。有没有办法只使用numpy和scipy包来处理卷积中的缺失值?

例如:

@func_f

卷积的期望结果(数组,内核,边界='换行'):

            1 - 3 4 5
            1 2 - 4 5
   Array =  1 2 3 - 5
            - 2 3 4 5
            1 2 3 4 -

  Kernel =  1  0
            0 -1

感谢Aguy的建议,这是一个非常好的方法来帮助计算卷积后的结果。现在假设我们可以从Array.mask获取Array的掩码,这将给我们一个

的结果
               -1  - -1 -1 4
               -1 -1  - -1 4
    Result =   -1 -1 -1  - 5
                - -1 -1  4 4
                1 -1 -1 -1 -

如何使用此蒙版将卷积后的结果转换为蒙版数组?

2 个答案:

答案 0 :(得分:3)

我不认为用0代替是正确的方法,你将这些共同的值推向0.这些缺失应该被视为"缺失"。因为它们代表了缺失的信息,并且没有理由假设它们可能是0,并且它们根本不应该参与任何计算。

我尝试将缺失值设置为numpy.nan然后进行卷积,结果表明内核与任何缺失之间的任何重叠都会在结果中产生nan,即使重叠为0 in内核,所以你在结果中得到了一个扩大的漏洞。根据您的应用,这可能是理想的结果。

但是在某些情况下,你不想丢弃这么多信息只是为了一次失踪(可能<= 50%的失踪仍然是可以容忍的)。在这种情况下,我发现另一个模块 astropy 具有更好的实现:numpy.nan被忽略(或用插值替换?)。

因此,使用astropy,您将执行以下操作:

from astropy.convolution import convolve
inarray=numpy.where(inarray.mask,numpy.nan,inarray) # masking still doesn't work, has to set to numpy.nan
result=convolve(inarray,kernel)

但是,你仍然无法控制多少失踪是可以容忍的。为实现这一目标,我创建了一个使用scipy.ndimage.convolve()进行初始卷积的函数,但只要涉及缺失(numpy.nan),就会手动重新计算值:

def convolve2d(slab,kernel,max_missing=0.5,verbose=True):
    '''2D convolution with missings ignored

    <slab>: 2d array. Input array to convolve. Can have numpy.nan or masked values.
    <kernel>: 2d array, convolution kernel, must have sizes as odd numbers.
    <max_missing>: float in (0,1), max percentage of missing in each convolution
                   window is tolerated before a missing is placed in the result.

    Return <result>: 2d array, convolution result. Missings are represented as
                     numpy.nans if they are in <slab>, or masked if they are masked
                     in <slab>.

    '''

    from scipy.ndimage import convolve as sciconvolve

    assert numpy.ndim(slab)==2, "<slab> needs to be 2D."
    assert numpy.ndim(kernel)==2, "<kernel> needs to be 2D."
    assert kernel.shape[0]%2==1 and kernel.shape[1]%2==1, "<kernel> shape needs to be an odd number."
    assert max_missing > 0 and max_missing < 1, "<max_missing> needs to be a float in (0,1)."

    #--------------Get mask for missings--------------
    if not hasattr(slab,'mask') and numpy.any(numpy.isnan(slab))==False:
        has_missing=False
        slab2=slab.copy()

    elif not hasattr(slab,'mask') and numpy.any(numpy.isnan(slab)):
        has_missing=True
        slabmask=numpy.where(numpy.isnan(slab),1,0)
        slab2=slab.copy()
        missing_as='nan'

    elif (slab.mask.size==1 and slab.mask==False) or numpy.any(slab.mask)==False:
        has_missing=False
        slab2=slab.copy()

    elif not (slab.mask.size==1 and slab.mask==False) and numpy.any(slab.mask):
        has_missing=True
        slabmask=numpy.where(slab.mask,1,0)
        slab2=numpy.where(slabmask==1,numpy.nan,slab)
        missing_as='mask'

    else:
        has_missing=False
        slab2=slab.copy()

    #--------------------No missing--------------------
    if not has_missing:
        result=sciconvolve(slab2,kernel,mode='constant',cval=0.)
    else:
        H,W=slab.shape
        hh=int((kernel.shape[0]-1)/2)  # half height
        hw=int((kernel.shape[1]-1)/2)  # half width
        min_valid=(1-max_missing)*kernel.shape[0]*kernel.shape[1]

        # dont forget to flip the kernel
        kernel_flip=kernel[::-1,::-1]

        result=sciconvolve(slab2,kernel,mode='constant',cval=0.)
        slab2=numpy.where(slabmask==1,0,slab2)

        #------------------Get nan holes------------------
        miss_idx=zip(*numpy.where(slabmask==1))

        if missing_as=='mask':
            mask=numpy.zeros([H,W])

        for yii,xii in miss_idx:

            #-------Recompute at each new nan in result-------
            hole_ys=range(max(0,yii-hh),min(H,yii+hh+1))
            hole_xs=range(max(0,xii-hw),min(W,xii+hw+1))

            for hi in hole_ys:
                for hj in hole_xs:
                    hi1=max(0,hi-hh)
                    hi2=min(H,hi+hh+1)
                    hj1=max(0,hj-hw)
                    hj2=min(W,hj+hw+1)

                    slab_window=slab2[hi1:hi2,hj1:hj2]
                    mask_window=slabmask[hi1:hi2,hj1:hj2]
                    kernel_ij=kernel_flip[max(0,hh-hi):min(hh*2+1,hh+H-hi), 
                                     max(0,hw-hj):min(hw*2+1,hw+W-hj)]
                    kernel_ij=numpy.where(mask_window==1,0,kernel_ij)

                    #----Fill with missing if not enough valid data----
                    ksum=numpy.sum(kernel_ij)
                    if ksum<min_valid:
                        if missing_as=='nan':
                            result[hi,hj]=numpy.nan
                        elif missing_as=='mask':
                            result[hi,hj]=0.
                            mask[hi,hj]=True
                    else:
                        result[hi,hj]=numpy.sum(slab_window*kernel_ij)

        if missing_as=='mask':
            result=numpy.ma.array(result)
            result.mask=mask

    return result

下面是一个展示输出的图。左边是一个30x30的随机地图,其中有3个numpy.nan个洞,大小为:

  1. 的1x1
  2. 3×3
  3. 5×5
  4. enter image description here

    右边是卷积输出,5x5内核(全1),容差水平为50%(max_missing=0.5)。

    所以前两个较小的孔用附近的值填充,而在最后一个,因为缺失的数量&gt; 0.5x5x5 = 12.5numpy.nan用于表示缺失的信息。

答案 1 :(得分:3)

我发现了一个黑客。而不是 nan 使用虚数(它是 nan 将其更改为 1i)运行卷积并将其设置为虚数值高于阈值的任何地方,它是 nan。每当它是波纹管时就取真正的价值。这是一个代码片段:

frames_complex = np.zeros_like(frames_, dtype=np.complex64)
frames_complex[np.isnan(frames_)] = np.array((1j))
frames_complex[np.bitwise_not(np.isnan(frames_))] =                         
frames_[np.bitwise_not(np.isnan(frames_))]
convolution = signal.convolve(frames_complex, gaussian_window, 'valid')
convolution[np.imag(convolution)>0.2] = np.nan
convolution = convolution.astype(np.float32)