I just want to interpolate, in the simplest possible terms, a 3D dataset. Linear interpolation, nearest neighbour, all that would suffice (this is to start off some algorithm, so no accurate estimate is required).
In new scipy versions, things like griddata would be useful, but currently I only have scipy 0.8. So I have a “cube” (data[:,:,:], (NixNjxNk)) array, and an array of flags (flags[:,:,:,], True or False) of the same size. I want to interpolate my data for the elements of data where the corresponding element of flag is False, using eg the nearest valid datapoint in data, or some linear combination of “close by” points.
There can be large gaps in the dataset in at least two dimensions. Other than coding a full-blown nearest neighbour algorithm using kdtrees or similar, I can’t really find a generic, N-dimensional nearest-neighbour interpolator.
You can set up a crystal-growth-style algorithm shifting a view alternately along each axis, replacing only data that is flagged with a
Falsebut has aTrueneighbor. This gives a “nearest-neighbor”-like result (but not in Euclidean or Manhattan distance — I think it might be nearest-neighbor if you are counting pixels, counting all connecting pixels with common corners) This should be fairly efficient with NumPy as it iterates over only axis and convergence iterations, not small slices of the data.Crude, fast and stable. I think that’s what you were after:
For good measure, here’s a visualization (2D) of the zones seeded by the data originally flagged
True.