Skip to content

Instantly share code, notes, and snippets.

@yjzhang
yjzhang / plot_clusterings.py
Created February 16, 2014 05:32
Plotting clusterings with matplotlib and numpy
# Plotting clusterings with matplotlib
import numpy as np
import matplotlib.pyplot as plt
def plot_clusterings(data, clusterings, k):
"""
Args:
data - n x d numpy array
clusterings - 1 x n numpy array, values from 0 to k-1
@yjzhang
yjzhang / random_string.py
Created February 5, 2014 20:27
Making a random string
from random import randint
def random_string(n):
return ''.join(chr(randint(0, 25) + ord('a')) for i in range(n))
@yjzhang
yjzhang / list_product.py
Last active September 17, 2015 01:53
Given a list of chars, generate all possible permutations of the chars where the chars can repeat. Equivalent (should be) to itertools.product(chars, repeat=num_chars)
def password_gen(num_chars, chars):
"""
This is a generator
that outputs all passwords of lowercase letters of length
num_chars.
Inputs:
num_chars- the length of the password
chars- a list of the characters that can be part of the password
"""
@yjzhang
yjzhang / gist:53f13a493d8c7a17f7a2
Last active December 20, 2015 06:28
How to use /dev/urandom in python
import os
import binascii
print binascii.hexlify(os.urandom(10))
@yjzhang
yjzhang / sparse_mean_variance.pyx
Last active February 7, 2018 05:01
Find the mean and variance of a sparse csc matrix in cython.
cimport cython
import numpy as np
cimport numpy as np
from libc.math cimport log2
ctypedef fused int2:
short
int
@yjzhang
yjzhang / pandoc-build-html.sh
Created February 14, 2018 04:13
uses pandoc to convert a bunch of files to html
#!/bin/bash
for var in "$@"; do
filename=${var%.*}
#echo $filename
#echo $@
echo "writing to $filename.html"
pandoc $var --self-contained --toc -c /home/yjzhang/Dropbox/Notes/buttondown.css -s --mathml -o $filename.html
done
@yjzhang
yjzhang / draw_logo.py
Last active March 2, 2018 02:31
Creates a DNA motif logo
# code to draw a DNA sequence logo...
import matplotlib.pyplot as plt
from matplotlib import transforms
import matplotlib.patheffects
import numpy as np
def entropy(row):
return -sum(x*np.log2(x+0.0000001) for x in row)
@yjzhang
yjzhang / particle_filter.py
Created March 10, 2018 23:37
particle filter implementation for localization in a rectangular room with 1 gaussian observation.
import numpy as np
from scipy.stats import norm
# particle filter for localization using echolocation
# given: measurements, accelerometer readings, heading
# user's state: [x, y, theta]
# control: d(theta), d(position)
# simple implementation of quantile normalization?
import numpy as np
def quantile_norm(data):
"""
Source: https://en.wikipedia.org/wiki/Quantile_normalization
Note: this doesn't deal with ties very well.
Args:
class TrieNode(object):
def __init__(self, char = '', children = {}, parent = None, wordEnd = False, value = True):
self.char = char
self.children = children
self.parent = parent
self.wordEnd = wordEnd
self.value = value