Skip to content

Instantly share code, notes, and snippets.

@mandli
mandli / colorbar_add_break.py
Created March 23, 2014 16:33
`__add__` colorbar functionality with break control
def add(colormaps, break_location=0.5):
lhs_dict = colormaps[0]._segmentdata
rhs_dict = colormaps[1]._segmentdata
new_dict = dict(red=[], green=[], blue=[])
# Scale rhs by half
for key in rhs_dict:
val_list = rhs_dict[key]
print(key, val_list)
@mandli
mandli / gist:9c4a831f00249ee31035
Last active August 29, 2015 14:07
Demo of tick label rotation
#!/usr/bin/env python
import numpy
import matplotlib.pyplot as plt
X, Y = numpy.meshgrid(numpy.linspace(-2, 2, 200, numpy.linspace(-2 ,2 ,20))
fig = plt.figure(figsize=[16,8])
axes = []
for n in xrange(2):
@mandli
mandli / rel_err_formula.py
Created September 10, 2017 22:14
Python script to compare two formulas for relative error.
# Based on the code by Nick Higham
# https://gist.github.com/higham/6f2ce1cdde0aae83697bca8577d22a6e
# Compares relative error formulations using single precision and compared to double precision
N = 501 # Note: Use 501 instead of 500 to avoid the zero value
d = numpy.finfo(numpy.float32).eps * 1e4
a = 3.0
x = a * numpy.ones(N, dtype=numpy.float32)
y = [x[i] + numpy.multiply((i - numpy.divide(N, 2.0, dtype=numpy.float32)), d, dtype=numpy.float32) for i in range(N)]
@mandli
mandli / regression_tests.py
Created April 27, 2019 18:59
Draft Adjoint Based Regression Tests Class
#!/usr/bin/env python
r"""chile2010_adjoint regression test for GeoClaw
To create new regression data use
`python regression_tests.py True`
"""
from __future__ import absolute_import
import os
@mandli
mandli / macros.tex
Created September 17, 2020 15:47
Macros for Writing LaTeX
\newcommand{\eg}{{e.g.}\xspace}
% ==============================================================================
% Generic math macros
\renewcommand{\v}[1]{\boldsymbol{#1}}
\newcommand{\m}[1]{\text{\textsf#1}}
\newcommand{\pd}[2]{\ensuremath{\frac{\partial #1}{\partial #2}}} % partial
\newcommand{\dee}{\ensuremath{\mathrm{d}}} % d symbol
\newcommand{\diff}[2]{\ensuremath{\frac{\dee #1}{\dee #2}}} % Derivative d/dx
@mandli
mandli / create_html_bib.py
Created October 30, 2020 18:00
Take a bibtex file and create an html version of the compiled bibliography via pandoc.
#!/usr/bin/env python
import os
import subprocess
html_header = r"""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
@mandli
mandli / make_movie.py
Created November 23, 2020 02:53
Make a mp4 out of a set of png using ffmpeg
#!/usr/bin/env python
"""
ffmpeg, convert png to mpg
ffmpeg -r FRAMES_PER_SECOND -i IMAGE_PATHS -q:a 0 -q:v 0 -vcodec mpeg4 -vb 20M -r FRAMES_PER_SECOND_MOV OUTPUT_FILE
`-qscale 0` - Forces lossless quality conversion -> should use `-q:a 0 -q:v 0`
`FRAMES_PER_SECOND` - Number of images to display per second, can be fractional
`IMAGE_PATHS` - Image paths, use normal substitution macros, i.e. frame%04dfig4.png would expand to frameXXXXfig4.png where XXXX is zero filled.