Justin Cabot-Miller
Justin Cabot-Miller

Reputation: 43

I think scipy.interpolate might be broken or am I using it wrong?

I'm working with some geographic data that's missing values, and I'm trying to impute missing values along their paths. I've already gotten interpolate to work with other values, including a set of latitudes (working_x) and longitudes (working_y) that I've included here. However, the following data consisting of the broke_x and broke_y cause an error.

from scipy import interpolate

working_x = [50.2577, 50.0482, 50.0486, 51.7697, 54.5125, 56.6626, 59.1144, 61.2571, 65.0806, 66.541, 68.8, 70.672, 72.5613, 74.7929, 75.7798, 78.119, 79.4935, 82.3863, 85.0249, 88.0613, 91.878, 94.1954, 96.7093, 98.3678, 100.5983, 102.5626]
working_y = [27.1174, 26.9498, 26.9501, 27.0585, 26.1905, 26.3719, 24.5836, 23.4857, 21.4881, 20.4295, 18.0235, 16.0258, 13.9794, 11.0714, 9.0558, 6.7257, 6.0434, 5.6889, 5.7001, 5.6913, 5.7709, 6.167, 5.7973, 4.9382, 3.1171, 1.7426]
broke_x = [50.3603, 50.2219, 50.2246, 50.182, 50.182, 50.182, 52.0052, 54.3371, 56.7533, 58.8949, 65.2883, 67.1264, 68.3829, 70.0129, 72.003, 73.8859, 75.1131, 75.6843, 77.3321, 78.6119, 81.6834, 83.8053, 87.7575, 89.1056, 92.7804, 95.1509, 100.2694, 101.2058]
broke_y = [27.0834, 26.678, 26.6786, 26.6716, 26.6716, 26.6716, 27.0163, 26.2115, 26.2435, 25.1276, 24.7504, 23.3715, 21.7267, 19.6113, 17.2461, 13.7936, 10.9089, 9.8805, 7.5112, 6.6072, 5.8491, 6.1449, 6.5355, 6.5364, 6.316, 6.1539, 3.4317, 2.6727]
tck1, u1 = interpolate.splprep([working_x, working_y], s=1)
tck, u = interpolate.splprep([broke_x, broke_y], s=1)

Whenever I run the above code I get this error.

ValueError                                Traceback (most recent call last)
<ipython-input-232-cad98765c4aa> in <module>
      6 broke_y = [27.0834, 26.678, 26.6786, 26.6716, 26.6716, 26.6716, 27.0163, 26.2115, 26.2435, 25.1276, 24.7504, 23.3715, 21.7267, 19.6113, 17.2461, 13.7936, 10.9089, 9.8805, 7.5112, 6.6072, 5.8491, 6.1449, 6.5355, 6.5364, 6.316, 6.1539, 3.4317, 2.6727][:10]
      7 tck1, u1 = interpolate.splprep([working_x, working_y], s=1)
----> 8 tck, u = interpolate.splprep([broke_x, broke_y], s=1)

C:\ProgramData\Anaconda3\envs\geo\lib\site-packages\scipy\interpolate\fitpack.py in splprep(x, w, u, ub, ue, k, task, s, t, full_output, nest, per, quiet)
    155     """
    156     res = _impl.splprep(x, w, u, ub, ue, k, task, s, t, full_output, nest, per,
--> 157                         quiet)
    158     return res
    159 

C:\ProgramData\Anaconda3\envs\geo\lib\site-packages\scipy\interpolate\_fitpack_impl.py in splprep(x, w, u, ub, ue, k, task, s, t, full_output, nest, per, quiet)
    278     iwrk = _parcur_cache['iwrk']
    279     t, c, o = _fitpack._parcur(ravel(transpose(x)), w, u, ub, ue, k,
--> 280                                task, ipar, s, t, nest, wrk, iwrk, per)
    281     _parcur_cache['u'] = o['u']
    282     _parcur_cache['ub'] = o['ub']

ValueError: Invalid inputs. ```

Upvotes: 3

Views: 1602

Answers (1)

Arne
Arne

Reputation: 10545

Your broke lists contain successive identical points, which evidently interpolate.splprep() can't handle. Here's a minimal example highlighting the problem:

from scipy import interpolate

working_x = [1, 2, 3, 4]
working_y = [1, 2, 3, 4]

tck1, u1 = interpolate.splprep([working_x, working_y], s=1)

works, whereas

broke_x = [1, 2, 3, 3] 
broke_y = [1, 2, 3, 3]

tck, u = interpolate.splprep([broke_x, broke_y], s=1)

throws the error.

Upvotes: 4

Related Questions