/
inpaint.jl
270 lines (227 loc) · 8.41 KB
/
inpaint.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
inpaint(A)
Inpaints `missing` values if `A` contains some.
Inspired by MATLAB's `inpaint_nans`'s (by John d'Errico: [link](https://www.mathworks.com/matlabcentral/fileexchange/4551-inpaint_nans)).
# Example
```jldoctest
julia> A = convert(Array{Union{Float64, Missing},2}, (1:3)*(1:4)') ; A[1:2, 1:2] .= missing ; A
3×4 Array{Union{Missing, Float64},2}:
missing missing 3.0 4.0
missing missing 6.0 8.0
3.0 6.0 9.0 12.0
julia> inpaint(A)
3×4 Array{Union{Missing, Float64},2}:
1.0 2.0 3.0 4.0
2.0 4.0 6.0 8.0
3.0 6.0 9.0 12.0
```
"""
inpaint(A::AbstractArray{Union{Missing, T}}; method=1, cycledims=Int64[]) where {T<:AbstractFloat} = inpaint(ismissing, A, method=method, cycledims=cycledims)
"""
inpaint(A)
Inpaints `NaN` values if the elements of `A` are all float (i.e., `eltype(A) <: AbstractFloat`).
# Example
```jldoctest
julia> A = float((1:3)*(1:4)') ; A[1:2, 1:2] .= NaN ; A
3×4 Array{Float64,2}:
NaN NaN 3.0 4.0
NaN NaN 6.0 8.0
3.0 6.0 9.0 12.0
julia> inpaint(A)
3×4 Array{Float64,2}:
1.0 2.0 3.0 4.0
2.0 4.0 6.0 8.0
3.0 6.0 9.0 12.0
```
"""
inpaint(A::AbstractArray{T}; method=1, cycledims=Int64[]) where {T<:AbstractFloat} = inpaint(isnan, A, method=method, cycledims=cycledims)
"""
inpaint(A, missing)
Inpaints `missing` values.
Should be the same as `inpaint(A)`.
"""
inpaint(A::AbstractArray, ::Missing; method=1, cycledims=Int64[]) = inpaint(ismissing, A, method=method, cycledims=cycledims)
"""
inpaint(A, value_to_fill)
Inpaints `value_to_fill` values (can be `NaN`).
Useful in the case data was generated by filling missing values with an
arbitrary chosen one.
# Examples
```jldoctest
julia> A = float((1:3)*(1:4)') ; A[1:2, 1:2] .= 999 ; A
3×4 Array{Float64,2}:
999.0 999.0 3.0 4.0
999.0 999.0 6.0 8.0
3.0 6.0 9.0 12.0
julia> inpaint(A, 999)
3×4 Array{Float64,2}:
1.0 2.0 3.0 4.0
2.0 4.0 6.0 8.0
3.0 6.0 9.0 12.0
```
```jldoctest
julia> A = float((1:3)*(1:4)') ; A[1:2, 1:2] .= NaN ; A
3×4 Array{Float64,2}:
NaN NaN 3.0 4.0
NaN NaN 6.0 8.0
3.0 6.0 9.0 12.0
julia> inpaint(A, NaN)
3×4 Array{Float64,2}:
1.0 2.0 3.0 4.0
2.0 4.0 6.0 8.0
3.0 6.0 9.0 12.0
```
"""
function inpaint(A::AbstractArray{T}, value_to_fill; method=1, cycledims=Int64[]) where T<:AbstractFloat
return if isnan(value_to_fill)
inpaint(isnan, A, method=method, cycledims=cycledims)
else
inpaint(x -> x == value_to_fill, A, method=method, cycledims=cycledims)
end
end
"""
inpaint(f, A)
Inpaints values of `A` for which `f(A) == true`.
# Examples
```jldoctest
julia> A = float((1:3)*(1:4)') ; A[1:2, 1:2] .= 999 ; A
3×4 Array{Float64,2}:
999.0 999.0 3.0 4.0
999.0 999.0 6.0 8.0
3.0 6.0 9.0 12.0
julia> inpaint(x -> x == 999, A)
3×4 Array{Float64,2}:
1.0 2.0 3.0 4.0
2.0 4.0 6.0 8.0
3.0 6.0 9.0 12.0
```
```jldoctest
julia> A = float((1:3)*(1:4)') ; A[1:2, [1, end]] .= NaN ; A
3×4 Array{Float64,2}:
NaN 2.0 3.0 NaN
NaN 4.0 6.0 NaN
3.0 6.0 9.0 12.0
julia> inpaint(isnan, A)
3×4 Array{Float64,2}:
1.0 2.0 3.0 4.0
2.0 4.0 6.0 8.0
3.0 6.0 9.0 12.0
```
inpaint(f, A; method=1, cycledims=[])
With optional arguments, you can chose the inpainting method and if dimensions are cyclic.
```jldoctest
julia> A = float((1:3)*(1:4)') ; A[1:2, [1, end]] .= NaN ; A
3×4 Array{Float64,2}:
NaN 2.0 3.0 NaN
NaN 4.0 6.0 NaN
3.0 6.0 9.0 12.0
julia> inpaint(A, cycledims=[2])
3×4 Array{Float64,2}:
2.16475 2.0 3.0 2.83525
3.76245 4.0 6.0 6.23755
3.0 6.0 9.0 12.0
```
"""
function inpaint(f, A; method=1, cycledims=Int64[])
return @match method begin
1 => inpaint_method1(f, A, cycledims=cycledims) # Default
_ => error("Method $method not available yet. Suggest it on Github!")
end
end
"""
list_neighbors(A, idx, neighbors)
Lists all the neighbors of the indices in `idx` in Array `A`.
Neighbors already in `idx` are accepted.
The argument `neighnors` contains a list of the neighbors about the
origin coordinate `(0, 0, ...)`.
In other words, it is a `Vector` of `CartesianIndex` such that
the direct neighbors of index `i` are given by `i + n for n in neighbors`.
Inspired by MATLAB's `inpaint_nans`'s `identify_neighbors` (by John d'Errico).
See https://www.mathworks.com/matlabcentral/fileexchange/4551-inpaint_nans.
"""
function list_neighbors(R, idx, neighbors)
out = [i + n for i in idx for n in neighbors if i + n ∈ R]
out = sort(unique([out; idx]))
end
list_neighbors(R, idx::Vector{Int64}, neighbors) = list_neighbors(R, CartesianIndex.(idx), neighbors)
"""
inpaint_method1(f, A::Array, cycledims=Int64[])
Inpaints values in `A` that `f` gives `true` on by solving a simple diffusion PDE.
Default method for `inpaint`.
The partial differential equation (PDE) is defined by the standard Laplacian, `Δ = ∇^2`.
Inspired by MATLAB's `inpaint_nans`'s method `0` for matrices (by John d'Errico).
See https://www.mathworks.com/matlabcentral/fileexchange/4551-inpaint_nans.
The discrete 1D stencil used for `Δ` looks like
```
┌───┐ ┌───┐ ┌───┐
│ 1 ├─┤-2 ├─┤ 1 │
└───┘ └───┘ └───┘
```
and is applied where it fits at the borders in all dimensions.
However, the user can supply a list of dimensions that should be considered cyclic.
In this case, the sentil will be used also at the borders and "jump" to the other side.
This is particularly useful for, e.g., world maps with longitudes spanning the entire globe.
"""
function inpaint_method1(f, A::Array; cycledims=Int64[])
I_unknown = findall(@. f(A))
I_known = findall(@. !f(A))
# direct neighbors
Ns = [CartesianIndex(ntuple(x -> x == d ? 1 : 0, ndims(A))) for d in 1:ndims(A)]
neighbors = [Ns; -Ns]
# Add neighbors on opposite border if dimensions are cyclic
N′s = [CartesianIndex(ntuple(x -> x == d ? size(A,d) - 1 : 0, ndims(A))) for d in 1:ndims(A)]
for d in cycledims # Add neighbors in cyclic case
neighbors = push!(neighbors, N′s[d], -N′s[d])
end
# "working" list: the indices of all unkowns and their neighbors
R = CartesianIndices(size(A))
Iwork = list_neighbors(R, I_unknown, neighbors)
iwork = LinearIndices(size(A))[Iwork]
# Preallocate the Laplacian (not the fastest way but easier-to-read code)
nA = length(A)
Δ = sparse([], [], Vector{Float64}(), nA, nA)
# Fill the Laplacian within the borders
for d in 1:ndims(A)
N = Ns[d]
# R′ is the range of indices without the borders in dimension `d`
R′ = CartesianIndices(size(A) .- 2 .* N.I)
R′ = [r + N for r in R′]
# Convert to linear indices to build the Laplacian
u = vec(LinearIndices(size(A))[R′])
n = LinearIndices(size(A))[first(R) + N] - LinearIndices(size(A))[first(R)]
# Build the Laplacian (not the fastest way but easier-to-read code)
Δ += sparse(u, u , -2.0, nA, nA)
Δ += sparse(u, u .- n, +1.0, nA, nA)
Δ += sparse(u, u .+ n, +1.0, nA, nA)
end
# Add Laplacian along border if cyclic along dimension `d`
for d in cycledims
N = Ns[d] # usual neighbor
N′ = N′s[d] # neighbor on the opposite border
R′₊ = [r for r in R if r + N ∉ R] # one border
R′₋ = [r for r in R if r - N ∉ R] # the opposite border
# Convert to linear indices to build the Laplacian
n = LinearIndices(size(A))[first(R) + N] - LinearIndices(size(A))[first(R)]
n′ = LinearIndices(size(A))[first(R) + N′] - LinearIndices(size(A))[first(R)]
u₊ = LinearIndices(size(A))[R′₊]
u₋ = LinearIndices(size(A))[R′₋]
# Add to the Laplacian
Δ += sparse(u₊, u₊ , -2.0, nA, nA)
Δ += sparse(u₊, u₊ .- n , +1.0, nA, nA)
Δ += sparse(u₊, u₊ .- n′, +1.0, nA, nA)
Δ += sparse(u₋, u₋ , -2.0, nA, nA)
Δ += sparse(u₋, u₋ .+ n , +1.0, nA, nA)
Δ += sparse(u₋, u₋ .+ n′, +1.0, nA, nA)
end
# Use linear indices to access the sparse Laplacian
i_unknown = LinearIndices(size(A))[I_unknown]
i_known = LinearIndices(size(A))[I_known]
# Place the knowns to right hand side
rhs = -Δ[iwork, i_known] * A[i_known]
# Solve for the unknowns
B = zeros(size(A))
B[i_known] .= A[i_known]
B[i_unknown] .= Δ[iwork, i_unknown] \ rhs
return B
end
export inpaint