mantidimaging.gui.windows.spectrum_viewer.presenter module#

class mantidimaging.gui.windows.spectrum_viewer.presenter.ExportMode(*values)[source]#

Bases: Enum

IMAGE_MODE = 1#
ROI_MODE = 0#
class mantidimaging.gui.windows.spectrum_viewer.presenter.SpectrumFitResult(params: dict[str, float], rss: float, rss_per_dof: float, status: Literal['Fitted', 'Failed'])[source]#

Bases: NamedTuple

Result of fitting single spectrum.

property is_good_fit: bool#

True if the fit converged and produced valid chi2 residuals

params: dict[str, float]#

Alias for field number 0

rss: float#

Alias for field number 1

rss_per_dof: float#

Alias for field number 2

status: Literal['Fitted', 'Failed']#

Alias for field number 3

class mantidimaging.gui.windows.spectrum_viewer.presenter.SpectrumViewerWindowPresenter(view: SpectrumViewerWindowView, main_window: MainWindowView)[source]#

Bases: BasePresenter

The presenter for the spectrum viewer window.

This presenter is responsible for handling user interaction with the view and updating the model and view accordingly to look after the state of the window.

add_loaded_rois(loaded_rois_list: list[dict[str, str]]) None[source]#
add_rits_roi() None[source]#

Add the RITS ROI to the spectrum widget and initialize it with default dimensions.

auto_find_flat_stack(new_dataset_id: UUID) None[source]#
build_composite_image(full_map: ndarray, levels: tuple[float, float], opacity: float, sample_image: ndarray | None = None) ndarray[source]#

Colour (viridis) parameter map overlay over sample image.

@param full_map: Full-resolution float32 NaN canvas from build_full_sample_parameter_map @param levels: (lower, upper) colour limits used to normalise the overlay @param opacity: Overlay opacity between 0 and 1 @param sample_image: Background image to composite over. Pass None for map-only export. @return: colour array

change_roi_colour(roi_name: str, new_colour: tuple[int, int, int, int]) None[source]#

Change the colour of a given ROI in both the spectrum widget and the table.

@param roi_name: Name of the ROI to change color. @param new_colour: The new color for the ROI.

change_selected_menu_option(opt: str) None[source]#
changed_roi: SpectrumROI#
clear_spectrum() None[source]#
compute_spectrum_fit(spectrum: ndarray, fitting_region: FittingRegion, tof_data: ndarray, init_params: list[float], bound_params: list[tuple[float | None, float | None]]) SpectrumFitResult[source]#

Fit a single spectrum and return result, rss, rss_per_dof, and status.

Parameters#

spectrum: The spectrum data to fit. fitting_region: The region of the spectrum to fit, defined as a tuple of (min, max) in the current ToF units. tof_data: The ToF data corresponding to the spectrum, used for determining the x-axis values for fitting. init_params: A dictionary of initial parameter guesses for the fitting function, keyed by parameter name. bound_params: A dictionary of parameter bounds for the fitting function, keyed by parameter name, where each

value is a tuple of (min_bound, max_bound).

Returns#

SpectrumFitResult A named tuple containing: - params: A dictionary of fitted parameter values, keyed by parameter name. - rss: The residual sum of squares of the fit. - rss_per_dof: The residual sum of squares per degree of freedom of the fit. - status: A string indicating whether the fit was successful (“Fitted”) or failed (“Failed”).

current_norm_stack_uuid: UUID | None = None#
current_stack_uuid: UUID | None = None#
do_add_roi(roi_name: str | None = None, coords: list[int] | None = None, from_load: bool = False) None[source]#

Add a new ROI to the spectrum

do_add_roi_to_table(roi_name: str) None[source]#

Add a given ROI to the table by ROI name

@param roi_name: Name of the ROI to add

do_adjust_roi() None[source]#
do_remove_roi(roi_name: str | None = None) None[source]#

Remove a given ROI from the table by ROI name or all ROIs from the table if no name is passed as an argument

@param roi_name: Name of the ROI to remove

export_mode: ExportMode#
fit_all_regions(progress: Progress) list[tuple[str, SpectrumFitResult]][source]#

Retrieves ROIs from get_rois_to_fit, computes a fit for each, and reports progress via the provided Progress object while supporting user cancellation.

Parameters#

progress: Progress reporter used for updating the UI with the current progress and checking for cancellation.

Returns#

list[tuple[str, SpectrumFitResult]]: A list of (ROI name, fit result) tuples for each processed ROI. If the operation is cancelled, only completed results are returned.

property fitting_spectrum: ndarray#
get_dataset_id_for_stack(stack_id: UUID | None) UUID | None[source]#
get_roi_names() list[str][source]#

@return: list of ROI names

get_rois_to_fit() Iterator[tuple[str, SensibleROI]][source]#

Get the ROIs to fit based on the current export mode. In ROI mode, yields user-defined ROIs from the spectrum widget. In Image mode, yields sub-ROIs defined by the current binner.

Yields#

Iterator[tuple[str, SensibleROI]]: Tuples of (ROI name, ROI object) to be used for fitting.

handle_button_enabled() None[source]#

Enable the export button if the current stack is not None and normalisation is valid

handle_enable_normalised(enabled: bool) None[source]#
handle_experiment_setup_properties_change() None[source]#
handle_export_csv() None[source]#
handle_export_parameter_map() None[source]#

Select array shape based on export selection

handle_export_tab_change(index: int) None[source]#
handle_export_table() None[source]#

Export the ROI fitting results table to CSV.

handle_load_csv() None[source]#
handle_normalise_stack_change(normalise_uuid: UUID | None) None[source]#
handle_notify_roi_moved(roi: SpectrumROI) None[source]#
handle_open_beam_roi_choice_changed() None[source]#
handle_parameter_map_changed(param_name: str) None[source]#
handle_range_slide_moved(tof_range: tuple[float, float] | tuple[int, int]) None[source]#
handle_rits_export() None[source]#

Handle the export of the current spectrum to a RITS file format

handle_roi_clicked(roi: SpectrumROI) None[source]#
handle_roi_moved() None[source]#

Handle changes to any ROI position and size.

handle_roi_name_change(old_name: str, new_name: str) None[source]#
handle_sample_change(uuid: UUID | None) None[source]#

Called when the stack has been changed in the stack selector.

handle_stack_modified() None[source]#

Called when an image stack is modified somewhere else in MI, for example in the operations window

handle_tof_unit_change() None[source]#
handle_tof_unit_change_via_menu(unit_name: str) None[source]#
image_nan_mask_dict: dict[str, np.ma.MaskedArray] = {}#
initial_roi_calc() None[source]#
model: SpectrumViewerWindowModel#
on_fit_all_regions(task: TaskWorkerThread) None[source]#

Callback executed when the asynchronous fitting task completes. Handles errors, retrieves results, and updates the export table and parameter map

Parameters#

task : The completed task containing either results or an error.

on_map_display_settings_changed(value: int) None[source]#
redraw_all_rois() None[source]#

Redraw all ROIs and spectrum plots

redraw_spectrum(name: str) None[source]#

Redraw the spectrum with the given name

refresh_spectrum_plot() None[source]#
reset_units_menu() None[source]#
roi_to_process_queue: dict[str, SpectrumROI] = {}#
run_spectrum_calculation(roi: SpectrumROI, open_beam_roi: SensibleROI | None, spec_mode: SpecType, shutter_norm: bool, chunk_start: int, chunk_end: int) np.ndarray[source]#
set_shuttercount_error(enabled: bool = False) None[source]#

Set ShutterCount error message when a valid normalisation stack has been selected and shuttercount correction has been toggled on and redraw all ROIs to ensure the spectrum is updated when ShutterCount correction is toggled between enabled and disabled states.

setup_colour_range_dropdown() None[source]#

Populate colour range dropdown with available options

show_new_sample() None[source]#

Show the new sample in the view and update the spectrum and image view accordingly. Resets the ROIs.

spectrum_calculation_setup() tuple[int, int][source]#
spectrum_mode: SpecType = 1#
thread_cleanup(thread: TaskWorkerThread) None[source]#
thread_finished_processing(thread: TaskWorkerThread) None[source]#
try_next_mean_chunk() None[source]#
update_displayed_image(autoLevels: bool = True) None[source]#

Fetches the correct image (normalized or not) and updates the display

If normalisation is enabled, but no longer valud due to changes to image stacks, the normalisation error message will be updated and the unnormalised image will be shown until the issue is resolved.

Parameters:

autoLevels – Whether to automatically set the levels of the displayed image, or keep them fixed.

Returns:

None

update_parameter_map() None[source]#

Update the map based on the selected parameter in the export settings.

update_unit_labels_and_menus() None[source]#

Update all unit-related axis and range labels, as well as unit selection menus in both the main spectrum plot in the image tab and the fitting tab.

view: SpectrumViewerWindowView#