NeuroRuler.GUI package

Submodules

NeuroRuler.GUI.helpers module

GUI helper functions.

class NeuroRuler.GUI.helpers.ErrorMessageBox(message: str)[source]

Bases: QMessageBox

__init__(message: str)[source]
Parameters:

message (str) – Error message

class NeuroRuler.GUI.helpers.InformationDialog(title: str, message: str)[source]

Bases: QDialog

__init__(title: str, message: str)[source]
Parameters:
  • title (str) – Title of window

  • message (str) – Informational message

NeuroRuler.GUI.helpers.InformationMessageBox(title: str, message: str)[source]
NeuroRuler.GUI.helpers.WINDOW_TITLE_PADDING: int = 12

Used in InformationDialog to add width to the dialog to prevent the window title from being truncated.

NeuroRuler.GUI.helpers.mask_QImage(q_img: QImage, binary_mask: ndarray, color: QColor) None[source]

Given 2D q_img and 2D binary_mask of the same shape, apply binary_mask on q_img to change q_img pixels corresponding to binary_mask``=1 to ``color. Mutates q_img.

QImage and numpy use [reversed w,h order](https://stackoverflow.com/a/68220805/18479243).

This function checks that q_img.size().width() == binary_mask.shape[0] and q_img.size().height() == binary_mask.shape[1].

Parameters:
  • q_img (QImage) –

  • binary_mask (np.ndarray) – 0|1 elements

  • color (QColor) –

Raise:

exceptions.ArraysDifferentShape if the arrays are of different shape

Returns:

None

Return type:

None

NeuroRuler.GUI.helpers.sitk_slice_to_qimage(sitk_slice: Image) QImage[source]

Convert a 2D sitk.Image slice to a QImage.

This function calls sitk.GetArrayFromImage, which returns the transpose. It also calls qimage2ndarray.array2qimage with normalize=True, normalizing the pixels to 0..255.

Parameters:

sitk_slice (sitk.Image) – 2D slice

Returns:

0..255 normalized QImage

Return type:

QImage

NeuroRuler.GUI.helpers.string_to_QColor(name_or_hex: str) QColor[source]

Convert a name (e.g. red) or 6-hexit rrggbb string to a QColor.

Parameters:

name_or_hex (str) – name of color (e.g. blue) or rrggbb (hexits)

Returns:

QColor

Return type:

QColor

Raise:

exceptions.InvalidColor if name_or_hex not in specified formats

NeuroRuler.GUI.main module

Defines MainWindow and main(), the entrypoint of the GUI.

Loads NeuroRuler/GUI/mainwindow.ui, made in QtDesigner.

Loads .qss stylesheets and resources.py (icons) files, generated by BreezeStyleSheets. Our fork of the repo: https://github.com/NIRALUser/BreezeStyleSheets.

If adding a new GUI element (in the GUI or in the menubar, whatever), you’ll have to modify modify __init__ and settings_view_toggle.

Edge cases: If this element should be disabled after enable_elements or enabled after disable_elements, then you will need to modify those.

class NeuroRuler.GUI.main.MainWindow[source]

Bases: QMainWindow

Main window of the application.

Settings mode and circumference mode.

__init__()[source]

Load main file and connect GUI events to methods/functions.

Sets window title and icon.

browse_files(extend: bool, path=None) None[source]

Called after File > Open or File > Add Images.

If extend, then IMAGE_DICT will be updated with new images.

Else, IMAGE_DICT will be cleared and (re)initialized (e.g. when choosing files for the first time or re-opening).

Opens file menu.

Renders various elements depending on the value of extend.

If called in circumference mode, then will toggle to settings mode.

Parameters:
  • extend (bool) – Whether to clear IMAGE_DICT and (re)initialize or add images to it. Determines which GUI elements are rendered.

  • path – Used for unit testing, when only one path is imported. Normally, the path(s) are selected by user in a QFileDialog.

Returns:

None

disable_binary_threshold_inputs() None[source]

Called when Otsu filter button is clicked.

Disable binary threshold input boxes.

Returns:

None

disable_elements() None[source]

Called when the list is now empty, i.e. just removed from list of length 1.

Explicitly enables elements that should never be disabled and sets default text.

Returns:

None

enable_binary_threshold_inputs() None[source]

Called when Binary filter button is clicked.

Restore binary input box.

Returns:

None

enable_elements() None[source]

Called after File > Open.

Enables GUI elements. Explicitly disables some (e.g., Export CSV menu item and binary threshold inputs, since Otsu is default).

Returns:

None

export_curr_slice_as_img(extension: str) None[source]

Called when an Export as image menu item is clicked.

Exports self.image to constants.OUTPUT_DIR/image_stem/. Thus, calling this when SETTINGS_VIEW_ENABLED will save a non-contoured image. Calling this when not SETTINGS_VIEW_ENABLED will save a contoured image.

Filename has format <file_name>[_contoured].<extension>

_contoured will be in the name if not SETTINGS_VIEW_ENABLED.

Supported formats in this function are the ones supported by QPixmap, namely BMP, JPG, JPEG, PNG, PPM, XBM, XPM.

Parameters:
  • extension (str) – BMP, JPG, JPEG, PNG, PPM, XBM, XPM

  • path (Path) –

Returns:

None

export_json() None[source]

Called when “export” button is clicked and when Menu > Export > JSON is clicked.

Exported parameters include: input_image_path, output_contoured_slice_path, x_rotation, y_rotation, z_rotation, slice, smoothing_conductance, smoothing_iterations, smoothing_time_step, threshold_filter, upper_binary_threshold, lower_binary_threshold, and circumference

Returns:

None

import_json() None[source]

Called when “import” button is clicked

Imported parameters include input_image_path, output_contoured_slice_path, x_rotation, y_rotation, z_rotation, slice, smoothing_conductance, smoothing_iterations, smoothing_time_step, threshold_filter, upper_binary_threshold, lower_binary_threshold, and circumference

input_image_path is the only mandatory field.

Returns:

None

next_img() None[source]

Called when Next button is clicked.

Advance index and render.

Returns:

None

orient_curr_image() None[source]

Orient the current image for the current view (global_vars.VIEW) by applying ORIENT_FILTER on it.

This mutates the image.

Returns:

None

previous_img() None[source]

Called when Previous button is clicked.

Decrement index and render.

Returns:

None

remove_curr_img() None[source]

Called after File > Remove File.

Removes current image from IMAGE_DICT. Since IMAGE_DICT is a reference to an image dict in IMAGE_GROUPS, it’s removed from IMAGE_GROUPS as well.

Returns:

None

render_all_sliders() None[source]

Sets all slider values to the global rotation and slice values. Also updates maximum value of slice slider.

Called on reset. Will need to be called when updating batch index, if we implement this.

Not called when the user updates a slider.

Also updates rotation and slice num labels.

Returns:

None

render_circumference(binary_contour_slice: ndarray) float[source]

Called after pressing Apply or when (not SETTINGS_VIEW_ENABLED and (pressing Next or Previous or Remove Image))

Computes circumference from binary_contour_slice and renders circumference label.

binary_contour_slice is always the return value of render_curr_slice since render_curr_slice must have already been called. If calling this function, render_curr_slice must have been called first.

Parameters:

binary_contour_slice (np.ndarray) – Result of previously calling render_curr_slice when not SETTINGS_VIEW_ENABLED

Returns:

circumference

Return type:

float

render_curr_slice() ndarray | None[source]

Resamples the currently selected image using its rotation and slice settings, then renders the resulting slice (scaled to the size of self.image) in the GUI.

DOES NOT set text for image_num_label and file path labels.

If not SETTINGS_VIEW_ENABLED, also calls imgproc.contour() and outlines the contour of the QImage (mutating it).

Additionally, also returns a view of the binary contoured slice if not SETTINGS_VIEW_ENABLED. This saves work when computing circumference.

Returns:

np.ndarray if not SETTINGS_VIEW_ENABLED else None

Return type:

np.ndarray or None

render_image_num_and_path() None[source]

Set image_num_label, image_path_label, and status tip of the image.

Called when pressing Next or Previous (next_img, prev_img), and after File > Open (browse_files).

Also called when removing an image.

Returns:

None

render_scaled_qpixmap_from_qimage(q_img: QImage) None[source]

Convert q_img to QPixmap and set self.image’s pixmap to that pixmap scaled to self.image’s size.

Sets UNSCALED_PIXMAP to the unscaled pixmap generated from the q_img.

Parameters:

q_img (QImage) –

Returns:

None

render_smooth_slice() None[source]

Renders smooth slice in GUI. Allows user to preview result of smoothing settings.

Returns:

None

render_threshold() None[source]

Render filtered image slice on UI.

Returns:

None

reset_settings() None[source]

Called when Reset is clicked.

Resets rotation values to 0 and slice num to the default int((z-1)/2) for the current image, then renders current image and sliders.

Returns:

None

resizeEvent(event: QResizeEvent) None[source]

This method is called every time the window is resized. Overrides PyQt6’s resizeEvent.

Sets pixmap to UNSCALED_QPIXMAP scaled to self.image’s size.

Parameters:

event (QResizeEvent) –

Returns:

None

rotate_x() None[source]

Called when the user updates the x slider.

Render image and set x_rotation_label.

Returns:

None

rotate_y() None[source]

Called when the user updates the y slider.

Render image and set y_rotation_label.

Returns:

None

rotate_z() None[source]

Called when the user updates the z slider.

Render image and set z_rotation_label.

Returns:

None

set_view_z() None[source]

Set global_vars.VIEW to View.Z and set the z radio button to checked.

Returns:

None

settings_export_view_toggle() None[source]

Called when clicking Apply (in settings mode) or Adjust (in circumference mode).

Toggle SETTINGS_VIEW_ENABLED, change apply button text, render stuff depending on the current mode.

Enables/disables GUI elements depending on the value of SETTINGS_VIEW_ENABLED.

Returns:

None

slice_update() None[source]

Called when the user updates the slice slider.

Render image and set slice_num_label.

Returns:

None

test_stuff() None[source]

Connected to Debug > Test stuff. Dummy button and function for easily testing stuff.

Assume that anything you put here will be overwritten freely.

Returns:

None

toggle_setting_to_false() None[source]

Used in testing.

Flipping the SETTINGS_VIEW_ENABLED

Returns:

None

toggle_setting_to_true() None[source]

Used in testing.

Flipping the SETTINGS_VIEW_ENABLED

Returns:

None

update_binary_filter_settings(set_global_vars_to_GUI_text: bool) None[source]

Updates binary threshold filter text in the GUI and set BINARY_THRESHOLD_FILTER parameters.

Parameters:

set_global_vars_to_GUI_text (bool) – If True, will first try to modify global_vars variables to the text in the GUI before updating GUI text and filter parameters. If False, will not do so.

Returns:

None

update_smoothing_settings(set_global_vars_to_GUI_text: bool) None[source]

Update smoothing text in the GUI and set SMOOTHING_FILTER parameters.

Parameters:

set_global_vars_to_GUI_text (bool) – If True, will first try to modify global_vars variables to the text in the GUI before updating GUI text and filter parameters. If False, will not do so.

Returns:

None

update_view() None[source]

Called when clicking on any of the three view radio buttons.

Sets global_vars.VIEW to the correct value. Then orients the current image and renders.

Returns:

None

NeuroRuler.GUI.main.SETTINGS_VIEW_ENABLED: bool = True

Whether the user is able to adjust settings (settings screen) or not (circumference and contoured image screen).

NeuroRuler.GUI.main.UNSCALED_QPIXMAP: QPixmap

Unscaled QPixmap from which the scaled version is rendered in the GUI.

When any slice (rotated, smoothed, previewed) is rendered from an unscaled QImage, this variable is set to the QPixmap generated from that unscaled QImage.

This variable will not change on resizeEvent. resizeEvent will scale this. Otherwise, if scaling self.image’s pixmap (which is already scaled), there would be loss of detail.

NeuroRuler.GUI.main.display_dimensions() None[source]

Display current image’s dimensions in window or terminal.

Returns:

None

NeuroRuler.GUI.main.display_direction() None[source]

Display current image’s direction in window or terminal.

Returns:

None

NeuroRuler.GUI.main.display_metadata() None[source]

Display metadata in window or terminal. Internally, uses sitk.GetMetaData, which doesn’t return all metadata (e.g., doesn’t return spacing values whereas sitk.GetSpacing does).

Typically, this returns less metadata for NRRD than for NIfTI.

Returns:

None

NeuroRuler.GUI.main.display_properties() None[source]

Display properties in window or terminal.

Internally, the properties tuple is a tuple of values only and doesn’t contain field names. This function creates a dictionary with field names for printing. But the dictionary doesn’t exist in the program.

Returns:

None

NeuroRuler.GUI.main.display_spacing() None[source]

Display current image’s spacing in window or terminal.

Returns:

None

NeuroRuler.GUI.main.error_message_box(message: str) None[source]

Creates a message box with an error message and red warning icon.

Parameters:

message (str) – the error message to be displayed

Returns:

None

NeuroRuler.GUI.main.information_dialog(title: str, message: str) None[source]

Create an informational dialog QDialog window with title and message.

Parameters:
  • title (str) –

  • message (str) –

Returns:

None

NeuroRuler.GUI.main.main() None[source]

Main entrypoint of GUI.

Module contents

After installing NeuroRuler via pip, the functions here are importable like so

from NeuroRuler import {function}

where NeuroRuler is the name of the package this __init__.py file is in.

NeuroRuler.GUI.gui() None[source]

Run GUI.

Will create gui_config.json using package’s gui_config.json if it doesn’t already exist.