0

I'm referencing this question and this documentation in trying to turn a set of points (the purple dots in the image below) into an interpolated grid.

enter image description here

As you can see, the image has missing spots where dots should be. I'd like to figure out where those are.

import numpy as np
from scipy import interpolate

CIRCLES_X = 25 # There should be 25 circles going across
CIRCLES_Y = 10 # There should be 10 circles going down

points = []
values = []
# Points range from 0-800 ish X, 0-300 ish Y 
for point in points:
    points.append([points.x, points.y])
    values.append(1) # Not sure what this should be

grid_x, grid_y = np.mgrid[0:CIRCLES_Y, 0:CIRCLES_X]
grid = interpolate.griddata(points, values, (grid_x, grid_y), method='linear')
print(grid)

Whenever I print out the result of the grid, I get nan for all of my values.

Where am I going wrong? Is my problem even the correct use case for interpolate.grid?

Community
  • 1
  • 1
Jack Guy
  • 8,346
  • 8
  • 55
  • 86
  • 1
    What does your "incomplete grid" look like as data? Your code has nothing like one (and no hints on what you are leaving out). If this is your actual code, this may be the answer :^) – ivan_pozdeev Aug 27 '16 at 06:11
  • @ivan_pozdeev That's the `points` array above. :) The coordinates are definitely good. This code is all that's needed to reproduce my problem (minus the dummy data in points). – Jack Guy Aug 27 '16 at 06:31
  • still, `points.append([points.x, points.y])` doesn't make any sense. Even leaving out the fact the fact it probably should be `point.x` and `.y`, you're adding to the list elements of... itself? – ivan_pozdeev Aug 27 '16 at 11:42
  • Man, it looks like you have absolutely no idea how to tackle your problem from math standpoint and hope that `scipy` will somehow magically figure that out for you. It won't. It's only a tool. Apply it in a meaningless way - and you'll get a result but a meaningless result. – ivan_pozdeev Aug 27 '16 at 13:15
  • Anyway, the state of affairs is there's no solution to be implemented (so it's not a programming question) and even the problem statement is missing some important elements. I outlined some ways the problem can be approached that can be derived from the information given and can't see what else can be done without fully solving the problem for you (which I can't anyway due to the missing info). – ivan_pozdeev Aug 28 '16 at 10:26

1 Answers1

1

First, your uncertain points are mainly at an edge, so it's actually extrapolation. Second, interpolation methods built into scipy deal with continuous functions defined on the entire plane and approximate it as a polynomial. While yours is discrete (1 or 0), somewhat periodic rather than polynomial and only defined in a discrete "grid" of points.

So you have to invent some algorithm to inter/extrapolate your specific kind of function. Whether you'll be able to reuse an existing one - from scipy or elsewhere - is up to you.

  • One possible way is to replace it with some function (continuous or not) defined everywhere, then calculate that approximation in the missing points - whether as one step as scipy.interpolate non-class functions do or as two separate steps.

    • e.g. you can use a 3-D parabola with peaks in your dots and troughs exactly between them. Or just with ones in the dots and 0's in the blanks and hope the resulting approximation in the grid's points is good enough to give a meaningful result (random overswings are likely). Then you can use scipy.interpolate.RegularGridInterpolator for both inter- and extrapolation.

    • or as a harmonic function - then what you're seeking is Fourier transformation

  • Another possible way is to go straight for a discrete solution rather than try to shoehorn the continual mathanalysis' methods into your case: design a (probably entirely custom) algorithm that'll try to figure out the "shape" and "dimensions" of your "grids of dots" and then simply fill in the blanks. I'm not sure if it is possible to add it into the scipy.interpolate's harness as a selectable algorithm in addition to the built-in ones.

And last but not the least. You didn't specify whether the "missing" points are points where the value is unknown or are actual part of the data - i.e. are incorrect data. If it's the latter, simple interpolation is not applicable at all as it assumes that all the data are strictly correct. Then it's a related but different problem: you can approximate the data but then have to somehow "throw away irregularities" (higher order of smallness entities after some point).

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • While I genuinely appreciate your taking the time to answer this question, I think you're reading ambiguity into it that simply isn't there. You mention that `scipy` deals exclusively with continuous functions, but the question I [linked to above](http://stackoverflow.com/questions/5146025/python-scipy-2d-interpolation-non-uniform-data) does not and achieves satisfactory results. That was the use case I was going off of, having missing points in what should be a grid-like arrangement of points (not having inaccurate points). – Jack Guy Aug 27 '16 at 13:56
  • I'll admit my mathematics aren't the strongest, but I do know that 1) I have unstructured data 2) it's interpolation (because I will have missing points in between others, like in the second column). But you're right, it looks like I will have to design something specific to my use case, which is fine. The question above and the documentation lead me to believe there might be an easier solution. And may as well try what's out there before reinventing the wheel. :) – Jack Guy Aug 27 '16 at 13:56
  • [c65705277](http://stackoverflow.com/questions/39178021/interpolate-a-discrete-grid-of-dots/39181082#comment65705277_39181082): Linear and cubic interpolation treat the data as points of a polynomial function. Sure, the actual data can be anything, it's just treated so. In the linked question, the OP appears to have a genuine continual function. – ivan_pozdeev Aug 28 '16 at 01:26
  • [c65705280](http://stackoverflow.com/questions/39178021/interpolate-a-discrete-grid-of-dots/39181082#comment65705280_39181082): you have only one point "between others", all the others are at an edge. – ivan_pozdeev Aug 28 '16 at 10:17