Skip to content

Instantly share code, notes, and snippets.

Avatar

Thomas Aarholt thomasaarholt

  • University of Oslo
  • Oslo, Norway
View GitHub Profile
@thomasaarholt
thomasaarholt / 2D_explorer.py
Last active Nov 20, 2020
Matplotlib Navigator and Signal plot with draggable marker
View 2D_explorer.py
%matplotlib qt
import numpy as np
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots(ncols=1)
fig2, ax2 = plt.subplots(ncols=1)
imdata = np.random.random((20,20))
im = ax1.imshow(imdata, animated=True)
@thomasaarholt
thomasaarholt / autoscale.py
Last active Sep 16, 2020
Matplotlib autoscale
View autoscale.py
def autoscale(ax=None, axis='y', margin=0.1):
'''Autoscales the x or y axis of a given matplotlib ax object
to fit the margins set by manually limits of the other axis,
with margins in fraction of the width of the plot
Defaults to current axes object if not specified.
'''
import matplotlib.pyplot as plt
import numpy as np
if ax is None:
View ffmpeg_script.sh
ffmpeg -r 20 -i img_%03d.png -c:v libx264 -pix_fmt yuv420p -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -y out.mp4
@thomasaarholt
thomasaarholt / ipywidget_file_browser.py
Last active Sep 12, 2020
ipywidgets file browser based on the Select widget. Single click folders to enter, click Load to load a file.
View ipywidget_file_browser.py
import ipywidgets as widgets
import os
from pathlib import Path
cwd = Path(os.getcwd())
FOLDERLABEL = '-------FOLDERS-------'
FILESLABEL = '-------FILES-------'
def get_folder_contents(folder):
View invnm_to_mrad.py
import numpy as np
from scipy.constants import electron_mass, elementary_charge, c, hbar, h
def relativistic_wavelength(kV=300):
"Returns relativistic wavelength in meters"
V = 1e3 * kV
top = h * c
bottom = np.sqrt(
elementary_charge * V * (2 * electron_mass * c ** 2 + elementary_charge * V)
)
@thomasaarholt
thomasaarholt / gms_load_hyperspy.py
Last active Sep 3, 2020
Python script to load any format supported by hyperspy directly into GMS 3.4+
View gms_load_hyperspy.py
'''
Python script to load any format supported by hyperspy directly into GMS
Must be copied and pasted into Gatan DigitalMicrograph (aka Gatan Microscopy Suite) version 3.4+
Call by `load_img(filepath)` at the bottom of the script. Can not be called outside of GMS.
Does not automatically convert the data type to EELS or EDS
Written by Thomas Aarholt, see https://gist.github.com/thomasaarholt/fccf06d56ff84cf76345b44dae30871e for newer versions
Feedback and forks are very welcome.
MUST: First import of hyperspy (or scipy) must NOT be run with "Execute on background thread" checked. One
can then swap to background thread and rerun.
v. 0.3: Added delete statements to ensure python objects don't stay in memory.
@thomasaarholt
thomasaarholt / dist.py
Created Aug 28, 2020
Stackoverflow Distortion Example
View dist.py
import numpy as np
import matplotlib.pyplot as plt
N = 600
probe_positions = np.arange(N)
def probe_function(probe_positions):
return np.sin(2*np.pi*probe_positions / 150)**2
strength = 3
period = 50
@thomasaarholt
thomasaarholt / cif_to_STEM.py
Last active Aug 27, 2020
Create a TEM-like image (HAADF) from a cif file using ASE and HyperSpy
View cif_to_STEM.py
import hyperspy.api as hs
import numpy as np
import matplotlib.pyplot as plt
def cell2sig(cell, pixel_size = 0.05, sigma=0.25):
Signal2D = hs.signals.Signal2D
Gaussian2D = hs.model.components2D.Gaussian2D
XLEN, YLEN = (cell.cell.diagonal()[:2] // pixel_size).astype(int)
@thomasaarholt
thomasaarholt / gaussian.py
Last active Jul 25, 2020
Fastest found numpy method of generating a gaussian kernel of size n x nand standard deviation std.
View gaussian.py
import numpy as np
from scipy import signal
def gaussian_kernel(n, std, normalised=False):
'''
Generates a n x n matrix with a centered gaussian
of standard deviation std centered on it. If normalised,
its volume equals 1.'''
gaussian1D = signal.gaussian(n, std)
gaussian2D = np.outer(gaussian1D, gaussian1D)
@thomasaarholt
thomasaarholt / eels_onset_component.py
Created Jun 18, 2020
Hyperspy Expression component for EELS Bandgap Onsets
View eels_onset_component.py
import hyperspy.api as hs
expr = "where(Eg < x, A*(x-Eg)*n, 0)"
bandgap = hs.model.components1D.Expression(expr, 'Bandgap', Eg=3.0, n=1, A=1)
You can’t perform that action at this time.