Skip to content

Instantly share code, notes, and snippets.

Avatar

Thomas Aarholt thomasaarholt

  • University of Oslo
  • Oslo, Norway
View GitHub Profile
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
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 / 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 / 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 / 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 / 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.