Skip to content

Instantly share code, notes, and snippets.

@emanuele
emanuele / rescale.py
Created March 8, 2022 08:43
Rescale all predictions between prediction_min and prediction_max keeping the same ranking within each moon. Rescaling is necessary to avoid invalid submissions having predictions < 0.0 or > 1.0.
def rescale_predictions_moons(prediction, moon_test, prediction_min=0.001, prediction_max = 0.999):
"""Rescale all predictions between prediction_min and prediction_max
keeping the same ranking within each moon.
Rescaling is necessary to avoid invalid submissions having
predictions < 0.0 or > 1.0.
@emanuele
emanuele / tck2trk.py
Created December 5, 2019 08:51 — forked from MarcCote/tck2trk.py
Script that converts TCK to TRK using https://github.com/MarcCote/nibabel/tree/streamlines_tck
import os
import argparse
import nibabel as nib
from nibabel.streamlines import Field
from nibabel.orientations import aff2axcodes
def build_argparser():
DESCRIPTION = "Convert tractograms (TCK -> TRK)."
@emanuele
emanuele / tck2trk.py
Created December 5, 2019 08:51 — forked from MarcCote/tck2trk.py
Script that converts TCK to TRK using https://github.com/MarcCote/nibabel/tree/streamlines_tck
import os
import argparse
import nibabel as nib
from nibabel.streamlines import Field
from nibabel.orientations import aff2axcodes
def build_argparser():
DESCRIPTION = "Convert tractograms (TCK -> TRK)."
@emanuele
emanuele / log
Created May 28, 2019 17:03
Error when compiling the current git-annex from sources on armel (armv5tel, kirkwood).
cabal build
Building git-annex-7.20190507...
Preprocessing executable 'git-annex' for git-annex-7.20190507...
[379 of 612] Compiling Remote.S3 ( Remote/S3.hs, dist/build/git-annex/git-annex-tmp/Remote/S3.o )
Remote/S3.hs:462:33: error:
Not in scope: ‘S3.getBucketObjectVersions’
Module ‘Aws.S3’ does not export ‘getBucketObjectVersions’.
Remote/S3.hs:480:19: error:
@emanuele
emanuele / remap.py
Created February 27, 2019 22:24
resize image then remap resized image to a given limited palette
#!/usr/bin/env python3
# from: https://stackoverflow.com/questions/53477624/python-pil-image-convert-not-replacing-color-with-the-closest-palette
import subprocess
import numpy as np
from PIL import Image
palette = [
0,0,0,
@emanuele
emanuele / test_bresenham_vs_streamline_mapping.py
Last active November 16, 2018 14:12
Bresenham vs streamline_mapping()
"""
N-D Bresenham line algo , from http://code.activestate.com/recipes/578112-bresenhams-line-algorithm-in-n-dimensions/
"""
import numpy as np
def _bresenhamline_nslope(slope):
"""
Normalize slope for Bresenham's line algorithm.
>>> s = np.array([[-2, -2, -2, 0]])
>>> _bresenhamline_nslope(s)
@emanuele
emanuele / load_as_tractome.py
Last active August 29, 2015 14:20
This function loads a trackvis file and filters out streamlines in the exact same way as tractome does. This is a necessary step in order to use segmentations done with tractome (.seg files).
from nibabel import trackvis
from dipy.tracking.metrics import length
def load_tractome_tractography(filename_tractography):
"""
Load trk tractography following nasty (i.e. to be fixed) tractome
convention, i.e. removing short streamlines first.
See:
https://github.com/FBK-NILab/tractome/issues/17
@emanuele
emanuele / experiments_MEG.py
Created March 30, 2012 09:31
Testing multiclass pattern discrimination: experiments on MEG data - PRNI2012
"""Statistical tests for multiclass pattern discrimination.
Confusion matrices from ICANN2011 MEG competition.
See http://www.cis.hut.fi/icann2011/meg/megicann_proceedings.pdf
p.14
"""
import numpy as np
from scipy.special import gammaln
from scipy.stats import binom
@emanuele
emanuele / polya_issue.py
Created March 3, 2012 20:45
Issue with multivariate Polya distribution estimation
import numpy as np
from scipy.special import gammaln
def log_multivariate_polya(X, alpha):
N = X.sum()
A = alpha.sum()
log_likelihood = gammaln(N+1) - gammaln(X+1).sum()
log_likelihood += gammaln(A) - gammaln(alpha).sum()
log_likelihood += gammaln(X + alpha).sum() - gammaln(N + A)
return log_likelihood