Commit 4e611ac1 authored by Martyn Gigg's avatar Martyn Gigg
Browse files

Tidy up includes and code modernization on PeaksWorkspace+tests

Refs #28696
parent f4f45f0b
......@@ -12,6 +12,7 @@
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAlgorithms/CheckWorkspacesMatch.h"
#include "MantidAlgorithms/CreatePeaksWorkspace.h"
......
......@@ -9,6 +9,7 @@
#include <cxxtest/TestSuite.h>
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAlgorithms/CloneWorkspace.h"
#include "MantidAlgorithms/CompareWorkspaces.h"
......
......@@ -12,6 +12,7 @@
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAlgorithms/CompareWorkspaces.h"
#include "MantidAlgorithms/CreatePeaksWorkspace.h"
......
......@@ -9,7 +9,9 @@
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/TableRow.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/VectorHelper.h"
......
......@@ -12,6 +12,7 @@
#include "MantidAPI/Sample.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/ArrayProperty.h"
......
......@@ -9,6 +9,8 @@
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
using Mantid::API::ITableWorkspace;
......
......@@ -8,10 +8,12 @@
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/TextAxis.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/Peak.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Crystal/PointGroupFactory.h"
......
......@@ -61,24 +61,18 @@ void SortPeaksWorkspace::exec() {
PeaksWorkspace_sptr inputWS = getProperty("InputWorkspace");
PeaksWorkspace_sptr outputWS = getProperty("OutputWorkspace");
try {
// Try to get the column. This will throw if the column does not exist.
inputWS->getColumn(columnToSortBy);
// Try to get the column. This will throw if the column does not exist.
inputWS->getColumn(columnToSortBy);
if (inputWS != outputWS) {
outputWS = inputWS->clone();
}
// Perform the sorting.
std::vector<std::pair<std::string, bool>> sortCriteria;
sortCriteria.emplace_back(
std::pair<std::string, bool>(columnToSortBy, sortAscending));
outputWS->sort(sortCriteria);
setProperty("OutputWorkspace", outputWS);
} catch (std::invalid_argument &) {
this->g_log.error("Specified ColumnToSortBy does not exist");
throw;
if (inputWS != outputWS) {
outputWS = inputWS->clone();
}
// Perform the sorting.
std::vector<PeaksWorkspace::ColumnAndDirection> sortCriteria;
sortCriteria.emplace_back(std::make_pair(columnToSortBy, sortAscending));
outputWS->sort(sortCriteria);
setProperty("OutputWorkspace", outputWS);
}
} // namespace Crystal
......
......@@ -20,7 +20,6 @@ using namespace Mantid::DataObjects;
using Mantid::Crystal::LoadIsawUB;
using Mantid::Crystal::SetCrystalLocation;
using Mantid::Crystal::ShowPeakHKLOffsets;
using Mantid::DataObjects::TableWorkspace;
using Mantid::Kernel::V3D;
using namespace Mantid::API;
......
......@@ -23,7 +23,6 @@ using Mantid::DataHandling::LoadNexusProcessed;
using namespace Mantid::DataObjects;
using Mantid::Crystal::LoadIsawUB;
using Mantid::Crystal::ShowPeakHKLOffsets;
using Mantid::DataObjects::TableWorkspace;
class ShowPeakHKLOffsetsTest : public CxxTest::TestSuite {
public:
......
......@@ -6,6 +6,7 @@
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidAPI/TableRow.h"
#include "MantidCrystal/SortPeaksWorkspace.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
......
......@@ -10,6 +10,7 @@
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/LatticeDomain.h"
#include "MantidAPI/TableRow.h"
#include "MantidCurveFitting/LatticeDomainCreator.h"
#include "MantidCurveFitting/LatticeFunction.h"
#include "MantidDataObjects/PeaksWorkspace.h"
......
......@@ -6,24 +6,14 @@
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidAPI/Column.h"
#include "MantidAPI/ExperimentInfo.h"
#include "MantidAPI/IPeaksWorkspace.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidDataObjects/DllConfig.h"
#include "MantidDataObjects/Peak.h"
#include "MantidDataObjects/PeakColumn.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/Matrix.h"
#include "MantidKernel/System.h"
#include "MantidGeometry/Crystal/IPeak.h"
#include "MantidKernel/SpecialCoordinateSystem.h"
#include "MantidKernel/V3D.h"
#include <boost/optional.hpp>
#include <string>
#include <utility>
#include <vector>
// IsamplePosition should be IsampleOrientation
namespace Mantid {
......@@ -43,12 +33,17 @@ namespace DataObjects {
@author Ruth Mikkelson, SNS ORNL
@date 3/10/2010
*/
class DLLExport PeaksWorkspace : public Mantid::API::IPeaksWorkspace {
class MANTID_DATAOBJECTS_DLL PeaksWorkspace
: public Mantid::API::IPeaksWorkspace {
public:
using ColumnAndDirection = std::pair<std::string, bool>;
public:
const std::string id() const override { return "PeaksWorkspace"; }
PeaksWorkspace();
PeaksWorkspace &operator=(const PeaksWorkspace &other) = delete;
/** Get access to shared pointer containing workspace porperties. This
function is there to provide common interface of iTableWorkspace
* Despite it is non-constant method, one should be very carefull using it to
......
......@@ -5,39 +5,21 @@
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/Column.h"
#include "MantidAPI/ColumnFactory.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/Peak.h"
#include "MantidDataObjects/TableColumn.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Instrument/Goniometer.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/IPropertyManager.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/Quat.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/UnitConversion.h"
#include "MantidKernel/V3D.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <memory>
// clang-format off
#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
// clang-format on
#include <ostream>
#include <string>
#include <cmath>
using namespace Mantid::API;
using namespace Mantid::Kernel;
......@@ -67,8 +49,8 @@ PeaksWorkspace::PeaksWorkspace()
/** Copy constructor
*
* @param other :: other PeaksWorkspace to copy from
* @return
*/
// PeaksWorkspace::PeaksWorkspace(const PeaksWorkspace &other) = default;
PeaksWorkspace::PeaksWorkspace(const PeaksWorkspace &other)
: IPeaksWorkspace(other), peaks(other.peaks), columns(), columnNames(),
m_coordSystem(other.m_coordSystem) {
......@@ -77,39 +59,38 @@ PeaksWorkspace::PeaksWorkspace(const PeaksWorkspace &other)
setNumberOfDetectorGroups(0);
}
//=====================================================================================
//=====================================================================================
/** Comparator class for sorting peaks by one or more criteria
*/
class PeakComparator {
public:
std::vector<std::pair<std::string, bool>> &criteria;
using ColumnAndDirection = PeaksWorkspace::ColumnAndDirection;
std::vector<ColumnAndDirection> &criteria;
/** Constructor for the comparator for sorting peaks
* @param criteria : a vector with a list of pairs: column name, bool;
* where bool = true for ascending, false for descending sort.
*/
explicit PeakComparator(std::vector<std::pair<std::string, bool>> &criteria)
explicit PeakComparator(std::vector<ColumnAndDirection> &criteria)
: criteria(criteria) {}
/** Compare two peaks using the stored criteria */
inline bool operator()(const Peak &a, const Peak &b) {
for (auto &name : criteria) {
std::string &col = name.first;
bool ascending = name.second;
for (const auto &name : criteria) {
const auto &col = name.first;
const bool ascending = name.second;
bool lessThan = false;
if (col == "BankName") {
// If this criterion is equal, move on to the next one
std::string valA = a.getBankName();
std::string valB = b.getBankName();
const std::string valA = a.getBankName();
const std::string valB = b.getBankName();
// Move on to lesser criterion if equal
if (valA == valB)
continue;
lessThan = (valA < valB);
} else {
// General double comparison
double valA = a.getValueByColName(col);
double valB = b.getValueByColName(col);
const double valA = a.getValueByColName(col);
const double valB = b.getValueByColName(col);
// Move on to lesser criterion if equal
if (valA == valB)
continue;
......@@ -134,7 +115,7 @@ public:
* The peaks are sorted by the first criterion first, then the 2nd if
*equal, etc.
*/
void PeaksWorkspace::sort(std::vector<std::pair<std::string, bool>> &criteria) {
void PeaksWorkspace::sort(std::vector<ColumnAndDirection> &criteria) {
PeakComparator comparator(criteria);
std::stable_sort(peaks.begin(), peaks.end(), comparator);
}
......@@ -667,8 +648,8 @@ void PeaksWorkspace::initColumns() {
**/
void PeaksWorkspace::addPeakColumn(const std::string &name) {
// Create the PeakColumn.
columns.emplace_back(std::shared_ptr<DataObjects::PeakColumn>(
new DataObjects::PeakColumn(this->peaks, name)));
columns.emplace_back(
std::make_shared<DataObjects::PeakColumn>(this->peaks, name));
// Cache the names
columnNames.emplace_back(name);
}
......
......@@ -17,8 +17,6 @@
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/SpecialCoordinateSystem.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include "MantidKernel/V3D.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/NexusTestHelper.h"
......@@ -26,7 +24,6 @@
#include <cmath>
#include <cxxtest/TestSuite.h>
#include <fstream>
#include <stdio.h>
#include <Poco/File.h>
......@@ -50,7 +47,7 @@ public:
Instrument_sptr inst =
ComponentCreationHelper::createTestInstrumentRectangular2(1, 10);
inst->setName("SillyInstrument");
auto pw = PeaksWorkspace_sptr(new PeaksWorkspace);
auto pw = std::make_shared<PeaksWorkspace>();
pw->setInstrument(inst);
std::string val = "value";
pw->mutableRun().addProperty("TestProp", val);
......@@ -81,12 +78,15 @@ public:
public:
TestablePeaksWorkspace(const PeaksWorkspace &other)
: PeaksWorkspace(other) {}
using ExperimentInfo::numberOfDetectorGroups;
};
void test_copyConstructor() {
auto pw = buildPW();
auto pw2 = PeaksWorkspace_sptr(new TestablePeaksWorkspace(*pw));
auto pw2 = std::make_shared<TestablePeaksWorkspace>(*pw);
checkPW(*pw2);
TS_ASSERT_EQUALS(0, pw2->numberOfDetectorGroups());
}
void test_clone() {
......@@ -97,7 +97,7 @@ public:
void test_sort() {
auto pw = buildPW();
Instrument_const_sptr inst = pw->getInstrument();
const auto inst = pw->getInstrument();
Peak p0 = Peak(pw->getPeak(0)); // Peak(inst, 1, 3.0)
Peak p1(inst, 1, 4.0);
Peak p2(inst, 1, 5.0);
......
......@@ -10,6 +10,7 @@
#include <cxxtest/TestSuite.h>
#include "MantidAPI/Axis.h"
#include "MantidAPI/TableRow.h"
#include "MantidAlgorithms/AddSampleLog.h"
#include "MantidAlgorithms/AddTimeSeriesLog.h"
#include "MantidAlgorithms/ConvertUnits.h"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment