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 / DefaultImports.py
Last active February 25, 2020 08:05
Basic imports to get started
# For Python files.
# Created 2017, Zack Gainsforth
import matplotlib
#matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
from rich.traceback import install; install()
import pdb
#pdb.set_trace()
@ZGainsforth
ZGainsforth / QuickPlot.py
Last active January 18, 2018 03:16
One function to do configurable plots in matplotlib. This should make about 90% of plots a one-liner to plot. Allows scaling of size, plotting of multiple lines, and returns the figure and axes in case the user wants to do his own tweaking of the plot.
# Created 2014, Zack Gainsforth
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
from itertools import chain
def QuickPlot(x, y, linestyle='', xlabel=None, ylabel=None, title=None, legendstrs=None, legendloc=None, savefile=None, boldlevel=1, figax=None, plottype=None, xlim=None, ylim=None, **kwargs):
"""
@ZGainsforth
ZGainsforth / NumbaTemplate.py
Last active July 22, 2016 16:37
Simple example using numba.
from numba import jit
import numpy as np
from timeit import timeit
x = np.random.rand(100000)
@jit
def myfunc(x):
for i in range(len(x)-1):
x[i] **= x[i+1]
@ZGainsforth
ZGainsforth / Beautify.py
Created July 22, 2014 22:33
Makes an existing matplotlib plot beautiful.
# Created 2014, Zack Gainsforth
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
def Beautify(xlabel=None, ylabel=None, title=None, legendstrs=None, savefile=None, boldlevel=1, figax=None):
"""
:param xlabel: String (can include TeX) for the label of the x-axis.
:param ylabel: String (can include TeX) for the label of the y-axis.
@ZGainsforth
ZGainsforth / PlotPyroxeneStacking.py
Created July 22, 2014 22:42
Reads in an HRTEM image of pyroxene and allows one to rotate and draw an intensity plot along the a-axis to quantify ortho/clino/half plane stackings.
# coding: utf-8
# Created 2014, Zack Gainsforth
# Definitions:
# CLEN: CLinoENstatite, unit cell a = 9 Å
# OREN: ORthoENstatite, unit cell a = 18 Å
# HALF: Half unit cell, a = 4.5 Å
# PEN: ProtoENstatite, unit cell a = 18 Å
# An OREN -> CLEN transition always has to transform an entire 18 Å cell. Hence always even numbers of CLEN cells.
# For PEN origin, you can get half-planes. Don't know why.
@ZGainsforth
ZGainsforth / Sobel.py
Created July 24, 2014 22:09
Apply a Sobel filter to an image.
import numpy as np
import Image
import scipy.ndimage as ndimage
def sobel(img):
gx = ndimage.filters.convolve(img, np.array([[-1, 0, +1], [-2, 0, +2], [-1, 0, +1]]))
gy = ndimage.filters.convolve(img, np.array([[+1, +2, +1], [0, 0, 0], [-1, -2, -1]]))
imgout = np.sqrt(gx**2+gy**2)
@ZGainsforth
ZGainsforth / Common OS X and linux terminal commands.md
Last active March 2, 2023 06:03
List of common terminal commands to do useful things on OS X and/or linux

View network usage by process.

Make sure to make the terminal window the width of the full screen (~200 chars wide is good). Press 'd' to switch to delta mode and then every second it reports what was sent in the last second. You can use right and left arrow to see all columns if you can't make the terminal window wide enough.

nettop

Find a file under the current directory matching a wildcard.

`

@ZGainsforth
ZGainsforth / ReadBrukerRaw.py
Created August 1, 2014 00:22
Read Bruker raw datacube into numpy
from numpy import *
from matplotlib.pyplot import *
# Read in the raw
x = fromfile('Stack 1 - 360s.raw', dtype='int8')
# reshape to x, y, E
x.shape=(300,300,2048)
# Make integrated image and show it.
@ZGainsforth
ZGainsforth / RelPath.md
Created August 2, 2014 05:25
Relative path in IPython Notebook

First cell stores the path to a variable:

%%javascript
// This gets the name of the notebook so we can use 	relative paths.
var kernel = IPython.notebook.kernel;
var thename = window.document.getElementById("notebook_name").innerHTML;
var command = "NotebookPath = " + "'"+thename+"'";
kernel.execute(command);
@ZGainsforth
ZGainsforth / FigSizeipynb.py
Last active August 29, 2015 14:04
IPython Notebook, change inline figure size
%pylab inline
import matplotlib.pylab as pylab
import os
pylab.rcParams['figure.figsize'] = 8, 6 # that's default image size for this interactive session