Skip to content

Instantly share code, notes, and snippets.

View jcheong0428's full-sized avatar
🥕

Jin Hyun Cheong jcheong0428

🥕
View GitHub Profile
Loading
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
Loading
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jcheong0428
jcheong0428 / hungarian1.py
Created July 14, 2019 05:46
First gist for Hungarian post.
# First gist for Hungarian post.
# Brute force estimation of optimal assignment for given preference.
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context('talk')
sns.set_style('white')
import itertools
import numpy as np
@jcheong0428
jcheong0428 / hungarian2.py
Created July 14, 2019 05:53
Estimates the time needed for brute force optimal assignment for group size 10.
%%timeit
# Second gist for Hungarian post.
# Estimates the time needed for brute force optimal assignment for group size 10.
# Takes about 3 minutes.
maxsums, colixs = [], []
group_size = 10
np.random.seed(0)
preferences = np.random.rand(group_size,group_size)
rowix = list(range(group_size)) # rows don't need to be permuted