Skip to content

Instantly share code, notes, and snippets.

@kchawla-pi
kchawla-pi / best_practices_example_before.py
Created January 2, 2020 20:45
A piece of code to point out better coding practices
import glob, hashlib, csv
# replace with file location
path = r'/mnt/chromeos/MyFiles/Downloads/CSVs/'
files = glob.glob(path + r"/*.csv")
output = r"\output.csv"
# this is where we'll store the values
hits = []
@kchawla-pi
kchawla-pi / replace_parameters.py
Last active April 20, 2019 05:53
A python decorator for deprecating parameters from a function/method
"""
Gist made by Kshitij Chawla (Github name: kchawla-pi) for the Nilearn library in Feb/March 2019.
GPLv3
"""
def replace_parameters(replacement_params,
end_version='future',
lib_name='Nilearn',
):
"""
@kchawla-pi
kchawla-pi / nistats_error.py
Created January 30, 2019 19:29
A gist to replicate a specific error in Nistats
import os
from tempfile import NamedTemporaryFile
import pandas as pd
from nose.tools import assert_true
from nistats import datasets
def test_make_events_file_localizer_first_level():
def _download_spm_auditory_data(data_dir, subject_dir, subject_id):
print("Data absent, downloading...")
url = ("http://www.fil.ion.ucl.ac.uk/spm/download/data/MoAEpilot/"
"MoAEpilot.zip")
archive_path = os.path.join(subject_dir, os.path.basename(url))
_fetch_file(url, subject_dir)
try:
_uncompress_file(archive_path)
except:
def fetch_spm_auditory(data_dir=None, data_name='spm_auditory',
subject_id="sub001", verbose=1):
"""Function to fetch SPM auditory single-subject data.
Parameters
----------
data_dir: string
Path of the data directory. Used to force data storage in a specified
location. If the data is already present there, then will simply
glob it.
Returns
@kchawla-pi
kchawla-pi / slice_problem.py
Last active September 11, 2018 14:24
Gist to replicate an error in param checks for nistats.second_level_model.SecondLevelModel.fit()
import nilearn
import os
import pandas
from nistats.second_level_model import SecondLevelModel
# 4D images paths
img1 = nilearn.image.load_img(os.path.expanduser('~/nilearn_data/adhd/data/0010128/0010128_rest_tshift_RPI_voreg_mni.nii.gz'))
img2 = nilearn.image.load_img(os.path.expanduser('~/nilearn_data/adhd/data/2497695/2497695_rest_tshift_RPI_voreg_mni.nii.gz'))
# Single 4D image
@kchawla-pi
kchawla-pi / iterables_for_SearchLight.py
Last active July 23, 2018 13:54
Shows the case where nilearn.decoding.SearchLight gives an error if the cv parameter passed is a generator not an iterable. (line# 75 onwards)
"""
From
nilearn/examples/02_decoding/plot_haxby_searchlight.py
Erring code section is after Line # 75.
"""
"""
Searchlight analysis of face vs house recognition
==================================================
Searchlight analysis requires fitting a classifier a large amount of
@kchawla-pi
kchawla-pi / words_from_textfile.py
Last active February 2, 2018 16:06
Accepts the path to a text file and extracts the words from it, without repetition, and writs them to a text file 'unique_words_list.txt' in the samedirectory/folder.
# -*- encoding: utf-8 -*-
# !/usr/bin/env python3
"""
Accepts the path to a text file and extracts the words from it, without repetition,
and writs them to a text file 'unique_words_list.txt' in the samedirectory/folder.
"""
from pathlib import Path
def extract_words(filepath):