-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinpaint_nan3.py
More file actions
26 lines (23 loc) · 835 Bytes
/
inpaint_nan3.py
File metadata and controls
26 lines (23 loc) · 835 Bytes
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
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from scipy.io import loadmat
from scipy.interpolate import CubicSpline
import pdb
def inpaint_nans(image):
valid_mask = ~np.isnan(image)
bscans = np.where(~valid_mask[-1,:])[0]
for i in bscans:
inpaint = image[max(np.where(valid_mask[:,i])[0]),i]
image[-1,i] = inpaint
valid_mask[-1,i] = True
bscans = np.where(~valid_mask[0,:])[0]
for i in bscans:
inpaint = image[min(np.where(valid_mask[:,i])[0]),i]
image[0,i] = inpaint
valid_mask[0,i] = True
coords = np.array(np.nonzero(valid_mask)).T
values = image[valid_mask]
it = interpolate.LinearNDInterpolator(coords, values, fill_value=0)
filled = it(list(np.ndindex(image.shape))).reshape(image.shape)
return filled