Skip to content

Instantly share code, notes, and snippets.

Avatar

Thomas Aarholt thomasaarholt

  • University of Oslo
  • Oslo, Norway
View GitHub Profile
@thomasaarholt
thomasaarholt / make_signals_equal_length.py
Created Jun 7, 2016
Makes the spectrum image "bss_spectra[0]" and the single spectra in "mlls_objects" the same lengths.
View make_signals_equal_length.py
# Make signal starting point and length equal
signal_min = bss_spectra[0].axes_manager[-1].axis.min() # This is a float
signal_shortest_len = len(bss_spectra[0].axes_manager[-1].axis) # This is an int
# Get largest value of s
for s in mlls_objects:
if s.axes_manager[0].axis.min() > signal_min:
signal_min = s.axes_manager[0].axis.min()
# Crop all the spectra to start at the same energy index
View ploterror.py
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-d6ad06492590> in <module>()
----> 1 bss_spectra[0].plot()
/Users/thomas/Dropbox/0_Git/GitHub Desktop/hyperspy/hyperspy/signal.py in plot(self, navigator, axes_manager, **kwargs)
2029
2030 self._plot.plot(**kwargs)
-> 2031 self.events.data_changed.connect(self.update_plot, [])
2032 if self._plot.signal_plot:
@thomasaarholt
thomasaarholt / read_bytes.m
Created Jul 10, 2016
Matlab code to read bytes from a NanoSIMS image file
View read_bytes.m
function [out,h] = read_bytes(fid, location, num_bytes, ttype, reverse_bytes, b8)
fseek(fid,location,'bof');
h=fread(fid,num_bytes,ttype);
if reverse_bytes
% when data have been stored under Windows, the bytes are reversed
h=h(end:-1:1);
end;
@thomasaarholt
thomasaarholt / read_im_file.m
Created Jul 10, 2016
Matlab code to read in a NanoSIMS image file
View read_im_file.m
function [im,pp,p]=read_im_file(ff,ask_for_planes,load_accumulated)
% Read the binary im file produced by the NanoSIMS 50L machine, and output
% the raw images as well as the additional parameters characterizing the
% dataset.
% NOTE: I don't know why, but data in some binary files are stored as uint8
% (e.g. from MPI Bremen), and in some others they are stored as ushort
% (e.g., IoW Warnemuende). It seems that this difference can be "detected"
% based on the value of the pixel_size in the Header_image structure.
% Therefore, the below coding relies on this to automatically find out how
@thomasaarholt
thomasaarholt / model_restore_error.py
Last active Aug 27, 2016
Error thrown when trying to restore a model
View model_restore_error.py
>>> s = hs.load(...)
>>> m = s.create_model()
...
>>> m.store()
>>> s.models()
└── a
├── components
│ ├── C_K
│ ├── O_K
@thomasaarholt
thomasaarholt / mybin.py
Created Oct 10, 2016
Binning function for hyperspy signals
View mybin.py
def mybin(s, xbin=1, ybin=1, sbin=1):
"""Return spectrum s binned by factors xbin, ybin, sbin"""
if (xbin < 1) or (ybin < 1) or (sbin < 1):
raise ValueError("One of your binnings is smaller than 1. For signal axis to remain constant, leave as 1")
if all(n==1 for n in (xbin, ybin, sbin)):
print("Not binning because all binnings == 1")
return s # End Early
# Get signal channels
@thomasaarholt
thomasaarholt / linear.py
Last active Nov 4, 2016
Fitting linear components to hyperspy signals
View linear.py
# In model.py
from numpy.linalg import lstsq as linear_lstsq
(...)
elif fitter == "linear_lstsq":
signal_axis = self.axis.axis[np.where(self.channel_switches)]
component_data = np.array([component.function(signal_axis) for component in self])
print(weights)
output = linear_lstsq(component_data.T, self.signal()[np.where(self.channel_switches)], **kwargs)
self.p0 = tuple([old*factor for old, factor in zip(self.p0, output[0])])
self.fit_output = output
@thomasaarholt
thomasaarholt / Interactive_Rectangle_ROI.py
Last active Nov 10, 2016
Function for creating a rectangle on a hyperspy signal
View Interactive_Rectangle_ROI.py
def ROI_Rect(s, roi_XY=[], hide=False):
"""
Plots a hyperspy signal and draws an interactive ROI on it on the top left tenth of the image.
Can take a list of [x1, y1, x2, y2] to set a known intial ROI.
Returns a tuple of (roi, roi_signal). Use hide=True to not show the plot.
Please report bugs/improvements to thomasaarholt@gmail.com
"""
import hyperspy.api as hs
if s.axes_manager.navigation_dimension < 2:
@thomasaarholt
thomasaarholt / samfire1.py
Created Dec 2, 2016
Issue with SAMFire on a single-component signal
View samfire1.py
>>> m = sc.create_model(auto_add_lines=False)
>>> samf = m.create_samfire()
[0:apply]:
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<string> in <module>()
/Users/thomas/Dropbox/0_Git/GitHub/hyperspy_aar/hyperspy/samfire_utils/samfire_pool.py in <lambda>(worker, m_dict)
148 self.rworker = ipp_Reference('worker')
149 direct_view.apply(lambda worker, m_dict:
--> 150 worker.create_model(m_dict, 'z'), self.rworker,
151 m_dict)
@thomasaarholt
thomasaarholt / SAMFire2.py
Last active Dec 2, 2016
Issue with SAMFire with a "homemade" component added
View SAMFire2.py
>>> m = sc.create_model()
>>> m.components
# | Attribute Name | Component Name | Component Type
---- | ------------------- | ------------------- | -------------------
0 | background_order_6 | background_order_6 | Polynomial
1 | Al_Ka | Al_Ka | Gaussian
2 | Al_Kb | Al_Kb | Gaussian
3 | C_Ka | C_Ka | Gaussian
4 | Ca_Ka | Ca_Ka | Gaussian
...
You can’t perform that action at this time.