Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / get_individual_model_components.py
Created January 22, 2018 12:40
Trick for saving individual components from a model fro plotting
# See below for use
def get_individual_model_components(m):
"""
Exports the signals from the individual components present in the model
Returns a list of signals, from the current position in the model.
"""
def get_A(component):
return component.A.value
def set_A(component, value):
@thomasaarholt
thomasaarholt / rotate_ase.py
Created March 17, 2018 12:01
Awful way of rotating a cell
def rotate90(sample):
from ase.build import rotate as rot
a1 = (1,0,0)
a2 = (0,0,1)
b1 = (0,0,1)
b2 = (1,0,0)
rot(sample, a1, a2, b1, b2, rotate_cell=False)
sample.cell[0,0], sample.cell[2,2] = sample.cell[2,2], sample.cell[0,0]
@thomasaarholt
thomasaarholt / prism.py
Created March 17, 2018 13:30
Pyprismatic function that saves to the hyperspy format
def prism(file, path=None, focus=0, FP=20, alpha=20.0e-3):
import gc
gc.collect()
import os
import pyprismatic as pr
file = os.path.abspath(file)
save_path = path
path, filename = os.path.split(file)
name, _ = os.path.splitext(filename)
@thomasaarholt
thomasaarholt / litenoppgave.py
Created April 2, 2018 11:21
liten oppgave.py
mylist = [20, 30, 1, 3, 50]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-1-bcff7cee2750> in <module>()
1 get_ipython().run_line_magic('matplotlib', 'notebook')
2 import hyperspy.api as hs
----> 3 hs.preferences.gui()
c:\users\me\documents\github\hyperspy\hyperspy\ui_registry.py in pg(self, display, toolkit, **kwargs)
172 def pg(self, display=True, toolkit=None, **kwargs):
173 return get_gui(self, toolkey=toolkey, display=display,
@thomasaarholt
thomasaarholt / ipywidget_file_browser.py
Last active September 12, 2020 00:17
ipywidgets file browser based on the Select widget. Single click folders to enter, click Load to load a file.
import ipywidgets as widgets
import os
from pathlib import Path
cwd = Path(os.getcwd())
FOLDERLABEL = '-------FOLDERS-------'
FILESLABEL = '-------FILES-------'
def get_folder_contents(folder):
@thomasaarholt
thomasaarholt / tanh_traceback.py
Created May 31, 2018 14:47
Traceback from an tanh overflow warning
File "D:\Anaconda\envs\py36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "D:\Anaconda\envs\py36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\Anaconda\envs\py36\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "D:\Anaconda\envs\py36\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "D:\Anaconda\envs\py36\lib\site-packages\ipykernel\kernelapp.py", line 486, in start
self.io_loop.start()
string = 'a*x**r+b*x+c'
expression = sympy.sympify(string)
expr = hs.model.components1D.Expression(string, 'test')
data = np.random.random((5,300))
s = hs.signals.Signal1D(data)
m = s.create_model()
m.append(expr)
@thomasaarholt
thomasaarholt / plot_im.py
Created July 6, 2018 12:50
Plot hyperspy signal nicely
def plot_im(s, ax=None, scalebar=True, label=True, clim=None, cmap=None, cbar=True, loc=4, axislabel=False, colorbar_format="scientific", colorbar_decimals=1, dont_show=False, **kwargs):
"""
Plots the HS image nicely with a scalebar. Label can be modified or turned off.
Use dont_show=True to not show a plot, but still return it.
Returns:
fig - the figure
ax - the axes
im - the image data
"""
import matplotlib.pyplot as plt
@thomasaarholt
thomasaarholt / linear_regression.py
Created July 8, 2018 15:05
Linear Regression on multidimensional arrays
def linear_regression(y, comp_data):
'''
Performs linear regression on single pixels as well
as multidimensional arrays
Parameters
----------
y : array_like, shape: (signal_axis) or (nav_shape, signal_axis)
The data to be fit to
comp_data : array_like, shape: (number_of_comp, signal_axis)