In [1]:
import numpy as np
import pandas as pd
import scipy.interpolate as ip
import bt_periodicity as btp
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
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()
Out[2]:
In [3]:
find_periodicity = btp.sequence_signal(y, x)
seq = find_periodicity(.2)
seq
Out[3]:
Plot clusters
In [4]:
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()
In [5]:
period_seg = btp.find_principal_period(find_periodicity, .2)
period_seg
Out[5]:
Plot periods
In [6]:
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()