1

I am using ndimage to interpolate as follows:

ndimage.map_coordinates(input_data, coords, output, 2, prefilter=False)

Now, the problem is that I do not have valid measurements over my whole input data. So, I have a masked array which tells me which data points are valid. So, when doing the interpolation, I would like to only use pixels which are valid and also adjust the weightings accordingly (to ensure that the weights sum to 1).

However, I see that there is no easy way to do this. I was wondering if someone knows of a good way to do this or can point me to some library or code that I can use. I am coming from a C++ background, so still finding my way around python.

Luca
  • 10,458
  • 24
  • 107
  • 234

2 Answers2

1

It sounds like you need to focus on the interpolation of data and then extract values from desired coordinates. For 1D splrep and 2D bisplrep are the interpolation functions you need to check out (A good overview). Both of these functions can be weighted and provide fine tune control over the spline function you interpolate with.

Once you have filtered the data with the desired weights you can then determine the value at specified coordinates using.

ndimage.map_coordinates(input_data, coords, output, prefilter=True)

note the prefilter key word argument is not needed as that is the default value

MrAlias
  • 1,316
  • 15
  • 26
  • The problem is that the interpolation data cannot handle masked arrays. Basically, I do not want it to use values which are not in the mask. However, I am not sure how to do that. I see some tickets now on scipy about this but I do not think this has been solved yet. https://github.com/scipy/scipy/issues/1682 – Luca Jun 18 '14 at 19:53
  • 1
    A good way to exclude data would be to weight that data at 0. – MrAlias Jun 18 '14 at 19:54
  • 1
    Otherwise you could set unwanted values to `nan` and then possibly do something similar to stackoverflow.com/questions/6518811/interpolate-nan-values-in-a-numpy-array – MrAlias Jun 18 '14 at 19:55
0

You can use scipy.interpolate.griddata to get interpolated values from arbitrary known data points. This example

import numpy as np
from scipy.interpolate import griddata

# data array
a = np.arange(9).reshape((3,3)).astype(float)
a[1, 1] = np.nan
print(a)
a = a.flatten()

# coordinate arrays
ii, jj = np.indices((3,3))
ij = np.stack((ii.flatten(), jj.flatten()), axis=1)

# filter out unknowns
mask = ~np.isnan(a)
a = a[mask]
ij = ij[mask]

# interpolate for the missing a[1, 1] element
res = griddata(ij, a, (1, 1), method='cubic')
print(res)

produces

[[ 0.  1.  2.]
 [ 3. nan  5.]
 [ 6.  7.  8.]]

4.000000157826586