<jupyter_start><jupyter_text>02_plot_headmotion_figure<jupyter_code>import numpy as np
from os.path import join as pjoin
from os.path import isdir
import os
import matplotlib.pyplot as plt
from matplotlib import cm, colors
import mne_bids
import mne
from mne_bids import write_raw_bids, BIDSPath
from scipy import stats
import re
from scipy import signal
import pandas as pd
from scipy import signal, fftpack<jupyter_output><empty_output><jupyter_text>define functions<jupyter_code>def plot_sub_hmv_hist(hpi, save, save_pth, fig_info):
    """
    use hpi channels data to plot histgram of head movement
    Parameters:
        hpi: list of hpi channels data, include channel name and data
        save: bool 
        fig_info: [sub_idx, run_idx]
    """
    coord = ['nasion', 'lpa', 'rpa']
    color = ['lightskyblue', 'lightgreen', 'pink']
    hpi_de = []
    for chn, val in hpi:
        hpi_de.append((chn, 1000*(val-val[0])))

    fig, axes = plt.subplots(1,3,figsize=(16,5))
    fig.suptitle('Head Movement')
    for j in np.arange(3):
        mv = [np.sqrt(hpi_de[j*4][1][n]**2 + hpi_de[j*4+1][1][n]**2 + hpi_de[j*4+2][1][n]**2) for n in np.arange(len(hpi_de[j-1][1]))]
        x, bins, p = axes[j].hist(mv, alpha=0.4, density=True, bins=20, label=coord[j], color=color[j])
        height = []
        for item in p:
            height.append(item.get_height()/sum(x))
            item.set_height(item.get_height()/sum(x))
        axes[j].set_ylim(top=np.ceil(max(height)*10)/10)
        axes[j].legend()
        axes[j].set_xlabel('mm')
        axes[j].set_ylabel('normalized histgram')
        
    if save:
        plt.savefig(pjoin(save_pth, f'sub-{fig_info[0]}_run-{fig_info[1]}_deriv.jpeg'),dpi=600)
        plt.close()<jupyter_output><empty_output><jupyter_text>define variables<jupyter_code>sub_list = ['{0:0>2d}'.format(sub) for sub in np.arange(1,12)]
run_list = ['{0:0>2d}'.format(run) for run in np.arange(1,9)]
coord = ['nasion', 'lpa', 'rpa']
# change path
data_pth = '/nfs/s2/userhome/daiyuxuan/workingdir/MEG-paper/output_data'
bids_root = '/nfs/e2/workingshop/daiyuxuan/MEG-paper/preproc_data'
fig_save_pth = '/nfs/s2/userhome/daiyuxuan/workingdir/MEG-paper/head_mv_fig'<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>hpi_data = pd.read_pickle(pjoin(data_pth, 'hpi_data.pickle'))<jupyter_output><empty_output><jupyter_text>head movement visualization<jupyter_code>head_mv = []
coord = ['nasion', 'lpa', 'rpa']
for j, coil in enumerate(coord):
    coil_mv = np.array([])
    for sub in sub_list:
        for run in run_list:
            hpi = hpi_data[sub][run]
            hpi_de = []
            for i, (chn, val) in enumerate(hpi):
                hpi_de.append((chn, 1000*(val-val[0])))
            mv = [np.sqrt(hpi_de[j*4][1][n]**2 + hpi_de[j*4+1][1][n]**2 + hpi_de[j*4+2][1][n]**2) for n in np.arange(len(hpi_de[j-1][1]))]
            if np.where(np.array(mv)>10)[0].size > 0:
                print(f'lager head movement: sub-{sub} run-{run} coil:{coil}')
            coil_mv = np.append(coil_mv, np.array(mv))
    head_mv.append((coil, coil_mv))

color = ['lightskyblue', 'lightgreen', 'pink']
fig, axes = plt.subplots(1,3,figsize=(16,5))
fig.suptitle('Head Movement')
for j in np.arange(3):
    x, bins, p = axes[j].hist(mv, alpha=0.4, density=True, bins=50, label=coord[j], color=color[j])
    height = []
    for item in p:
        height.append(item.get_height()/sum(x))
        item.set_height(item.get_height()/sum(x))
    axes[j].set_ylim(top=np.ceil(max(height)*10)/10)
    axes[j].legend()
    axes[j].set_xlabel('mm')
    axes[j].set_ylabel('normalized histgram')

plt.savefig(pjoin(save_pth, f'sub-total_run-total_deriv.jpeg'),dpi=600)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16715857257848135, 'fraction_numerical': 0.024684733029246043, 'mean_word_length': 3.9182058047493404, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '350812', 'license': 'MIT', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1501, 'n_words': 261, 'path': 'tech_val/.ipynb_checkpoints/02_plot_headmotion_figure-checkpoint.ipynb', 'repo_name': 'Yuxuan-Dai-psycho/MEG_Gump', 'size': '3727'}