Skip to content

Instantly share code, notes, and snippets.

View tacaswell's full-sized avatar

Thomas A Caswell tacaswell

View GitHub Profile
@tacaswell
tacaswell / .gitignore
Created July 4, 2012 18:20 — forked from sixtenbe/analytic_wfm.py
Peak detection in Python
*.pyc
@tacaswell
tacaswell / simp_zoom.py
Last active February 12, 2023 07:19
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
tacaswell / euler_rot.py
Created October 29, 2012 01:35
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(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 January 16, 2013 06:03
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.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 January 17, 2013 02:54
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]
else:
return r'$\frac{%s}{%s}$' % tuple(sp)
frac_size = 4
@tacaswell
tacaswell / qt_exception_decorator.py
Created January 24, 2013 15:16
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
tacaswell / gist:5329385
Last active December 15, 2015 21:59
bouncy annotated scatter plot
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 December 16, 2015 19:00
Small helper program for extracting the meta-data from metamorph generated tifs
#!/usr/bin/python
#Copyright 2009-2013 Thomas A Caswell
#[email protected]
#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 August 2, 2013 02:47
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
tacaswell / hough.py
Last active December 21, 2015 03:29
Tweaked version of code at http://mail.scipy.org/pipermail/scipy-user/attachments/20060803/ad246212/attachment.py with minor bug fix and improved documentation.
## Copyright (C) 2006 Stefan van der Walt <[email protected]>
##
## 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