import numpy as np
data_sets = {}
time = np.arange(0, 150, .1)
velocity = (1 + np.sin(time / 10)) * 60
data_sets['ref'] = dict(
    time=time,                                               # [10 Hz]
    velocity=velocity / 3.6                                  # [m/s]
)
data_sets['obd'] = dict(
    time=time[::10] + 12,                                    # 1 Hz
    velocity=velocity[::10] + np.random.normal(0, 5, 150),   # [km/h]
    engine_rpm=np.maximum(
        np.random.normal(velocity[::10] * 3 + 600, 5), 800
    )                                                        # [RPM]
)
data_sets['dyno'] = dict(
    time=time + 6.66,                                        # 10 Hz
    velocity=velocity + np.random.normal(0, 1, 1500)         # [km/h]
)
#
# To synchronise the data-sets and plot the workflow:
#
# .. dispatcher:: sol
#     :opt: workflow=True, graph_attr={'ratio': '1'}
#     :code:
#
from syncing.model import dsp
sol = dsp(dict(
    data=data_sets, x_label='time', y_label='velocity',
    reference_name='ref', interpolation_method='cubic'
))
sol.plot(view=False)
# Expected:
## SiteMap(...)
#
# Finally, we can analyze the time shifts and the synchronised and re-sampled
# data-sets:
#
import pandas as pd
import schedula as sh
pd.DataFrame(sol['shifts'], index=[0])  # doctest: +SKIP
# Expected:
##      obd  dyno
## ...
df = pd.DataFrame(dict(sh.stack_nested_keys(sol['resampled'])))
df.columns = df.columns.map('/'.join)
df['ref/velocity'] *= 3.6
ax = df.set_index('ref/time').plot(secondary_y='obd/engine_rpm')
ax.set_ylabel('[km/h]'); ax.right_ax.set_ylabel('[RPM]')
# Expected:
## Text(...)
