Skip to content

Instantly share code, notes, and snippets.

View jcheong0428's full-sized avatar
🥕

Jin Hyun Cheong jcheong0428

🥕
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jcheong0428
jcheong0428 / publication_ready_plots_in_matplotlib.py
Last active September 14, 2018 17:57
Creating illustrator ready matplotlib plots
# This sets the pdf font type so that it imports correctly in Adobe Illustrator.
# Source: http://jonathansoma.com/lede/data-studio/matplotlib/exporting-from-matplotlib-to-open-in-adobe-illustrator/
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
# Generating 2x retina plots
%config InlineBackend.figure_format = 'retina'
# Set base figure size
@jcheong0428
jcheong0428 / parse_triangle.py
Created September 14, 2018 18:04
Helper function to easily extract parts of a symmetrical N by N matrix
import numpy as np
import pandas as pd
def parse_triangle(df, condition='upper'):
'''
This function grabs the upper triangle of a correlation matrix
by masking out the bottom triangle (tril) and returns the values.
You can use scipy.spatial.distance.squareform to recreate matrix from upper triangle
Args:
df: pandas or numpy correlation matrix
@jcheong0428
jcheong0428 / rpy2_setup.py
Created September 15, 2018 02:42
Set of codes to setup rpy2 environment to python notebook
# First load rpy2
%load_ext rpy2.ipython
# Then load packages in a separate cell
%%R
require('lme4')
require('lmerTest')
require('lattice')
require('boot')
require('sjPlot')
@jcheong0428
jcheong0428 / correct_df.py
Created September 15, 2018 03:21
Correcting degrees of freedom from lmer output
# Correct p-value by adjusting degrees of freedom.
# Degrees of freedom is divided by two and then looked up
# Reference:
# https://www.ncbi.nlm.nih.gov/pubmed/27751943
# Chen, G., Taylor, P. A., Shin, Y. W., Reynolds, R. C., & Cox, R. W. (2017). Untangling the relatedness among correlations, Part II: Inter-subject correlation group analysis through linear mixed-effects modeling. Neuroimage, 147, 825-840.
from scipy.stats import t
tt = 9.83
df = 17.6/2.
pval = t.sf(np.abs(tt), df)*2
@jcheong0428
jcheong0428 / FPL_form_v_fixture_share.ipynb
Created January 1, 2019 18:22
FPL_form_v_fixture_share
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jcheong0428
jcheong0428 / crosscorr.py
Created March 6, 2019 19:30
Cross Correlation Function
def crosscorr(datax, datay, lag=0):
""" Lag-N cross correlation.
Calculates cross correlations using pandas functionality that can be used in a list comprehension.
Parameters
----------
lag : int, default 0
datax, datay : pandas.Series objects of equal length
Returns
@jcheong0428
jcheong0428 / synchrony01.py
Last active March 13, 2023 19:45
synchrony_pearsonr
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as stats
df = pd.read_csv('synchrony_sample.csv')
overall_pearson_r = df.corr().iloc[0,1]
print(f"Pandas computed Pearson r: {overall_pearson_r}")
@jcheong0428
jcheong0428 / moving window synchrony
Last active November 25, 2021 16:10
synchrony02.py
# Set window size to compute moving window synchrony.
r_window_size = 120
# Interpolate missing data.
df_interpolated = df.interpolate()
# Compute rolling window synchrony
rolling_r = df_interpolated['S1_Joy'].rolling(window=r_window_size, center=True).corr(df_interpolated['S2_Joy'])
f,ax=plt.subplots(2,1,figsize=(14,6),sharex=True)
df.rolling(window=30,center=True).median().plot(ax=ax[0])
ax[0].set(xlabel='Frame',ylabel='Smiling Evidence')
rolling_r.plot(ax=ax[1])
@jcheong0428
jcheong0428 / synchrony03.py
Last active February 7, 2022 04:25
Cross correlation function
def crosscorr(datax, datay, lag=0, wrap=False):
""" Lag-N cross correlation.
Shifted data filled with NaNs
Parameters
----------
lag : int, default 0
datax, datay : pandas.Series objects of equal length
Returns