Skip to content

Instantly share code, notes, and snippets.

View kingjr's full-sized avatar

Jean-Rémi KING kingjr

View GitHub Profile
@kingjr
kingjr / tanh_scaler.py
Created October 29, 2020 15:02
tanh_scaler
import numpy as np
from sklearn.preprocessing import RobustScaler
from sklearn.base import TransformerMixin
class TanhScaler(TransformerMixin):
def __init__(self,
factor=2.,
decim=1,
n_max=None,
shuffle=False,
class TimeGenScorer:
def __init__(self, n_times, train=None, cuda=False):
self.test_times = range(n_times)
if train is None:
train = slice(None, None, 1)
self.train_times = self.test_times[train]
self.cuda = cuda
def __call__(self, Y_true, Y_pred):
@kingjr
kingjr / cluster_api.py
Created February 19, 2020 11:01
cluster test
def stats(X, n_jobs=-1, seed=0, **kwargs):
from mne.stats import spatio_temporal_cluster_1samp_test
X = np.asarray(X)
if X.ndim==2:
X = X[:, :, None]
_, clusters, c_pv, _ = spatio_temporal_cluster_1samp_test(
X, n_jobs=n_jobs, seed=seed, out_type='mask', **kwargs
)
p_vals = [(mask*p + (mask==0)) for mask, p in zip(clusters, c_pv)
if p < .05]
@kingjr
kingjr / utils.py
Last active September 25, 2020 16:44
scale correlate
def scale(X):
m = X.mean(0, keepdims=True)
s = X.std(0, keepdims=True)
s[s == 0] = 1 # avoid nan
X -= m
X /= s
return X
@kingjr
kingjr / extract_2d.py
Created January 17, 2020 10:08
extract_2d.py
import mne
import numpy as np
import matplotlib.pyplot as plt
import scipy
import torch
from itertools import product
import pandas as pd
import seaborn as sns
def cart_to_pol(x, y):
@kingjr
kingjr / plot_arch.ipynb
Created January 13, 2020 14:23
plot_arch.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
@kingjr
kingjr / block_ridge.py
Last active December 20, 2019 14:24
block ridge: WIP
from sklearn.linear_model import RidgeCV
import numpy as np
from scipy import linalg
class BlockRidgeCV(RidgeCV):
def __init__(self, alphas, blocks, fit_intercept=False, **kwargs):
super().__init__(**kwargs)
self.fit_intercept = False
self.alphas = alphas
@kingjr
kingjr / for_danilo.py
Created December 3, 2019 13:31
OLS vs PLS
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.cross_decomposition import PLSRegression
from sklearn.model_selection import cross_val_score
from scipy.stats import pearsonr
ols = LinearRegression()
pls = PLSRegression()
@kingjr
kingjr / arch_plots.py
Last active December 6, 2019 15:25
architecture_plot
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
n_layers = 4
n_steps = 4
fig, axes = plt.subplots(1, 3, sharex=True, sharey=True, figsize=[10, 3])