<jupyter_start><jupyter_text>api_dev_correlation_function_development* Author: <NAME>* Description: developing rolling window correlation function in avatar<jupyter_code>import datetime
import avatarpy
today = datetime.datetime.today().strftime('%Y-%m-%d')
print('Date: ', today, '\nVersion: ', avatarpy.__version__)
from avatarpy import Avatar, dataset
import os
import glob
import itertools
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import cufflinks as cf
import plotly.express as px
from scipy import stats
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
cf.go_offline(connected=True)
csv_path = dataset['freely_moving']
ava = Avatar(csv_path)<jupyter_output><empty_output><jupyter_text>Example) limb-wise velocity correlation<jupyter_code>corr = ava.velocity.corr()
corr<jupyter_output><empty_output><jupyter_text>* Output: exact square,  to remove duplicate, apply triangular mask<jupyter_code>mask = np.tril(np.ones(corr.shape).astype(np.bool))
corr.mask(mask)
# total unique inter-node correlations
corr.mask(mask).count().sum()
# flatten 2d-arr
corr_flatten = corr.mask(mask).stack().reset_index()
corr_flatten
labels = pd.Index(['-'.join(x) for x in zip( corr_flatten['level_0'], corr_flatten['level_1'])])
corrs = corr_flatten.set_index(labels)[0]
corrs<jupyter_output><empty_output><jupyter_text>Applying Rolling Window for Correlation Note that there is kwarg applied to rolling window, `center=True`.  The calculated correlation values will be appear at center of window.<jupyter_code>window = 20
rolling_corr = ava.velocity[['rfoot', 'lfoot', 'lhand', 'rhand']].rolling(window, center=True).corr()
rolling_corr.dropna()<jupyter_output><empty_output><jupyter_text>Function for flattening correlation matrix<jupyter_code>def corr_flatten(df):
    mask = np.tril(np.ones(df.shape).astype(np.bool))
    s = df.mask(mask).stack()
    s.index = s.index.map(lambda x: '_'.join(x[-2:]))
    return s
corr_flatten(corr)<jupyter_output><empty_output><jupyter_text>Apply row by row<jupyter_code>rolling_corr.groupby(level=0).apply(lambda x: corr_flatten(x)).unstack()
def get_rolling_corr(data, window=20, center=True):
    rolling_corr = data.rolling(window, center=center).corr()
    return rolling_corr.groupby(level=0).apply(lambda x: corr_flatten(x)).unstack()<jupyter_output><empty_output><jupyter_text>Usage<jupyter_code>get_rolling_corr(ava.velocity[['rfoot', 'lfoot', 'lhand', 'rhand']]).iplot(kind='heatmap', colorscale='-rdbu')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13002733307301836, 'fraction_numerical': 0.004295197188598204, 'mean_word_length': 8.02112676056338, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '646890', 'license': 'MIT', 'n_tokens_mistral': 918, 'n_tokens_neox': 870, 'n_words': 178, 'path': 'jupyter/dev/api_dev_correlation_function_development.ipynb', 'repo_name': 'jeakwon/avatar', 'size': '2563'}