Skip to content

Commit

Permalink
Add requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedAlmaki committed May 1, 2024
1 parent ad60a2c commit ffa6d39
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 40 deletions.
4 changes: 3 additions & 1 deletion Framework/API/inc/MantidAPI/WorkspaceGroup.h
Expand Up @@ -69,8 +69,10 @@ class MANTID_API_DLL WorkspaceGroup : public Workspace {
/// This method returns true if the group is empty (no member workspace)
bool isEmpty() const;
bool areNamesSimilar() const;
/// Inidicates that the workspace group can be treated as multiperiod.
/// Indicates that the workspace group can be treated as multiperiod.
bool isMultiperiod() const;
/// Check if the workspace group contains just peak workspaces
bool isGroupPeaksWorkspaces() const;
/// Check if a workspace is included in this group or any nested groups.
bool isInGroup(const Workspace &workspaceToCheck, size_t level = 0) const;
/// Prints the group to the screen using the logger at debug
Expand Down
13 changes: 11 additions & 2 deletions Framework/API/src/WorkspaceGroup.cpp
Expand Up @@ -6,6 +6,7 @@
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/IPeaksWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Run.h"
#include "MantidKernel/IPropertyManager.h"
Expand Down Expand Up @@ -455,6 +456,14 @@ bool WorkspaceGroup::isMultiperiod() const {
return true;
}

/**
* @return :: True if all of the workspaces in the group are peak workspaces
*/
bool WorkspaceGroup::isGroupPeaksWorkspaces() const {
return std::all_of(m_workspaces.begin(), m_workspaces.end(),
[](auto ws) { return dynamic_cast<IPeaksWorkspace *>(ws.get()) != nullptr; });
}

/**
* @param workspaceToCheck :: A workspace to check.
* @param level :: The current nesting level. Intended for internal use only
Expand All @@ -471,7 +480,7 @@ bool WorkspaceGroup::isInGroup(const Workspace &workspaceToCheck, size_t level)
for (const auto &workspace : m_workspaces) {
if (workspace.get() == &workspaceToCheck)
return true;
auto *group = dynamic_cast<WorkspaceGroup *>(workspace.get());
const auto *group = dynamic_cast<WorkspaceGroup *>(workspace.get());
if (group) {
if (group->isInGroup(workspaceToCheck, level + 1))
return true;
Expand Down Expand Up @@ -503,7 +512,7 @@ namespace Mantid::Kernel {
template <>
MANTID_API_DLL Mantid::API::WorkspaceGroup_sptr
IPropertyManager::getValue<Mantid::API::WorkspaceGroup_sptr>(const std::string &name) const {
auto *prop = dynamic_cast<PropertyWithValue<Mantid::API::WorkspaceGroup_sptr> *>(getPointerToProperty(name));
const auto *prop = dynamic_cast<PropertyWithValue<Mantid::API::WorkspaceGroup_sptr> *>(getPointerToProperty(name));
if (prop) {
return *prop;
} else {
Expand Down
Expand Up @@ -74,17 +74,13 @@ list retrieveGroupPeaksWorkspaces(AnalysisDataServiceImpl const *const self, con

auto wsSharedPtrs = self->retrieveWorkspaces(Converters::PySequenceToVector<std::string>(names)(), false);

auto isGroupPeakWorkspace = [](const Workspace_sptr &wksp) {
auto isNotGroupPeakWorkspace = [](const Workspace_sptr &wksp) {
if (auto gws = dynamic_cast<WorkspaceGroup *>(wksp.get())) {
for (auto it = gws->begin(); it != gws->end(); it++) {
if (auto ws = dynamic_cast<IPeaksWorkspace *>(it->get())) {
return false;
}
}
return !gws->isGroupPeaksWorkspaces();
}
return true;
return false;
};
auto end = std::remove_if(wsSharedPtrs.begin(), wsSharedPtrs.end(), isGroupPeakWorkspace);
auto end = std::remove_if(wsSharedPtrs.begin(), wsSharedPtrs.end(), isNotGroupPeakWorkspace);
wsSharedPtrs.erase(end, wsSharedPtrs.end());

std::vector<WeakPtr> wsWeakPtrs;
Expand Down
10 changes: 4 additions & 6 deletions buildconfig/CMake/CppCheck_Suppressions.txt.in
Expand Up @@ -399,12 +399,10 @@ constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/SpectraAxisValidator.
constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/ConnectedComponentLabeling.cpp:337
constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/ConnectedComponentLabeling.cpp:338
constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/CalculateUMatrix.cpp:65
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:78
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:437
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:474
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:519
useStlAlgorithm:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:125
useStlAlgorithm:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:406
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:79
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:438
useStlAlgorithm:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:126
useStlAlgorithm:${CMAKE_SOURCE_DIR}/Framework/API/src/WorkspaceGroup.cpp:407
constVariableReference:${CMAKE_SOURCE_DIR}/Framework/API/src/MultiPeriodGroupWorker.cpp:91
constParameterPointer:${CMAKE_SOURCE_DIR}/Framework/API/src/MultiPeriodGroupWorker.cpp:151
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/MultiPeriodGroupWorker.cpp:191
Expand Down
Expand Up @@ -39,13 +39,18 @@ def __init__(self, ws: WorkspaceGroup):
self.supports(ws)

self.ws: WorkspaceGroup = ws
self.group_name = self.ws.name()
self.child_names = self.ws.getNames()
self.ws_num_rows = sum(peakWs.rowCount() for peakWs in ws)
self.ws_num_cols = self.ws[0].columnCount() + 2
self.ws_num_cols = 0
if self.ws.size() != 0:
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()
if self.ws_num_cols:
self._load_col_types()

def _make_row_mapping(self):
row_index = 0
Expand Down Expand Up @@ -76,10 +81,15 @@ def build_current_labels(self):
return self.marked_columns.build_labels()

def get_name(self):
return self.ws.name()
return self.group_name

def get_child_names(self):
return self.child_names

def get_column_headers(self):
return ["Group Index", "WS Index"] + self.ws[0].getColumnNames()
if self.ws.size() != 0:
return ["WS Index", "Group Index"] + self.ws[0].getColumnNames()
return []

def get_column(self, index):
column_data = []
Expand Down Expand Up @@ -116,25 +126,26 @@ def get_cell(self, row, column):
group_index, ws_index = self._row_mapping[row]

if column == 0:
return group_index
return ws_index

if column == 1:
return ws_index
return group_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":
col_name = self.get_column_header(col)

p = self.ws[group_index].getPeak(ws_index)

if col_name == "h":
p.setH(data)
elif self.ws.getColumnNames()[col] == "k":
elif col_name == "k":
p.setK(data)
elif self.ws.getColumnNames()[col] == "l":
elif col_name == "l":
p.setL(data)

def delete_rows(self, selected_rows):
Expand All @@ -152,3 +163,10 @@ def get_statistics(self, selected_columns):

stats = StatisticsOfTableWorkspace(self.ws, selected_columns)
return stats

def sort(self, column_index, sort_ascending):
from mantid.simpleapi import SortPeaksWorkspace

column_name = self.get_column_header(column_index)

SortPeaksWorkspace(InputWorkspace=self.ws, OutputWorkspace=self.ws, ColumnNameToSortBy=column_name, SortAscending=sort_ascending)
Expand Up @@ -21,8 +21,8 @@ def __init__(self, data_model, parent=None):
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)))
ws_index = int(model.data(model.index(index.row(), 0)))
group_index = int(model.data(model.index(index.row(), 1)))
row = (group_index, ws_index)

try:
Expand Down
Expand Up @@ -22,8 +22,18 @@ def __init__(self, *args, **kwargs):
self.proxy_model.setSourceModel(self.table_model)
self.setModel(self.proxy_model)

# Always hide the ws index column, this column is just used for indexing in different models operations
super().hideColumn(0)

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)

def showColumn(self, column_index):
# Never show the ws index column, this column is just used for indexing in different models operations
if column_index == 0:
return

super().showColumn(column_index)
Expand Up @@ -8,6 +8,7 @@
from functools import partial
from qtpy.QtCore import Qt

from mantid.api import mtd
from mantid.kernel import logger
from mantid.plots.utility import legend_set_draggable
from mantidqt.widgets.observers.ads_observer import WorkspaceDisplayADSObserver
Expand Down Expand Up @@ -160,15 +161,15 @@ def supports(cls, ws):
raise ValueError("The workspace type is not supported: {0}".format(ws))

def replace_workspace(self, workspace_name, workspace):
if self.presenter.model.workspace_equals(workspace_name) and not self.presenter.model.block_model_replace:
if not self.group and self.presenter.model.workspace_equals(workspace_name) and not self.presenter.model.block_model_replace:
self.presenter.view.blockSignals(True)
self.presenter.model = TableWorkspaceDisplayModel(workspace)
self.presenter.load_data(self.presenter.view)
self.presenter.view.blockSignals(False)

if self.group and workspace_name in self.model.ws.getNames() and not self.presenter.model.block_model_replace:
if self.group and workspace_name in self.model.get_child_names() and not self.presenter.model.block_model_replace:
self.presenter.view.blockSignals(True)
self.presenter.model = GroupTableWorkspaceDisplayModel(self.model.ws)
self.presenter.model = GroupTableWorkspaceDisplayModel(mtd[self.model.get_name()])
self.presenter.load_data(self.presenter.view)
self.presenter.view.blockSignals(False)

Expand Down Expand Up @@ -302,9 +303,9 @@ def action_sort(self, sort_ascending):
except ValueError:
return

if not self.group:
self.presenter.model.sort(selected_column, sort_ascending)
else:
self.presenter.model.sort(selected_column, sort_ascending)

if self.group:
self.presenter.sort(selected_column, sort_ascending)

def action_plot(self, plot_type):
Expand Down
Expand Up @@ -32,8 +32,8 @@ def delete_rows(self, selected_rows):
group_ws_rows = []
model = self.view.model()
for row in selected_rows:
group_index = int(model.data(model.index(row, 0)))
ws_index = int(model.data(model.index(row, 1)))
ws_index = int(model.data(model.index(row, 0)))
group_index = int(model.data(model.index(row, 1)))
group_ws_rows.append((group_index, ws_index))
self.model.delete_rows(group_ws_rows)

Expand Down
2 changes: 1 addition & 1 deletion qt/widgets/common/src/MantidTreeWidget.cpp
Expand Up @@ -127,7 +127,7 @@ void MantidTreeWidget::mouseDoubleClickEvent(QMouseEvent *e) {
auto wsName = wsNames.front();
Mantid::API::WorkspaceGroup_sptr grpWSPstr;
grpWSPstr = std::dynamic_pointer_cast<WorkspaceGroup>(m_ads.retrieve(wsName.toStdString()));
if (!grpWSPstr) {
if (!grpWSPstr || grpWSPstr->isGroupPeaksWorkspaces()) {
if (!wsName.isEmpty()) {
m_doubleClickAction(wsName);
return;
Expand Down

0 comments on commit ffa6d39

Please sign in to comment.