BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IntensityDataItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/IntensityDataItem.cpp
6 //! @brief Implements class IntensityDataItem
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 
23 
24 namespace {
25 ComboProperty gradientCombo()
26 {
27  ComboProperty result;
28  result << "Grayscale"
29  << "Hot"
30  << "Cold"
31  << "Night"
32  << "Candy"
33  << "Geography"
34  << "Ion"
35  << "Thermal"
36  << "Polar"
37  << "Spectrum"
38  << "Jet"
39  << "Hues";
40  result.setValue("Jet");
41  return result;
42 }
43 } // namespace
44 
45 const QString x_axis_default_name = "X [nbins]";
46 const QString y_axis_default_name = "Y [nbins]";
47 
48 const QString IntensityDataItem::P_TITLE = "Title";
49 const QString IntensityDataItem::P_PROJECTIONS_FLAG = "Projections";
50 const QString IntensityDataItem::P_IS_INTERPOLATED = "Interpolation";
51 const QString IntensityDataItem::P_GRADIENT = "Gradient";
52 const QString IntensityDataItem::P_XAXIS = "x-axis";
53 const QString IntensityDataItem::P_YAXIS = "y-axis";
54 const QString IntensityDataItem::P_ZAXIS = "color-axis";
55 const QString IntensityDataItem::T_MASKS = "Mask tag";
56 const QString IntensityDataItem::T_PROJECTIONS = "Projection tag";
57 
59 {
60  addProperty(P_TITLE, QString())->setVisible(false);
61 
64  addProperty(P_GRADIENT, gradientCombo().variant());
65 
66  auto basicAxis = addProperty<BasicAxisItem>(P_XAXIS);
67  basicAxis->getItem(BasicAxisItem::P_NBINS)->setVisible(false);
68 
69  basicAxis = addProperty<BasicAxisItem>(P_YAXIS);
70  basicAxis->getItem(BasicAxisItem::P_NBINS)->setVisible(false);
71 
72  auto amplitudeAxis = addProperty<AmplitudeAxisItem>(P_ZAXIS);
73  amplitudeAxis->getItem(BasicAxisItem::P_NBINS)->setVisible(false);
74 
77 
78  registerTag(T_MASKS, 0, -1, QStringList() << "MaskContainer");
80 
81  registerTag(T_PROJECTIONS, 0, -1, QStringList() << "ProjectionContainer");
82 }
83 
85 {
86  ASSERT(data && "Assertion failed in IntensityDataItem::setOutputData: nullptr data passed");
87  if (data->rank() != 2)
88  throw GUIHelpers::Error(
89  "Error in IntensityDataItem::setOutputData: cannot handle non-2D data");
91 
95 
97 }
98 
100 {
101  return xAxisItem()->binCount();
102 }
103 
105 {
106  return yAxisItem()->binCount();
107 }
108 
110 {
111  return xAxisItem()->lowerBound();
112 }
113 
115 {
116  return xAxisItem()->upperBound();
117 }
118 
120 {
121  const double defaultXmin(0.0);
122  return m_data ? m_data->axis(0).lowerBound() : defaultXmin;
123 }
124 
126 {
127  const double defaultXmax(1.0);
128  return m_data ? m_data->axis(0).upperBound() : defaultXmax;
129 }
130 
132 {
133  return yAxisItem()->lowerBound();
134 }
135 
137 {
138  return yAxisItem()->upperBound();
139 }
140 
142 {
143  const double defaultYmin(0.0);
144  return m_data ? m_data->axis(1).lowerBound() : defaultYmin;
145 }
146 
148 {
149  const double defaultYmax(1.0);
150  return m_data ? m_data->axis(1).upperBound() : defaultYmax;
151 }
152 
154 {
155  return zAxisItem()->lowerBound();
156 }
157 
159 {
160  return zAxisItem()->upperBound();
161 }
162 
164 {
165  ComboProperty combo_property = getItemValue(P_GRADIENT).value<ComboProperty>();
166  return combo_property.getValue();
167 }
168 
170 {
171  return zAxisItem()->isLogScale();
172 }
173 
175 {
176  return getItemValue(P_IS_INTERPOLATED).toBool();
177 }
178 
180 {
181  return xAxisItem()->title();
182 }
183 
185 {
186  return yAxisItem()->title();
187 }
188 
190 {
192 }
193 
195 {
197 }
198 
199 void IntensityDataItem::setXaxisTitle(const QString& title)
200 {
201  xAxisItem()->setTitle(title);
202 }
203 
204 void IntensityDataItem::setYaxisTitle(const QString& title)
205 {
206  yAxisItem()->setTitle(title);
207 }
208 
209 //! set zoom range of x,y axes to axes of input data
211 {
212  setLowerX(getXmin());
213  setUpperX(getXmax());
214  setLowerY(getYmin());
215  setUpperY(getYmax());
216 }
217 
219 {
220  MaskUnitsConverter converter;
221  converter.convertToNbins(this);
222 
223  JobItemUtils::updateDataAxes(this, instrument);
224 
225  converter.convertFromNbins(this);
226 }
227 
228 std::vector<int> IntensityDataItem::shape() const
229 {
230  return {getNbinsX(), getNbinsY()};
231 }
232 
234 {
235  ASSERT(data.unitsLabel() == "nbins");
236  ComboProperty combo = ComboProperty() << data.unitsLabel();
239 
240  setXaxisTitle(data.axisLabel(0));
241  setYaxisTitle(data.axisLabel(1));
242  MaskUnitsConverter converter;
243  converter.convertToNbins(this);
244  setOutputData(std::move(data).intensityData().release());
246  converter.convertFromNbins(this);
247 }
248 
250 {
252 }
253 
255 {
257 }
258 
260 {
262 }
263 
265 {
267 }
268 
269 void IntensityDataItem::setLowerAndUpperZ(double zmin, double zmax)
270 {
271  if (getLowerZ() != zmin)
272  setLowerZ(zmin);
273 
274  if (getUpperZ() != zmax)
275  setUpperZ(zmax);
276 }
277 
279 {
280  zAxisItem()->setLowerBound(zmin);
281 }
282 
284 {
285  zAxisItem()->setUpperBound(zmax);
286 }
287 
289 {
290  zAxisItem()->setLogScale(logz);
291 }
292 
294 {
296 }
297 
298 //! Sets zoom range of X,Y axes, if it was not yet defined.
299 
301 {
302  // set zoom range of x-axis to min, max values if it was not set already
303  if (getUpperX() < getLowerX()) {
304  setLowerX(getXmin());
305  setUpperX(getXmax());
306  }
307 
308  // set zoom range of y-axis to min, max values if it was not set already
309  if (getUpperY() < getLowerY()) {
310  setLowerY(getYmin());
311  setUpperY(getYmax());
312  }
313 
314  const int nx = static_cast<int>(m_data->axis(0).size());
315  xAxisItem()->setBinCount(nx);
316  const int ny = static_cast<int>(m_data->axis(1).size());
317  yAxisItem()->setBinCount(ny);
318 }
319 
320 //! Init axes labels, if it was not done already.
321 
323 {
324  if (getXaxisTitle().isEmpty())
325  setXaxisTitle(QString::fromStdString(m_data->axis(0).getName()));
326 
327  if (getYaxisTitle().isEmpty())
328  setYaxisTitle(QString::fromStdString(m_data->axis(1).getName()));
329 }
330 
331 //! Sets min,max values for z-axis, if axes is not locked, and ranges are not yet set.
332 
334 {
335  if (isZAxisLocked())
336  return;
337 
339 }
340 
342 {
343  QPair<double, double> minmax = dataRange();
344  setLowerAndUpperZ(minmax.first, minmax.second);
345 }
346 
347 //! Init zmin, zmax to match the intensity values range.
348 QPair<double, double> IntensityDataItem::dataRange() const
349 {
350  const OutputData<double>* data = getOutputData();
351  double min(*std::min_element(data->begin(), data->end()));
352  double max(*std::max_element(data->begin(), data->end()));
353  if (isLogz()) {
354  if (max > 10000) {
355  min = 1.0;
356  max = max * 1.1;
357  } else {
358  min = max / 10000;
359  max = max * 1.1;
360  }
361  } else {
362  max = max * 1.1;
363  }
364 
365  return QPair<double, double>(min, max);
366 }
367 
369 {
370  return item<BasicAxisItem>(P_XAXIS);
371 }
372 
374 {
375  return item<BasicAxisItem>(P_XAXIS);
376 }
377 
379 {
380  return item<BasicAxisItem>(P_YAXIS);
381 }
382 
384 {
385  return item<BasicAxisItem>(P_YAXIS);
386 }
387 
389 {
390  return item<AmplitudeAxisItem>(P_ZAXIS);
391 }
392 
394 {
395  return item<AmplitudeAxisItem>(P_ZAXIS);
396 }
397 
398 //! Set axes viewport to original data.
399 
401 {
402  if (!m_data)
403  return;
404 
406  if (!isZAxisLocked())
408 }
409 
411 {
412  return dynamic_cast<MaskContainerItem*>(getItem(IntensityDataItem::T_MASKS));
413 }
414 
416 {
417  return dynamic_cast<MaskContainerItem*>(getItem(IntensityDataItem::T_MASKS));
418 }
419 
421 {
423 }
424 
426 {
428 }
429 
431 {
433 }
434 
436 {
438 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines various axis items.
Defines class GUIHelpers functions.
Defines ImportDataUtils namespace.
const QString y_axis_default_name
const QString x_axis_default_name
Defines class IntensityDataItem.
Defines class JobItemUtils.
Defines MaskItems classes.
Defines class MaskUnitsConverter.
Defines items related to projections over color map.
bool isLogScale() const
Definition: AxesItems.cpp:113
static const QString P_LOCK_MIN_MAX
Definition: AxesItems.h:56
void setLogScale(bool value)
Definition: AxesItems.cpp:118
void setBinCount(int value)
Definition: AxesItems.cpp:37
static const QString P_NBINS
Definition: AxesItems.h:26
double upperBound() const
Definition: AxesItems.cpp:52
QString title() const
Definition: AxesItems.cpp:62
int binCount() const
Definition: AxesItems.cpp:32
double lowerBound() const
Definition: AxesItems.cpp:42
void setLowerBound(double value)
Definition: AxesItems.cpp:47
void setUpperBound(double value)
Definition: AxesItems.cpp:57
void setTitle(const QString &title)
Definition: AxesItems.cpp:67
Custom property to define list of string values with multiple selections.
Definition: ComboProperty.h:25
QVariant variant() const
Constructs variant enclosing given ComboProperty.
void setValue(const QString &name)
QString getValue() const
Provides common functionality for IntensityDataItem and SpecularDataItem.
Definition: DataItem.h:29
OutputData< double > * getOutputData()
Definition: DataItem.h:36
virtual void setOutputData(OutputData< double > *data)=0
The given pointer becomes owned by this class!!
Definition: DataItem.cpp:24
static const QString P_AXES_UNITS
Definition: DataItem.h:34
std::unique_ptr< OutputData< double > > m_data
simulation results
Definition: DataItem.h:72
Carries information about loaded data.
QString unitsLabel() const
QString axisLabel(size_t axis_index) const
void setLowerX(double value)
std::vector< int > shape() const override
double getYmax() const
static const QString P_ZAXIS
static const QString P_YAXIS
static const QString T_PROJECTIONS
void setXaxisTitle(const QString &title) override
void setYaxisTitle(const QString &title) override
double getLowerZ() const
returns lower and upper zoom ranges of z-axis
bool hasProjections() const
static const QString P_PROJECTIONS_FLAG
QString getGradient() const
void setLogz(bool logz)
static const QString P_TITLE
MaskContainerItem * maskContainerItem()
void updateAxesZoomLevel()
Sets zoom range of X,Y axes, if it was not yet defined.
double getYmin() const
returns min and max range of y-axis as given by IntensityData
const AmplitudeAxisItem * zAxisItem() const
QPair< double, double > dataRange() const
Init zmin, zmax to match the intensity values range.
static const QString P_XAXIS
QString getXaxisTitle() const
void setLowerY(double value)
void setLowerAndUpperZ(double zmin, double zmax)
void setUpperZ(double zmax)
bool isZAxisLocked() const
return true if min, max range of Z-axis is locked (change not allowed)
void setAxesRangeToData() override
set zoom range of x,y axes to axes of input data
void setLowerZ(double zmin)
double getLowerY() const
returns lower and upper zoom ranges of y-axis
static const QString P_GRADIENT
void reset(ImportDataInfo data) override
Returns data to the state defined by user (imported) data.
void resetView()
Set axes viewport to original data.
const BasicAxisItem * yAxisItem() const
void setInterpolated(bool interp)
void setUpperX(double value)
QString getYaxisTitle() const
double getXmin() const
returns min and max range of x-axis as given by IntensityData
void updateDataRange()
Sets min,max values for z-axis, if axes is not locked, and ranges are not yet set.
double getUpperY() const
const BasicAxisItem * xAxisItem() const
double getUpperX() const
bool isInterpolated() const
void setUpperY(double value)
double getXmax() const
double getLowerX() const
returns lower and upper zoom ranges of x-axis
void setOutputData(OutputData< double > *data) override
The given pointer becomes owned by this class!!
ProjectionContainerItem * projectionContainerItem()
static const QString T_MASKS
double getUpperZ() const
void updateAxesLabels()
Init axes labels, if it was not done already.
void updateAxesUnits(const InstrumentItem *instrument) override
static const QString P_IS_INTERPOLATED
void setZAxisLocked(bool state)
Container holding various masks as children.
Definition: MaskItems.h:24
The MaskUnitsConverter converts coordinates of all masks from one units to anoter.
void convertToNbins(IntensityDataItem *intensityData)
Converts all masks on board of IntensityDataItem into bin-fraction coordinates.
void convertFromNbins(IntensityDataItem *intensityData)
Converts all masks on board of IntensityDataItem from bin-fraction coordinates to coordinates of axes...
iterator end()
Returns read/write iterator that points to the one past last element.
Definition: OutputData.h:93
size_t rank() const
Returns number of dimensions.
Definition: OutputData.h:56
iterator begin()
Returns read/write iterator that points to the first element.
Definition: OutputData.h:343
A container to hold ProjectionItems, intended to store projections of color map on X,...
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag.
bool registerTag(const QString &name, int min=0, int max=-1, QStringList modelTypes={})
Add new tag to this item with given name, min, max and types.
QVariant value() const
Get value.
void setVisible(bool enabled)
Flags accessors.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
void setDefaultTag(const QString &tag)
Set default tag.
void setItemValue(const QString &tag, const QVariant &variant)
Directly set value of item under given tag.
void emitDataChanged(int role=Qt::DisplayRole)
Notify model about data changes.
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
bool hasChildren() const
Indicates whether this SessionItem has any child items.
Definition: SessionItem.cpp:87
void updateDataAxes(DataItem *intensityItem, const InstrumentItem *instrumentItem)
updates axes of OutputData in IntensityData item