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 / 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ryanpeach
ryanpeach / setup.cfg
Last active October 1, 2024 03:25
A good setup.cfg for python
[tool.ruff.lint]
select = ["ALL"]
ignore = [
"D203", # Should ignore one of D203 or D211 bc they are conflicting
"D213", # Should ignore one of D212 or D213 bc they are conflicting
"COM812", # Don't require trailing commas
"D205", # Don't require 1 blank line between summary and description
"TD002", # Don't require author for TODOs
"TD003", # Don't require issues for TODOs
"S101", # Allow asserts
@ryanpeach
ryanpeach / .pre-commit-config.yaml
Created September 11, 2024 19:43
My favorite pre-commits
exclude: "docs|.git"
default_stages: [commit]
fail_fast: false
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
@ryanpeach
ryanpeach / Cargo.toml
Last active January 1, 2025 18:28
My favorite rust packages and settings
[profile.dev]
opt-level = 0
debug = true
[profile.release]
opt-level = 3
debug = false
[dependencies]
# Errors
@ryanpeach
ryanpeach / funnel.scad
Last active April 20, 2025 01:03
Anycubic Photon Mono X2 funnel
// Anycubic Photon Mono X2 funnel
// build_x, build_y, build_z from manufacturer specs
build_x = 122; // mm, max X (left–right)
build_y = 196; // mm, max Y (front–back)
build_z = 200; // mm, max Z (vertical)
tolerance = 2; // Avoid the edges of the max build spec
// funnel parameters
wall_thickness = 6; // mm, wall thickness
base_dia = 25.4; // mm, spout diameter ≈1 in