Skip to content

Instantly share code, notes, and snippets.

View rldotai's full-sized avatar

Brendan Bennett rldotai

View GitHub Profile
@rldotai
rldotai / latex_table_maker.py
Created December 19, 2015 06:40
A quick script to format tables that were in the `verbatim` environment as proper tables using Pandas.
#!python3
"""
A script to read a document containing tables that I left as "verbatim" and
format them properly in LaTeX.
We go from this:
\begin{verbatim}
sparsity num_active accuracy test_accuracy train_accuracy
count 4000.000000 4000.000000 4000.000000 4000.000000 4000
@rldotai
rldotai / Makefile
Last active November 23, 2020 20:59
A makefile for LaTeX and Skim on OS X
PDFLATEX = pdflatex -interaction=batchmode -synctex=1
SH = /bin/bash
ASCRIPT = /usr/bin/osascript
SOURCE = master.tex
BASE = "$(basename $(SOURCE))"
default : pdf view
.PHONY: pdf graphics
@rldotai
rldotai / quicktex.sh
Created December 19, 2015 07:10
A quick way to build LaTeX files while purging the auxiliary files pdflatex generates.
#!/bin/bash
# Generate a PDF from a `.tex` file, then remove the auxiliary files with the same base name.
#
# Usage:
# ./quicktex.sh myfile.tex
# the first argument should be the tex file, either with or without extension
file="$1"
# remove the suffix (if it's .tex)
@rldotai
rldotai / vector.py
Last active December 15, 2016 17:58
Pure Python Vector class
"""
A simple implementation of vectors in Python modelled after tuples, but having
altered some of the operations to be closer to vector arithmetic.
It targets Python 3.5+ in order to support __matmul__, allowing for dot
products, which I have heard are important in linear algebra.
We check for whether it is operating on a number in order to implement
broadcasting; otherwise it performs the operations elementwise.
We also use `zip_longest` because it performs an implicit check for sequences
of unequal length; it pads the iteration with `None`, which will raise an error
@rldotai
rldotai / sliding_window.py
Created January 21, 2016 02:10
Extracting patches from arrays
"""
Code for extracting sliding windows from arrays.
Particularly useful when you want to take patches from images prior to performing
some operation on them, like convolution or computing the mean.
"""
import numpy as np
from itertools import zip_longest
from PIL import Image
from numpy.lib.stride_tricks import as_strided
@rldotai
rldotai / README.md
Last active February 8, 2016 07:46
Javascript Development with NPM and Browserify

Sometimes it is convenient to try to develop with just npm rather than splitting into client and server versions using bower or whatever else.

Setup

npm init
npm install -D browserify
npm install -D browserify-shim
npm install -D watchify
@rldotai
rldotai / flatten_tree.js
Created January 31, 2016 06:21
Flatten a tree into an array by combining the keys.
// Flatten a tree into an array by combining the keys.
var flatten_tree = function(obj) {
var sep = '/';
var ret = [];
function _flat(elem, base) {
base = ((base === undefined) ? '' : base + sep);
for (let i in elem){
if (!elem.hasOwnProperty(i)) continue;
let val = elem[i];
@rldotai
rldotai / interruptible_threads.py
Created May 7, 2016 23:58
Python threads that can be safely terminated via KeyboardInterrupt (e.g., ctrl-c)
#!/python3
"""
Python threads with a `threading.Event` flag to allow for safe termination with
a `KeyboardInterrupt`.
While it is possible to have all threads abruptly terminate by setting
`daemon == True` on the thread object, sometimes you need to perform cleanup,
so we essentially set a flag for the threads to check, assuming they all work
via an ongoing loop.
Note that this flag could be any object that evaluates to `True` or `False`,
@rldotai
rldotai / logging_snippet.py
Created October 11, 2016 22:41
Quick logging setup
"""
A quick logging setup and some helper functions.
Some code taken from: http://inventwithpython.com/blog/2012/04/06/stop-using-print-for-debugging-a-5-minute-quickstart-guide-to-pythons-logging-module/
"""
import logging
# get a logger, set the logging level
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
@rldotai
rldotai / convenient_sort.py
Created February 5, 2018 04:04
A slightly more convenient alternative to the builtin `sorted()` which allows you to specify a key as something other than a function.
"""
A slightly more convenient alternative to the builtin `sorted()` which allows
you to specify a key as something other than a function.
For example, with a list of lists, if you wanted to sort by the last element you
could use `sort(lstseq, key=-1)` rather than `sorted(lstseq, key=lambda x: x[-1])`.
If your key *is* a function, it behaves identically to `sorted()`, because it is
really just wrapping the builtin.
Examples
--------