BornAgain  1.19.79
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/Model/Data/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 
16 #include "Base/Axis/IAxis.h"
23 #include "GUI/Util/Error.h"
24 
25 namespace {
26 
27 ComboProperty gradientCombo()
28 {
29  ComboProperty result;
30  result << "Grayscale"
31  << "Hot"
32  << "Cold"
33  << "Night"
34  << "Candy"
35  << "Geography"
36  << "Ion"
37  << "Thermal"
38  << "Polar"
39  << "Spectrum"
40  << "Jet"
41  << "Hues";
42  result.setValue("Thermal");
43  return result;
44 }
45 
46 } // namespace
47 
48 const QString x_axis_default_name = "X [nbins]";
49 const QString y_axis_default_name = "Y [nbins]";
50 
52  : DataItem(M_TYPE)
53 {
54  addProperty(P_TITLE, QString());
55 
56  addProperty(P_PROJECTIONS, false);
58  addProperty(P_GRADIENT, gradientCombo().variant());
59  addProperty<BasicAxisItem>(P_XAXIS);
60  addProperty<BasicAxisItem>(P_YAXIS);
61  addProperty<AmplitudeAxisItem>(P_ZAXIS);
62 
65 
68 
70 }
71 
72 void IntensityDataItem::setDatafield(Datafield* data)
73 {
74  ASSERT(data && "Assertion failed in IntensityDataItem::setDatafield: nullptr data passed");
75  if (data->rank() != 2)
76  throw Error("Error in IntensityDataItem::setDatafield: cannot handle non-2D data");
78 
82 
84 }
85 
87 {
88  return xAxisItem()->binCount();
89 }
90 
92 {
93  return yAxisItem()->binCount();
94 }
95 
97 {
98  return xAxisItem()->min();
99 }
100 
102 {
103  return xAxisItem()->max();
104 }
105 
107 {
108  const double defaultXmin(0.0);
109  return m_data ? m_data->axis(0).min() : defaultXmin;
110 }
111 
113 {
114  const double defaultXmax(1.0);
115  return m_data ? m_data->axis(0).max() : defaultXmax;
116 }
117 
119 {
120  return yAxisItem()->min();
121 }
122 
124 {
125  return yAxisItem()->max();
126 }
127 
129 {
130  const double defaultYmin(0.0);
131  return m_data ? m_data->axis(1).min() : defaultYmin;
132 }
133 
135 {
136  const double defaultYmax(1.0);
137  return m_data ? m_data->axis(1).max() : defaultYmax;
138 }
139 
141 {
142  return zAxisItem()->min();
143 }
144 
146 {
147  return zAxisItem()->max();
148 }
149 
151 {
152  return getGradient().getValue();
153 }
154 
156 {
157  return getItemValue(P_GRADIENT).value<ComboProperty>();
158 }
159 
161 {
162  setItemValue(P_GRADIENT, gradient.variant());
163 }
164 
166 {
168 }
169 
171 {
172  return zAxisItem()->isLogScale();
173 }
174 
176 {
177  return getItemValue(P_IS_INTERPOLATED).toBool();
178 }
179 
181 {
182  return xAxisItem()->title();
183 }
184 
186 {
187  return yAxisItem()->title();
188 }
189 
191 {
192  return item<AmplitudeAxisItem>(P_ZAXIS)->isLocked();
193 }
194 
196 {
197  item<AmplitudeAxisItem>(P_ZAXIS)->setLocked(state);
198 }
199 
200 void IntensityDataItem::setXaxisTitle(const QString& title)
201 {
202  xAxisItem()->setTitle(title);
203 }
204 
205 void IntensityDataItem::setYaxisTitle(const QString& title)
206 {
207  yAxisItem()->setTitle(title);
208 }
209 
210 //! set zoom range of x,y axes to axes of input data
212 {
213  setLowerX(xMin());
214  setUpperX(xMax());
215  setLowerY(yMin());
216  setUpperY(yMax());
217 }
218 
220 {
221  MaskUnitsConverter converter;
222  converter.convertToNbins(this);
223 
225 
226  converter.convertFromNbins(this);
227 }
228 
229 std::vector<int> IntensityDataItem::shape() const
230 {
231  return {xSize(), ySize()};
232 }
233 
235 {
236  ASSERT(data.unitsLabel() == "nbins");
237  ComboProperty combo = ComboProperty() << data.unitsLabel();
238  setAxesUnits(combo);
239  setXaxisTitle(data.axisLabel(0));
240  setYaxisTitle(data.axisLabel(1));
241  MaskUnitsConverter converter;
242  converter.convertToNbins(this);
243  setDatafield(std::move(data).intensityData().release());
245  converter.convertFromNbins(this);
246 }
247 
249 {
250  return name == P_IS_INTERPOLATED;
251 }
252 
254 {
255  return name == P_GRADIENT;
256 }
257 
259 {
260  return name == P_XAXIS;
261 }
262 
264 {
265  return name == P_YAXIS;
266 }
267 
269 {
270  return name == P_ZAXIS;
271 }
272 
274 {
276 }
277 
279 {
281 }
282 
284 {
286 }
287 
289 {
291 }
292 
293 void IntensityDataItem::setLowerAndUpperZ(double zmin, double zmax)
294 {
295  if (getLowerZ() != zmin)
296  setLowerZ(zmin);
297 
298  if (getUpperZ() != zmax)
299  setUpperZ(zmax);
300 }
301 
303 {
304  zAxisItem()->setLowerBound(zmin);
305 }
306 
308 {
309  zAxisItem()->setUpperBound(zmax);
310 }
311 
313 {
314  zAxisItem()->setLogScale(logz);
315 }
316 
318 {
320 }
321 
322 //! Sets zoom range of X,Y axes, if it was not yet defined.
323 
325 {
326  // set zoom range of x-axis to min, max values if it was not set already
327  if (getUpperX() < getLowerX()) {
328  setLowerX(xMin());
329  setUpperX(xMax());
330  }
331 
332  // set zoom range of y-axis to min, max values if it was not set already
333  if (getUpperY() < getLowerY()) {
334  setLowerY(yMin());
335  setUpperY(yMax());
336  }
337 
338  const int nx = static_cast<int>(m_data->axis(0).size());
339  xAxisItem()->setBinCount(nx);
340  const int ny = static_cast<int>(m_data->axis(1).size());
341  yAxisItem()->setBinCount(ny);
342 }
343 
344 //! Init axes labels, if it was not done already.
345 
347 {
348  if (getXaxisTitle().isEmpty())
349  setXaxisTitle(QString::fromStdString(m_data->axis(0).axisName()));
350 
351  if (getYaxisTitle().isEmpty())
352  setYaxisTitle(QString::fromStdString(m_data->axis(1).axisName()));
353 }
354 
355 //! Sets min,max values for z-axis, if axes is not locked, and ranges are not yet set.
356 
358 {
359  if (isZAxisLocked())
360  return;
361 
363 }
364 
366 {
367  QPair<double, double> minmax = dataRange();
368  setLowerAndUpperZ(minmax.first, minmax.second);
369 }
370 
371 //! Init zmin, zmax to match the intensity values range.
372 QPair<double, double> IntensityDataItem::dataRange() const
373 {
374  const Datafield* data = getDatafield();
375 
376  const auto vec = data->flatVector();
377  double min(*std::min_element(vec.cbegin(), vec.cend()));
378  double max(*std::max_element(vec.cbegin(), vec.cend()));
379  if (isLogz()) {
380  if (max > 10000) {
381  min = 1.0;
382  max = max * 1.1;
383  } else {
384  min = max / 10000;
385  max = max * 1.1;
386  }
387  } else {
388  max = max * 1.1;
389  }
390 
391  return QPair<double, double>(min, max);
392 }
393 
395 {
396  return item<BasicAxisItem>(P_XAXIS);
397 }
398 
400 {
401  return item<BasicAxisItem>(P_XAXIS);
402 }
403 
405 {
406  return item<BasicAxisItem>(P_YAXIS);
407 }
408 
410 {
411  return item<BasicAxisItem>(P_YAXIS);
412 }
413 
415 {
416  return item<AmplitudeAxisItem>(P_ZAXIS);
417 }
418 
420 {
421  return item<AmplitudeAxisItem>(P_ZAXIS);
422 }
423 
424 //! Set axes viewport to original data.
425 
427 {
428  if (!m_data)
429  return;
430 
432  if (!isZAxisLocked())
434 }
435 
437 {
438  return dynamic_cast<MaskContainerItem*>(getItem(T_MASKS));
439 }
440 
442 {
443  return dynamic_cast<MaskContainerItem*>(getItem(T_MASKS));
444 }
445 
447 {
449  if (!result)
450  result = model()->insertItem<MaskContainerItem>(this, -1, T_MASKS);
451  return result;
452 }
453 
455 {
456  return dynamic_cast<ProjectionContainerItem*>(getItem(T_PROJECTIONS));
457 }
458 
460 {
461  return dynamic_cast<ProjectionContainerItem*>(getItem(T_PROJECTIONS));
462 }
463 
465 {
467  if (!result)
468  result = model()->insertItem<ProjectionContainerItem>(this, -1, T_PROJECTIONS);
469  return result;
470 }
471 
473 {
474  return (maskContainerItem() && !maskContainerItem()->maskItems().isEmpty());
475 }
476 
478 {
480 }
Defines various axis items.
Defines error class.
Defines ImportDataInfo helper struct.
const QString y_axis_default_name
const QString x_axis_default_name
Defines class IntensityDataItem.
Defines namespace GUI::Model::JobItemUtils.
Defines MaskItems classes.
Defines class MaskUnitsConverter.
Defines items related to projections over color map.
bool isLogScale() const
Definition: AxesItems.cpp:178
void setLogScale(bool value)
Definition: AxesItems.cpp:183
DoubleDescriptor min(const QString &unit=QString()) const
Definition: AxesItems.cpp:41
QString title() const
Definition: AxesItems.cpp:80
int binCount() const
Definition: AxesItems.cpp:26
void setBinCount(size_t value)
Definition: AxesItems.cpp:31
void setLowerBound(double value)
Definition: AxesItems.cpp:48
void setUpperBound(double value)
Definition: AxesItems.cpp:65
DoubleDescriptor max(const QString &unit=QString()) const
Definition: AxesItems.cpp:58
void setTitle(const QString &title)
Definition: AxesItems.cpp:85
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
void setValue(const QString &name)
QString getValue() const
Abstract base class for IntensityDataItem and SpecularDataItem. Owns one simulated data set of type D...
Definition: DataItem.h:34
virtual void setDatafield(Datafield *data)=0
The given pointer becomes owned by this class!!
Definition: DataItem.cpp:20
Datafield * getDatafield()
Definition: DataItem.h:41
void setAxesUnits(const ComboProperty &units)
Definition: DataItem.cpp:96
std::unique_ptr< Datafield > m_data
simulation results
Definition: DataItem.h:88
Carries information about loaded data.
QString unitsLabel() const
QString axisLabel(size_t axis_index) const
Abstract base class for instrument-specific item classes.
void setLowerX(double value)
std::vector< int > shape() const override
static constexpr auto P_ZAXIS
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
void setLogz(bool logz)
void setGradient(const ComboProperty &gradient)
MaskContainerItem * maskContainerItem()
void updateAxesZoomLevel()
Sets zoom range of X,Y axes, if it was not yet defined.
const AmplitudeAxisItem * zAxisItem() const
QPair< double, double > dataRange() const
Init zmin, zmax to match the intensity values range.
double xMin() const
Returns min and max range of x-axis as given by IntensityData.
QString getXaxisTitle() const
SelectionDescriptor< QString > gradient() const
void setLowerY(double value)
void setLowerAndUpperZ(double zmin, double zmax)
void setUpperZ(double zmax)
bool isZAxisLocked() const
Returns 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)
MaskContainerItem * getOrCreateMaskContainerItem()
static bool isXaxisPropertyName(const QString &name)
double getLowerY() const
Returns lower and upper zoom ranges of y-axis.
ProjectionContainerItem * getOrCreateProjectionContainerItem()
void reset(ImportDataInfo data) override
Returns data to the state defined by user (imported) data.
static bool isZaxisPropertyName(const QString &name)
static constexpr auto P_YAXIS
void setDatafield(Datafield *data) override
The given pointer becomes owned by this class!!
void resetView()
Set axes viewport to original data.
const BasicAxisItem * yAxisItem() const
void setInterpolated(bool interp)
void setUpperX(double value)
QString getYaxisTitle() const
static constexpr auto P_PROJECTIONS
ComboProperty getGradient() const
static bool isInterpolatedPropertyName(const QString &name)
void updateDataRange()
Sets min,max values for z-axis, if axes is not locked, and ranges are not yet set.
QString getGradientValue() const
double getUpperY() const
static constexpr auto P_IS_INTERPOLATED
static constexpr auto P_XAXIS
static constexpr auto T_PROJECTIONS
const BasicAxisItem * xAxisItem() const
static bool isYaxisPropertyName(const QString &name)
double getUpperX() const
static constexpr auto P_TITLE
static constexpr auto T_MASKS
bool isInterpolated() const
void setUpperY(double value)
double getLowerX() const
Returns lower and upper zoom ranges of x-axis.
ProjectionContainerItem * projectionContainerItem()
double yMin() const
Returns min and max range of y-axis as given by IntensityData.
double getUpperZ() const
void updateCoords(const InstrumentItem *instrument) override
static bool isGradientPropertyName(const QString &name)
void updateAxesLabels()
Init axes labels, if it was not done already.
static constexpr auto P_GRADIENT
void setZAxisLocked(bool state)
Container holding various masks as children.
Definition: MaskItems.h:202
static constexpr auto M_TYPE
Definition: MaskItems.h:204
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...
A container to hold ProjectionItems, intended to store projections of color map on X,...
static constexpr auto M_TYPE
Describes a selection (various possibilities and the current one).
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag. name is the tag name and the display name....
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. max = -1 -> unlimited,...
QVariant value() const
Get value.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
void setItemValue(const QString &tag, const QVariant &variant) const
Directly set value of item under given tag.
void setDefaultTag(const QString &tag)
Set default tag.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60
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:81
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:137
void updateDataAxes(DataItem *intensityItem, const InstrumentItem *instrumentItem)
updates axes of Datafield in IntensityData item
QString const & name(EShape k)
Definition: particles.cpp:20