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 / ComputeRDF.py
Created September 4, 2014 23:55
Subtract a single scattering profile from a monochromated diffraction pattern and compute the RDF from it.
# -*- coding: utf-8 -*-
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
@ZGainsforth
ZGainsforth / BinImage.py
Last active August 29, 2015 14:06
Bins an image when the image is a 2d numpy array.
def BinImage(img, binsize):
imgbin = zeros(array(img.shape)/binsize)
for i in range(binsize):
for j in range(binsize):
imgbin += img[i::binsize, j::binsize]
return imgbin
@ZGainsforth
ZGainsforth / imshowz.py
Last active March 2, 2021 20:07
Version of imshow which shows the value under the cursor. Modified from poppy: http://www.stsci.edu/~mperrin/software/poppy/_modules/poppy/utils.html
def imshowz(image, ax=None, *args, **kwargs):
""" wrapper for matplotlib imshow that displays the value under the cursor position
Wrapper for pyplot.imshow that sets up a custom mouseover display formatter
so that mouse motions over the image are labeled in the status bar with
pixel numerical value as well as X and Y coords.
Why this behavior isn't the matplotlib default, I have no idea...
"""
if ax is None: ax = plt.gca()
@ZGainsforth
ZGainsforth / fullprint.py
Last active August 29, 2015 14:14
Print a full numpy array
def fullprint(*args, **kwargs):
from pprint import pprint
import numpy
opt = numpy.get_printoptions()
numpy.set_printoptions(threshold='nan')
pprint(*args, **kwargs)
numpy.set_printoptions(**opt)
@ZGainsforth
ZGainsforth / PlotMnToCoAbsorptionByCuKa.py
Last active August 29, 2015 14:14
Plot absorption cross-sections for Mn-Co by Cu-Ka
#-*- coding: utf-8 -*-
# Written by Zack Gainsforth 2015
# Using the constants from
#
# Low-Energy X-ray Interaction Coefficients:
# Photoabsorption, Scattering, and Reflection
# E = 30-30,000 eV, Z = 1-92
#
# B. L. Henke, E. M. Gullikson, and J. C. Davis
@ZGainsforth
ZGainsforth / ParseAlphaMELTS.py
Created February 27, 2015 17:44
Read alphaMELTS output files into numpy arrays for plotting and analysis
# Created 2014, Zack Gainsforth
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
#import QuickPlot
import re
from StringIO import StringIO
import glob
import os

def GetalphaMELTSSection(FileName, SectionName):
 # Read the alphaMELTS file that has all the output.
 try:
 with open (FileName, 'r') as myfile:
 data=myfile.read()
 except:
 print 'Failed to open ' + FileName + '. Skipping.'
 return

 DataLines = StringIO(data)

 reout = re.compile(r'%s.*?(?:\n\n|\Z)' % (SectionName), re.S)
# reout = re.compile(r'%s.*?%s' % (SectionName, stop), re.S)
 try:
 SectionStr = reout.search(data).group(0)
 except:
 # It is possible that this MELTS computation didn't produce this mineral. If so, just bail.
 return None
 # Convert it into a numpy array.
 SectionData = np.genfromtxt(StringIO(SectionStr), skip_header=1, skip_footer=0, names=True)
 return SectionData

def ListalphaMELTSSections(Fi
@ZGainsforth
ZGainsforth / MakeDiffMapImages.py
Last active November 13, 2015 23:16
Go through a diffraction map and produce a sum image, average image, stdev image, and a real space residual image.
# Created 2015, Zack Gainsforth
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
from matplotlib.image import imsave
import numpy as np
import sys
import math
import os
from numba import jit
@ZGainsforth
ZGainsforth / BackgroundSubtractBrukerQuant.py
Created May 5, 2015 21:11
Using two Bruker element quant output files, subtracts the counts of one from the other.
# Created 2015, Zack Gainsforth
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
import wx
import pandas as pd
import sys, os
if 'app' not in locals():
@ZGainsforth
ZGainsforth / HowToReadEMDFile.py
Created May 6, 2015 18:37
How to read an NCEM EMD file
# coding: utf-8
import h5py
h5py.File('stack.emd', 'r')
f = h5py.File('stack.emd', 'r')
f[0]
f['data']
f['data'][0]
f['data'][1]
f['data']
@ZGainsforth
ZGainsforth / multiprocess_sample_3d_numpy.py
Created July 2, 2015 19:31
Multiprocessing pool example to use all your CPU cores on a computationally intensive stack using a 3D numpy array as input.
from multiprocessing import Pool
import numpy as np
n = 1000000
x = 5
y = 5
z = 100
def f(x):
#print x.shape