Skip to content

Instantly share code, notes, and snippets.

View ryanpeach's full-sized avatar

Ryan Peach ryanpeach

View GitHub Profile
@ryanpeach
ryanpeach / timeout.py
Created September 23, 2016 06:57
Good for single loop timeout functions which may or may not contain a non-nested timeout element themselves. Ran into issues when nesting this function within one of it's own, timeout is not referenced within sub-scopes.
from time import time, sleep
from functools import wraps
import socket
import unittest
class TimeoutError(socket.timeout):
pass
none_val = lambda x: 0.0 if x is None else float(x) # Returns float(x), or 0 if x is None
@ryanpeach
ryanpeach / hungarian.py
Created September 28, 2016 18:03
A Python 2 implementation of both the Hungarian and Murty's Algorithm, which I believe runs in O(n^3) time, with sources, memoization, testing methods, and capable of being run in parallel instances with multiprocessing.pool.
# Ryan Peach 3/1/2016
# References Used for this Implementation
# https://en.wikipedia.org/wiki/Hungarian_algorithm
# https://github.com/bmc/munkres/blob/master/munkres.py
# http://csclab.murraystate.edu/bob.pilgrim/445/munkres.html
# ---
# No copying and pasting of online code was performed, though some code may turn out to be similar due to the standardized nature of this algorithm.
# This is a very different implementation due to the fact that it heavily uses numpy, vastly simplifies many poorly pythonized coding elements
# removes the "class" approach for a function based one. Etc.
# Improvements that need to be made is to require the matrix class, and to vectorize iterations through the matrix.
@ryanpeach
ryanpeach / frozenlakeq675.md
Created November 2, 2016 23:43
Frozen Lake v0 Q Learner Writeup 675
@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
@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}
}
"""
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 / 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))
@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 / 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):
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.