22
33
44def replace_nans (array , max_iter , tol , kernel_size = 2 , method = "disk" ):
5-
65 """Replace NaN elements in an array using an iterative image inpainting
76 algorithm.
87
@@ -18,26 +17,15 @@ def replace_nans(array, max_iter, tol, kernel_size=2, method="disk"):
1817 threshold.
1918
2019 Methods:
21-
22- localmean - A square kernel where all elements have the same value,
23- weights are equal to n/( (2*kernel_size+1)**2 -1 ),
24- where n is the number of non-NaN elements.
25- disk - A circular kernel where all elements have the same value,
26- kernel is calculated by::
27- if ((S-i)**2 + (S-j)**2)**0.5 <= S:
28- kernel[i,j] = 1.0
29- else:
30- kernel[i,j] = 0.0
31- where S is the kernel radius.
32- distance - A circular inverse distance kernel where elements are
33- weighted proportional to their distance away from the
34- center of the kernel, elements farther away have less
35- weight. Elements outside the specified radius are set
36- to 0.0 as in 'disk', the remaining of the weights are
37- calculated as::
38- maxDist = ((S)**2 + (S)**2)**0.5
39- kernel[i,j] = -1*(((S-i)**2 + (S-j)**2)**0.5 - maxDist)
40- where S is the kernel radius.
20+ localmean - A square kernel where all elements have the same weight.
21+ disk - A circular kernel where all elements have the same weight.
22+ distance - A circular kernel where the weight of each element depends
23+ on its distance from the center of the kernel. The weights
24+ are given by a function of the form:
25+ w_i = 1 - (d_i / d_max)^2
26+ where d_i is the distance from the center, and d_max is the
27+ distance of the element farthest from the center.
28+ This method requires SciPy.
4129
4230 Parameters
4331 ----------
@@ -68,6 +56,9 @@ def replace_nans(array, max_iter, tol, kernel_size=2, method="disk"):
6856 a copy of the input array, where NaN elements have been replaced.
6957
7058 """
59+ # Check if there are any NaNs to replace
60+ if not np .any (np .isnan (array )):
61+ return array .copy ()
7162
7263 kernel_size = int (kernel_size )
7364 filled = array .copy ()
0 commit comments