Skip to content

Instantly share code, notes, and snippets.

View ofgulban's full-sized avatar

Omer Faruk Gulban ofgulban

View GitHub Profile
@ofgulban
ofgulban / nii_max_intensity_projection.py
Created November 2, 2022 15:54
Maximum intensity projection on nifti image
"""Maximum intensity projection over one dimension for a window."""
import os
import numpy as np
from nibabel import load, Nifti1Image, save
nii = load('/path/to/file.nii.gz')
w = 10 # window, total width is double of this number
@ofgulban
ofgulban / make_video.sh
Last active March 7, 2022 17:51
Make MP4 and GIF files from multiple PNGs using ffmpeg.
# Compile an MP4 from a folder containing PNGs
ffmpeg -y -i /path/to/movie_frame-%03d.png -vb 20M -c:v libx264 -vf fps=24 -pix_fmt yuv420p path/to/movie.mp4
# Convert mp4 into a high quality gif
ffmpeg -y -i path/to/movie.mp4 -vf "fps=24, scale=768:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 path/to/movie.gif
@ofgulban
ofgulban / segmentation_polish.py
Created February 21, 2022 08:37
Smoothen manually edited MRI segmentations to reduce jittery borders.
"""Polish manually edited MRI white & gray matter segmentations."""
import os
import nibabel as nb
import numpy as np
from scipy.ndimage import morphology, generate_binary_structure
from scipy.ndimage import gaussian_filter
# Segmentation file
FILE = '/path/to/segmentation_01.nii.gz'
@ofgulban
ofgulban / pearson_1986_spurious_correlations.py
Created February 2, 2022 10:54
Simulating the spurious correlation of ratios. Run this script multiple times and see the correlations between variables.
"""Simulate the spurious correlation of ratios.
Run this script multiple times and see the correlations between variables.
Reference
---------
- Pearson, K. (1896). Mathematical Contributions to the Theory of Evolution.
On a Form of Spurious Correlation Which May Arise When Indices Are Used in the
Measurement of Organs. Proceedings of the Royal Society of London, 60, 489–498.
<https://doi.org/10.1098/rspl.1896.0076>
@ofgulban
ofgulban / pyvista_360.py
Created October 24, 2021 11:23
Generate 360 rotating with pyvista
"""Used for rendering frames as png files."""
import sys
import os
import numpy as np
import pyvista as pv
import nibabel as nb
FILENAME = "/path/to/image3D.nii.gz"
OUTDIR = "/path/to/output"
@ofgulban
ofgulban / VOItoVMR.m
Created April 24, 2021 15:14
Convert BrainVoyager reions of interests in volume (VOI) to .vmr files
% Convert BrainVoyager reions of interests in volume (VOI) to .vmr files
%
% Similar to draw VOI in VMR option in BV but assigns unique numbers for
% each voi.
clear all
vmr_file = '/path/to/file.vmr';
voi_file = '/path/to/file.voi';
@ofgulban
ofgulban / nii_fix_affine.py
Last active January 18, 2021 19:00
Make affine matrix of a nifti file identity matrix.
"Make affine matrix of a nifti file identity matrix."
import os
import nibabel as nb
import numpy as np
NII = "/path/to/tile.nii.gz"
nii = nb.load(NII)
data = np.asarray(nii.dataobj)
@ofgulban
ofgulban / nifti_nonzero_volume.py
Last active September 24, 2020 18:17
Compute volume of non-zero voxels in a nifti image.
"""Compute volume of non-zero voxels in a nifti image."""
import numpy as np
import nibabel as nb
INPUT = "/path/to/image.nii.gz"
# Load data
nii = nb.load(INPUT)
img = nii.get_fdata()
@ofgulban
ofgulban / mask_detect.py
Last active September 30, 2020 05:42
A simple score that might be useful to detect masked nifti files.
"""Masked-ness score by source and derivative zero count ratio for images."""
import numpy as np
import nibabel as nb
INPUT = "/path/to/image.nii.gz"
# =============================================================================
def maskedness_score(img):
@ofgulban
ofgulban / SRF_to_OBJ.m
Last active February 23, 2020 14:08
Conversion from Brainvoyager SRF triangular mesh format to Wavefront OBJ format.
% BrainVoyager SRF triangular mesh format to Wavefront OBJ format conversion.
%{
Dependencies
------------
1- Neuroelf should be added to Matlab path. Source: http://neuroelf.net/,
tested version: 1.0
2- Resulting file can be loaded to e.g. Meshlab.
%}
clear all;