Code#
Module : mainwindow#
This module contains the main window of the application.
The code uses PySide6 to create the GUI. Each widget and layout in the code are structured using a with blocks structure to ensure clean code and proper memory management.
- class mainwindow.Window#
Window Class
This class represents the main application window for STEM EDX ML. It manages the user interface, signals, and interactions with the objects and graphs.
Attributes#
- keystr
A default key identifier for object selection.
- objectsObjectsManager
Manages the objects within the application. (Objects designed STEM-EDX data or results from decomposition algorithms).
- signalsdict
A dictionary storing different signal instances for UI updates.
- graphslist
A list storing references to currently added graphs. (Graphs designed the plots from decomposition results).
- selected_objectobject
Keeps track of the currently selected object in the tree view.
Methods#
- __onLoadFileClicked():
Opens a file dialog to load a .pts file and adds it to the object manager.
- __onAddGraphClicked():
Handles the addition of graphs based on selected objects in the tree widget.
- __onSelectObjectChanged():
Updates the displayed image based on the selected object and colourscale.
- __onPCAClicked():
Applies the PCA algorithm on selected objects and displays progress.
- __onNMFClicked():
Applies the NMF algorithm on selected objects and displays progress.
- __onImageTitleChanged(title):
Updates the image title based on user input.
- class SignalManager#
Signal Manager Class
s : Signal - A signal that transmits three object parameters.
Module : stem#
This module contains the functions to manage STEM-EDX data. It includes functions to load data, apply decomposition algorithms (PCA, NMF), and visualize results.
- class stem.Decomposition(data, method='svd', n_components=3, normalize_poissonian_noise=True, parent_object=None)#
- next_component()#
- save_plot(path)#
Save the plot of factors and loadings to a specified file path.
Parameters#
- pathstr
The file path where the plot will be saved.
Notes#
This function creates a figure with two subplots: one for the factors and one for the loadings. The factors plot displays the intensity as a function of energy (in keV), and the loadings plot displays the loadings data as an image. The figure is saved as a transparent image file at the specified path.
- class stem.STEM_EDX_DATA(path, data=None, name='STEM_EDX_Object', signal_type='EDS_TEM', rebin_energy=1)#
- apply_NMF(n_components=3)#
Apply Non-negative Matrix Factorisation (NMF) to the data. This method applies NMF to the data stored in the instance. It checks if the NMF has already been applied with the same parameters and skips the computation if so. If the NMF is successful, it stores the result in self.sub_items.
Returns#
- str
‘Success’ if NMF is applied successfully, ‘Failed’ otherwise.
- apply_PCA()#
Apply Principal Component Analysis (PCA) to the data. This method applies PCA using Singular Value Decomposition (SVD) with a specified number of components and an option to normalise Poissonian noise. If the decomposition has already been applied, it will not be re-applied.
Returns#
- str
‘Success’ if PCA was successfully applied, ‘Failed’ otherwise.
Module : utils#
This module contains utility functions used in the application. In particular, PySide6 widgets were updated to be more suitable for mainwindow code structure.
- class utils.CustomToolbar(canvas, parent=None)#
A custom toolbar for a matplotlib canvas, removing unwanted buttons and adding a spin box for selecting components.
Parameters#
- canvasFigureCanvas
The matplotlib canvas to which this toolbar is attached.
- parentQWidget, optional
The parent widget of this toolbar (default is None).
Methods#
- handle_component(value)
Handles the change in selected component and updates the display.
- class utils.ObjectsManager(parent)#
A manager for handling objects with unique names.
Attributes#
- objectsdict
A dictionary storing objects with their names as keys.
- idint
An identifier for the number of objects added.
Methods#
- __getitem__(key)
Retrieves an object by its name.
- add_object(obj)
Adds a new object to the manager.
- class utils.Worker(objects, selected_objects, action, n_components=3)#
A QThread subclass that performs actions on selected objects and emits progress signals.
Parameters#
- objectsdict
A dictionary of objects on which actions will be performed.
- selected_objectslist
A list of object names to be processed.
- actionstr
The action to be performed on the selected objects. Supported actions are ‘PCA’ and ‘NMF’.
Attributes#
- progressSignal
Signal emitted to indicate progress as an integer percentage.
- errorslist
A list of object names for which the action failed.
Methods#
- run()
Executes the specified action on the selected objects and emits progress signals.
- run()#
Executes the specified action (‘PCA’ or ‘NMF’) on selected objects and updates the progress.
The function iterates over the selected objects and applies the specified action (‘PCA’ or ‘NMF’) to each object.
Progress is emitted as a percentage after processing each object.
If the action fails for any object, the object’s name is appended to the errors list.
- utils.show_message(title, message, type='error')#
Displays a message box with a specified title, message, and type.
Parameters#
- titlestr
The title of the message box.
- messagestr
The content of the message to be displayed.
- typestr, optional
The type of the message box, either ‘error’ or ‘information’. Modifies the icon displayed. Default is ‘error’.
Returns#
None
- class utils.x_GraphTabWidget(key, parent, parent_object, child_object)#
A custom QWidget for displaying graph tabs with factors and loadings plots.
Parameters#
- keystr
A unique identifier for the widget.
- parentQWidget
The parent widget.
- parent_objectobject
An object containing the data and settings for the plots.
- child_objectobject
An object containing the selected component and its data.
Attributes#
- keystr
A unique identifier for the widget.
- parentQWidget
The parent widget.
- parent_objectobject
An object containing the data and settings for the plots.
- child_objectobject
An object containing the selected component and its data.
- layoutQVBoxLayout
The layout manager for the widget.
- xlist
The x-axis values for the factors plot.
- figFigure
The matplotlib figure object.
- factors_plotAxes
The axes for the factors plot.
- loadings_plotAxes
The axes for the loadings plot.
- canvasFigureCanvas
The canvas for rendering the matplotlib figure.
- toolbarCustomToolbar
The custom toolbar for the canvas.
Methods#
- update()
Updates the plots with the current data.
- update()#
Updates the plots for factors and loadings based on the selected component. This method clears the current plots and updates them with the data from the selected component of the child object. It sets the x-axis limits, labels, titles, and grid for the factors plot, and sets the title for the loadings plot. It also adjusts the layout and redraws the canvas.
- class utils.x_QComboBox(parent, name, objects, signals, signal='', tooltip='', label='')#
A custom QComboBox widget with additional functionality for handling signals and updating items dynamically.
Parameters#
- parentQWidget
The parent widget.
- namestr
The name of the combobox.
- objectsobject
An object containing the items to be added to the combobox.
- signalsdict
A dictionary of signals for the combobox.
- signalstr, optional
The signal key for updating the combobox (default is ‘’).
- tooltipstr, optional
The tooltip text for the combobox (default is ‘’).
- labelstr, optional
The label text to be displayed next to the combobox (default is ‘’).
Attributes#
- parentQWidget
The parent widget.
- namestr
The name of the combobox.
- keystr
A unique key for the combobox.
- objectsobject
An object containing the items to be added to the combobox.
- signalsdict
A dictionary of signals for the combobox.
- signalpyqtSignal
The signal for updating the combobox.
- layoutQHBoxLayout
The layout containing the combobox and optional label.
- comboboxQComboBox
The combobox widget.
Methods#
- update(items)
Updates the items in the combobox while preserving the current index.
- class utils.x_QHBoxLayout(parent, name)#
A custom QHBoxLayout class that supports context management and automatic key generation.
Parameters#
- parentQWidget or QLayout
The parent widget or layout.
- namestr
The name to be appended to the parent’s key or used as the key.
Attributes#
- parentQWidget or QLayout
The parent widget or layout.
- keystr
The generated key based on the parent’s key and the provided name.
Methods#
- __enter__()
Sets the layout for the parent widget or adds the layout to the parent layout.
- __exit__(*args)
Placeholder for context management exit.
- class utils.x_QImage_Label(parent, signals, signal='', coord_label=None, title_input=None)#
A custom QLabel widget for displaying and interacting with images.
Parameters#
- parentQWidget
The parent widget.
- signalsdict
A dictionary of signals for communication.
- signalstr, optional
The signal key for updating the combobox (default is ‘’).
- coord_labelQLabel, optional
A label for displaying coordinates (default is None).
- title_inputQLineEdit, optional
An input for the title (default is None).
Attributes#
- parentQWidget
The parent widget.
- layoutQHBoxLayout
The layout for the widget.
- coord_labelQLabel
A label for displaying coordinates.
- title_inputQLineEdit
An input for the title.
- titlestr
The title of the image.
- pixmapQPixmap
The pixmap of the image.
- imagendarray
The image data.
- pixmap_offsettuple
The offset of the pixmap.
- pixel_sizeint
The size of each pixel.
- min_img_xfloat
The minimum x-coordinate of the image.
- min_img_yfloat
The minimum y-coordinate of the image.
- max_img_xfloat
The maximum x-coordinate of the image.
- max_img_yfloat
The maximum y-coordinate of the image.
- signalsdict
A dictionary of signals for communication.
- signalpyqtSignal
The signal for updating the image.
Methods#
- update(image, colorscale=’Greyscale’)
Updates the displayed image with the given colourscale.
- set_pixmap_params(image, offset, pixel_size, min_img_x, min_img_y, max_img_x, max_img_y)
Sets the parameters for the pixmap.
- mousePressEvent(event)
Handles mouse press events.
- mouseMoveEvent(event)
Handles mouse move events.
- enterEvent(event)
Handles the cursor when entering the widget.
- leaveEvent(event)
Handles the cursor when leaving the widget.
- enterEvent(event)#
Handles the event when the cursor enters the specific area.
- leaveEvent(event)#
Handles the event when the cursor leaves the specific area.
- mouseMoveEvent(event: QMouseEvent)#
Handle the mouse move event to update the coordinate label with the current mouse position and corresponding image value.
Parameters#
- eventQMouseEvent
The mouse event containing the current position of the mouse cursor.
- set_pixmap_params(image, offset, pixel_size, min_img_x, min_img_y, max_img_x, max_img_y)#
Set parameters for the pixmap.
Parameters#
- imageQImage
The image to be used for the pixmap.
- offsetQPoint
The offset position of the pixmap.
- pixel_sizeint
The size of each pixel in the pixmap.
- min_img_xint
The minimum x-coordinate of the image.
- min_img_yint
The minimum y-coordinate of the image.
- max_img_xint
The maximum x-coordinate of the image.
- max_img_yint
The maximum y-coordinate of the image.
- update(image, colorscale='Greyscale')#
Update the display with a new image and apply a colourscale.
Parameters#
- imagenumpy.ndarray or str
The image to be displayed. If a string is provided, the function returns without updating.
- colorscalestr, optional
The colourscale to be applied to the image. Default is ‘Greyscale’.
- class utils.x_QLabel(parent, label='', alignment=AlignmentFlag.AlignLeft)#
A custom QLabel wrapper class.
Parameters#
- parentQWidget
The parent widget to which this label belongs.
- labelstr, optional
The text to be displayed on the label (default is an empty string).
- alignmentQt.Alignment, optional
The alignment of the label text (default is Qt.AlignLeft).
Attributes#
- parentQWidget
The parent widget.
- labelQLabel
The QLabel instance with the specified text and alignment.
Methods#
- __init__(parent, label=’’, alignment=Qt.AlignLeft)
Initialises the x_QLabel instance and adds it to the parent’s layout.
- class utils.x_QLineEdit(parent, name, tooltip='', label='', placeholder='', default='')#
A custom QLineEdit widget with additional features.
Parameters#
- parentQWidget
The parent widget.
- namestr
The name of the QLineEdit widget.
- tooltipstr, optional
The tooltip text for the QLineEdit widget (default is ‘’).
- labelstr, optional
The label text to be displayed alongside the QLineEdit widget (default is ‘’).
- placeholderstr, optional
The placeholder text for the QLineEdit widget (default is ‘’).
- defaultstr, optional
The default text for the QLineEdit widget (default is ‘’).
Attributes#
- parentQWidget
The parent widget.
- namestr
The name of the QLineEdit widget.
- keystr
A unique key generated by combining the parent’s key and the name.
- layoutQHBoxLayout
The layout containing the QLineEdit and optional QLabel.
- line_editQLineEdit
The QLineEdit widget.
- class utils.x_QPushButton(parent, name, objects, tooltip='', label='')#
A custom QPushButton class that integrates with a parent layout and allows for additional configuration such as tooltips and labels.
Parameters#
- parentQWidget
The parent widget to which this button belongs.
- namestr
The name of the button.
- objectslist
A list of objects associated with this button.
- tooltipstr, optional
The tooltip text for the button (default is an empty string).
- labelstr, optional
The label text to be displayed alongside the button (default is an empty string).
Attributes#
- parentQWidget
The parent widget.
- namestr
The name of the button.
- keystr
A unique key generated by combining the parent’s key and the button’s name.
- objectslist
A list of objects associated with this button.
- layoutQHBoxLayout
The horizontal box layout that contains the button and its label.
- buttonQPushButton
The QPushButton instance created by this class.
- class utils.x_QSeparator(window, parent, orientation='H')#
A custom QFrame separator widget. Parameters ———- window : QWidget
The parent window for the separator.
- parentQBoxLayout
The layout to which the separator will be added.
- orientationstr, optional
The orientation of the separator, either ‘H’ for horizontal or ‘V’ for vertical. Default is ‘H’.
Methods#
- __init__(window, parent, orientation=’H’)
Initializes the separator with the given orientation and adds it to the parent layout.
- class utils.x_QTab(parent, name)#
A custom QWidget class for creating a tab with a specific name and parent.
Parameters#
- parentQWidget
The parent widget to which this tab belongs.
- namestr
The name of the tab.
Attributes#
- namestr
The name of the tab.
- parentQWidget
The parent widget to which this tab belongs.
- keystr
A unique key for the tab, derived from the parent’s key and the tab’s name.
- layoutQVBoxLayout
The layout manager for the tab, aligned to the top.
Methods#
- __enter__()
Adds the tab to the parent widget.
- __exit__(*args)
Placeholder for context manager exit, does nothing.
- class utils.x_QTabWidget(parent, name, movable=False, closable=False)#
A custom QTabWidget with additional functionality.
Parameters#
- parentQWidget
The parent widget.
- namestr
The name of the tab widget.
- movablebool, optional
If True, tabs can be moved by the user (default is False).
- closablebool, optional
If True, tabs can be closed by the user (default is False).
Attributes#
- namestr
The name of the tab widget.
- parentQWidget
The parent widget.
- keystr
A unique key for the tab widget, combining the parent’s key and name.
Methods#
- __enter__()
Adds the widget to the parent when used in a context manager.
- __exit__(*args)
Placeholder for context manager exit, does nothing.
- class utils.x_QTreeWidget(parent, name, objects, signals, window, columns=1, headers=['Column'])#
A custom QTreeWidget class for managing and displaying hierarchical data.
Parameters#
- parentQWidget
The parent widget.
- namestr
The name of the tree widget.
- objectsobject
The object containing the data to be displayed.
- signalsdict
A dictionary of signals for communication.
- windowQMainWindow
The main window, used for displaying message boxes.
- columnsint, optional
The number of columns in the tree widget (default is 1).
- headerslist of str, optional
The headers for the columns (default is [‘Column’]).
Methods#
- __enter__()
Enter the runtime context related to this object.
- __exit__(*args)
Exit the runtime context related to this object.
- update()
Update the tree widget with the current data.
- __context_menu(position)
Display a context menu at the given position.
- update()#
Update the tree widget with the current objects and their sub-items. Clears the existing tree and repopulates it with the objects and their respective sub-items. Each item is made checkable and enabled. Emits a signal after updating the tree.
- class utils.x_QVBoxLayout(parent, name, ratio=1)#
A custom QVBoxLayout class that supports context management and layout ratio.
Parameters#
- parentQWidget or QLayout
The parent widget or layout to which this layout will be added.
- namestr
A unique name for this layout, used to generate a key.
- ratioint, optional
The ratio for adding this layout to its parent, by default 1.
Attributes#
- parentQWidget or QLayout
The parent widget or layout.
- ratioint
The ratio for adding this layout to its parent.
- keystr
A unique key generated from the parent’s key and the provided name.
Methods#
- __enter__()
Sets the layout for the parent widget or adds it to the parent layout.
- __exit__(*args)
Placeholder for context management exit, does nothing.
Module : colorscales#
This module contains the color scales used in the application. The color scales are used to represent the datacube in the top right layout.
- colorscales.Greyscale(N)#
- colorscales.Hot(N)#
- colorscales.Inferno(N)#
- colorscales.Jet(N)#
- colorscales.Plasma(N)#
- colorscales.Viridis(N)#