openpiv.lib.replace_nans

openpiv.lib.replace_nans(array, max_iter, tol, kernel_size=2, method='disk')[source]
Replace NaN elements in an array using an iterative image inpainting
algorithm.

The algorithm is the following:

  1. For each element in the input array, replace it by a weighted average of the neighbouring elements which are not NaN themselves. The weights depend on the method type. See Methods below.
  2. Several iterations are needed if there are adjacent NaN elements. If this is the case, information is “spread” from the edges of the missing regions iteratively, until the variation is below a certain threshold.

Methods:

localmean - A square kernel where all elements have the same value,
weights are equal to n/( (2*kernel_size+1)**2 -1 ), where n is the number of non-NaN elements.
disk - A circular kernel where all elements have the same value,
kernel is calculated by::
if ((S-i)**2 + (S-j)**2)**0.5 <= S:
kernel[i,j] = 1.0
else:
kernel[i,j] = 0.0

where S is the kernel radius.

distance - A circular inverse distance kernel where elements are

weighted proportional to their distance away from the center of the kernel, elements farther away have less weight. Elements outside the specified radius are set to 0.0 as in ‘disk’, the remaining of the weights are calculated as:

maxDist = ((S)**2 + (S)**2)**0.5
kernel[i,j] = -1*(((S-i)**2 + (S-j)**2)**0.5 - maxDist)

where S is the kernel radius.

Parameters:
  • array (2d or 3d np.ndarray) – an array containing NaN elements that have to be replaced if array is a masked array (numpy.ma.MaskedArray), then the mask is reapplied after the replacement
  • max_iter (int) – the number of iterations
  • tol (float) – On each iteration check if the mean square difference between values of replaced elements is below a certain tolerance tol
  • kernel_size (int) – the size of the kernel, default is 1
  • method (str) – the method used to replace invalid values. Valid options are localmean, disk, and distance.
Returns:

filled – a copy of the input array, where NaN elements have been replaced.

Return type:

2d or 3d np.ndarray