New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add table view for group peak workspace #37166
Open
MohamedAlmaki
wants to merge
7
commits into
main
Choose a base branch
from
36592_add_group_peak_ws
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bc8ef6
Add group peak workspaces model
MohamedAlmaki 3c1fae4
Add show data to group peak workspaces
MohamedAlmaki a4dbf4b
Add group presenter and table model
MohamedAlmaki cc1d261
Add support for sorting, deleting, plotting, stats, and batch loading
MohamedAlmaki ad60a2c
Fix presenter view test
MohamedAlmaki 86ba65d
Add requested changes
MohamedAlmaki a13f61c
Fix error and hide ws index column
MohamedAlmaki File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
qt/python/mantidqt/mantidqt/widgets/workspacedisplay/table/group_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# Mantid Repository : https://github.com/mantidproject/mantid | ||
# | ||
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI, | ||
# NScD Oak Ridge National Laboratory, European Spallation Source, | ||
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS | ||
# SPDX - License - Identifier: GPL - 3.0 + | ||
# coding=utf-8 | ||
# This file is part of the mantidqt package. | ||
from mantid.api import WorkspaceGroup | ||
from mantidqt.widgets.workspacedisplay.table.error_column import ErrorColumn | ||
from mantidqt.widgets.workspacedisplay.table.marked_columns import MarkedColumns | ||
from mantidqt.widgets.workspacedisplay.table.model import TableWorkspaceColumnTypeMapping | ||
from collections import defaultdict | ||
|
||
|
||
class GroupTableWorkspaceDisplayModel: | ||
SPECTRUM_PLOT_LEGEND_STRING = "{}-{}" | ||
BIN_PLOT_LEGEND_STRING = "{}-bin-{}" | ||
EDITABLE_COLUMN_NAMES = ["h", "k", "l"] | ||
|
||
ALLOWED_WORKSPACE_TYPES = [WorkspaceGroup] | ||
|
||
@classmethod | ||
def supports(cls, ws: WorkspaceGroup): | ||
""" | ||
Checks that the provided workspace is supported by this display. | ||
:param ws: Workspace to be checked for support | ||
:raises ValueError: if the workspace is not supported | ||
""" | ||
if not any(isinstance(ws, allowed_type) for allowed_type in cls.ALLOWED_WORKSPACE_TYPES): | ||
raise ValueError("The workspace type is not supported: {0}".format(ws)) | ||
|
||
def __init__(self, ws: WorkspaceGroup): | ||
""" | ||
Initialise the model with the workspace | ||
:param ws: Workspace to be used for providing data | ||
:raises ValueError: if the workspace is not supported | ||
""" | ||
self.supports(ws) | ||
|
||
self.ws: WorkspaceGroup = ws | ||
self.ws_num_rows = sum(peakWs.rowCount() for peakWs in ws) | ||
self.ws_num_cols = self.ws[0].columnCount() + 2 | ||
self.marked_columns = MarkedColumns() | ||
self._original_column_headers = self.get_column_headers() | ||
self.block_model_replace = False | ||
self._row_mapping = self._make_row_mapping() | ||
self._load_col_types() | ||
|
||
def _make_row_mapping(self): | ||
row_index = 0 | ||
row_mapping = [] | ||
for group_index, peaksWs in enumerate(self.ws): | ||
group_start = row_index | ||
group_end = row_index + len(peaksWs) | ||
row_mapping.extend([(group_index, index - group_start) for index in range(group_start, group_end)]) | ||
row_index += len(peaksWs) | ||
return row_mapping | ||
|
||
def _load_col_types(self): | ||
for col in range(2, len(self._original_column_headers)): | ||
plot_type = self.ws[0].getPlotType(col - 2) | ||
if plot_type == TableWorkspaceColumnTypeMapping.X: | ||
self.marked_columns.add_x(col) | ||
elif plot_type == TableWorkspaceColumnTypeMapping.Y: | ||
self.marked_columns.add_y(col) | ||
elif plot_type == TableWorkspaceColumnTypeMapping.YERR: | ||
err_for_column = self.ws[0].getLinkedYCol(col - 2) | ||
if err_for_column >= 0: | ||
self.marked_columns.add_y_err(ErrorColumn(col, err_for_column + 2)) | ||
|
||
def original_column_headers(self): | ||
return self._original_column_headers[:] | ||
|
||
def build_current_labels(self): | ||
return self.marked_columns.build_labels() | ||
|
||
def get_name(self): | ||
return self.ws.name() | ||
|
||
def get_column_headers(self): | ||
return ["Group Index", "WS Index"] + self.ws[0].getColumnNames() | ||
|
||
def get_column(self, index): | ||
column_data = [] | ||
|
||
for i, ws_item in enumerate(self.ws): | ||
if index == 0: | ||
column_data.extend([i] * ws_item.rowCount()) | ||
elif index == 1: | ||
column_data.extend([i for i in range(ws_item.rowCount())]) | ||
else: | ||
column_data.extend(ws_item.column(index - 2)) | ||
|
||
return column_data | ||
|
||
def get_number_of_rows(self): | ||
return self.ws_num_rows | ||
|
||
def get_number_of_columns(self): | ||
return self.ws_num_cols | ||
|
||
def get_column_header(self, index): | ||
return self.get_column_headers()[index] | ||
|
||
def is_editable_column(self, icol): | ||
return self.get_column_headers()[icol] in self.EDITABLE_COLUMN_NAMES | ||
|
||
def set_column_type(self, col, type, linked_col_index=-1): | ||
self.ws[0].setPlotType(col - 2, type, linked_col_index - 2 if linked_col_index != -1 else linked_col_index) | ||
|
||
def workspace_equals(self, workspace_name): | ||
return self.ws.name() == workspace_name | ||
|
||
def get_cell(self, row, column): | ||
group_index, ws_index = self._row_mapping[row] | ||
|
||
if column == 0: | ||
return group_index | ||
|
||
if column == 1: | ||
return ws_index | ||
|
||
column = column - 2 | ||
|
||
return self.ws[group_index].cell(ws_index, column) | ||
|
||
def set_cell_data(self, row, col, data, is_v3d): | ||
col = col - 2 | ||
|
||
group_index, ws_index = row | ||
p = self.ws[group_index][ws_index] | ||
if self.ws.getColumnNames()[col] == "h": | ||
p.setH(data) | ||
elif self.ws.getColumnNames()[col] == "k": | ||
p.setK(data) | ||
elif self.ws.getColumnNames()[col] == "l": | ||
p.setL(data) | ||
|
||
def delete_rows(self, selected_rows): | ||
from mantid.simpleapi import DeleteTableRows | ||
|
||
row_to_ws_index = defaultdict(list) | ||
for group_index, ws_index in selected_rows: | ||
row_to_ws_index[group_index].append(ws_index) | ||
|
||
for group_index in row_to_ws_index: | ||
DeleteTableRows(self.ws[group_index], ",".join(map(str, row_to_ws_index[group_index]))) | ||
|
||
def get_statistics(self, selected_columns): | ||
from mantid.simpleapi import StatisticsOfTableWorkspace | ||
|
||
stats = StatisticsOfTableWorkspace(self.ws, selected_columns) | ||
return stats |
49 changes: 49 additions & 0 deletions
49
qt/python/mantidqt/mantidqt/widgets/workspacedisplay/table/group_table_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Mantid Repository : https://github.com/mantidproject/mantid | ||
# | ||
# Copyright © 2024 ISIS Rutherford Appleton Laboratory UKRI, | ||
# NScD Oak Ridge National Laboratory, European Spallation Source, | ||
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS | ||
# SPDX - License - Identifier: GPL - 3.0 + | ||
# This file is part of the mantidqt package. | ||
|
||
from qtpy.QtCore import Qt | ||
|
||
from mantidqt.widgets.workspacedisplay.table.table_model import TableModel | ||
|
||
from mantid.kernel import V3D | ||
from numpy import ndarray | ||
|
||
|
||
class GroupTableModel(TableModel): | ||
def __init__(self, data_model, parent=None): | ||
super().__init__(data_model, parent=parent) | ||
|
||
def setData(self, index, value, role): | ||
if index.isValid() and role == Qt.EditRole: | ||
model = index.model() | ||
group_index = int(model.data(model.index(index.row(), 0))) | ||
ws_index = int(model.data(model.index(index.row(), 1))) | ||
row = (group_index, ws_index) | ||
|
||
try: | ||
self._data_model.set_cell_data(row, index.column(), value, False) | ||
except ValueError: | ||
print(self.ITEM_CHANGED_INVALID_DATA_MESSAGE) | ||
return False | ||
except Exception as x: | ||
print(self.ITEM_CHANGED_UNKNOWN_ERROR_MESSAGE.format(x)) | ||
return False | ||
self.dataChanged.emit(index, index) | ||
return True | ||
else: | ||
return False | ||
|
||
def data(self, index, role=Qt.DisplayRole): | ||
if not index.isValid(): | ||
return None | ||
if index.row() >= self.max_rows() or index.row() < 0: | ||
return None | ||
if role in (Qt.DisplayRole, Qt.EditRole): | ||
data = self._data_model.get_cell(index.row(), index.column()) | ||
return str(data) if isinstance(data, (V3D, ndarray)) else data | ||
return None |
29 changes: 29 additions & 0 deletions
29
qt/python/mantidqt/mantidqt/widgets/workspacedisplay/table/group_view.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Mantid Repository : https://github.com/mantidproject/mantid | ||
# | ||
# Copyright © 2024 ISIS Rutherford Appleton Laboratory UKRI, | ||
# NScD Oak Ridge National Laboratory, European Spallation Source, | ||
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS | ||
# SPDX - License - Identifier: GPL - 3.0 + | ||
# This file is part of the mantidqt package. | ||
|
||
from qtpy.QtCore import Qt, QSortFilterProxyModel | ||
from mantidqt.widgets.workspacedisplay.table.view import TableWorkspaceDisplayView | ||
|
||
|
||
class GroupTableWorkspaceDisplayView(TableWorkspaceDisplayView): | ||
"""Specialization of a table view to display peaks | ||
Designed specifically to be used by PeaksViewerView | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
TableWorkspaceDisplayView.__init__(self, *args, **kwargs) | ||
self.source_model = TableWorkspaceDisplayView.model(self) | ||
self.proxy_model = QSortFilterProxyModel() | ||
self.proxy_model.setSourceModel(self.table_model) | ||
self.setModel(self.proxy_model) | ||
|
||
def model(self): | ||
return self.source_model | ||
|
||
def sortBySelectedColumn(self, selected_column, sort_ascending): | ||
self.sortByColumn(selected_column, Qt.AscendingOrder if sort_ascending else Qt.DescendingOrder) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a lot of similar code here and in
TableWorkspaceDisplayModel
- is there any way this could inherit or is that not possible? if so it could remove quite a lot of the boiler plate getters etc.