Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / make_signals_equal_length.py
Created June 7, 2016 15:41
Makes the spectrum image "bss_spectra[0]" and the single spectra in "mlls_objects" the same lengths.
# 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
---------------------------------------------------------------------------
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 July 10, 2016 19:28
Matlab code to read bytes from a NanoSIMS image file
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 July 10, 2016 19:29
Matlab code to read in a NanoSIMS image file
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 August 27, 2016 14:24
Error thrown when trying to restore a model
>>> s = hs.load(...)
>>> m = s.create_model()
...
>>> m.store()
>>> s.models()
└── a
├── components
│ ├── C_K
│ ├── O_K
@thomasaarholt
thomasaarholt / mybin.py
Created October 10, 2016 14:04
Binning function for hyperspy signals
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 November 4, 2016 12:06
Fitting linear components to hyperspy signals
# 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 November 10, 2016 14:02
Function for creating a rectangle on a hyperspy signal
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 December 2, 2016 12:55
Issue with SAMFire on a single-component signal
>>> 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 December 2, 2016 13:01
Issue with SAMFire with a "homemade" component added
>>> 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
...