Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / dist.py
Created August 28, 2020 23:14
Stackoverflow Distortion Example
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 August 27, 2020 12:01
Create a TEM-like image (HAADF) from a cif file using ASE and HyperSpy
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 / eels_onset_component.py
Created June 18, 2020 12:20
Hyperspy Expression component for EELS Bandgap Onsets
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)
@thomasaarholt
thomasaarholt / Mn_whitelineratio
Created April 21, 2020 13:40 — forked from winston-song/Mn_whitelineratio
Mn L3/L2 white line ratio using gussian component plus H-S step edge
%matplotlib widget
import hyperspy.api as hs
import matplotlib.pyplot as plt
import numpy as np
ll_sum = hs.load('ll_sum.hspy')
s_sum = hs.load('s_sum.hspy')
s_sum.metadata.Acquisition_instrument.TEM.beam_energy=200
s_sum.metadata.Acquisition_instrument.TEM.convergence_angle=22.5
s_sum.metadata.Acquisition_instrument.TEM.Detector.EELS.collection_angle=37.9
@thomasaarholt
thomasaarholt / fit_Zr_whitelines.py
Created April 20, 2020 08:09
Curve fitting the white lines on zirconium oxide with hyperspy
def fit_Zr_L(sZr, ll=None):
"Returns the model only"
print("Will produce 10 progress bars")
mZr = sZr.create_model(ll = ll, GOS="Hartree-Slater", auto_add_edges=False)
mZr.fit_component(mZr["PowerLaw"], bounded=True, signal_range=[2150.,2210.], fit_independent=True, only_current=True)
mZr.assign_current_values_to_all()
mZr.fit_component(mZr["PowerLaw"], bounded=True, signal_range=[2150.,2210.], fit_independent=True, only_current=False)
#mZr["PowerLaw"].set_parameters_not_free()
@thomasaarholt
thomasaarholt / axisrotation.py
Created April 10, 2020 21:37
hyperspy rotation of axes calibrated
%matplotlib widget
import hyperspy.api as hs
import numpy as np
import matplotlib.pyplot as plt
s = hs.datasets.example_signals.object_hologram()
ax0 = s.axes_manager[0]
ax1 = s.axes_manager[1]
ax0.scale=2
ax1.scale=1
@thomasaarholt
thomasaarholt / parallelspline.py
Created April 9, 2020 07:56
Strange behaviour with parallel map and UnivariateSpline
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
from concurrent.futures import ThreadPoolExecutor
from os import cpu_count
executor = ThreadPoolExecutor(max_workers=cpu_count())
print(f"Processors available: {cpu_count()}")
# parallel=True uses the following map function
@thomasaarholt
thomasaarholt / augumented_hdf5_matrix.py
Created April 7, 2020 21:38 — forked from wassname/augumented_hdf5_matrix.py
How to do data augmentation on a keras HDF5Matrix
"""Another way, note this one will load the whole array into memory ."""
from keras.preprocessing.image import ImageDataGenerator
import h5py
from keras.utils.io_utils import HDF5Matrix
seed=0
batch_size=32
# we create two instances with the same arguments
data_gen_args = dict(
rotation_range=90.,
@thomasaarholt
thomasaarholt / plot_polar.py
Last active April 10, 2020 11:38
Plot a 4D hyperspy dataset with the signal in polar coordinates
import hyperspy.api as hs
import numpy as np
from ipywidgets.widgets import HBox, VBox, Label, IntSlider, Output, IntRangeSlider
import matplotlib.pyplot as plt
from IPython.display import display
def plot_polar(s, clim=(10000, 100000), clim_max=(0, 250000)):
"""
Plot a 4D hyperspy dataset with the signal in polar coordinates
Arguments:
@thomasaarholt
thomasaarholt / spanplot.py
Last active March 20, 2020 11:35
Hyperspy function to emulate GMS behaviour on spectrum images
%matplotlib widget
import hyperspy.api as hs
import matplotlib.pyplot as plt
from ipywidgets.widgets import HBox, Label, Dropdown
# Optional, for notebook only, not lab
# from IPython.core.display import display, HTML
# display(HTML("<style>.container { width:100% !important; }</style>"))
def plot(s):