…接上
正如理论部分所讨论的,我们为了可视化效果或者计算效率经常需要对MTF图像进行聚合压缩。详细信息请参见理论部分马尔可夫转换场。
image_size = 48
window_size, remainder = divmod(n_timestamps, image_size)
X_amtf = np.reshape(
X_mtf, (image_size, window_size, image_size, window_size)
).mean(axis=(1,3))
另外我们也可采用分段聚合近似法(piecewise aggregation approximation),pyts包中已经为我们实现了这种算法。
fig = plt.figure(figsize=(5,4))
ax = fig.add_subplot(1,1,1)
_, mappable_image = tsia.plot.plot_markov_transition_field(mtf=X_amtf, ax=ax, reversed_cmap=True)
plt.colorbar(mappable_image);
_=tsia.plot.plot_mtf_metrics(X_amtf)
MTF的对角线表示bin值或者状态的自转换概率。我们可以通过上图看一下自转换概率的分布和其均值、标准差等。另外一个对角线比较难以解释,但可以通过图像给我们一些直观感觉。
mtf_map = tsia.markov.get_mtf_map(
tag_df, X_amtf, reversed_cmap=True
)
tsia.plot.plot_colored_timeseries(tag_df, mtf_map)
def plot_colored_timeseries(tag, image_size=96, colormap='jet'):
# Loads the signal from disk:
tag_df = pd.read_csv( f'{tag}.csv')
tag_df['timestamp'] = pd.to_datetime(tag_df['timestamp'], format='%Y-%m-%dT%H:%M:%S.%f')
tag_df = tag_df.set_index('timestamp')
# Build the MTF for this signal:
X = tag_df.values.reshape(1, -1)
mtf = MarkovTransitionField(image_size=image_size, n_bins=n_bins, strategy=strategy)
tag_mtf = mtf.fit_transform(X)
# Initializing figure:
fig = plt.figure(figsize=(28, 4))
gs = gridspec.GridSpec(1, 2, width_ratios=[1,4])
# Plotting MTF:
ax = fig.add_subplot(gs[0])
ax.set_title('Markov transition field')
_, mappable_image = tsia.plot.plot_markov_transition_field(mtf=tag_mtf[0], ax=ax, reversed_cmap=True)
plt.colorbar(mappable_image)
# Plotting signal:
ax = fig.add_subplot(gs[1])
ax.set_title(f'Signal timeseries for tag {tag}')
mtf_map = tsia.markov.get_mtf_map(tag_df, tag_mtf[0], reversed_cmap=True, step_size=0)
_ = tsia.plot.plot_colored_timeseries(tag_df, mtf_map, ax=ax)
return tag_mtf
我们使用窗口8、把粒度放粗来观察一下。
stats = []
mtf = plot_colored_timeseries('signal-1', image_size=8)
s = tsia.markov.compute_mtf_statistics(mtf[0])
s.update({'Signal': 'signal-1'})
stats.append(s)
mtf = plot_colored_timeseries('signal-2', image_size=48)
_ = tsia.plot.plot_mtf_metrics(mtf[0])
s = tsia.markov.compute_mtf_statistics(mtf[0])
s.update({'Signal': 'signal-2'})
stats.append(s)
mtf = plot_colored_timeseries('signal-3', image_size=48)
_ = tsia.plot.plot_mtf_metrics(mtf[0])
s = tsia.markov.compute_mtf_statistics(mtf[0])
s.update({'Signal': 'signal-3'})
stats.append(s)
mtf = plot_colored_timeseries('signal-4', image_size=48)
_ = tsia.plot.plot_mtf_metrics(mtf[0])
s = tsia.markov.compute_mtf_statistics(mtf[0])
s.update({'Signal': 'signal-4'})
stats.append(s)
mtf = plot_colored_timeseries('signal-5', image_size=48)
_ = tsia.plot.plot_mtf_metrics(mtf[0])
s = tsia.markov.compute_mtf_statistics(mtf[0])
s.update({'Signal': 'signal-5'})
stats.append(s)
mtf = plot_colored_timeseries('signal-6', image_size=48)
_ = tsia.plot.plot_mtf_metrics(mtf[0])
s = tsia.markov.compute_mtf_statistics(mtf[0])
s.update({'Signal': 'signal-6'})
stats.append(s)
mtf = plot_colored_timeseries('signal-7', image_size=48)
_ = tsia.plot.plot_mtf_metrics(mtf[0])
s = tsia.markov.compute_mtf_statistics(mtf[0])
s.update({'Signal': 'signal-7'})
stats.append(s)
stats = pd.DataFrame(stats)
stats.set_index('Signal')