"""
Cluster plot options
====================

In **spm1d** v0.4 cluster plotting was supported only as highlighting
suprathreshold areas for a test statistic continuum. Version 0.5 adds
two new functions that allow you to superimpose cluster results on 
arbitrary axes:

- ``plot_cluster_patches``: clusters as vertical bars on any axes
- ``plot_cluster_bars``: clusters as horizontal bars on a separate axes
"""

# sphinx_gallery_thumbnail_path = '_static/thumbs/cluster_plots.png'
import numpy as np
from matplotlib import pyplot as plt
import spm1d
import jikudata as jd

dataset = jd.SpeedGRFcategorical()  # 1D dataset:  vertical ground reaction force at three walking speeds
y, A    = dataset.y, dataset.x      # (60,101) responses, (60,) speed labels
y0, y1  = y[A == 2][:7], y[A == 3][:7]   # normal and fast speeds
ti      = spm1d.stats.ttest2( y1, y0 ).inference( 0.05 )

plt.rc( 'font', family='sans-serif' )
plt.rc( 'font', **{'sans-serif': ['Helvetica Neue', 'Helvetica', 'Arial', 'Liberation Sans', 'DejaVu Sans']} )
fig = plt.figure( figsize=(8, 6) )
ax0 = fig.add_axes( [0.02, 0.30, 0.96, 0.68] )
ax1 = fig.add_axes( [0.02, 0.08, 0.96, 0.20] )
spm1d.plot.plot_mean_sd( y0, ax=ax0, linecolor='k', facecolor='0.7' )   # normal:  mean and SD
spm1d.plot.plot_mean_sd( y1, ax=ax0, linecolor='#0e6e84', facecolor='#2eb0ce' )     # fast:  mean and SD
ti.plot_cluster_patches( ax=ax0, color='#6fd3e8', alpha=0.4 )                # cluster bands on the data
ti.plot_cluster_bars( ax=ax1, color='#0e6e84', pvalues=True, fontsize=14, height=0.8 )   # cluster bars beneath
ax0.set_xticks( [] );  ax0.set_yticks( [] )
ax1.set_yticks( [] )
ax1.set_xlim( ax0.get_xlim() )
ax1.tick_params( labelsize=14 )
for ax in (ax0, ax1):
    for side in ('left', 'right', 'top'):
        ax.spines[side].set_visible( False )
ax0.spines['bottom'].set_visible( False )

# %%
#  Version 0.4's suprathreshold cluster plot is shown below as a reference.
plt.figure( figsize=(8, 4.5), tight_layout=True )
ti.plot( ax=plt.gca() )
plt.title( f'SPM{{t}}:  {len( ti.clusters )} clusters' )
