Skip to content

Instantly share code, notes, and snippets.


Thomas A Caswell tacaswell

  • Brookhaven National Lab
  • New York
View GitHub Profile
tacaswell / .gitignore
Created Jul 4, 2012 — forked from sixtenbe/
Peak detection in Python
tacaswell /
Last active May 27, 2021
factory for adding zoom callback to matplotlib graphs
import matplotlib.pyplot as plt
def zoom_factory(ax,base_scale = 2.):
def zoom_fun(event):
# get the current x and y limits
cur_xlim = ax.get_xlim()
cur_ylim = ax.get_ylim()
# set the range
cur_xrange = (cur_xlim[1] - cur_xlim[0])*.5
tacaswell /
Created Oct 29, 2012
A simple function that implements rotation by Euler angles.
import numpy as np
def euler_rot(XYZ,phi,theta,psi):
'''Returns the points XYZ rotated by the given euler angles'''
ERot = np.array([[np.cos(theta)*np.cos(psi),
-np.cos(phi)*np.sin(psi) + np.sin(phi)*np.sin(theta)*np.cos(psi),
np.sin(phi)*np.sin(psi) + np.cos(phi)*np.sin(theta)*np.cos(psi)],
np.cos(phi)*np.cos(psi) + np.sin(phi)*np.sin(theta)*np.sin(psi),
tacaswell /
Created Jan 16, 2013
A quick class for binding keys to step through an iterable in matplotlib
class push_to_advance(object):
def __init__(self):
self.fig = plt.figure() = self.fig.gca()
self.bound_keys = []
self.bound_cid = {}
def add_step_through(self, gen, key):
key = key[0] # make a single char
if key in self.bound_keys:
tacaswell /
Created Jan 17, 2013
Format nicely printed labels based on pi for matplotlib x-axis
def format_frac(fr):
'''Convert a/b to latex'''
sp = str(fr).split('/')
if len(sp) == 1:
return sp[0]
return r'$\frac{%s}{%s}$' % tuple(sp)
frac_size = 4
tacaswell /
Created Jan 24, 2013
A short example of how to override sys.excepthook to get exception out of pyqt
from PyQt4 import QtGui
import time
#app = QtGui.QApplication([])
class exception_munger(object):
def __init__(self):
self.flag = True
self.txt = ''
self.type = None
def indicate_fail(self,etype=None, txt=None):
tacaswell / gist:5329385
Last active Dec 15, 2015
bouncy annotated scatter plot
View gist:5329385
import networkx as nx
N = 15
scatter_data = rand(3, N) ** 2
data_nodes = []
init_pos = {}
for j, b in enumerate(scatter_data.T):
x, y, _ = b
tacaswell / md2txt
Last active Dec 16, 2015
Small helper program for extracting the meta-data from metamorph generated tifs
View md2txt
#Copyright 2009-2013 Thomas A Caswell
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 3 of the License, or (at
#your option) any later version.
tacaswell /
Created Aug 2, 2013
Line2D handler which lets you tweak the x location of the markers.
from matplotlib.legend_handler import HandlerLine2D
class HandlerXoffset(HandlerLine2D):
def __init__(self, marker_pad=0.3, numpoints=1, x_offset=0, **kw):
HandlerLine2D.__init__(self, marker_pad=marker_pad, numpoints=numpoints, **kw)
self._xoffset = x_offset
def get_xdata(self, legend, xdescent, ydescent, width, height, fontsize):
numpoints = self.get_numpoints(legend)
if numpoints > 1:
tacaswell /
Last active Dec 21, 2015
Tweaked version of code at with minor bug fix and improved documentation.
## Copyright (C) 2006 Stefan van der Walt <>
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
## 1. Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in