Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View ryanpeach's full-sized avatar

Ryan Peach ryanpeach

View GitHub Profile
@ryanpeach
ryanpeach / flatten.py
Created April 26, 2020 03:18
Flatten
import collections
def flatten(l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
yield from flatten(el)
else:
yield el
@ryanpeach
ryanpeach / throttling_decorator.py
Created April 26, 2020 03:12
Boto3 Throttling Replay Decorator
import time
import functools
from boto3.exceptions import ThrottlingException
def throttling_handler(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
while True:
try:
@ryanpeach
ryanpeach / get_project_dir.sh
Created April 26, 2020 03:09
Get the project directory as defined by the nearest git repo up your directory chain.
#!/usr/bin/env bash
export PROJECT_DIR=$(git rev-parse --show-toplevel)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ryanpeach
ryanpeach / duality.py
Last active December 2, 2018 08:44
Duality Theorum Solver
from copy import deepcopy
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
G = nx.DiGraph()
class Literal(str):
@ryanpeach
ryanpeach / window_whitelist.py
Last active January 10, 2018 16:22
Using wmctrl in Linux, close any windows who's titles are not recognized via a whitelist (with regex).
# coding: utf-8
import subprocess
import re
def get_windows():
list_of_windows = subprocess.check_output(["wmctrl","-l"]).split(b"\n")
meta_list_of_windows = [s.decode("utf-8").split() for s in list_of_windows if s]
user_processes = {}
for line in meta_list_of_windows:
@ryanpeach
ryanpeach / gridplot.py
Created April 23, 2017 01:07
Useful if you have a dataframe, with a certain number of unique categories or values in x and y, and you want to compare them visually to a third continuous value.
def grid_plot(x_label, y_label, z_label, data, ax=None):
""" Useful if you have a dataframe, with a certain number of unique categories or values in x and y,
and you want to compare them visually to a third continuous value. """
if ax is None:
ax = plt.gca()
x_val = np.sort(np.unique(data[x_label]))
y_val = np.sort(np.unique(data[y_label]))
x_idx = np.arange(len(x_val))
y_idx = np.arange(len(y_val))
import random as rand
from functools import partial
def twiddle(run, args, p, dp, tol = 0.2, N = 100, logger = None):
""" Uses gradient descent to find the optimal value of p as input for function run.
run is a function which takes p as an argument and returns an error (with 0 being optimal) as an output.
dp is the initial magnitute for each index of p to begin
N is the max number of iterations, after which the best value of p is returned.
tol is the max error allowed, under which this function will terminate. """
best_err, best_p, best_dp, n = 1000000, None, None, 0
@ryanpeach
ryanpeach / earlystopping.py
Last active November 28, 2023 08:22
A Python 3 implementation of the early stopping algorithm described in the Deep Learning book by Ian Goodfellow. Untested, needs basic syntax correction.
""" Python 3 implementation of Deep Learning book early stop algorithm.
@book{Goodfellow-et-al-2016,
title={Deep Learning},
author={Ian Goodfellow and Yoshua Bengio and Aaron Courville},
publisher={MIT Press},
note={\url{http://www.deeplearningbook.org}},
year={2016}
}
"""
@ryanpeach
ryanpeach / ransac.py
Created December 1, 2016 04:16
An implementation of RANSAC with a linear fit model.
import numpy as np
import cv2
import random
import scipy.stats
# Matplotlib
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt