BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SaveProjectionsAssistant Class Reference

Assistant class which save all projections of IndensityDataItem into ASCII file. More...

Classes

struct  Projection
 
struct  ProjectionsData
 

Public Member Functions

 SaveProjectionsAssistant ()
 
 ~SaveProjectionsAssistant ()
 
void saveProjections (QWidget *parent, IntensityDataItem *intensityItem)
 Calls file open dialog and writes projection data as ASCII. More...
 

Private Member Functions

QString projectionFileHeader (ProjectionsData &projectionsData)
 Returns projections header. More...
 
QVector< SessionItem * > projectionItems (const QString &projectionsType, IntensityDataItem *intensityItem)
 Returns vector of ProjectionItems sorted according to axis value. More...
 
ProjectionsData projectionsData (const QString &projectionsType, IntensityDataItem *intensityItem)
 Returns projections data for all projections of given type (horizontal, vertical). More...
 
QString projectionsToString (const QString &projectionsType, IntensityDataItem *intensityItem)
 Generates multi-line string with projections data of given type (horizontal, vertical). More...
 

Private Attributes

std::unique_ptr< Histogram2Dm_hist2d
 

Detailed Description

Assistant class which save all projections of IndensityDataItem into ASCII file.

Definition at line 29 of file SaveProjectionsAssistant.h.

Constructor & Destructor Documentation

◆ SaveProjectionsAssistant()

SaveProjectionsAssistant::SaveProjectionsAssistant ( )
default

◆ ~SaveProjectionsAssistant()

SaveProjectionsAssistant::~SaveProjectionsAssistant ( )
default

Member Function Documentation

◆ projectionFileHeader()

QString SaveProjectionsAssistant::projectionFileHeader ( ProjectionsData projectionsData)
private

Returns projections header.

For projections along x it will be "# x y=6.0194 y=33.5922 y=61.9417"

Definition at line 165 of file SaveProjectionsAssistant.cpp.

166 {
167  QString xcol, ycol;
168 
169  projectionsData.is_horizontal ? xcol = "# x" : xcol = "# y";
170  projectionsData.is_horizontal ? ycol = " y=" : ycol = " x=";
171 
172  QString result;
173  result.append(QString("%1").arg(xcol, -bin_centers_colwidth));
174 
175  for (auto& data : projectionsData.projections)
176  result.append(QString("%1%2").arg(ycol).arg(data.axis_value,
177  -(bin_values_colwidth - ycol.size()), 'f', 4));
178  result.append("\n");
179 
180  return result;
181 }
ProjectionsData projectionsData(const QString &projectionsType, IntensityDataItem *intensityItem)
Returns projections data for all projections of given type (horizontal, vertical).

References SaveProjectionsAssistant::ProjectionsData::is_horizontal, SaveProjectionsAssistant::ProjectionsData::projections, and projectionsData().

Referenced by projectionsToString().

Here is the call graph for this function:

◆ projectionItems()

QVector< SessionItem * > SaveProjectionsAssistant::projectionItems ( const QString &  projectionsType,
IntensityDataItem intensityItem 
)
private

Returns vector of ProjectionItems sorted according to axis value.

Definition at line 147 of file SaveProjectionsAssistant.cpp.

149 {
150  auto result = intensityItem->projectionContainerItem()->getChildrenOfType(projectionsType);
151  std::sort(result.begin(), result.end(), [=](SessionItem* item1, SessionItem* item2) {
152  QString propertyName = HorizontalLineItem::P_POSY;
153  if (projectionsType != "HorizontalLineMask")
154  propertyName = VerticalLineItem::P_POSX;
155 
156  return item1->getItemValue(propertyName).toDouble()
157  < item2->getItemValue(propertyName).toDouble();
158  });
159  return result;
160 }
ProjectionContainerItem * projectionContainerItem()
QVector< SessionItem * > getChildrenOfType(const QString &model_type) const
Returns a vector of all children of the given type.

References SessionItem::getChildrenOfType(), and IntensityDataItem::projectionContainerItem().

Referenced by projectionsData().

Here is the call graph for this function:

◆ projectionsData()

SaveProjectionsAssistant::ProjectionsData SaveProjectionsAssistant::projectionsData ( const QString &  projectionsType,
IntensityDataItem intensityItem 
)
private

Returns projections data for all projections of given type (horizontal, vertical).

Definition at line 109 of file SaveProjectionsAssistant.cpp.

111 {
112  ProjectionsData result;
113  projectionsType == "VerticalLineMask" ? result.is_horizontal = false
114  : result.is_horizontal = true;
115 
116  for (auto item : projectionItems(projectionsType, intensityItem)) {
117  std::unique_ptr<Histogram1D> hist;
119 
120  if (item->modelType() == "HorizontalLineMask") {
121  data.axis_value = item->getItemValue(HorizontalLineItem::P_POSY).toDouble();
122  hist.reset(m_hist2d->projectionX(data.axis_value));
123  } else {
124  data.axis_value = item->getItemValue(VerticalLineItem::P_POSX).toDouble();
125  hist.reset(m_hist2d->projectionY(data.axis_value));
126  }
127 
128 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
129  auto values = hist->binValues();
130  auto centers = hist->binCenters();
131  data.bin_values = QVector<double>(values.begin(), values.end());
132  if (result.bin_centers.isEmpty())
133  result.bin_centers = QVector<double>(centers.begin(), centers.end());
134 #else
135  data.bin_values = QVector<double>::fromStdVector(hist->binValues());
136  if (result.bin_centers.isEmpty())
137  result.bin_centers = QVector<double>::fromStdVector(hist->binCenters());
138 #endif
139 
140  result.projections.push_back(data);
141  }
142  return result;
143 }
static const QString P_POSY
Definition: MaskItems.h:81
std::unique_ptr< Histogram2D > m_hist2d
QVector< SessionItem * > projectionItems(const QString &projectionsType, IntensityDataItem *intensityItem)
Returns vector of ProjectionItems sorted according to axis value.
static const QString P_POSX
Definition: MaskItems.h:73
QVector< double > fromStdVector(const std::vector< double > &data)
Definition: GUIHelpers.cpp:225
double axis_value
value on axis where projection has been made

References SaveProjectionsAssistant::Projection::axis_value, SaveProjectionsAssistant::ProjectionsData::bin_centers, SaveProjectionsAssistant::Projection::bin_values, GUIHelpers::fromStdVector(), SaveProjectionsAssistant::ProjectionsData::is_horizontal, m_hist2d, VerticalLineItem::P_POSX, HorizontalLineItem::P_POSY, projectionItems(), and SaveProjectionsAssistant::ProjectionsData::projections.

Referenced by projectionFileHeader(), and projectionsToString().

Here is the call graph for this function:

◆ projectionsToString()

QString SaveProjectionsAssistant::projectionsToString ( const QString &  projectionsType,
IntensityDataItem intensityItem 
)
private

Generates multi-line string with projections data of given type (horizontal, vertical).

Definition at line 81 of file SaveProjectionsAssistant.cpp.

83 {
84  QString result;
85  QTextStream out(&result);
86 
87  auto projData = projectionsData(projectionsType, intensityItem);
88 
89  if (projData.projections.isEmpty())
90  return result;
91 
92  out << projectionFileHeader(projData);
93 
94  auto bin_centers = projData.bin_centers;
95 
96  for (int i_point = 0; i_point < bin_centers.size(); ++i_point) {
97  out << to_double_str(bin_centers[i_point]) << " ";
98  for (auto& data : projData.projections) {
99  out << to_scientific_str(data.bin_values[i_point]);
100  }
101  out << "\n";
102  }
103  return result;
104 }
QString projectionFileHeader(ProjectionsData &projectionsData)
Returns projections header.

References projectionFileHeader(), and projectionsData().

Referenced by saveProjections().

Here is the call graph for this function:

◆ saveProjections()

void SaveProjectionsAssistant::saveProjections ( QWidget *  parent,
IntensityDataItem intensityItem 
)

Calls file open dialog and writes projection data as ASCII.

Definition at line 49 of file SaveProjectionsAssistant.cpp.

50 {
51  ASSERT(intensityItem);
52 
53  QString defaultName = ProjectUtils::userExportDir() + "/untitled.txt";
54  QString fileName = QFileDialog::getSaveFileName(parent, "Save projections data", defaultName);
55 
56  if (fileName.isEmpty())
57  return;
58 
59  QFile file(fileName);
60  if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
61  throw GUIHelpers::Error("TestProjectUtils::createTestFile() -> Error. "
62  "Can't create file");
63 
64  m_hist2d = std::make_unique<Histogram2D>(*intensityItem->getOutputData());
65 
66  QTextStream out(&file);
67 
68  out << "# Projections along x-axis (horizontal projections) \n";
69  out << projectionsToString("HorizontalLineMask", intensityItem);
70  out << "\n";
71 
72  out << "# Projections along y-axis (vertical projections) \n";
73  out << projectionsToString("VerticalLineMask", intensityItem);
74  out << "\n";
75 
76  file.close();
77 }
#define ASSERT(condition)
Definition: Assert.h:31
OutputData< double > * getOutputData()
Definition: DataItem.h:36
QString projectionsToString(const QString &projectionsType, IntensityDataItem *intensityItem)
Generates multi-line string with projections data of given type (horizontal, vertical).
QString userExportDir()
Returns directory which user is normally using to export files.

References ASSERT, DataItem::getOutputData(), m_hist2d, projectionsToString(), and ProjectUtils::userExportDir().

Referenced by ProjectionsEditorActions::onSaveAction().

Here is the call graph for this function:

Member Data Documentation

◆ m_hist2d

std::unique_ptr<Histogram2D> SaveProjectionsAssistant::m_hist2d
private

Definition at line 50 of file SaveProjectionsAssistant.h.

Referenced by projectionsData(), and saveProjections().


The documentation for this class was generated from the following files: