def interpolate_missing_data(Y):
"""
Interpolate any missing data using nearest neighbor interpolation.
Missing data is identified as entries with values NaN
Input:
Y np.ndarray (3D)
movie, raw data in 3D format (d1 x d2 x T)
Outputs:
Y np.ndarray (3D)
movie, data with interpolated entries (d1 x d2 x T)
coor list
list of interpolated coordinates
"""
coor=[];
if np.any(np.isnan(Y)):
raise Exception('The algorithm has not been tested with missing values (NaNs). Remove NaNs and rerun the algorithm.')
# need to
for idx,row in enumerate(Y):
nans=np.where(np.isnan(row))[0]
n_nans=np.where(~np.isnan(row))[0]
coor.append((idx,nans))
Y[idx,nans]=np.interp(nans, n_nans, row[n_nans])
# mis_data = np.isnan(Y)
# coor = mis_data.nonzero()
# ok_data = ~mis_data
# coor_ok = ok_data.nonzero()
# Yvals=[np.where(np.isnan(Y)) for row in Y]
#
# Yvals = griddata(coor_ok,Y[coor_ok],coor,method='nearest')
# un_t = np.unique(coor[-1])
# coorx = []
# coory = []
# Yvals = []
# for i, unv in enumerate(un_t):
# tm = np.where(coor[-1]==unv)
# coorx.append(coor[0][tm].tolist())
# coory.append(coor[1][tm].tolist())
# Yt = Y[:,:,unv]
# ok = ~np.isnan(Yt)
# coor_ok = ok.nonzero()
# ytemp = griddata(coor_ok,Yt[coor_ok],(coor[0][tm],coor[1][tm]),method='nearest')
# Yvals.append(ytemp)
return Y, coor
#%%
评论列表
文章目录