<jupyter_start><jupyter_text>Group_Data_Analysis_PCA_10th_adding multiple params* Version: '0.0.4'* Date: 2021-05-03* Author: <NAME>* Description: PCA analysis with multiple params 3D plot<jupyter_code>from avatarpy import Avatar
import os
import glob
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import cufflinks as cf
from scipy.stats import zscore
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
cf.go_offline(connected=True)
root = r"C:\Users\Jay\Desktop\avatar_young_adult\data\best1_20210503"
avatars = dict(
    wt=dict(
        young=[],
        adult=[],
    ),
    ko=dict(
        young=[],
        adult=[],
    )
)
for path, subdirs, files in os.walk(root):
    for name in files:
        if name.lower().endswith('.csv'):
            csv_path = os.path.join(path, name)
            age = os.path.basename(os.path.dirname(path))
            genotype = os.path.basename(os.path.dirname(os.path.dirname(path)))
            
            avatars[genotype][age].append(Avatar(csv_path=csv_path, ID=name))<jupyter_output><empty_output><jupyter_text>Create walking event data Definition of walking- Moved more than 5 cm in 1 second(20=Frame)- More details take a look Group_Data_Analysis_PCA_1st_Trial  Event Search function<jupyter_code>def get_event_indices(boo, event_length):
    """Returns list of event indices.
    
    ex) [(start 1, end 1), (start 2, end 2), (start 3, end 3), ..., (start N, end N)]
    """
    indices = np.arange(len(boo))
    condition = np.nonzero(boo[1:] != boo[:-1])[0] + 1
    split_indices = np.split(indices, condition)
    true_indices = split_indices[0::2] if boo[0] else split_indices[1::2]
    event_indice_pair = [(idx[0]-event_length+1, idx[0]+1) for idx in true_indices]
    return event_indice_pair<jupyter_output><empty_output><jupyter_text>Features<jupyter_code>wt_young_event_data = []
for avatar in avatars['wt']['young']:
    boo = (avatar.distance['anus'].rolling(20).sum()>5).values # boolean array
    event_indices = get_event_indices(boo, 20)
    avatar_aoa = avatar.transform.align_on_axis()
    avatar_aop = avatar.transform.align_on_plane()
    for i, idx in enumerate(event_indices):
        raw_coords = avatar.data.loc[avatar.index[idx[0]:idx[1]]]
        aoa_coords = avatar_aoa.data.loc[avatar.index[idx[0]:idx[1]]]
        aop_coords = avatar_aop.data.loc[avatar.index[idx[0]:idx[1]]]
        velocity = avatar.velocity.loc[avatar.index[idx[0]:idx[1]]]
        acceleration = avatar.acceleration.loc[avatar.index[idx[0]:idx[1]]]
        angle = avatar.angle.loc[avatar.index[idx[0]:idx[1]]]
        angle_diff = avatar.angle.diff().loc[avatar.index[idx[0]:idx[1]]]
        vector_length = avatar.vector_length.loc[avatar.index[idx[0]:idx[1]]]
        acc_corr = acceleration.corr()
        mask = np.triu(np.ones_like(acc_corr, dtype=bool), 1)
        acc_corr = acc_corr.values.flatten()[mask.flatten()]
        ang_corr = angle_diff.corr()
        mask = np.triu(np.ones_like(ang_corr, dtype=bool), 1)
        ang_corr = ang_corr.values.flatten()[mask.flatten()]
        
        if raw_coords.shape[0]!=20:continue
#         elif aoa_coords.shape[0]!=20:continue
#         elif aop_coords.shape[0]!=20:continue
        X1 = raw_coords.values.flatten()
        X2 = aoa_coords.values.flatten()
        X3 = aop_coords.values.flatten()
        X4 = velocity.values.flatten()
        X5 = acceleration.values.flatten()
        X6 = angle.values.flatten()
        X7 = angle_diff.values.flatten()
        X8 = vector_length.values.flatten()
        X9 = acc_corr
        X10 = ang_corr
        X = np.concatenate([X1,X2,X3,X4,X5,X6,X7,X8,X9,X10])
        wt_young_event_data.append(X)
wt_young_event_data = np.stack(wt_young_event_data)
wt_adult_event_data = []
for avatar in avatars['wt']['adult']:
    boo = (avatar.distance['anus'].rolling(20).sum()>5).values # boolean array
    event_indices = get_event_indices(boo, 20)
    avatar_aoa = avatar.transform.align_on_axis()
    avatar_aop = avatar.transform.align_on_plane()
    for i, idx in enumerate(event_indices):
        raw_coords = avatar.data.loc[avatar.index[idx[0]:idx[1]]]
        aoa_coords = avatar_aoa.data.loc[avatar.index[idx[0]:idx[1]]]
        aop_coords = avatar_aop.data.loc[avatar.index[idx[0]:idx[1]]]
        velocity = avatar.velocity.loc[avatar.index[idx[0]:idx[1]]]
        acceleration = avatar.acceleration.loc[avatar.index[idx[0]:idx[1]]]
        angle = avatar.angle.loc[avatar.index[idx[0]:idx[1]]]
        angle_diff = avatar.angle.diff().loc[avatar.index[idx[0]:idx[1]]]
        vector_length = avatar.vector_length.loc[avatar.index[idx[0]:idx[1]]]
        acc_corr = acceleration.corr()
        mask = np.triu(np.ones_like(acc_corr, dtype=bool), 1)
        acc_corr = acc_corr.values.flatten()[mask.flatten()]
        ang_corr = angle_diff.corr()
        mask = np.triu(np.ones_like(ang_corr, dtype=bool), 1)
        ang_corr = ang_corr.values.flatten()[mask.flatten()]
        
        if raw_coords.shape[0]!=20:continue
#         elif aoa_coords.shape[0]!=20:continue
#         elif aop_coords.shape[0]!=20:continue
        X1 = raw_coords.values.flatten()
        X2 = aoa_coords.values.flatten()
        X3 = aop_coords.values.flatten()
        X4 = velocity.values.flatten()
        X5 = acceleration.values.flatten()
        X6 = angle.values.flatten()
        X7 = angle_diff.values.flatten()
        X8 = vector_length.values.flatten()
        X9 = acc_corr
        X10 = ang_corr
        X = np.concatenate([X1,X2,X3,X4,X5,X6,X7,X8,X9,X10])
        wt_adult_event_data.append(X)
wt_adult_event_data = np.stack(wt_adult_event_data)<jupyter_output>c:\users\jay\anaconda3\envs\avatar\lib\site-packages\avatarpy\core.py:22: RuntimeWarning:

invalid value encountered in true_divide<jupyter_text>total 1857 events acquired from 5 wt young mice with 5 session.  total 2248 events acquired from 5 wt adult mice with 5 session.<jupyter_code>X = np.concatenate([wt_young_event_data, wt_adult_event_data])
X_ = StandardScaler().fit_transform(X)
X_[np.isnan(X_)] = 0
pca = PCA(n_components=3)
pc = pca.fit_transform(X_)
df = pd.DataFrame(pc,columns=['PC1','PC2', 'PC3'])
y = np.concatenate([np.zeros(wt_young_event_data.shape[0]), np.ones(wt_adult_event_data.shape[0])])
lbl = ['young']*wt_young_event_data.shape[0] + ['adult']*wt_adult_event_data.shape[0]
df['class'] = y
df['genotype'] = lbl
import plotly.express as px
fig = px.scatter_3d(df, x='PC1', y='PC2', z='PC3', color='genotype', opacity=0.5, 
                    range_x=[-50, 50], range_y=[-50, 50], range_z=[-50, 50])
fig.update_traces(marker=dict(size=1))
fig.update_layout(scene_aspectmode='cube')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14436358235556215, 'fraction_numerical': 0.029407396405762662, 'mean_word_length': 3.762376237623762, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '40935', 'license': 'MIT', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2465, 'n_words': 397, 'path': 'jupyter/pca/Group_Data_Analysis_PCA_10th_adding multiple params.ipynb', 'repo_name': 'jeakwon/avatar', 'size': '6735'}