Skip to content

Instantly share code, notes, and snippets.

mattmcd / Hello.g4
Last active Mar 11, 2021
Simple ANTLR4 grammar example
View Hello.g4
// define a grammar called Hello
grammar Hello;
r : 'hello' ID;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;
mattmcd /
Created Jan 24, 2021
Printing a SymPy LaTeX expression to png e.g. for Markdown or Medium
from subprocess import run
import sympy as sp
def print_to_file(fname, expr):
# See
t_start = r"""\documentclass[border=2pt]{standalone}
mattmcd / Expr.g4
Created Mar 30, 2013
ANTLR4 simple expression grammar. Note that left recursion is now allowed and operator precedence is just order of definition.
View Expr.g4
grammar Expr;
// Need to call recursive rule expr from non-recursive rule
r : expr+ ;
// ANTLR4 : Left recursion!
// Operator precedence matches order of definition
expr : '-' expr // Unary minus
| expr ('*' | '/' ) expr
| expr ('+' | '-' ) expr
View business_plan.json
"verbs" : ["aggregate", "architect", "benchmark", "brand", "cultivate", "deliver", "deploy", "disintermediate",
"disrupt", "drive", "e-enable", "embrace", "empower", "enable", "engage", "engineer", "enhance",
"envisioneer", "evolve", "expedite", "exploit", "extend", "facilitate", "generate", "grow", "harness", "implement", "incentivize", "incubate", "innovate", "integrate",
"iterate", "leverage", "matrix", "maximize", "mesh", "monetize", "morph", "optimize", "orchestrate", "productize", "recontextualize", "redefine", "reintermediate",
"reinvent", "repurpose", "revolutionize", "scale", "seize", "strategize", "streamline", "syndicate", "synergize", "synthesize", "target",
"transform", "transition", "unleash", "utilize", "visualize", "whiteboard"],
"adjectives": ["24/365", "24/7", "B2B", "B2C", "back-end", "best-of-breed", "bleeding-edge", "bricks-and-clicks", "clicks-and-mortar",
"collaborative", "compelling", "cross-p
mattmcd /
Last active Apr 6, 2018
Kullback Leibler divergence between two Gaussians
import sympy as sp
x = sp.symbols('x', real=True)
p, q = sp.symbols('p q', positive=True)
KL = sp.Integral(p*sp.log(p) - p*sp.log(q), (x, -sp.oo, sp.oo))
mu, mu1, mu2 = sp.symbols('mu mu1 mu2', real=True)
sig, sig1, sig2 = sp.symbols('sig sig1 sig2', positive=True)
n = 1/sp.sqrt(2*sp.pi*sig**2)*sp.exp(-(x - mu)**2/(2*sig**2))
KL_n = sp.simplify(
KL.subs({p: n.subs({mu: mu1, sig: sig1}), q: n.subs({mu: mu2, sig: sig2})}).doit()
mattmcd /
Last active Mar 31, 2018
10 minutes to Mask R-CNN
# Amazon #Sagemaker instance
# open Jupyter
# new terminal
cd SageMaker
git clone
git clone
cd coco/PythonAPI
source activate tensorflow_p36
python install
mattmcd /
Created Mar 20, 2018
Hook for AWS Lambda function to retrieve library files from S3 and add to local path in tmp directory
import boto3
import os
import sys
import zipfile
HERE = os.path.dirname(os.path.realpath(__file__))
def library_install():
"""Dynamically add libraries to path, retrieving from S3 if necessary
mattmcd / Dockerfile
Created Mar 20, 2018
TensorFlow library install on AmazonLinux for AWS Lambda
View Dockerfile
FROM amazonlinux:latest
MAINTAINER Matt McDonnell ""
RUN yum -y -q update
RUN yum -y -q install python27-pip zip
RUN pip install -q virtualenv
mattmcd / s3_upload.json
Created May 8, 2016
IAM policy for uploading files to S3
View s3_upload.json
"Version": "2012-10-17",
"Statement": [
"Effect": "Allow",
"Action": [
mattmcd /
Created Dec 23, 2016
PyStan version of two compartment model from "Stan: A probabilistic programming language for Bayesian inference and optimization" Gelman, Lee, Guo (2015)
import pystan
import numpy as np
# Two compartment model from
# "Stan: A probabilistic programming language for
# Bayesian inference and optimization" Gelman, Lee, Guo (2015)
a = np.array([0.8, 1.0])
b = np.array([2, 0.1])