Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / widget_figure_workaround.py
Last active November 19, 2019 18:34
Temporary workaround for traditional mpl figure behaviour with the ipympl widget backend
import matplotlib.pyplot as plt
def figure(figsize=None):
'Temporary workaround for traditional figure behaviour with the ipympl widget backend'
fig = plt.figure()
if figsize:
w, h = figsize
else:
w, h = plt.rcParams['figure.figsize']
fig.canvas.layout.height = str(h) + 'in'
@thomasaarholt
thomasaarholt / zigzag.py
Created August 6, 2019 15:02
Zigzag scanning hyperspy example
import hyperspy.api as hs
import numpy as np
import matplotlib.pyplot as plt
def Gauss2D(X, Y, A=1, cx=0, cy=0, sx=1, sy=1):
return A*np.exp(-( ((X - cx)**2)/(2*sx**2) + ((Y - cy)**2)/(2*sy**2)))
def Gauss(x, A=1, c=0, s=1):
return A*np.exp(-((x - c)**2)/(2*s**2))
@thomasaarholt
thomasaarholt / gaussian_filter_tensorflow.py
Last active July 12, 2019 16:01
Gaussian filter of 2D image in Tensorflow 2.0 on GPU
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_probability as tfp
from time import time
def toImage(tensor):
return tensor.reshape(tensor.shape[:2])
def gaussian_kernel(size: int,
@thomasaarholt
thomasaarholt / gaussian.py
Last active November 25, 2024 02:41
Fastest found numpy method of generating a 2D gaussian kernel of size n x n and standard deviation std.
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 / odemis_acq.py
Last active July 4, 2019 12:58
Rough approximation of our approach to odemis SI acquisition
def approx_acquisition_method():
self.spect.data.synchronizedOn(self.escan.newPosition)
startt = time.time()
self.spect.data.subscribe(self.receive_spect_point)
sed_data = self.sed.data.get()
# wait the last point is fully acquired
self.acq_complete.wait()
@thomasaarholt
thomasaarholt / autoscale.py
Last active January 23, 2023 15:52
Matplotlib autoscale
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:
(/shared/users/thomasaar/py37) /shared/users/thomasaar/downloads/q-e-gpu-qe-gpu-6.4.1a1: ./configure --with-cuda=/opt/pgi/linux86-64/2018/cuda/9.2/bin --with-cuda-runtime=9.2 --with-cuda-cc=7.5 --enable-openmp --with-scalapack=no --prefix=/shared/users/thomasaar/compiled/q-e-gpu
checking build system type... x86_64-pc-linux-gnu
checking ARCH... x86_64
checking setting AR... ... /shared/users/thomasaar/py37/bin/x86_64-conda_cos6-linux-gnu-ar
checking setting ARFLAGS... ... ruv
checking whether the Fortran compiler works... yes
checking for Fortran compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
import numpy as np
x = np.array([500, 500, 500, 1000, 1000, 1000, 1500])
y = np.array([1,2,3,4,5,6,7])
data = np.stack([x,y]).T
means = []
stds = []
for xi in np.unique(data[:,0]):
@thomasaarholt
thomasaarholt / velox_dcfi.py
Last active June 17, 2019 15:19
Get DCFI shifts from a velox STEM file, then apply them to all the raw signals in the dataset.
# Script should be applied to a single Velox emd file, where the file contains a single DCFI dataset
# that has been applied to the detector signal that one wishes to apply to the other detector signals
# A good example is using DCFI on a DF image, and then applying this correction to HAADF, BF and ABF.
import hyperspy.api as hs
import json
import h5py
import numpy as np
def get_nonDCFI_signals(list_of_signals):
@thomasaarholt
thomasaarholt / gist:5fc33ed59c846bbf64999a17324b1432
Created June 15, 2019 18:03
make.inc.in from attempted compiling of quantum espresso GPU version
(/shared/users/thomasaar/py37) /shared/users/thomasaar/downloads/q-e-gpu/install: more make.inc.in
# @configure_input@
# compilation rules
.SUFFIXES :
.SUFFIXES : .o .c .f .f90
# most fortran compilers can directly preprocess c-like directives: use
# $(MPIF90) $(F90FLAGS) -c $<