Skip to content

Instantly share code, notes, and snippets.

Avatar

Thomas A Caswell tacaswell

  • Brookhaven National Lab
  • New York
View GitHub Profile
@tacaswell
tacaswell / .gitignore
Created Jul 4, 2012 — forked from sixtenbe/analytic_wfm.py
Peak detection in Python
@tacaswell
tacaswell / simp_zoom.py
Last active May 27, 2021
factory for adding zoom callback to matplotlib graphs
View simp_zoom.py
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
tacaswell / euler_rot.py
Created Oct 29, 2012
A simple function that implements rotation by Euler angles.
View euler_rot.py
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(theta)*np.sin(psi),
np.cos(phi)*np.cos(psi) + np.sin(phi)*np.sin(theta)*np.sin(psi),
@tacaswell
tacaswell / push_to_advance.py
Created Jan 16, 2013
A quick class for binding keys to step through an iterable in matplotlib
View push_to_advance.py
class push_to_advance(object):
def __init__(self):
self.fig = plt.figure()
self.ax = 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
tacaswell / pi_format.py
Created Jan 17, 2013
Format nicely printed labels based on pi for matplotlib x-axis
View pi_format.py
def format_frac(fr):
'''Convert a/b to latex'''
sp = str(fr).split('/')
if len(sp) == 1:
return sp[0]
else:
return r'$\frac{%s}{%s}$' % tuple(sp)
frac_size = 4
@tacaswell
tacaswell / qt_exception_decorator.py
Created Jan 24, 2013
A short example of how to override sys.excepthook to get exception out of pyqt
View qt_exception_decorator.py
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
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
G=nx.Graph()
data_nodes = []
init_pos = {}
for j, b in enumerate(scatter_data.T):
x, y, _ = b
@tacaswell
tacaswell / md2txt
Last active Dec 16, 2015
Small helper program for extracting the meta-data from metamorph generated tifs
View md2txt
#!/usr/bin/python
#Copyright 2009-2013 Thomas A Caswell
#tcaswell@uchicago.edu
#http://jfi.uchicago.edu/~tcaswell
#
#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
tacaswell / handler_xoffset.py
Created Aug 2, 2013
Line2D handler which lets you tweak the x location of the markers.
View handler_xoffset.py
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
tacaswell / hough.py
Last active Dec 21, 2015
Tweaked version of code at http://mail.scipy.org/pipermail/scipy-user/attachments/20060803/ad246212/attachment.py with minor bug fix and improved documentation.
View hough.py
## Copyright (C) 2006 Stefan van der Walt <stefan@sun.ac.za>
##
## 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