nb_pt = 100
x = np.linspace(0, 500., nb_pt)

# Create fake dist
y = np.sin(2 * np.pi * x)
y += 0.5*np.sin(0.3 * np.pi * x)
y += np.random.normal(0, 0.25, nb_pt)

# Interpolate it
interp = ip.UnivariateSpline(x, y, k=4, s=0.)
x_interp = np.linspace(0, 500., nb_pt*10)
interp_signal = pd.DataFrame(interp(x_interp), index=x_interp, columns=['interp'])
ax = interp_signal.plot()

# Plot dist
ax.scatter(x, y)

interp_signal.head()

"""
Explanation: Create Signal
We create here a periodic signal and introduce some noise
End of explanation
"""


find_periodicity = btp.sequence_signal(y, x)
seq = find_periodicity(.2)
seq

"""
Explanation: Cluster signal segments
We attempt to cluster together portion of the signal (segments) with respect to their shapes (with a tolerance)
End of explanation
"""


fig, ax = plt.subplots(1)

color_dict = dict(zip(set(seq.values()), plt.cm.get_cmap('tab20').colors))

for (x_min, x_max), clu in seq.iteritems():
    plot_df = interp_signal[(interp_signal.index >= x_min) & (interp_signal.index <= x_max)]
    
    if len(plot_df.values) > 0:
        ax = plot_df['interp'].plot(ax=ax, color=color_dict[clu])
        
        
plt.show()

"""
Explanation: Plot clusters
End of explanation
"""


period_seg = btp.find_principal_period(find_periodicity, .2)
period_seg

"""
Explanation: Find principal period
Based on HMM, we are able to evaluate clusters of segment repeat themself aka. principal period.
End of explanation
"""


fig, ax = plt.subplots(1)

color_iter = iter(plt.cm.get_cmap('tab20').colors)

for (x_min, x_max) in period_seg:
    plot_df = interp_signal[(interp_signal.index >= x_min) & (interp_signal.index <= x_max)]
    
    ax = plot_df['interp'].plot(ax=ax, color=next(color_iter))
    
        
plt.show()

"""
Explanation: Plot periods
End of explanation
"""


