pycat.ui#

Interface Components#

User-Interface (UI) Module for PyCAT

This module contains the UI class for the toolbox functions, which provides a user interface for various toolbox functions within a Napari viewer. This class integrates with the central management system to facilitate image analysis operations, offering a variety of tools such as opening images, measuring lines, and running analyses like wavelet noise subtraction and correlation function analysis.

This is the main UI class which is used to setup individual functions, analysis methods, and the menu bar in the napari viewer application. It provides a variety of methods for creating dropdown menus for layer selection, updating these dropdowns based on viewer layer changes, handling button clicks, and managing dock widgets.

New analysis methods and individual functions can be created and added to this module following the existing structure, which includes methods for adding the functions to the toolbox and incorporating them into the viewer interface.

Author#

Christian Neureuter, GitHub: cneureuter

Date#

4-20-2024

class BaseUIClass(viewer)[source]#

Bases: object

A base UI class designed to provide utility functions for managing UI elements and interactions within a napari viewer instance. This class includes methods for creating dropdown menus for layer selection, updating these dropdowns based on viewer layer changes, handling button clicks, and managing dock widgets.

viewer#

The napari viewer instance with which the UI components will interact.

Type:

napari.Viewer

__init__(viewer)[source]#

Initializes the BaseUIClass with a reference to the napari viewer instance.

Parameters:

viewer (napari.Viewer) – The napari viewer instance to interact with.

create_layer_dropdown(layer_type)[source]#

Creates a dropdown (QComboBox) widget that lists layers of a specific type.

Parameters:

layer_type (type) – The type of layer to list in the dropdown, e.g., napari.layers.Image or napari.layers.Labels.

Returns:

dropdown – The created dropdown widget populated with layers of the specified type.

Return type:

QComboBox

update_dropdown_items(dropdown, layer_type)[source]#

Updates the items in the dropdown based on the current layers in the viewer that match the specified type. Optionally ensures a ‘None’ option is available in the dropdown.

Parameters:
  • dropdown (QComboBox) – The dropdown widget to update.

  • layer_type (type) – The type of layer to include in the dropdown.

add_text_label(layout, text, font_size=10, bold=False)[source]#

Adds a text label above a dropdown widget in the given layout, with an option to make the text bold.

Parameters:
  • layout (QLayout) – The layout to which the label will be added.

  • text (str) – The text of the label.

  • font_size (int, optional) – The font size of the label text.

  • bold (bool, optional) – If True, the label text will be bold.

on_general_button_clicked(processing_function, viewer=None, *args, **kwargs)[source]#

A general-purpose method to be connected to button click signals. It extracts selected layers from dropdowns, filters out non-layer arguments, and calls a specified processing function with these layers and any additional arguments.

Parameters:
  • processing_function (callable) – The function to call with the extracted layers and additional arguments.

  • viewer (napari.Viewer, optional) – The napari viewer instance, if not already provided as part of the class.

clear_dock()[source]#

Removes all dock widgets from the viewer’s window.

update_tool(event)[source]#

Updates the active tool based on the currently active layer. This could adjust brush sizes for label layers or switch modes for shape layers.

Parameters:

event (Event) – The event that triggered the tool update, not directly used.

class ToolboxFunctionsUI(viewer, central_manager)[source]#

Bases: BaseUIClass

Provides a user interface for various toolbox functions within a Napari viewer.

This class integrates with the central management system to facilitate image analysis operations, offering a variety of tools such as opening images, measuring lines, and running analyses like wavelet noise subtraction and cross-correlation function analysis.

Parameters:
  • viewer (napari.Viewer) – The Napari viewer instance to which the toolbox functions will be added.

  • central_manager (CentralManager) – The central management system handling data and operations across tools.

central_manager#

Stores the central management system instance for accessing and managing data.

Type:

CentralManager

__init__(viewer, central_manager)[source]#

Initialize the UI with a Napari viewer and a central management system.

class AnalysisMethodsUI(viewer, central_manager)[source]#

Bases: BaseUIClass

A user interface (UI) class designed to manage and switch between different analysis methodologies within a Napari Viewer environment. It facilitates the dynamic instantiation of data processing classes and their associated UIs based on the user’s selection, supporting a flexible and modular approach to data analysis.

viewer#

The graphical viewer instance that the UI class interacts with. This viewer is used to display and manage the visual elements of the analysis interfaces.

Type:

napari.Viewer

central_manager#

An instance of a central management class that coordinates the active data and analysis state across different components of the application.

Type:

CentralManager Class

_switch_analysis(data_class, ui_class, \*data_class_args, \*\*data_class_kwargs):

Dynamically switches the analysis method by instantiating the given data processing class and its associated UI class, effectively updating the analysis interface.

_switch_to_condensate_analysis(*args, \*\*kwargs):

Switches the analysis interface to condensate analysis, a specific type of analysis method.

_switch_to_object_coloc_analysis(*args, \*\*kwargs):

Switches the analysis interface to object colocalization analysis.

_switch_to_pixel_coloc_analysis(*args, \*\*kwargs):

Switches the analysis interface to pixel colocalization analysis.

_switch_to_general_analysis(*args, \*\*kwargs):

Switches the analysis interface to a general analysis mode.

_switch_to_fibril_analysis(*args, \*\*kwargs):

Switches the analysis interface to fibril analysis, focusing on fibril structures.

__init__(viewer, central_manager)[source]#

Initializes the AnalysisMethodsUI class with a viewer and central manager.

Parameters:
  • viewer (napari.Viewer) – The graphical viewer instance to be used by the UI class.

  • central_manager (CentralManagerType) – The central management instance responsible for managing data and analysis state.

class CondensateAnalysisUI(viewer, central_manager)[source]#

Bases: AnalysisMethodsUI

A specialized user interface class for condensate analysis within a larger analytical framework. Inherits from AnalysisMethodsUI to utilize the base functionalities and to add specific components relevant to condensate analysis.

This class sets up a custom layout for the analysis of condensates, incorporating a series of predefined analysis and processing steps. It dynamically constructs the UI components based on the requirements of condensate analysis, facilitating an efficient workflow for users.

viewer#

The graphical viewer instance used for display and interaction purposes.

Type:

napari.Viewer

central_manager#

A central management instance responsible for managing data and analysis state, facilitating the interaction between different components of the application.

Type:

CentralManagerType

condensate_layout#

The layout manager for arranging UI components vertically. It is used to organize the specific UI components required for condensate analysis.

Type:

QVBoxLayout

setup_ui():

Initializes and arranges the UI components specific to condensate analysis into the application’s interface, ensuring a user-friendly environment for conducting analyses.

__init__(viewer, central_manager)[source]#

Initializes the CondensateAnalysisUI class with a viewer and central manager, setting up the initial layout for further UI component addition.

Parameters:
  • viewer (napari.Viewer) – The graphical viewer instance to be used for UI display and interaction.

  • central_manager (CentralManagerType) – The central management instance for coordinating data and analysis flow.

setup_ui()[source]#

Sets up the specific UI components necessary for conducting condensate analysis. This includes initializing and arranging various analysis and processing steps in the user interface.

class ObjectColocAnalysisUI(viewer, central_manager)[source]#

Bases: AnalysisMethodsUI

A specialized user interface (UI) class for object-based colocalization analysis within a larger analytical framework. Inherits from AnalysisMethodsUI to leverage foundational functionalities while introducing specific components necessary for comprehensive object-based colocalization analysis.

This class facilitates the assembly of UI components tailored to the analysis requirements of object colocalization, enabling researchers to perform detailed analyses with an emphasis on spatial relationships between different objects within an image.

viewer#

The graphical viewer instance utilized for displaying and interacting with the analysis tools and results.

Type:

napari.Viewer

central_manager#

The central management instance that oversees the flow of data and analysis across various components, ensuring a cohesive operational experience.

Type:

CentralManager Class

object_coloc_layout#

A vertical layout manager to sequentially arrange UI components for object colocalization analysis, ensuring an organized presentation within the UI.

Type:

QVBoxLayout

setup_ui():

Initializes and organizes the specific UI components for object-based colocalization analysis, constructing an intuitive and efficient workspace for users to conduct their analysis.

__init__(viewer, central_manager)[source]#

Initializes the ObjectColocAnalysisUI with essential components such as the viewer and central manager, and prepares the vertical layout for subsequent UI component additions.

Parameters:
  • viewer (napari.Viewer) – The graphical viewer used for visual interaction within the analysis UI.

  • central_manager (CentralManagerType) – A central manager that facilitates coordination between different analysis and data management components.

setup_ui()[source]#

Sets up the UI components specifically required for object-based colocalization analysis, detailing the process flow and enabling comprehensive analysis features through a structured UI layout.

class PixelColocAnalysisUI(viewer, central_manager)[source]#

Bases: AnalysisMethodsUI

A user interface (UI) class tailored for pixel-wise colocalization analysis. Inherits from AnalysisMethodsUI to provide a specialized framework that integrates pixel-based analysis tools into a cohesive graphical interface. This class focuses on facilitating the exploration of spatial correlations at the pixel level between different channels or markers within an image.

viewer#

The graphical viewer for displaying and interacting with images and analysis results.

Type:

napari.Viewer

central_manager#

Manages the flow of data and analysis operations, ensuring seamless integration of various analysis components.

Type:

CentralManagerType

pixel_coloc_layout#

Organizes UI components vertically, tailored for pixel colocalization analysis workflows.

Type:

QVBoxLayout

setup_ui():

Sets up the UI for pixel-wise colocalization analysis, incorporating various image processing and analysis functions designed for detailed spatial correlation studies.

__init__(viewer, central_manager)[source]#

Initializes the PixelColocAnalysisUI with essential components such as the viewer and central manager, and prepares the vertical layout for subsequent UI component additions.

Parameters:
  • viewer (napari.Viewer) – The graphical viewer used for visual interaction within the analysis UI.

  • central_manager (CentralManagerType) – A central manager that facilitates coordination between different analysis and data management components.

setup_ui()[source]#

Sets up the UI components specifically required for pixel-wise correlation coefficient analysis, detailing the process flow and enabling comprehensive analysis features through a structured UI layout.

class GeneralAnalysisUI(viewer, central_manager)[source]#

Bases: AnalysisMethodsUI

A user interface (UI) class designed for general analysis purposes within a broader analytical software framework. Inherits from AnalysisMethodsUI, providing a versatile and adaptable UI that supports a wide range of image processing and analysis operations. This class is ideal for users seeking a generalized analysis toolset that can be applied to various types of data.

viewer#

A graphical viewer for visual interaction with analysis tools and data.

Type:

napari.Viewer

central_manager#

Coordinates the overall analysis workflow and data management across the application.

Type:

CentralManagerType

general_layout#

Manages the arrangement of UI components for a flexible and comprehensive general analysis workflow.

Type:

QVBoxLayout

setup_ui():

Initializes and arranges UI components for general analysis, offering a broad spectrum of image processing and analysis functionalities to suit diverse research needs.

__init__(viewer, central_manager)[source]#

Initializes the GeneralAnalysisUI class with a viewer and central manager, setting up the initial layout for further UI component addition.

Parameters:
  • viewer (napari.Viewer) – The graphical viewer instance to be used for UI display and interaction.

  • central_manager (CentralManagerType) – The central management instance for coordinating data and analysis state.

setup_ui()[source]#

Sets up the UI components specifically required for general analysis, detailing the process flow and enabling comprehensive analysis features through a structured UI layout.

class FibrilAnalysisUI(viewer, central_manager)[source]#

Bases: AnalysisMethodsUI

Specializes in the analysis of fibrillar structures within images, extending the AnalysisMethodsUI class to provide specific functionalities for fibril identification, characterization, and quantification. This UI class is tailored for researchers focused on studying fibrous proteins, DNA, or other fibrillar components, offering dedicated tools for enhanced visualization and analysis of fibril morphology.

viewer#

Serves as the interface for visual data exploration and analysis interaction.

Type:

napari.Viewer

central_manager#

Ensures integrated and efficient management of data and analysis workflows specific to fibril analysis.

Type:

CentralManagerType

fibril_layout#

Arranges UI components that facilitate fibril analysis operations, promoting an organized and intuitive user experience.

Type:

QVBoxLayout

setup_ui():

Constructs the UI for fibril analysis, incorporating specialized image processing and analysis techniques aimed at extracting and analyzing fibrillar features within complex biological or material science images.

__init__(viewer, central_manager)[source]#

Initializes the FibrilAnalysisUI class with a viewer and central manager, setting up the initial layout for further UI component addition.

Parameters:
  • viewer (napari.Viewer) – The graphical viewer instance to be used for UI display and interaction.

  • central_manager (CentralManagerType) – The central management instance for coordinating data and analysis state.

setup_ui()[source]#

Sets up the UI components specifically required for fibril analysis, detailing the process flow and enabling comprehensive analysis features through a structured UI layout.

class MenuManager(viewer, central_manager)[source]#

Bases: object

Manages the setup and addition of menu items to a napari viewer instance. This class integrates a variety of analysis, file I/O, and toolbox functions into the viewer’s menu bar, allowing for easy access to different functionalities within the application.

viewer#

The napari Viewer instance to which the menus will be added.

Type:

napari.Viewer

central_manager#

An instance of a custom class managing central functionalities, including file I/O operations, analysis methods, and toolbox functions.

Type:

CentralManager

_setup_menu_bar():

Sets up the main menu bar with specific menu items and their associated actions.

make_lambda(action_method, kwargs):

Creates a lambda function for triggering actions with arguments.

_add_actions_to_menu(actions_dict, menu):

Adds actions to a given menu based on a dictionary of action names and methods.

_add_file_io_methods_to_menu():

Adds file I/O methods as menu items under the file menu.

_add_analysis_methods_to_menu():

Adds analysis methods as menu items under the analysis methods menu.

_add_toolbox_to_menu():

Adds toolbox functions as menu items under the toolbox menu.

__init__(viewer, central_manager)[source]#

Initializes the MenuManager with a viewer and a central_manager instance, and sets up the menu bar.

Parameters:
  • viewer (Viewer) – The napari Viewer instance to which the menus will be added.

  • central_manager (CentralManager) – An instance managing central functionalities, like file I/O and analysis methods.

make_lambda(action_method, kwargs)[source]#

Creates a lambda function for triggering actions with arguments. This allows for the dynamic execution of methods with specific parameters directly from menu action triggers.

Parameters:
  • action_method (callable) – The method to be executed when the action is triggered.

  • kwargs (dict) – A dictionary of keyword arguments to be passed to the action method.

Returns:

A lambda function that calls action_method with kwargs when triggered.

Return type:

function

UI Utilities#

User-Interface (UI) Utilities Module for PyCAT

This module provides functions for displaying dataframes in a pop-up dialog window and adding images with a default colormap to a Napari viewer. The functions are designed to enhance the user experience when working with dataframes and image data in the Napari viewer. The module also includes a function to refresh the viewer with new data, as sometimes modifying an active layer in napari requires manually ‘refreshing’ it to see the changes.

Author#

Christian Neureuter, GitHub: cneureuter

Date#

4-20-2024

class DataFrameModel(df=Empty DataFrame Columns: [] Index: [])[source]#

Bases: QAbstractTableModel

A custom table model to interface with a pandas DataFrame for use within Qt’s Model-View-Controller (MVC) architecture.

_data#

The pandas DataFrame that backs this model.

Type:

pandas.DataFrame

Parameters:

df (pandas.DataFrame, optional) – The pandas DataFrame to be used by the model. Defaults to an empty DataFrame.

__init__(df=Empty DataFrame Columns: [] Index: [])[source]#

Initializes the DataFrameModel with a specified pandas DataFrame.

Parameters:

df (pandas.DataFrame, optional) – The pandas DataFrame to initialize the model. Defaults to an empty DataFrame.

rowCount(parent=None)[source]#

Returns the number of rows in the model. Overrides QAbstractTableModel.rowCount.

columnCount(parent=None)[source]#

Returns the number of columns in the model. Overrides QAbstractTableModel.columnCount.

data(index, role=0)[source]#

Returns the data stored at the specified index with the given role. Overrides QAbstractTableModel.data.

Parameters:
  • index (QModelIndex) – The index of the data to return.

  • role (int) – The role for which data is requested, typically Qt.DisplayRole for displaying text.

Returns:

The data at the given index as a string if the index is valid and the role is DisplayRole, otherwise None.

Return type:

str or None

headerData(section, orientation, role)[source]#

Returns the header data for the given role and section in the specified orientation. Overrides QAbstractTableModel.headerData.

Parameters:
  • section (int) – The section of the header, corresponding to column or row number.

  • orientation (Qt.Orientation) – The orientation (horizontal or vertical) of the header.

  • role (int) – The role for which the header data is requested, typically Qt.DisplayRole for displaying text.

Returns:

The header data as a QVariant if the role is DisplayRole, otherwise None.

Return type:

QVariant or None

create_table_view(dataframe)[source]#

Creates and configures a QTableView to display a pandas DataFrame within a the Napari viewer’s Qt application using the MVC (Model-View-Controller) design pattern. The function sets up a table model for the DataFrame, applies settings to optimize the display and interaction, and incorporates a context menu for additional functionalities such as copying and saving data.

Parameters:

dataframe (pandas.DataFrame) – The pandas DataFrame to be displayed in the QTableView. This DataFrame is used to populate the rows and columns of the table view.

Returns:

table_view – The configured QTableView object with the DataFrame model set, columns resized to fit content, and a custom context menu enabled for user interactions like copying data to the clipboard or saving it to a CSV file.

Return type:

QTableView

Notes

A custom context menu is added to provide additional functionalities directly accessible by right-clicking on the table view. The context menu is configured to work with the specific position and data of the clicked view through a lambda function connection.

table_context_menu(position, table_view, dataframe)[source]#

Creates and displays a context menu for a table view at a specified position, providing options for copying content and saving the data to a CSV file.

Parameters:
  • position (QPoint) – The position on the screen where the context menu is requested.

  • table_view (QTableView) – The table view instance where the context menu will be applied.

  • dataframe (pandas.DataFrame) – The pandas DataFrame associated with the table view, used for saving content.

Notes

The function adds “Copy” and “Save as CSV” actions to the context menu and performs the corresponding action based on user selection.

copy_table_content(table_view)[source]#

Copies the content of the specified table view to the clipboard in a tab-separated format, including column headers.

Parameters:

table_view (QTableView) – The table view whose content is to be copied to the clipboard.

Notes

The function constructs a string of tab-separated values from the table’s data, including headers, and copies it to the system clipboard.

save_table_as_csv(dataframe)[source]#

Prompts the user to select a file path and saves the specified pandas DataFrame to a CSV file at that location.

Parameters:

dataframe (pandas.DataFrame) – The DataFrame to be saved as a CSV file.

Notes

The function opens a file dialog for the user to select the save location and filename, then writes the DataFrame to a CSV file at the specified path.

show_dataframes_dialog(window_title, tables_info)[source]#

Displays a dialog window with a scrollable area that contains multiple dataframes shown as table views.

Parameters:
  • window_title (str) – The title of the dialog window.

  • tables_info (list of tuples) – A list where each tuple contains a title (str) for the table and a pandas DataFrame. The title is displayed as a label above the corresponding table view.

Notes

Each DataFrame from the tables_info list is displayed in a separate section within a scrollable area. The dialog includes an “OK” button to close the window.

add_image_with_default_colormap(data, viewer, colormap='viridis', **kwargs)[source]#

Adds an image to a Napari viewer using a specified colormap, with an emphasis on ‘viridis’ for enhanced visual inspection. This function is tailored for use with the Napari visualization tool, facilitating the addition of image data with a specified colormap for improved visual distinction and analysis.

Parameters:
  • data (numpy.ndarray) – The image data to be displayed in the Napari viewer. Compatible with the types of data Napari can visualize.

  • viewer (napari.Viewer) – An instance of the Napari Viewer that supports the add_image method.

  • colormap (str, optional) – The name of the colormap to apply to the image data, defaulting to ‘viridis’ for its effective visual clarity.

  • **kwargs (dict) – Additional keyword arguments to be passed to the add_image method of the Napari viewer, allowing for customization such as opacity, blending mode, scale, and more.

Notes

The default colormap ‘viridis’ is chosen due to its effectiveness in making distinct features stand out visually. Other colormaps provided by Napari can also be specified for different visual effects.

refresh_viewer_with_new_data(viewer, active_layer, new_data=None)[source]#

Update the Napari viewer by removing an active label layer and replacing it with a new layer that contains updated data. This function is designed to refresh the display after data modifications and can be used with either the current data or newly provided data.

Parameters:
  • viewer (napari.Viewer) – The Napari viewer instance in which the layer is displayed.

  • active_layer (napari.layers.Layer) – The currently active label layer that needs to be refreshed. This layer will be replaced.

  • new_data (numpy.ndarray, optional) – The new data to be displayed in the layer. If None, the function uses a copy of the current data in active_layer. Defaults to None.

Raises:

ValueError – If the active layer type is not supported for refreshing the viewer.

Notes

The function preserves the name of the active layer, removes the old layer from the viewer, and adds a new layer with the same name but updated data. If new_data is provided, it replaces the current data; otherwise, a copy of the current data is used to refresh the viewer.