Skip to content

Instantly share code, notes, and snippets.

View dojeda's full-sized avatar
🏠
Working from home

David Ojeda dojeda

🏠
Working from home
View GitHub Profile
import time
import random
from IPython import parallel
# create client & view
rc = parallel.Client()
dv = rc[:]
# scatter 'id', so id=0,1,2 on engines 0,1,2
ks.default <- function(rows) seq(2, max(3, rows %/% 4))
many_kmeans <- function(x, ks = ks.default(nrow(x)), ...) {
ldply(seq_along(ks), function(i) {
cl <- kmeans(x, centers = ks[i], ...)
data.frame(obs = seq_len(nrow(x)), i = i, k = ks[i], cluster = cl$cluster)
})
}
all_hclust <- function(x, ks = ks.default(nrow(x)), point.dist = "euclidean", cluster.dist = "ward") {
import logging
class QueueHandler(logging.Handler):
"""
This handler sends events to a queue. Typically, it would be used together
with a multiprocessing Queue to centralise logging to file in one process
(in a multi-process application), so as to avoid file write contention
between processes.
This code is new in Python 3.2, but this class can be copy pasted into
@ctokheim
ctokheim / matplotlib_barplot.md
Last active February 13, 2023 12:06
Matplotlib: Stacked and Grouped Bar Plot

Stacked and Grouped Bar Plot

Oddly enough ggplot2 has no support for a stacked and grouped (position="dodge") bar plot. The seaborn python package, although excellent, also does not provide an alternative. However, I knew it was surely possible to make such a plot in regular matplotlib. Matplotlib, although sometimes clunky, gives you enough flexibility to precisely place plotting elements which is needed for a stacked and grouped bar plot.

Below is a working example of making a stacked and grouped bar plot.

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
@stuaxo
stuaxo / processify.py
Last active April 29, 2023 21:20 — forked from schlamar/processify.py
processify
# modified from https://gist.github.com/schlamar/2311116#file-processify-py-L17
# also see http://stackoverflow.com/questions/2046603/is-it-possible-to-run-function-in-a-subprocess-without-threading-or-writing-a-se
import inspect
import os
import sys
import traceback
from functools import wraps
from multiprocessing import Process, Queue
@endolith
endolith / frequency_estimator.py
Last active October 30, 2023 18:08
Frequency estimation methods in Python
from __future__ import division
from numpy.fft import rfft
from numpy import argmax, mean, diff, log, nonzero
from scipy.signal import blackmanharris, correlate
from time import time
import sys
try:
import soundfile as sf
except ImportError:
from scikits.audiolab import flacread
@seberg
seberg / rolling_window.py
Created October 10, 2012 14:38
Multidimensional rolling_window for numpy
def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toend=True):
"""Create a view of `array` which for every point gives the n-dimensional
neighbourhood of size window. New dimensions are added at the end of
`array` or after the corresponding original dimension.
Parameters
----------
array : array_like
Array to which the rolling window is applied.
window : int or tuple
@iamatypeofwalrus
iamatypeofwalrus / roll_ipython_in_aws.md
Last active January 22, 2024 11:18
Create an iPython HTML Notebook on Amazon's AWS Free Tier from scratch.

What

Roll your own iPython Notebook server with Amazon Web Services (EC2) using their Free Tier.

What are we using? What do you need?

  • An active AWS account. First time sign-ups are eligible for the free tier for a year
  • One Micro Tier EC2 Instance
  • With AWS we will use the stock Ubuntu Server AMI and customize it.
  • Anaconda for Python.
  • Coffee/Beer/Time
@wadey
wadey / iterm2.zsh
Last active March 10, 2024 00:32
Change iTerm2 tab color when using SSH
# Usage:
# source iterm2.zsh
# iTerm2 tab color commands
# https://iterm2.com/documentation-escape-codes.html
if [[ -n "$ITERM_SESSION_ID" ]]; then
tab-color() {
echo -ne "\033]6;1;bg;red;brightness;$1\a"
echo -ne "\033]6;1;bg;green;brightness;$2\a"
@vertexclique
vertexclique / cracking.md
Last active April 8, 2024 18:24
Cracking guide for Sublime Text 3 Build 3059 / 3065 ( Mac / Win x86_64 / Windows x86 / Linux x64 / Linux x86 )

MacOS

Build 3059

MD5: 59bab8f71f8c096cd3f72cd73851515d

Rename it to: Sublime Text

Make it executable with: chmod u+x Sublime\ Text