Basic interpolation with PyDynamic.uncertainty.interpolate.interp1d_unc

This is the second notebook in the series to illustrate the use of our method interp1d_unc. We will conduct a simple interpolation and return the sensitivity coeffients.

Preparation

First we setup our Python and plotting environment and collect all previously extracted measurement data and their visualization.

Setup the Python environment

[1]:
import warnings

warnings.filterwarnings("ignore")
warnings.simplefilter("ignore")

import holoviews as hv
import numpy as np
import pickle

from PyDynamic.uncertainty import interp1d_unc

Setup plotting environment and labels

[2]:
# Set one of the available plotting backends ('plotly', 'bokeh', 'maplotlib').
hv.extension("bokeh")

# Define labels and units for plots.
timestamp_labels = hv.Dimension(("Time", "relative measurement time"), unit="s")
measurement_labels = hv.Dimension(("Current", "Primary Nominal Current"), unit="A")

Load results from previous lessons

[3]:
try:
    y = np.load("data_points.npy")
    uy = np.load("data_points_unc.npy")
    x = np.load("time_stamps.npy")
    with open("original_plot.p", "rb") as f:
        original_plot = pickle.load(f)
except FileNotFoundError:
    print(
        "The measurement data is not available. Please execute all steps in '01 "
        "Basic measurement data pre-processing' before you proceed."
    )

Setup interpolation timestamps

[4]:
# Setup list of vectors of interpolation timestamps with increasing number.
x_news = [
    np.linspace(start=x.min(), stop=x.max(), num=n_nodes)
    for n_nodes in np.arange(start=2, stop=20, step=3)
]
x_news
[4]:
[array([0.004, 0.01 ]),
 array([0.004 , 0.0055, 0.007 , 0.0085, 0.01  ]),
 array([0.004     , 0.00485714, 0.00571429, 0.00657143, 0.00742857,
        0.00828571, 0.00914286, 0.01      ]),
 array([0.004 , 0.0046, 0.0052, 0.0058, 0.0064, 0.007 , 0.0076, 0.0082,
        0.0088, 0.0094, 0.01  ]),
 array([0.004     , 0.00446154, 0.00492308, 0.00538462, 0.00584615,
        0.00630769, 0.00676923, 0.00723077, 0.00769231, 0.00815385,
        0.00861538, 0.00907692, 0.00953846, 0.01      ]),
 array([0.004   , 0.004375, 0.00475 , 0.005125, 0.0055  , 0.005875,
        0.00625 , 0.006625, 0.007   , 0.007375, 0.00775 , 0.008125,
        0.0085  , 0.008875, 0.00925 , 0.009625, 0.01    ])]

Conduct and visualize interpolation

To conduct the simplest interpolation, we specify new interpolation nodes and the original timestamps or frequencies, measurement values and uncertainties. This results by default in a linear interpolation. The available interpolation methods can be chosen in the dropdown menu in the following plot. Those can be specified using the parameter kind.

[5]:
# Create plots of interpolated values and uncertainties for the increasing number of
# interpolation nodes.
interpolation_dict = {}
for x_new in x_news:
    for i_kind in ("linear", "nearest", "next", "previous", "cubic"):
        # Conduct the actual interpolation for the current set of interpolation nodes and kind.
        x_new, y_new, uy_new = interp1d_unc(x_new=x_new, x=x, y=y, uy=uy, kind=i_kind)
        print(x_new, y_new, uy_new)

        # Create plot of the interpolated values.
        curve_interp = hv.Curve(
            (x_new, y_new),
            timestamp_labels,
            measurement_labels,
            label="interpolated values",
        )

        # Create plot of the interpolated uncertainties.
        interp_uncertainties = hv.Spread(
            (x_new, y_new, uy_new),
            vdims=[measurement_labels, "Associated Uncertainty"],
            kdims=timestamp_labels,
            label="interpolated uncertainties",
        )

        interpolation_dict[x_new.size, i_kind] = curve_interp * interp_uncertainties
[0.004 0.01 ] [2.47691988 1.41385091] [0.0371538  0.02120776]
[0.004 0.01 ] [2.47691988 1.41385091] [0.0371538  0.02120776]
[0.004 0.01 ] [2.47691988 1.41385091] [0.0371538  0.02120776]
[0.004 0.01 ] [2.47691988 1.41385091] [0.0371538  0.02120776]
[0.004 0.01 ] [2.47691988 1.41385091] [0.0371538  0.02120776]
[0.004  0.0055 0.007  0.0085 0.01  ] [2.47691988 3.71268867 4.03891907 3.31838263 1.41385091] [0.0371538  0.0473224  0.04284885 0.04478872 0.02120776]
[0.004  0.0055 0.007  0.0085 0.01  ] [2.47691988 4.1246116  4.1246116  3.95322654 1.41385091] [0.0371538  0.06186917 0.06186917 0.0592984  0.02120776]
[0.004  0.0055 0.007  0.0085 0.01  ] [2.47691988 4.1246116  3.95322654 1.41385091 1.41385091] [0.0371538  0.06186917 0.0592984  0.02120776 0.02120776]
[0.004  0.0055 0.007  0.0085 0.01  ] [2.47691988 2.47691988 4.1246116  3.95322654 1.41385091] [0.0371538  0.0371538  0.06186917 0.0592984  0.02120776]
[0.004  0.0055 0.007  0.0085 0.01  ] [2.47691988 3.86178517 4.30061078 3.5618237  1.41385091] [0.0371538  0.06658834 0.04827905 0.06395319 0.02120776]
[0.004      0.00485714 0.00571429 0.00657143 0.00742857 0.00828571
 0.00914286 0.01      ] [2.47691988 3.18307347 3.88922707 4.07564444 4.0021937  3.59045859
 2.50215475 1.41385091] [0.0371538  0.03396776 0.05329567 0.04732865 0.04589666 0.05091741
 0.02815519 0.02120776]
[0.004      0.00485714 0.00571429 0.00657143 0.00742857 0.00828571
 0.00914286 0.01      ] [2.47691988 2.47691988 4.1246116  4.1246116  3.95322654 3.95322654
 1.41385091 1.41385091] [0.0371538  0.0371538  0.06186917 0.06186917 0.0592984  0.0592984
 0.02120776 0.02120776]
[0.004      0.00485714 0.00571429 0.00657143 0.00742857 0.00828571
 0.00914286 0.01      ] [2.47691988 4.1246116  4.1246116  3.95322654 3.95322654 1.41385091
 1.41385091 1.41385091] [0.0371538  0.06186917 0.06186917 0.0592984  0.0592984  0.02120776
 0.02120776 0.02120776]
[0.004      0.00485714 0.00571429 0.00657143 0.00742857 0.00828571
 0.00914286 0.01      ] [2.47691988 2.47691988 2.47691988 4.1246116  4.1246116  3.95322654
 3.95322654 1.41385091] [0.0371538  0.0371538  0.0371538  0.06186917 0.06186917 0.0592984
 0.0592984  0.02120776]
[0.004      0.00485714 0.00571429 0.00657143 0.00742857 0.00828571
 0.00914286 0.01      ] [2.47691988 3.37061027 3.98779646 4.2852695  4.21982042 3.74824026
 2.82732008 1.41385091] [0.0371538  0.0585222  0.06543768 0.05221132 0.05062119 0.06278262
 0.05557142 0.02120776]
[0.004  0.0046 0.0052 0.0058 0.0064 0.007  0.0076 0.0082 0.0088 0.0094
 0.01  ] [2.47691988 2.9712274  3.46553491 3.95984243 4.09033459 4.03891907
 3.98750355 3.69928898 2.93747629 2.1756636  1.41385091] [0.0371538  0.03195152 0.03998588 0.05580607 0.05089637 0.04284885
 0.04902595 0.05341068 0.03657637 0.02317011 0.02120776]
[0.004  0.0046 0.0052 0.0058 0.0064 0.007  0.0076 0.0082 0.0088 0.0094
 0.01  ] [2.47691988 2.47691988 4.1246116  4.1246116  4.1246116  4.1246116
 3.95322654 3.95322654 3.95322654 1.41385091 1.41385091] [0.0371538  0.0371538  0.06186917 0.06186917 0.06186917 0.06186917
 0.0592984  0.0592984  0.0592984  0.02120776 0.02120776]
[0.004  0.0046 0.0052 0.0058 0.0064 0.007  0.0076 0.0082 0.0088 0.0094
 0.01  ] [2.47691988 4.1246116  4.1246116  4.1246116  3.95322654 3.95322654
 3.95322654 1.41385091 1.41385091 1.41385091 1.41385091] [0.0371538  0.06186917 0.06186917 0.06186917 0.0592984  0.0592984
 0.0592984  0.02120776 0.02120776 0.02120776 0.02120776]
[0.004  0.0046 0.0052 0.0058 0.0064 0.007  0.0076 0.0082 0.0088 0.0094
 0.01  ] [2.47691988 2.47691988 2.47691988 2.47691988 4.1246116  4.1246116
 4.1246116  3.95322654 3.95322654 3.95322654 1.41385091] [0.0371538  0.0371538  0.0371538  0.0371538  0.06186917 0.06186917
 0.06186917 0.0592984  0.0592984  0.0592984  0.02120776]
[0.004  0.0046 0.0052 0.0058 0.0064 0.007  0.0076 0.0082 0.0088 0.0094
 0.01  ] [2.47691988 3.12957009 3.65308495 4.0326438  4.25342597 4.30061078
 4.15937756 3.81490563 3.25237433 2.45696298 1.41385091] [0.0371538  0.04994546 0.0652044  0.06457347 0.05500119 0.04827905
 0.05297116 0.06192576 0.06258734 0.04591678 0.02120776]
[0.004      0.00446154 0.00492308 0.00538462 0.00584615 0.00630769
 0.00676923 0.00723077 0.00769231 0.00815385 0.00861538 0.00907692
 0.00953846 0.01      ] [2.47691988 2.85715643 3.23739298 3.61762953 3.99786608 4.09824467
 4.05869427 4.01914387 3.97959347 3.75788996 3.17188019 2.58587043
 1.99986067 1.41385091] [0.0371538  0.03194769 0.0348658  0.04433185 0.05718147 0.05313978
 0.04438177 0.04356442 0.05107041 0.05476129 0.04156812 0.02965537
 0.02129306 0.02120776]
[0.004      0.00446154 0.00492308 0.00538462 0.00584615 0.00630769
 0.00676923 0.00723077 0.00769231 0.00815385 0.00861538 0.00907692
 0.00953846 0.01      ] [2.47691988 2.47691988 2.47691988 4.1246116  4.1246116  4.1246116
 4.1246116  3.95322654 3.95322654 3.95322654 3.95322654 1.41385091
 1.41385091 1.41385091] [0.0371538  0.0371538  0.0371538  0.06186917 0.06186917 0.06186917
 0.06186917 0.0592984  0.0592984  0.0592984  0.0592984  0.02120776
 0.02120776 0.02120776]
[0.004      0.00446154 0.00492308 0.00538462 0.00584615 0.00630769
 0.00676923 0.00723077 0.00769231 0.00815385 0.00861538 0.00907692
 0.00953846 0.01      ] [2.47691988 4.1246116  4.1246116  4.1246116  4.1246116  3.95322654
 3.95322654 3.95322654 3.95322654 1.41385091 1.41385091 1.41385091
 1.41385091 1.41385091] [0.0371538  0.06186917 0.06186917 0.06186917 0.06186917 0.0592984
 0.0592984  0.0592984  0.0592984  0.02120776 0.02120776 0.02120776
 0.02120776 0.02120776]
[0.004      0.00446154 0.00492308 0.00538462 0.00584615 0.00630769
 0.00676923 0.00723077 0.00769231 0.00815385 0.00861538 0.00907692
 0.00953846 0.01      ] [2.47691988 2.47691988 2.47691988 2.47691988 2.47691988 4.1246116
 4.1246116  4.1246116  4.1246116  3.95322654 3.95322654 3.95322654
 3.95322654 1.41385091] [0.0371538  0.0371538  0.0371538  0.0371538  0.0371538  0.06186917
 0.06186917 0.06186917 0.06186917 0.0592984  0.0592984  0.0592984
 0.0592984  0.02120776]
[0.004      0.00446154 0.00492308 0.00538462 0.00584615 0.00630769
 0.00676923 0.00723077 0.00769231 0.00815385 0.00861538 0.00907692
 0.00953846 0.01      ] [2.47691988 2.98988055 3.42845353 3.78589296 4.05545297 4.2303877
 4.30395127 4.26939781 4.11998146 3.84895634 3.4495766  2.91509636
 2.23876975 1.41385091] [0.0371538  0.04451507 0.06025142 0.06650832 0.06402752 0.05662064
 0.04970399 0.04878094 0.0544091  0.06138933 0.06389044 0.05742552
 0.0391948  0.02120776]
[0.004    0.004375 0.00475  0.005125 0.0055   0.005875 0.00625  0.006625
 0.007    0.007375 0.00775  0.008125 0.0085   0.008875 0.00925  0.009625
 0.01    ] [2.47691988 2.78586208 3.09480427 3.40374647 3.71268867 4.02163087
 4.10318847 4.07105377 4.03891907 4.00678437 3.97464967 3.79451557
 3.31838263 2.8422497  2.36611677 1.88998384 1.41385091] [0.0371538  0.03233966 0.03282536 0.03841037 0.0473224  0.05804881
 0.05464062 0.04639633 0.04284885 0.04511994 0.05245928 0.05560805
 0.04478872 0.03462178 0.02588766 0.02050702 0.02120776]
[0.004    0.004375 0.00475  0.005125 0.0055   0.005875 0.00625  0.006625
 0.007    0.007375 0.00775  0.008125 0.0085   0.008875 0.00925  0.009625
 0.01    ] [2.47691988 2.47691988 2.47691988 4.1246116  4.1246116  4.1246116
 4.1246116  4.1246116  4.1246116  3.95322654 3.95322654 3.95322654
 3.95322654 3.95322654 1.41385091 1.41385091 1.41385091] [0.0371538  0.0371538  0.0371538  0.06186917 0.06186917 0.06186917
 0.06186917 0.06186917 0.06186917 0.0592984  0.0592984  0.0592984
 0.0592984  0.0592984  0.02120776 0.02120776 0.02120776]
[0.004    0.004375 0.00475  0.005125 0.0055   0.005875 0.00625  0.006625
 0.007    0.007375 0.00775  0.008125 0.0085   0.008875 0.00925  0.009625
 0.01    ] [2.47691988 4.1246116  4.1246116  4.1246116  4.1246116  4.1246116
 3.95322654 3.95322654 3.95322654 3.95322654 3.95322654 1.41385091
 1.41385091 1.41385091 1.41385091 1.41385091 1.41385091] [0.0371538  0.06186917 0.06186917 0.06186917 0.06186917 0.06186917
 0.0592984  0.0592984  0.0592984  0.0592984  0.0592984  0.02120776
 0.02120776 0.02120776 0.02120776 0.02120776 0.02120776]
[0.004    0.004375 0.00475  0.005125 0.0055   0.005875 0.00625  0.006625
 0.007    0.007375 0.00775  0.008125 0.0085   0.008875 0.00925  0.009625
 0.01    ] [2.47691988 2.47691988 2.47691988 2.47691988 2.47691988 2.47691988
 4.1246116  4.1246116  4.1246116  4.1246116  4.1246116  3.95322654
 3.95322654 3.95322654 3.95322654 3.95322654 1.41385091] [0.0371538  0.0371538  0.0371538  0.0371538  0.0371538  0.0371538
 0.06186917 0.06186917 0.06186917 0.06186917 0.06186917 0.0592984
 0.0592984  0.0592984  0.0592984  0.0592984  0.02120776]
[0.004    0.004375 0.00475  0.005125 0.0055   0.005875 0.00625  0.006625
 0.007    0.007375 0.00775  0.008125 0.0085   0.008875 0.00925  0.009625
 0.01    ] [2.47691988 2.89916327 3.27313417 3.59521425 3.86178517 4.06922862
 4.21392625 4.29225975 4.30061078 4.23536102 4.09289214 3.86958581
 3.5618237  3.16598748 2.67845883 2.09561941 1.41385091] [0.0371538  0.04116281 0.05527826 0.06422911 0.06658834 0.06366031
 0.05764441 0.051439   0.04827905 0.05001373 0.05533815 0.06103035
 0.06395319 0.06159038 0.05201839 0.03458366 0.02120776]
[6]:
# Visualize the interpolation result along-side the original data.
(
    original_plot
    * hv.HoloMap(interpolation_dict, kdims=["number of nodes", "kind of interpolation"])
).opts(width=600, height=800, legend_position="bottom")
[6]:

Return sensitivity coefficients

For (almost) all interpolation or extrapolation runs, the sensitivity coefficients can be returned by setting returnC=True. For details refer to the return value’s documentation.

[7]:
# Conduct linear interpolation for the last set of interpolation nodes and display c.
interp1d_unc(x_new=x_news[-1], x=x, y=y, uy=uy, returnC=True)[-1]
[7]:
array([[ 1.    ,  0.    ,  0.    ,  0.    ],
       [ 0.8125,  0.1875,  0.    ,  0.    ],
       [ 0.625 ,  0.375 ,  0.    ,  0.    ],
       [ 0.4375,  0.5625,  0.    ,  0.    ],
       [ 0.25  ,  0.75  ,  0.    ,  0.    ],
       [ 0.0625,  0.9375,  0.    ,  0.    ],
       [ 0.    ,  0.875 ,  0.125 ,  0.    ],
       [ 0.    ,  0.6875,  0.3125,  0.    ],
       [ 0.    ,  0.5   ,  0.5   ,  0.    ],
       [ 0.    ,  0.3125,  0.6875,  0.    ],
       [ 0.    ,  0.125 ,  0.875 ,  0.    ],
       [ 0.    ,  0.    ,  0.9375,  0.0625],
       [ 0.    ,  0.    ,  0.75  ,  0.25  ],
       [ 0.    ,  0.    ,  0.5625,  0.4375],
       [ 0.    ,  0.    ,  0.375 ,  0.625 ],
       [ 0.    ,  0.    ,  0.1875,  0.8125],
       [ 0.    ,  0.    , -0.    ,  1.    ]])