Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / horizontal_plot.py
Created March 17, 2020 12:48
Plotting horizontally in hyperspy using widgets
from ipywidgets.widgets import HBox, VBox
import matplotlib.pyplot as plt
def plot(s, orientation='horizontal'):
"""
Plot hyperspy signals horizontally using the %matplotlib widget interface.
Can also plot vertically using orientation='vertical'
"""
# if 1D signal, just plot normal
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\.conda\envs\py5\lib\site-packages\matplotlib\backend_bases.py in _wait_cursor_for_draw_cm(self)
2772 try:
-> 2773 self.set_cursor(cursors.WAIT)
2774 yield
~\.conda\envs\py5\lib\site-packages\matplotlib\backends\backend_webagg_core.py in set_cursor(self, cursor)
380 if cursor != self.cursor:
--> 381 self.canvas.send_event("cursor", cursor=cursor)
@thomasaarholt
thomasaarholt / eels_colours.py
Last active March 11, 2020 10:41
Plot low-loss EELS spectra with range 1.5 - 3.4 eV filled with the visible colours, otherwise black
import numpy as np
import matplotlib.pyplot as plt
# get electropy from thomasaarholt github
from electropy.units import eV_to_wavelength
# get colour by pip install colour-science
from colour.utilities import first_item, normalise_maximum
from colour.plotting import filter_cmfs, XYZ_to_plotting_colourspace, COLOUR_STYLE_CONSTANTS
from colour.colorimetry import wavelength_to_XYZ, ILLUMINANTS
@thomasaarholt
thomasaarholt / gms_load_hyperspy.py
Last active September 3, 2020 18:47
Python script to load any format supported by hyperspy directly into GMS 3.4+
'''
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 / coordconv2d.py
Last active February 10, 2020 22:34 — forked from Dref360/coordconv2d.py
Un-scaled version of CoordConv2D
import keras.backend as K
import tensorflow as tf
from keras.layers import Layer
from keras.layers import Conv2D
"""Not tested, I'll play around with GANs soon with it."""
class CoordConv2D(Layer):
def __init__(self, channel, kernel, padding='valid', activation=None, **kwargs):
@thomasaarholt
thomasaarholt / gist:5a2395307dec047208dc6c9004f993b6
Created February 10, 2020 15:55
Error from calling matmul on tensorflow 2.1. TensorRT errors are not the problem.
(tensor)/shared/users/thomasaar/2020Feb: python
Python 3.7.6 | packaged by conda-forge | (default, Jan 7 2020, 22:33:48)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2020-02-10 16:54:19.147934: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libnvinfer.so.6'; dlerror: libnvinfer.so.6: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.1/lib64:/usr/local/cuda-10.1/lib64:/shared/users/thomasaar/py37/lib:/shared/users/thomasaar/compiled/fftw/lib:/shared/users/thomasaar/compiled/prismatic/lib:
2020-02-10 16:54:19.149366: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libnvinfer_plugin.so.6'; dlerror: libnvinfer_plugin.so.6: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.1/lib64:/usr/local/cuda-10.1/lib64:/shared/users/thomasaar/py37/lib:/shared/users/th
# Content of script run in GMS
import sys
sys.path.append(r"C:\Users\thomasaar\Documents\GMS Scripts")
import thomas
from importlib import reload
reload(thomas)
thomas.helloworld()
# Content of thomas.py in GMS Scripts folder
@thomasaarholt
thomasaarholt / velox_k_factors.py
Created January 21, 2020 15:42
Export Velox EDS K-factors
# In Velox, choose a dataset and select all elements in the periodic table (This is a little tedious)
# From the Velox Menu, go "EDS" -> "Export Quantification Details..."
# This exports two files, one called "... Lines" and one called "... Composition". We want the former.
import pandas as pd
df = pd.read_csv(r"exported_eds_quant-Lines.csv")
DF = df.iloc[1:] # There was a single blank line in my dataset, so I get rid of it
DF.loc[:,'K-factor'] = DF['K-factor'].astype(float) # String to float on the k-factors
# Two functions that we map across the dataset to split the header into separate elements and line
def splitelement(entry):
@thomasaarholt
thomasaarholt / Second yticks and xticks
Created December 10, 2019 09:55
Matplotlib plot with 1/T and second units
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
X = np.linspace(1,1000,1000)
Y = np.cos(X/200)
@thomasaarholt
thomasaarholt / is_notebook.py
Created August 30, 2019 07:02
Check if code is running in jupyter notebook
def is_notebook():
try:
from IPython import get_ipython
if "IPKernelApp" not in get_ipython().config: # pragma: no cover
raise ImportError("console")
return False
if "VSCODE_PID" in os.environ: # pragma: no cover
raise ImportError("vscode")
return False