Skip to content

Instantly share code, notes, and snippets.

Thomas Aarholt thomasaarholt

  • University of Oslo
  • Oslo, Norway
View GitHub Profile
@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 / autoscale.py
Last active Jun 18, 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:
@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)
@thomasaarholt
thomasaarholt / gms_load_hyperspy.py
Last active Jun 5, 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.
If the script runs forever, then this is due to a bug with numpy's FFT algorithm.
To fix, in hyperspy\_signals\signal2d.py, move the line
@thomasaarholt
thomasaarholt / Mn_whitelineratio
Created Apr 21, 2020 — forked from winston-song/Mn_whitelineratio
Mn L3/L2 white line ratio using gussian component plus H-S step edge
View Mn_whitelineratio
%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 Apr 20, 2020
Curve fitting the white lines on zirconium oxide with hyperspy
View fit_Zr_whitelines.py
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 Apr 10, 2020
hyperspy rotation of axes calibrated
View axisrotation.py
%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 / plot_polar.py
Last active Apr 10, 2020
Plot a 4D hyperspy dataset with the signal in polar coordinates
View plot_polar.py
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 / parallelspline.py
Created Apr 9, 2020
Strange behaviour with parallel map and UnivariateSpline
View parallelspline.py
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 Apr 7, 2020 — forked from wassname/augumented_hdf5_matrix.py
How to do data augmentation on a keras HDF5Matrix
View augumented_hdf5_matrix.py
"""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.,
You can’t perform that action at this time.