BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ImportDataInfo.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/utils/ImportDataInfo.cpp
6 //! @brief Implements ImportDataInfo helper struct
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "Device/Data/OutputData.h"
16 #include "Device/Unit/AxisNames.h"
20 
21 namespace {
22 std::vector<Axes::Units> specularUnits()
23 {
24  std::vector<Axes::Units> result;
25  const auto units_map = AxisNames::InitSpecAxis();
26  for (auto& pair : units_map)
27  result.push_back(pair.first);
28  return result;
29 }
30 
31 // map: data rank --> available units
32 std::map<size_t, std::vector<Axes::Units>> available_units = {{1u, specularUnits()},
33  {2u, {Axes::Units::NBINS}}};
34 } // namespace
35 
37 
39  : m_data(std::move(other.m_data)), m_units(other.m_units)
40 {
41 }
42 
44  : m_data(units == Axes::Units::NBINS && data
46  : std::move(data))
47  , m_units(units)
48 {
49  checkValidity();
50 }
51 
52 ImportDataInfo::ImportDataInfo(std::unique_ptr<OutputData<double>> data, const QString& units_label)
53  : m_data(std::move(data)), m_units(JobItemUtils::axesUnitsFromName(units_label))
54 {
55  checkValidity();
56 }
57 
59  : ImportDataInfo(std::unique_ptr<OutputData<double>>(new OutputData<double>(std::move(data))),
60  units)
61 {
62 }
63 
65 
66 ImportDataInfo::operator bool() const
67 {
68  return static_cast<bool>(m_data);
69 }
70 
71 std::unique_ptr<OutputData<double>> ImportDataInfo::intensityData() const&
72 {
73  if (!m_data)
74  return nullptr;
75  return std::unique_ptr<OutputData<double>>(m_data->clone());
76 }
77 
78 std::unique_ptr<OutputData<double>> ImportDataInfo::intensityData() &&
79 {
80  return std::move(m_data);
81 }
82 
84 {
85  if (!m_data)
86  return 0;
87  return m_data->rank();
88 }
89 
91 {
93 }
94 
95 QString ImportDataInfo::axisLabel(size_t axis_index) const
96 {
97  if (!m_data)
98  return "";
99 
100  const size_t rank = m_data->rank();
101  if (rank == 2)
102  return axis_index == 0 ? "X [nbins]" : "Y [nbins]";
103  else if (rank == 1) {
104  if (axis_index > 0)
105  return "Signal [a.u.]";
106 
107  auto label_map = AxisNames::InitSpecAxis();
108  return QString::fromStdString(label_map[m_units]);
109  }
110  throw GUIHelpers::Error("Error in ImportDataInfo::axisLabel: unsupported data type");
111 }
112 
114 {
115  if (!m_data)
116  return;
117  auto iter = available_units.find(m_data->rank());
118  if (iter == available_units.end())
119  throw GUIHelpers::Error("Error in ImportDataInfo constructor: unsupported data type");
120  for (auto& value : iter->second)
121  if (m_units == value)
122  return;
123 
124  throw GUIHelpers::Error("Error in ImportDataInfo constructor: inacceptable units passed.");
125 }
Defines namespace AxisNames.
Defines class GUIHelpers functions.
Defines ImportDataUtils namespace.
Defines class JobItemUtils.
Defines and implements templated class OutputData.
Wrapper for detector axes units, required for a better representation of detector axes units in pytho...
Carries information about loaded data.
Axes::Units m_units
size_t dataRank() const
Returns number of dimensions.
QString unitsLabel() const
std::unique_ptr< OutputData< double > > m_data
QString axisLabel(size_t axis_index) const
std::unique_ptr< OutputData< double > > intensityData() const &
std::map< Axes::Units, std::string > InitSpecAxis()
Definition: AxisNames.cpp:82
Provides utility methods to import data files.
std::unique_ptr< OutputData< double > > CreateSimplifiedOutputData(const OutputData< double > &data)
Creates OutputData with bin-valued axes.
Contains set of convenience methods to set data to the IntensityDataItem from domain simulation.
Definition: JobItemUtils.h:30
Axes::Units axesUnitsFromName(const QString &name)
returns domain axes units type from their GUI name
QString nameFromAxesUnits(Axes::Units units)
returns axes units names from their domain counterpart
Constants and functions for physical unit conversions.
Definition: Units.h:30
Definition: filesystem.h:81