0

I am using the following code (adapted from Resample or normalize trajectory data so points are evenly spaced) to interpolate 2D X & Y positional data (with no time index) so that the points are evenly spaced. From my understanding, the answer for that question assumed that the x values follow a certain curve or pattern (e.g. exponential curve) but that isn't the case for all my trajectories.

I believe I need to interpolate X and Y separately. However, this code does not seem to produce evenly spaced output and I'm not sure how to solve this. The arrays are already 1000 points before interpolation and vary in 'shape'. Thus I don't know how to define x & x_new:

# Interpolation for X values 
from scipy.interpolate import interp1d
y = df['X']
x = np.linspace(y.min(),y.max(),1000)
# define interpolation function:
f = interp1d(x,y)
# create new df with desired x vals, generate y with interp function:
x_new = np.linspace(y.min(),y.max(),1000)
y_new = f(x_new)
X_interp = pd.DataFrame(np.array([y_new]).T, columns=["x_interp"])


# Interpolation for Y values 
from scipy.interpolate import interp1d
y = df['Y']
x = np.linspace(y.min(),y.max(),1000)
# define interpolation function:
f = interp1d(x,y)
# create new df with desired x vals, generate y with interp function:
x_new = np.linspace(y.min(),y.max(),1000)
y_new = f(x_new)
Y_interp = pd.DataFrame(np.array([y_new]).T, columns=["y_interp"])

But it doesnt change the data in any way.

As an example, the 2D data (X & Y positions with no time index) for one of the trajectories looks like this when plotted as a scatterplot:

# Note the interpolation doesnt change the data so it looks the same whether I plot using:
plt.scatter(X_interp['x_interp'], Y_interp['y_interp']) 
# or the original data
plt.scatter(df['X'], df['Y']))

enter image description here

CentauriAurelius
  • 504
  • 3
  • 21
  • @user202729 Ive added the cote to plot the data – CentauriAurelius Feb 16 '21 at 10:09
  • x and xnew are identical. How would you get distinct results ? – manu190466 Feb 16 '21 at 10:25
  • I see that you're not providing a [example] -- that is, since the data is not available it's not possible to run the data. Is that what you mean: given some data like (0, 0),(2, 2),(3, 3) (represent a list of points on a plane) you want to change it to (0,0),(1, 1),(2, 2),(3, 3)? – user202729 Feb 16 '21 at 10:26
  • @manu190466 I only use the interpolation function to return y_new, not x_new. I also state in the question that im not sure how to define x & x_new – CentauriAurelius Feb 16 '21 at 10:26
  • x is the input data, you should'nt have to define it. – manu190466 Feb 16 '21 at 10:29
  • [python - Resample trajectory to have equal euclidean distance in each sample - Stack Overflow](https://stackoverflow.com/questions/64441803/resample-trajectory-to-have-equal-euclidean-distance-in-each-sample) ? – user202729 Feb 16 '21 at 10:30
  • Or [algorithm - resampling a series of points - Stack Overflow](https://stackoverflow.com/questions/2765623/resampling-a-series-of-points) (not specific to Python) -- . [python - Resample time-series of position evenly in time - Stack Overflow](https://stackoverflow.com/questions/33808554/resample-time-series-of-position-evenly-in-time) is related but in this case the op want to sample in time, and https://stackoverflow.com/questions/27429784/equally-spaced-points-in-a-contour is the question in Matlab. – user202729 Feb 16 '21 at 10:31
  • @manu190466 if I dont define x then i get the error: NameError: name 'x' is not defined – CentauriAurelius Feb 16 '21 at 10:34
  • Your data are seemingly in a pandas dataframe - pandas has [an interpolation function integrated](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.interpolate.html), so you can simultaneously interpolate X and Y. And yes, a reproducible example code with inputs would help to provide the best solution. – Mr. T Feb 16 '21 at 10:34
  • @Mr.T I dont think the pandas interpolation function would work to keep the arrays the same length and just evenly space the points. It seems to be for filling missing values or stretching arrays? – CentauriAurelius Feb 16 '21 at 10:37
  • Is my understanding of the question explained above correct? – user202729 Feb 16 '21 at 10:39
  • @user202729 From what I can tell, I think that the "resample to have equal euclidian distance" is the closest to my question – CentauriAurelius Feb 16 '21 at 10:41
  • As I understand your task, you could combine your dataframe with an evenly spaced df, interpolate your data, then just keep the regular index. [An example is here.](https://stackoverflow.com/a/45062113/8881141) – Mr. T Feb 16 '21 at 10:45
  • @Mr.T I dont have any time index or array of unevenly spaced times, and thus dont know what to use for that part of the interpolation? I know I can create an array of evenly spaced times but that answer seems like it required the array of uneven times to define the interpolation function first – CentauriAurelius Feb 16 '21 at 10:55
  • And? Is there any problem with the answer in the linked question? That you can't use numba? – user202729 Feb 16 '21 at 10:58
  • Set your y-column as the index. Create a new df with evenly spaced y-values between min and max. Merge them. Interpolate X. Keep only the evenly spaced index. Voila. I think you will figure out now a solution with several options on offer. – Mr. T Feb 16 '21 at 10:59
  • And as I've said before, you should include a [example] -- that way people can make sure that they correctly understood the question. – user202729 Feb 16 '21 at 10:59
  • @user202729 The answer you linked "https://stackoverflow.com/questions/64441803/resample-trajectory-to-have-equal-euclidean-distance-in-each-sample" solved my problem. Works perfectly. – CentauriAurelius Feb 17 '21 at 01:16

0 Answers0