Skip to content

Instantly share code, notes, and snippets.

View raphaelvallat's full-sized avatar

Raphael Vallat raphaelvallat

View GitHub Profile
@raphaelvallat
raphaelvallat / lcm_range.py
Created March 28, 2020 22:30
Least common multiple of a range of numbers
import functools
import numpy as np
for k in range(2, 21):
digits = range(1, k)
i = abs(functools.reduce(np.lcm, digits))
print(f"The least common multiple of {digits} is {i}")
# Output:
@raphaelvallat
raphaelvallat / transition_matrix.py
Last active December 19, 2019 22:29
Markov transition matrix
import numpy as np
def transition_matrix(states):
"""Create Markov transition matrix from 1D array of states.
Parameters
----------
states : array_like
One-dimensional array of state. The dtype of ``x``
must be integer (e.g. [0, 2, 2, 1, 1, 1, ...])
@raphaelvallat
raphaelvallat / distcorr.py
Last active December 12, 2023 06:50 — forked from wladston/distcorr.py
Distance correlation with permutation test
import numpy as np
import multiprocessing
from joblib import Parallel, delayed
from scipy.spatial.distance import pdist, squareform
def _dcorr(y, n2, A, dcov2_xx):
"""Helper function for distance correlation bootstrapping.
"""
# Pairwise Euclidean distances
b = squareform(pdist(y, metric='euclidean'))
@raphaelvallat
raphaelvallat / mutual_info.py
Created February 14, 2019 18:55 — forked from GaelVaroquaux/mutual_info.py
Estimating entropy and mutual information with scikit-learn
'''
Non-parametric computation of entropy and mutual-information
Adapted by G Varoquaux for code created by R Brette, itself
from several papers (see in the code).
These computations rely on nearest-neighbor statistics
'''
import numpy as np
@raphaelvallat
raphaelvallat / plot_number_publications.ipynb
Last active April 2, 2024 20:13
Plot the number of PubMed publications on a specific topic given a range of years
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@raphaelvallat
raphaelvallat / spindles_detect.py
Last active September 23, 2018 00:54
A simple and efficient wavelet-based spindles detector
import numpy as np
def spindles_detect(x, sf, perc_threshold=90, wlt_params={'n_cycles': 7, 'central_freq': 'auto'}):
"""Simple spindles detector based on Morlet wavelet.
Parameters
----------
x : 1D-array
EEG signal
sf : float
@raphaelvallat
raphaelvallat / ecg_derived_respiration.ipynb
Last active December 5, 2023 13:45
Extract respiration signal and respiratory rate from ECG using R-R interval.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@raphaelvallat
raphaelvallat / useful_pandas_snippets.py
Created September 2, 2018 03:12 — forked from bsweger/useful_pandas_snippets.md
Useful Pandas Snippets
# List unique values in a DataFrame column
# h/t @makmanalp for the updated syntax!
df['Column Name'].unique()
# Convert Series datatype to numeric (will error if column has non-numeric values)
# h/t @makmanalp
pd.to_numeric(df['Column Name'])
# Convert Series datatype to numeric, changing non-numeric values to NaN
# h/t @makmanalp for the updated syntax!
@raphaelvallat
raphaelvallat / bruteforce.py
Last active April 22, 2024 20:54
Password brute-force in Python
"""
Password brute-force algorithm.
List of most probable passwords and english names can be found, respectively, at:
- https://github.com/danielmiessler/SecLists/blob/master/Passwords/probable-v2-top12000.txt
- https://github.com/dominictarr/random-name/blob/master/middle-names.txt
Author: Raphael Vallat
Date: May 2018
Python 3
@raphaelvallat
raphaelvallat / hrf.py
Created March 13, 2018 22:52
Hemodynamic Response Function
# Thanks to http://www.jarrodmillman.com/rcsds/lectures/convolution_background.html
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gamma
def hrf(x):
""" Return values for HRF at given times """
# Gamma pdf for the peak
peak_values = gamma.pdf(x, 6)
# Gamma pdf for the undershoot