Пример #1
0
def replace_outliers(u,
                     v,
                     method='localmean',
                     max_iter=5,
                     tol=1e-3,
                     kernel_size=1):
    """Replace invalid vectors in an velocity field using an iterative image inpainting algorithm.
    
    The algorithm is the following:
    
    1) For each element in the arrays of the ``u`` and ``v`` components, replace it by a weighted average
       of the neighbouring elements which are not invalid themselves. The weights depends
       of the method type. If ``method=localmean`` weight are equal to 1/( (2*kernel_size+1)**2 -1 )
       
    2) Several iterations are needed if there are adjacent invalid elements.
       If this is the case, inforation is "spread" from the edges of the missing
       regions iteratively, until the variation is below a certain threshold. 
    
    Parameters
    ----------
    
    u : 2d np.ndarray
        the u velocity component field
        
    v : 2d np.ndarray
        the v velocity component field
        
    max_iter : int
        the number of iterations
    fil
    kernel_size : int
        the size of the kernel, default is 1
        
    method : str
        the type of kernel used for repairing missing vectors
        
    Returns
    -------
    uf : 2d np.ndarray
        the smoothed u velocity component field, where invalid vectors have been replaced
        
    vf : 2d np.ndarray
        the smoothed v velocity component field, where invalid vectors have been replaced    
        
    """
    uf = u.copy()
    vf = v.copy()
    uf = replace_nans(uf,
                      method=method,
                      max_iter=max_iter,
                      tol=tol,
                      kernel_size=kernel_size)
    vf = replace_nans(vf,
                      method=method,
                      max_iter=max_iter,
                      tol=tol,
                      kernel_size=kernel_size)

    return uf, vf
Пример #2
0
def test_replace_nans():
    """ test of NaNs inpainting """

    u = np.nan * np.ones((5, 5))
    u[2, 2] = 1
    u = replace_nans(u, 2, 1e-3)
    assert ~np.all(np.isnan(u))

    u = np.ones((9, 9))
    u[1:-1, 1:-1] = np.nan
    u = replace_nans(u, 1, 1e-3, method="disk")
    assert np.sum(np.isnan(u)) == 9  # central core is nan

    u = np.ones((9, 9))
    u[1:-1, 1:-1] = np.nan
    u = replace_nans(u, 2, 1e-3, method="disk")
    assert np.allclose(np.ones((9, 9)), u)
Пример #3
0
def replace_outliers( u, v, method='localmean', max_iter=5, tol=1e-3, kernel_size=1):
    """Replace invalid vectors in an velocity field using an iterative image inpainting algorithm.
    
    The algorithm is the following:
    
    1) For each element in the arrays of the ``u`` and ``v`` components, replace it by a weighted average
       of the neighbouring elements which are not invalid themselves. The weights depends
       of the method type. If ``method=localmean`` weight are equal to 1/( (2*kernel_size+1)**2 -1 )
       
    2) Several iterations are needed if there are adjacent invalid elements.
       If this is the case, inforation is "spread" from the edges of the missing
       regions iteratively, until the variation is below a certain threshold. 
    
    Parameters
    ----------
    
    u : 2d np.ndarray
        the u velocity component field
        
    v : 2d np.ndarray
        the v velocity component field
        
    max_iter : int
        the number of iterations
    fil
    kernel_size : int
        the size of the kernel, default is 1
        
    method : str
        the type of kernel used for repairing missing vectors
        
    Returns
    -------
    uf : 2d np.ndarray
        the smoothed u velocity component field, where invalid vectors have been replaced
        
    vf : 2d np.ndarray
        the smoothed v velocity component field, where invalid vectors have been replaced    
        
    """
    
    u = replace_nans( u, method=method, max_iter=max_iter, tol=tol, kernel_size=kernel_size )
    v = replace_nans( v, method=method, max_iter=max_iter, tol=tol, kernel_size=kernel_size )
    
    return u, v
Пример #4
0
def test_replace_nans():
    """ test of NaNs inpainting """
    u = np.nan * np.ones((5, 5))
    u[2, 2] = 1
    replace_nans(u, 2, 1e-3)
    assert (~np.all(np.isnan(u)))
    v = np.ones((9, 9))
    v[1:-1, 1:-1] = np.nan
    u = v.copy()
    replace_nans(u, 1, 1e-3, method='disk')
    assert (np.sum(np.isnan(u)) == 9)  # central core is nan
    u = v.copy()
    replace_nans(u, 2, 1e-3, method='disk')
    assert (np.allclose(np.ones((9, 9)), u))
Пример #5
0
# ball shape with a gap of nans in the middle
center = (5, 5, 5)
size = (10, 10, 10)
distance = np.linalg.norm(np.subtract(np.indices(size).T, np.asarray(center)),
                          axis=len(center))
arr = np.ones(size) * (distance <= 5)
hide = arr == 0
arr[5:7] = np.nan

# %%
# displaying in 3d plots. Values outside of the original ball are hidden by setting to nan
arr_show = arr.copy()
arr_show[hide] = np.nan
fig9 = scatter_3D(arr_show, size=50, sca_args={"alpha": 0.6})
# replacing outliers
arr = replace_nans(arr, max_iter=2, tol=2, kernel_size=2, method='disk')

# %%
# displaying in 3d plots. Values outside of the original ball are hidden by setting to nan
arr_show = arr.copy()
arr_show[hide] = np.nan
fig10 = scatter_3D(arr_show, size=50, sca_args={"alpha": 0.6})

# %%
# saving the plots
if save_plots:
    fig9.savefig(os.path.join(out_put_folder, "replace_nan_gap.png"))
    fig10.savefig(os.path.join(out_put_folder, "replace_nan_filled.png"))

# %% [markdown]
# #################### real data example ############################