Scipy interpolation with masked data?

后端 未结 3 1975
忘了有多久
忘了有多久 2021-01-03 02:35

I am trying to interpolate a 2D array that contents masked data. I have used some of the SciPy module\'s methods available, including interp2d, bisplrep/b

3条回答
  •  无人及你
    2021-01-03 02:47

    The problem with the approaches outlined by @MSeifert is that the regular grid structure is lost, resulting in inefficient interpolation. It is justified only to fill in missing data by interpolation, but not for typical interpolation from one grid to another, where missing data should not be filled in.

    In this case, filling missing values with np.nan is the simplest approach. These will be propagated in the calculations and the resulting array will have nans wherever a missing value was used for interpolation.

    # fast interpolator that use the regular grid structure (x and y are 1D arrays)
    z = z_masked.filled(np.nan)
    zinterp = RegularGridInterpolator((x, y), z.T)
    
    # new grid to interpolate on
    X2, Y2 = np.meshgrid(x2, y2)
    newpoints = np.array((X2, Y2)).T
    
    # actual interpolation
    z2 = zinterp(newpoints)
    z2_masked = np.ma.array(z2, mask=np.isnan(z2))
    

    For completeness, another approach is to interpolate a second mask array (filled with 1 where data is missing) to fill in missing values on the new grid.

    # fast interpolator that use the regular grid structure (x and y are 1D arrays)
    zinterp = RegularGridInterpolator((x, y), z.T)
    minterp = RegularGridInterpolator((x, y), (mask+0.).T)
    
    # actual interpolation
    z2 = zinterp(newpoints)
    mask2 = minterp(newpoints) > 0  # apply threshold, e.g. 0.5 is considered contaminated and will be removed.
    z2[mask2] = np.nan  # fill with nans or whatever missing data flag
    

    Note both approaches should also work with RectBivariateSpline, if spline interoplation is desired. And either way, this should be much faster than using interp2d...

提交回复
热议问题