Skip to content

Instantly share code, notes, and snippets.

@bthirion
bthirion / position_label.py
Created January 28, 2022 16:41
How to get a difumo label for a given position
from nilearn.datasets import fetch_atlas_difumo
from nilearn.input_data import NiftiSpheresMasker
import numpy as np
d256 = fetch_atlas_difumo(dimension=256)
seeds = [[-52, 14, 0]] # this is your position, you may pick any you wish
masker = NiftiSpheresMasker(seeds=seeds, radius=2).fit()
weights = masker.transform(d256.maps)
label = d256.labels[np.argmax(weights)]
print('label:', label)
@bthirion
bthirion / script.py
Created June 15, 2017 11:33
Pysurfer issue
from nibabel import load
tex1 = np.array(load('sample_map_lh.gii').darrays[0].data)
tex2 = np.array(load('grp_stat_lh.gii').darrays[0].data)
brain = Brain("fsaverage", 'lh', "inflated", title='', views=['lat'])
brain.add_overlay(tex1, hemi='lh', min=3., max=8.)
brain.save_montage('sample_map_lh.png')
brain = Brain("fsaverage", 'lh', "inflated", title='', views=['lat'])
@bthirion
bthirion / neighbors.py
Created January 5, 2016 21:26
Get the neighbors of target voxels
"""
Get the neighbors of target voxels
"""
import numpy as np
from nilearn import datasets
from nilearn import input_data
from sklearn.feature_extraction import image
### Load nyu_rest dataset #####################################################
@bthirion
bthirion / problem_gcv_explained.py
Created January 3, 2016 08:46
sklearn's gcv is actually formally correct, i.e. equivalent to standard mse in leave-one-out, BUT in a setting (fit_intercept=False, data pre-centered) that implies that the result is wrong, at least when n_features >= n_samples - 1
"""
Experiment on gcv to understand the issue:
It is indeed equivalent to leave-one-out selection with mean_squared error
but in a setting without fit_intercept, and with y and X initially centered.
In theory, this is correct.
Author: Bertrand Thirion, 2016
"""
import numpy as np
from sklearn import linear_model
@bthirion
bthirion / issue_read_img_data.py
Last active August 29, 2015 13:58
Issue with nibabel.read_img_data
from os import path
import numpy as np
import matplotlib.pyplot as plt
from nibabel import load
from nibabel.loadsave import read_img_data
# Local import to $NIPY/examples/labs/need_data
from get_data_light import DATA_DIR, get_first_level_dataset
@bthirion
bthirion / problem_gcv.py
Last active December 15, 2015 08:49
Experiment on gcv illustrating the problem with sklearn's gcv. This occurs when p > n: due to centering, the design matrix becomes rank deficient in the scikit implementation, which messes numerical aspects and makes the sklearn's selection really bad.
import numpy as np
from sklearn import linear_model
def _gcv(X, y, alphas):
"""Local gcv"""
# singular values of the design matrix
_, s, _ = np.linalg.svd(X, 0)
clf = linear_model.Ridge()
best_gcv = np.inf
for alpha in alphas:
@bthirion
bthirion / README.rst
Created December 30, 2011 11:10 — forked from jakevdp/README.rst
GMM BIC/AIC test

This includes a test of the new GMM routines in https://github.com/bthirion/scikit-learn/tree/gmm-fixes

By changing the line

GMM = mixture.GMM

at the top of the file, we can plot the BIC and AIC for each variant of GMM. Standard GMM works beautifully: it settles in on 3 components, which are a good description of the data. DPGMM and VBGMM produce some unexpected results.