Skip to content

Instantly share code, notes, and snippets.

View ZGainsforth's full-sized avatar

Zack Gainsforth ZGainsforth

  • University of California at Berkeley
  • Los Angeles, CA, USA
View GitHub Profile
@ZGainsforth
ZGainsforth / PlotRFFT.py
Created May 21, 2014 02:23
Plot Real FFT
from matplotlib import *
use('Qt4Agg')
from matplotlib.pyplot import *
from numpy import *
def PlotRFFT(x, y):
### PlotRFFT(x, y): plots the FFT of y assuming y is real. Generates one plot with two subfigures: abscissa = frequency and abscissa = period.
f = fft.rfft(y)/len(y)*2
nu = fft.rfftfreq(len(y), x[1]-x[0])
# Script to sync Pythonista files to Dropbox
# Author: David Hutchison
# www: http://www.devwithimagination.com/
import webbrowser, os, pprint
import dropbox
import hashlib
import json
import difflib
import sys
@ZGainsforth
ZGainsforth / QuickwxDialogs.py
Created June 19, 2014 18:37
Use a wx DirDialog, FileDialog, or Messagebox without the overhead of starting wx, etc.
# Without having to worry about all the hassle of starting a wx app or remembering flags, just
# show a dialog box to get a directory, file or messagebox and be done with it.
# Zack Gainsforth 2014
import wx
if 'app' not in locals():
app = wx.App(None)
def DirDialog(DefaultDir='', Title='Select Directory:'):
dlg = wx.DirDialog (None, Title, DefaultDir, wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
@ZGainsforth
ZGainsforth / RunBatchesOfThreads.py
Last active August 29, 2015 14:02
Runs a parallelizable function in batches of threads, waiting for each batch to finish. This allows the full resources of the computer to be used, without overwheming it when you have hundreds or thousands of threads to run.
import threading
import os
# Normally this would be some busier function... Here it is a stub.
def PlotAlphaMELTS(PathName):
print PathName
# How many threads we want.
NumThreads = 10
# Counter for the thread in the loop.
@ZGainsforth
ZGainsforth / RunAlphaMELTSFromPython.py
Last active August 29, 2015 14:02
Run alphaMELTS from within Python.
import os
# Go to the directory where the alphaMELTS input files are.
os.chdir('/Users/Zack/Desktop/Testcmd')
# Run the shell script.
process = subprocess.Popen('./runit', shell=True)
process.wait()
print 'done'
# Script to sync Pythonista files to Dropbox
# Author: David Hutchison
# www: http://www.devwithimagination.com/
import webbrowser, os, pprint
import dropbox
import hashlib
import json
import difflib
import sys
@ZGainsforth
ZGainsforth / Marry2ColumnDomains.py
Created June 24, 2014 17:06
Takes (x1, y1) and (x2, y2) where x1, and x2 have some overlap. Merges them to produce: (xx1, yy1) and (xx2, yy2) where xx1 = xx2.
def Marry2ColumnDomains(x1, y1, x2, y2):
#Based on: http://stackoverflow.com/questions/24317761/numpy-function-to-find-indices-for-overlapping-vectors?noredirect=1#comment37587212_24317761
mask1 = np.in1d(x1, x2)
mask2 = np.in1d(x2, x1)
xx1 = x1[mask1]
yy1 = y1[mask1]
xx2 = x2[mask2]
yy2 = y2[mask2]
return xx1, yy1, xx2, yy2
@ZGainsforth
ZGainsforth / fullprint.py
Created July 2, 2014 23:06
Print an entire numpy array regardless of the current printoptions.
# implicit from numpy import *
def fullprint(*args, **kwargs):
from pprint import pprint
opt = get_printoptions()
set_printoptions(threshold='nan')
pprint(*args, **kwargs)
set_printoptions(**opt)
@ZGainsforth
ZGainsforth / cyfib.pyx
Created July 8, 2014 20:46
Minimal Cython implementation (modeled from training.enthought.com example)
def cyfib(int n):
cdef int a, b, i
a,b = 1,1
for i in range(n):
a,b = a+b, a
return a
@ZGainsforth
ZGainsforth / SimpleTimeit.py
Last active August 29, 2015 14:03
Simple use of the timeit module where you need to import a function or variable to time.
def myfunc(x):
x = x*3
return x
import timeit
n = 100000
print timeit.timeit(stmt='myfunc(5)', setup='from __main__ import myfunc', number=n)/n