Skip to content

Instantly share code, notes, and snippets.

@htwangtw
htwangtw / runtime_profile.py
Created October 25, 2022 15:42
Profiling run time of different nilearn nifti masker objects
# nilearn version 0.9.2
import time
import warnings
warnings.filterwarnings(action='ignore')
from pathlib import Path
import numpy as np
@htwangtw
htwangtw / plot_matrix.py
Created April 14, 2022 01:12
Matrix with numbers in the cell with the opposit colour
import string
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
n = 7
mat = np.random.rand(n, n)
labels = list(string.ascii_lowercase)[:n]
ind_array = np.arange(0, n, 1)
@htwangtw
htwangtw / run_tmux_fmriprep.sh
Last active March 16, 2022 20:18
run fmriprep in multiple tmux sessions in parallel
#!/bin/bash
# NOTE: make sure freesurfer licence is under ${HOME}/.freesurfer.txt before running the script
SING_IMG="/data/cisl/containers/fmriprep-20.2.1lts.sif"
DATASET="ds000228"
DATASET_DIR="/data/cisl/datasets/${DATASET}"
# run BIDS validator
singularity exec -B ${DATASET_DIR}/${DATASET}_openfmri:/DATA \
${SING_IMG} bids-validator \
/DATA
@htwangtw
htwangtw / hijack_templateflow.py
Last active February 8, 2022 22:15
Use TemplateFlow API to read self-defined directory with files in TemplateFlow naming scheme
from pathlib import Path
from templateflow import api as tflow
from templateflow import conf as tf_conf
TEMPLATEFLOW_HOME = "./data/raw/mytemplateflow"
tf_conf.TF_HOME = Path(TEMPLATEFLOW_HOME)
tf_conf.update() # disable if you don't need access to actual templateflow data
tf_conf.init_layout()
tflow.get("myspace", resolution="02", atlas="myatlas",
@htwangtw
htwangtw / smr2bids.py
Created August 5, 2021 12:47
CED Spike2 file convert to BIDS competible format with SONPY
from pathlib import Path
import json
import numpy as np
from numpy.core.fromnumeric import compress
import pandas as pd
import sonpy
@htwangtw
htwangtw / create_cifti.py
Created April 20, 2021 14:21
Create cifti with random data
import nibabel as nb
import numpy as np
original = nb.load("sub-A00018030_ses-FLU1_task-rest_acq-645_space-fsLR_den-91k_bold.dtseries.nii")
meta = {k: i for k, i in original.header.matrix.metadata.data.items()}
data = np.random.rand(30, 100)
vertices = nb.cifti2.Cifti2VertexIndices(np.arange(data.shape[-1]))
bm = nb.cifti2.Cifti2BrainModel(
@htwangtw
htwangtw / scca_environment.yml
Created November 14, 2019 15:42
Conda environment combining Python and R for sparse CCA package "PMA"
name: scca
channels:
- defaults
dependencies:
- _libgcc_mutex=0.1=main
- _r-mutex=1.0.0=anacondar_1
- binutils_impl_linux-64=2.31.1=h6176602_1
- binutils_linux-64=2.31.1=h6176602_8
- blas=1.0=mkl
- bwidget=1.9.11=1
@htwangtw
htwangtw / neuro_jupyter.yml
Created November 11, 2019 12:26
General purpose neuroimaging conda environment
name: neuro
channels:
- conda-forge
- defaults
dependencies:
- ca-certificates=2019.9.11=hecc5488_0
- certifi=2019.9.11=py37_0
- dbus=1.13.6=he372182_0
- dcm2niix=1.0.20190902=hc9558a2_0
- fontconfig=2.13.1=h86ecdb6_1001