Filling gaps in a numpy array

后端 未结 5 656
旧时难觅i
旧时难觅i 2020-12-05 01:39

I just want to interpolate, in the simplest possible terms, a 3D dataset. Linear interpolation, nearest neighbour, all that would suffice (this is to start off some algorith

5条回答
  •  独厮守ぢ
    2020-12-05 02:04

    You can set up a crystal-growth-style algorithm shifting a view alternately along each axis, replacing only data that is flagged with a False but has a True neighbor. This gives a "nearest-neighbor"-like result (but not in Euclidean or Manhattan distance -- I think it might be nearest-neighbor if you are counting pixels, counting all connecting pixels with common corners) This should be fairly efficient with NumPy as it iterates over only axis and convergence iterations, not small slices of the data.

    Crude, fast and stable. I think that's what you were after:

    import numpy as np
    # -- setup --
    shape = (10,10,10)
    dim = len(shape)
    data = np.random.random(shape)
    flag = np.zeros(shape, dtype=bool)
    t_ct = int(data.size/5)
    flag.flat[np.random.randint(0, flag.size, t_ct)] = True
    # True flags the data
    # -- end setup --
    
    slcs = [slice(None)]*dim
    
    while np.any(~flag): # as long as there are any False's in flag
        for i in range(dim): # do each axis
            # make slices to shift view one element along the axis
            slcs1 = slcs[:]
            slcs2 = slcs[:]
            slcs1[i] = slice(0, -1)
            slcs2[i] = slice(1, None)
    
            # replace from the right
            repmask = np.logical_and(~flag[slcs1], flag[slcs2])
            data[slcs1][repmask] = data[slcs2][repmask]
            flag[slcs1][repmask] = True
    
            # replace from the left
            repmask = np.logical_and(~flag[slcs2], flag[slcs1])
            data[slcs2][repmask] = data[slcs1][repmask]
            flag[slcs2][repmask] = True
    

    For good measure, here's a visualization (2D) of the zones seeded by the data originally flagged True.

    enter image description here

提交回复
热议问题