Mantid
Loading...
Searching...
No Matches
IndexProperty.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include <utility>
8
11#include "MantidIndexing/GlobalSpectrumIndex.h"
12#include "MantidIndexing/IndexInfo.h"
13#include "MantidIndexing/SpectrumNumber.h"
15
16namespace Mantid::API {
17IndexProperty::IndexProperty(const std::string &name, const IWorkspaceProperty &workspaceProp,
18 const IndexTypeProperty &indexTypeProp, const Kernel::IValidator_sptr &validator)
19 : ArrayProperty(name, "", validator), m_workspaceProp(workspaceProp), m_indexTypeProp(indexTypeProp), m_indices(0),
20 m_indicesExtracted(false) {}
21
22IndexProperty *IndexProperty::clone() const { return new IndexProperty(*this); }
23
24bool IndexProperty::isDefault() const { return m_value.empty(); }
25
26std::string IndexProperty::isValid() const {
27 std::string error;
28 // No workspace acts as an empty set and so is considered valid
30 return error;
31
32 try {
33 getIndices();
34 } catch (std::runtime_error &e) {
35 error = e.what();
36 } catch (std::out_of_range &e) {
37 error = "Indices provided to IndexProperty are out of range: ";
38 error.append(e.what());
39 } catch (std::logic_error &) {
40 error = "Duplicate indices supplied to IndexProperty.";
41 }
42
43 return error;
44}
45
48 return *this;
49}
50
51IndexProperty::operator Indexing::SpectrumIndexSet() const { return getIndices(); }
52
53Indexing::SpectrumIndexSet IndexProperty::getIndices() const {
54 const auto &indexInfo = getIndexInfoFromWorkspace();
56
57 if (m_value.empty()) {
58 return indexInfo.makeIndexSet();
59 } else {
60 auto min = m_value.front();
61 auto max = m_value.back();
62 auto isRange = (max - min) == static_cast<int>(m_value.size() - 1);
63 if (isRange) {
64 switch (type) {
66 return indexInfo.makeIndexSet(static_cast<Indexing::GlobalSpectrumIndex>(min),
67 static_cast<Indexing::GlobalSpectrumIndex>(max));
69 return indexInfo.makeIndexSet(static_cast<Indexing::SpectrumNumber>(static_cast<int32_t>(min)),
70 static_cast<Indexing::SpectrumNumber>(static_cast<int32_t>(max)));
71 }
72 } else {
73 MSVC_DIAG_OFF(4244);
74 switch (type) {
76 return indexInfo.makeIndexSet(std::vector<Indexing::GlobalSpectrumIndex>(m_value.begin(), m_value.end()));
78 std::vector<Indexing::SpectrumNumber> spectrumNumbers(m_value.cbegin(), m_value.cend());
79 return indexInfo.makeIndexSet(spectrumNumbers);
80 }
81 }
82 MSVC_DIAG_ON(4244);
83 }
84 }
85
86 m_indicesExtracted = true;
87 return m_indices;
88}
89
96Indexing::IndexInfo IndexProperty::getFilteredIndexInfo() const {
97 const auto &indexInfo = getIndexInfoFromWorkspace();
98 if (m_value.empty())
99 return indexInfo;
100 switch (m_indexTypeProp.selectedType()) {
102 return {std::vector<Indexing::GlobalSpectrumIndex>(m_value.begin(), m_value.end()), indexInfo};
104 std::vector<Indexing::SpectrumNumber> spectrumNumbers(m_value.cbegin(), m_value.cend());
105 return {spectrumNumbers, indexInfo};
106 }
107 default:
108 throw std::runtime_error("IndexProperty::getFilteredIndexInfo -- unsupported index type");
109 }
110}
111
112std::string IndexProperty::generatePropertyName(const std::string &name) { return name + "IndexSet"; }
113
114const Indexing::IndexInfo &IndexProperty::getIndexInfoFromWorkspace() const {
115 auto wksp = std::dynamic_pointer_cast<MatrixWorkspace>(m_workspaceProp.getWorkspace());
116 if (!wksp)
117 throw std::runtime_error("Invalid workspace type provided to "
118 "IndexProperty. Must be convertible to "
119 "MatrixWorkspace.");
120 return wksp->indexInfo();
121}
122
123} // namespace Mantid::API
const std::vector< double > & rhs
double error
Definition: IndexPeaks.cpp:133
#define MSVC_DIAG_ON(x)
#define MSVC_DIAG_OFF(x)
An interface that is implemented by WorkspaceProperty.
virtual Workspace_sptr getWorkspace() const =0
Get a pointer to the workspace.
IndexProperty : Implementation of a property type which returns a SpectrumIndexSet provided an input ...
Definition: IndexProperty.h:30
Indexing::SpectrumIndexSet getIndices() const
static std::string generatePropertyName(const std::string &name="")
bool isDefault() const override
Overriden function that returns if property has the same value that it was initialised with,...
const IWorkspaceProperty & m_workspaceProp
Definition: IndexProperty.h:57
IndexProperty(const std::string &name, const IWorkspaceProperty &workspaceProp, const IndexTypeProperty &indexTypeProp, const Kernel::IValidator_sptr &validator=Kernel::IValidator_sptr(new Kernel::NullValidator))
const IndexTypeProperty & m_indexTypeProp
Definition: IndexProperty.h:58
Indexing::SpectrumIndexSet m_indices
Definition: IndexProperty.h:59
std::string isValid() const override
Overridden function that checks whether the property, if not overriden returns "".
Indexing::IndexInfo getFilteredIndexInfo() const
Return IndexInfo created from workspace but containing selected spectra.
IndexProperty * clone() const override
'Virtual copy constructor'
IndexProperty & operator=(const IndexProperty &)=delete
const Indexing::IndexInfo & getIndexInfoFromWorkspace() const
IndexTypeProperty : Implementation of a property which stores the type of input indices users require...
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
std::string setValue(const std::string &value) override
Sets the values stored in the ArrayProperty from a string representation.
TYPE m_value
The value of the property.
const std::string & name() const
Get the property's name.
Definition: Property.cpp:60
const std::string type() const
Returns the type of the property as a string.
Definition: Property.cpp:76
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition: IValidator.h:26