<jupyter_start><jupyter_text>Inner workings of HyperSpy signals Note: for Notebook to work in JupyterLab, run : `conda install hyperspy hyperspy-gui-traitsui -c conda-forge` in the Anaconda prompt  Importing library and loading data<jupyter_code>%matplotlib qt5
import hyperspy.api as hs
s = hs.load("datasets/LSMO_STO_linescan.hdf5")<jupyter_output><empty_output><jupyter_text>Signal object and data Data-wise the signals can roughly be divided into three separate parts:- Raw data numbers, contained in `s.data` as a NumPy array- Axes information, in `s.axes_manager`, like scaling, offset, units, ...- Metadata, in `s.metadata` instrument information, markers, operator, ...  The data All the data is contained within an NumPy array, with the same size as the HyperSpy signal<jupyter_code>s.data<jupyter_output><empty_output><jupyter_text>This NumPy array can be changed directly<jupyter_code>s.data = s.data/10
s.data<jupyter_output><empty_output><jupyter_text>Interlude: scientific Python Scientific Python is a large group of libraries, which covers everything from large scale array operations, plotting, fitting, image analysis, ...The most important ones are:- NumPy: very fast array operations ++- Matplotlib: plotting and visualization- SciPy: fitting, image processing, interpolation, ...All these libraries easily interfaces with eachother, so matplotlib can directly plot at NumPy array, and SciPy can directly work with NumPy arrays.HyperSpy relies on all of these libraries (and more!), and this tight integration between them makes it very easy to apply many data processing techniques which hasn't been directly implemented in HyperSpy.For example:<jupyter_code>from scipy.ndimage import gaussian_filter1d
data = s.data[5]
data_blur = gaussian_filter1d(input=data, sigma=3)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(data)
ax.plot(data_blur)
plt.close()<jupyter_output><empty_output><jupyter_text>Axes manager All information about the different dimensions are contained in the `axes_manager` object.<jupyter_code>s.axes_manager<jupyter_output><empty_output><jupyter_text>Axes are of two different types: signal or navigation, which affects how the data is plotted and (sometimes) processed.Changing the axes properties does not change the raw data in `s.data`These values are changed by directly accessing the axis object. So to change the probe dimension axis:<jupyter_code>probe_axis = s.axes_manager.navigation_axes[0]
probe_axis.scale = 31.5
probe_axis.units = 'Å'
probe_axis.name = 'Probe'
probe_axis.offset = -93
s.axes_manager<jupyter_output><empty_output><jupyter_text>Notice that the y-axis for Figure 1 (the navigation plot) has changed to Ångstrøm<jupyter_code>s.plot()<jupyter_output><empty_output><jupyter_text>The values themselves in an axis object is accessed by:<jupyter_code>probe_axis.axis<jupyter_output><empty_output><jupyter_text>Note that the axes must be linear, meaning that the axis values must be evenly spaced. So in the signal `s`, the probe positions cannot be `[-100, 80, 30, 0]`. It is also possible to "switch" the navigation and signal axes by using transpose<jupyter_code>s_transpose = s.T
s_transpose<jupyter_output><empty_output><jupyter_text>So now we got the energy loss dimension as the navigation axis, which makes it easy to see which elements are located where.<jupyter_code>s_transpose.plot()<jupyter_output><empty_output><jupyter_text>The ability to "switch" our navigation and signal axes is a very powerful functionality, since it allows for the data to be "viewed" in different directions. In addition, this new signal `s_transpose` does not use any addition memory, since it uses the same data as `s`, just "viewed" in a different direction.  Metadata dictionary Lastly, information about the microscope, acquisition parameters, and similar metadata is located in `s.metadata`<jupyter_code>s.metadata<jupyter_output><empty_output><jupyter_text>Some of these experimental parameters are used when modelling EELS edges. They can be changed by directly change the value. Note that all these values can be "tab-completed" so it is easy to navigate this metadata "tree".<jupyter_code>s.metadata.Acquisition_instrument.TEM.Detector.EELS.collection_angle = 25
s.metadata<jupyter_output><empty_output><jupyter_text>The metadata also contains the elements added using `s.add_elements`<jupyter_code>s.add_elements(('Ti', 'Mn'))
s.metadata.Sample<jupyter_output><empty_output><jupyter_text>Slicing: inav and isig HyperSpy has extensive support for slicing, which essentially means cropping the data along one or several dimensions. The main functions are `s.inav` and `s.isig`:- `inav` slices in the navigation dimension- `isig` slices in the signal dimensionIf integers are used, it will slice in based on index. If decimal numbers are used, it will slice based on the scaling.To get just the Ti-L32 edge. (Note the use of decimal points.)<jupyter_code>s_crop = s.isig[400.:500.]
s_crop.plot()<jupyter_output><empty_output><jupyter_text>To grab the 20 first pixels in the EELS spectrum.<jupyter_code>s_crop2 = s.isig[0:20]
s_crop2.plot()<jupyter_output><empty_output><jupyter_text>An important thing to keep in mind is that the sliced signal `s_crop` uses the same data as `s`, so changing `s_crop` will also change `s`<jupyter_code>s_crop.data /= 2
s.plot()<jupyter_output><empty_output><jupyter_text>If want want to get a new copy of the signal, use `s.deepcopy()`.<jupyter_code>s_copy = s.deepcopy()<jupyter_output><empty_output><jupyter_text>HyperSpy and functions An important part of the HyperSpy signals are the methods in each signal. We've already seen some of them: `plot`, `create_model`, ... There are very many of these methods, and the easiest way to see them all is using tab-completion: in this case, typing the signal variable, add a `.`, and then press the __TAB__ button.<jupyter_code>s.<jupyter_output><empty_output><jupyter_text>The types of functions available will depend on which type of type the signal is. For example our current signal is an EELSSpectrum, which contains methods for aligning the zero loss peak, making EELS models and other similar utility functions.  Signal types and methods In addition to the `EELSSpectrum` signal type we've been working with this far, there are several different signal types: `Signal1D`, `Signal2D`, `EDSTEMSpectrum`, `EDSSEMSpectrum`, `HologramImage` and more.It is possible to convert between the different types, so for our signal `s`<jupyter_code>s_2d = s.as_signal2D((0, 1))
s_2d.plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07901948842874543, 'fraction_numerical': 0.007308160779537149, 'mean_word_length': 6.28270509977827, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '155654', 'license': 'MIT', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1959, 'n_words': 754, 'path': 'extra/hyperspy_inner_workings.ipynb', 'repo_name': 'mbercx/jlab-workshop', 'size': '6568'}