BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
gui2::Utils Namespace Reference

Collection of utility functions for running quick simulations. More...

Functions

DAREFLCORE_EXPORT std::string AddHtmlBackgroundTag (const std::string &line, const std::string &color)
 Returns string representing original 'line' wrapped in 'div' tag. More...
 
DAREFLCORE_EXPORT std::string AddHtmlColorTag (const std::string &line, const std::string &color)
 Returns string representing original 'line' wrapped in html color tag. More...
 
DAREFLCORE_EXPORT std::string AddHtmlColorTagToParts (const std::string &line, const std::vector< std::string > &parts, const std::string &color_parts, const std::string &color_rest)
 Returns string representing original 'line', where 'parts' are surrounded with color tag. More...
 
DAREFLCORE_EXPORT std::string AddHtmlDivTag (const std::string &line)
 Returns string representing original 'line' wrapped in 'div' tag. More...
 
DAREFLCORE_EXPORT std::vector< SlicecreateBornAgainSlices (const multislice_t &multislice)
 
DAREFLCORE_EXPORT GraphImportData CreateData (const std::vector< std::vector< std::string >> &text_data, const ColumnInfo &axis, const ColumnInfo &intensity)
 Creates structure from text data. More...
 
DAREFLCORE_EXPORT std::vector< double > CreateDiffVector (const std::vector< double > &a, const std::vector< double > &b)
 Returns vector representing elementwise 2*(a-b)/(a+b) difference over two vectors. More...
 
DAREFLCORE_EXPORT std::vector< std::pair< ColumnInfo, ColumnInfo > > CreateGraphInfoPairs (const std::vector< ColumnInfo > &column_info)
 Pack ColumnInfo into pairs representing {AxisType, IntensityType}. More...
 
DAREFLCORE_EXPORT std::vector< ModelView::ExternalPropertyCreateGraphProperties (ExperimentalDataModel *model)
 Returns vector of properties representing GraphItem content of the model. More...
 
DAREFLCORE_EXPORT accept_int_t CreateLineNumberPatternValidator (const std::string &pattern)
 Creates a callback to define if given line number satisfies line number pattern. More...
 
DAREFLCORE_EXPORT accept_string_t CreateLinePrefixValidator (const std::string &prefix_to_exclude)
 Creates a callback to define if given line has a valid content for further parsing. More...
 
DAREFLCORE_EXPORT multislice_t CreateMultiSlice (const MultiLayerItem &multilayer)
 Creates multi-slice presentation of internal multilayer structure. More...
 
DAREFLCORE_EXPORT ModelView::ExternalProperty CreateProperty (const ModelView::GraphItem *graph)
 Returns property representing given graph. More...
 
DAREFLCORE_EXPORT line_splitter_t CreateSeparatorBasedSplitter (const std::string &separator)
 Creates line splitter based on separator. More...
 
DAREFLCORE_EXPORT std::vector< std::pair< int, int > > ExpandLineNumberPattern (const std::string &pattern)
 Expands string representing line number pattern to inclusive pairs of line numbers. More...
 
DAREFLCORE_EXPORT std::pair< std::vector< double >, std::vector< double > > ExtractTwoColumns (const std::vector< std::vector< std::string >> &text_data, size_t col1, size_t col2)
 Extracts double values from two columns of a string array. More...
 
DAREFLCORE_EXPORT ModelView::ExternalProperty FindProperty (const std::vector< ModelView::ExternalProperty > &properties, const std::string &id)
 Finds the property with the same id in given vector and returns it. More...
 
DAREFLCORE_EXPORT std::vector< std::string > LoadASCIIFile (const std::string &file_name)
 Loads ASCII file, returns it in the form of vector of strings. More...
 
DAREFLCORE_EXPORT void SetDifference (const ModelView::Data1DItem *data1, const ModelView::Data1DItem *data2, ModelView::Data1DItem *target)
 Make target item represent difference of two Data1DItems. Target will get an axis as in data1. More...
 

Detailed Description

Collection of utility functions for running quick simulations.

Function Documentation

◆ AddHtmlBackgroundTag()

std::string gui2::Utils::AddHtmlBackgroundTag ( const std::string &  line,
const std::string &  color 
)

Returns string representing original 'line' wrapped in 'div' tag.

Definition at line 136 of file dataloader_utils.cpp.

137 {
138  const std::string open_tag = "<span style=\"background-color:" + color + "\">";
139  const std::string close_tag = "</span>";
140  std::string result;
141  return open_tag + line + close_tag;
142 }

Referenced by AddHtmlColorTagToParts().

◆ AddHtmlColorTag()

std::string gui2::Utils::AddHtmlColorTag ( const std::string &  line,
const std::string &  color 
)

Returns string representing original 'line' wrapped in html color tag.

Definition at line 128 of file dataloader_utils.cpp.

129 {
130  const std::string open_tag = "<font color=\"" + color + "\">";
131  const std::string close_tag = "</font>";
132  std::string result;
133  return open_tag + line + close_tag;
134 }

Referenced by AddHtmlColorTagToParts().

◆ AddHtmlColorTagToParts()

std::string gui2::Utils::AddHtmlColorTagToParts ( const std::string &  line,
const std::vector< std::string > &  parts,
const std::string &  color_parts,
const std::string &  color_rest 
)

Returns string representing original 'line', where 'parts' are surrounded with color tag.

Definition at line 144 of file dataloader_utils.cpp.

148 {
149  std::string result;
150  std::string_view view(line);
151 
152  if (parts.empty())
153  return AddHtmlDivTag(AddHtmlColorTag(line, color_rest));
154 
155  for (auto part : parts) {
156  auto it = view.find_first_of(part);
157  if (it > 0)
158  result.append(AddHtmlBackgroundTag(std::string(view.substr(0, it)), color_rest));
159  result.append(AddHtmlColorTag(part, color_parts));
160  view.remove_prefix(it + part.size());
161  }
162  return AddHtmlDivTag(result);
163 }
DAREFLCORE_EXPORT std::string AddHtmlDivTag(const std::string &line)
Returns string representing original 'line' wrapped in 'div' tag.
DAREFLCORE_EXPORT std::string AddHtmlColorTag(const std::string &line, const std::string &color)
Returns string representing original 'line' wrapped in html color tag.
DAREFLCORE_EXPORT std::string AddHtmlBackgroundTag(const std::string &line, const std::string &color)
Returns string representing original 'line' wrapped in 'div' tag.

References AddHtmlBackgroundTag(), AddHtmlColorTag(), and AddHtmlDivTag().

Referenced by gui2::LoaderPreviewPanel::showData().

Here is the call graph for this function:

◆ AddHtmlDivTag()

std::string gui2::Utils::AddHtmlDivTag ( const std::string &  line)

Returns string representing original 'line' wrapped in 'div' tag.

Definition at line 120 of file dataloader_utils.cpp.

121 {
122  const std::string open_div = "<div>";
123  const std::string close_div = "</div>";
124  std::string result;
125  return open_div + line + close_div;
126 }

Referenced by AddHtmlColorTagToParts().

◆ createBornAgainSlices()

std::vector< Slice > gui2::Utils::createBornAgainSlices ( const multislice_t multislice)

Definition at line 74 of file quicksimutils.cpp.

75 {
76  std::vector<Slice> result;
77  result.reserve(multislice.size());
78 
79  for (auto& slice : multislice) {
80  auto material = MaterialBySLD("", slice.material.real(), slice.material.imag());
81  auto roughness = LayerRoughness(slice.sigma, 0., 0.);
82 
83  result.emplace_back(slice.thickness, material, roughness);
84  }
85 
86  return result;
87 }
A roughness of interface between two layers.
Material MaterialBySLD()

References MaterialBySLD().

Referenced by gui2::MaterialProfile::CalculateProfile(), gui2::MaterialProfile::DefaultMaterialProfileLimits(), and gui2::SpecularToySimulation::runSimulation().

Here is the call graph for this function:

◆ CreateData()

GraphImportData gui2::Utils::CreateData ( const std::vector< std::vector< std::string >> &  text_data,
const ColumnInfo axis,
const ColumnInfo intensity 
)

Creates structure from text data.

Definition at line 201 of file dataloader_utils.cpp.

203 {
204  GraphImportData result;
205 
206  auto [axis_values, intensity_values] =
207  Utils::ExtractTwoColumns(text_data, axis.column, intensity.column);
208 
209  std::transform(intensity_values.begin(), intensity_values.end(), intensity_values.begin(),
210  [&intensity](auto x) { return x * intensity.multiplier; });
211 
212  result.bin_centers = axis_values;
213  result.axis_units = axis.units;
214 
215  result.bin_values = intensity_values;
216  result.signal_units = intensity.units;
217 
218  return result;
219 }
DAREFLCORE_EXPORT std::pair< std::vector< double >, std::vector< double > > ExtractTwoColumns(const std::vector< std::vector< std::string >> &text_data, size_t col1, size_t col2)
Extracts double values from two columns of a string array.

References gui2::GraphImportData::axis_units, gui2::GraphImportData::bin_centers, gui2::GraphImportData::bin_values, gui2::ColumnInfo::column, ExtractTwoColumns(), gui2::GraphImportData::signal_units, and gui2::ColumnInfo::units.

Referenced by gui2::DataLoaderDialog::onParseAllRequest().

Here is the call graph for this function:

◆ CreateDiffVector()

std::vector< double > gui2::Utils::CreateDiffVector ( const std::vector< double > &  a,
const std::vector< double > &  b 
)

Returns vector representing elementwise 2*(a-b)/(a+b) difference over two vectors.

Resulting vector will have size equal to min(a.size(), b.size())

Definition at line 66 of file modelutils.cpp.

68 {
69  size_t length = std::min(a.size(), b.size());
70  std::vector<double> result(length, 0.0);
71  for (size_t i = 0; i < length; ++i) {
72  double denom = a[i] + b[i];
73  result[i] = denom != 0.0 ? 2 * (a[i] - b[i]) / (a[i] + b[i]) : 0.0;
74  }
75  return result;
76 }

Referenced by SetDifference().

◆ CreateGraphInfoPairs()

std::vector< std::pair< ColumnInfo, ColumnInfo > > gui2::Utils::CreateGraphInfoPairs ( const std::vector< ColumnInfo > &  column_info)

Pack ColumnInfo into pairs representing {AxisType, IntensityType}.

For the moment we expect that only one column with AxisType exists. Number of intensity columns can be arbitrary.

Definition at line 185 of file dataloader_utils.cpp.

186 {
187  std::vector<std::pair<ColumnInfo, ColumnInfo>> result;
188 
189  auto axis_columns = columnsForType(column_info, Constants::AxisType);
190  auto intensity_columns = columnsForType(column_info, Constants::IntensityType);
191 
192  if (axis_columns.size() != 1)
193  throw std::runtime_error("There must be exactly one column with AxisType selected.");
194 
195  for (const auto& intensity_info : intensity_columns)
196  result.push_back(std::make_pair(axis_columns.back(), intensity_info));
197 
198  return result;
199 }
const std::string IntensityType
const std::string AxisType

References gui2::Constants::AxisType, and gui2::Constants::IntensityType.

Referenced by gui2::DataLoaderDialog::onParseAllRequest().

◆ CreateGraphProperties()

std::vector< ModelView::ExternalProperty > gui2::Utils::CreateGraphProperties ( ExperimentalDataModel model)

Returns vector of properties representing GraphItem content of the model.

Definition at line 46 of file modelutils.cpp.

47 {
48  std::vector<ModelView::ExternalProperty> result;
49  for (auto graph : ModelView::Utils::FindItems<ModelView::GraphItem>(model))
50  result.push_back(Utils::CreateProperty(graph));
51  return result;
52 }
DAREFLCORE_EXPORT ModelView::ExternalProperty CreateProperty(const ModelView::GraphItem *graph)
Returns property representing given graph.
Definition: modelutils.cpp:39

References CreateProperty().

Referenced by gui2::ExperimentalDataController::update_all().

Here is the call graph for this function:

◆ CreateLineNumberPatternValidator()

accept_int_t gui2::Utils::CreateLineNumberPatternValidator ( const std::string &  pattern)

Creates a callback to define if given line number satisfies line number pattern.

"1, 4-6" will accept numbers {1, 4, 5, 6} and will refuse all others.

Definition at line 79 of file dataloader_utils.cpp.

80 {
81  std::vector<std::pair<int, int>> expanded_pattern = Utils::ExpandLineNumberPattern(pattern);
82  auto result = [expanded_pattern](int line_number) {
83  for (auto pair : expanded_pattern) {
84  if (line_number >= pair.first && line_number <= pair.second)
85  return true;
86  }
87  return false;
88  };
89  return result;
90 }
DAREFLCORE_EXPORT std::vector< std::pair< int, int > > ExpandLineNumberPattern(const std::string &pattern)
Expands string representing line number pattern to inclusive pairs of line numbers.

References ExpandLineNumberPattern().

Referenced by gui2::DefaultParser::DefaultParser().

Here is the call graph for this function:

◆ CreateLinePrefixValidator()

accept_string_t gui2::Utils::CreateLinePrefixValidator ( const std::string &  prefix_to_exclude)

Creates a callback to define if given line has a valid content for further parsing.

Empty lines and lines starting from a given prefix will be excluded.

Definition at line 92 of file dataloader_utils.cpp.

93 {
94  auto result = [prefix_to_exclude](const std::string& line) {
95  // line contains spaces only
96  if (line.empty() || line.find_first_not_of(' ') == std::string::npos)
97  return false;
98  // line starts from pattern
99  return line.find_first_of(prefix_to_exclude) == 0 ? false : true;
100  };
101  return result;
102 }

Referenced by gui2::DefaultParser::DefaultParser().

◆ CreateMultiSlice()

multislice_t gui2::Utils::CreateMultiSlice ( const MultiLayerItem multilayer)

Creates multi-slice presentation of internal multilayer structure.

Definition at line 67 of file quicksimutils.cpp.

68 {
69  multislice_t result;
70  AddToMultiSlice(result, multilayer);
71  return result;
72 }
std::vector< SliceData > multislice_t

Referenced by gui2::QuickSimController::process_multilayer().

◆ CreateProperty()

ModelView::ExternalProperty gui2::Utils::CreateProperty ( const ModelView::GraphItem graph)

Returns property representing given graph.

Used to link with the graph from various editors.

Definition at line 39 of file modelutils.cpp.

40 {
41  std::string name = graph->parent()->displayName() + "/" + graph->displayName();
42  auto colorName = QString::fromStdString(graph->colorName());
43  return ModelView::ExternalProperty(name, QColor(colorName), graph->identifier());
44 }
std::string displayName() const override
Returns display name.
Property to carry text, color and identifier.
std::string colorName() const
Returns color name in #RRGGBB format.
Definition: graphitem.cpp:76
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
virtual std::string displayName() const
Returns display name.
Definition: sessionitem.cpp:94
QString const & name(EShape k)
Definition: particles.cpp:21

References ModelView::GraphItem::colorName(), ModelView::SessionItem::displayName(), ModelView::CompoundItem::displayName(), ModelView::SessionItem::identifier(), RealSpace::Particles::name(), and ModelView::SessionItem::parent().

Referenced by CreateGraphProperties(), and gui2::ExperimentalScanItem::setGraphItem().

Here is the call graph for this function:

◆ CreateSeparatorBasedSplitter()

line_splitter_t gui2::Utils::CreateSeparatorBasedSplitter ( const std::string &  separator)

Creates line splitter based on separator.

Definition at line 104 of file dataloader_utils.cpp.

105 {
106  if (separator.empty())
107  throw std::runtime_error("Error, empty separator.");
108 
109  bool is_space_only_separator = separator.find_first_not_of(' ') == std::string::npos;
110  auto result = [separator, is_space_only_separator](const std::string& line) {
111  std::vector<std::string> values;
112  std::string trimmed = ModelView::Utils::TrimWhitespace(line);
113  if (is_space_only_separator)
114  trimmed = ModelView::Utils::RemoveRepeatedSpaces(trimmed);
115  return ModelView::Utils::SplitString(trimmed, separator);
116  };
117  return result;
118 }
MVVM_MODEL_EXPORT std::string RemoveRepeatedSpaces(std::string str)
Removes repeating spaces for a string.
MVVM_MODEL_EXPORT std::string TrimWhitespace(const std::string &str)
Returns string after trimming whitespace surrounding, including tabs and carriage returns.
MVVM_MODEL_EXPORT std::vector< std::string > SplitString(const std::string &str, const std::string &delimeter)
Split string on substring using given delimeter. Reproduces Python's str.split() behavior.

References ModelView ::Utils::RemoveRepeatedSpaces(), ModelView ::Utils::SplitString(), and ModelView ::Utils::TrimWhitespace().

Referenced by gui2::DefaultParser::DefaultParser().

Here is the call graph for this function:

◆ ExpandLineNumberPattern()

std::vector< std::pair< int, int > > gui2::Utils::ExpandLineNumberPattern ( const std::string &  pattern)

Expands string representing line number pattern to inclusive pairs of line numbers.

"1" will be expanded to { {1, 1} }, "1, 3-5" will be expanded to { {1, 1}, {3, 5} }

Definition at line 58 of file dataloader_utils.cpp.

59 {
60  std::vector<std::pair<int, int>> result;
61 
62  // splitting "1, 2-3" first on comma-separated tokens
63  for (const auto& token : ModelView::Utils::SplitString(pattern, ",")) {
64  auto parts = ModelView::Utils::SplitString(token, "-");
65  // splitting on dash-separared tokens
66  if (!parts.empty()) {
67  // if no "-" is present, make from "1" a pair {1, 1}
68  // if "-" is present, make from "1-2" a pair {1,2}
69  auto conv0 = ModelView::Utils::StringToInteger(parts[0]);
70  auto conv1 = parts.size() > 1 ? ModelView::Utils::StringToInteger(parts[1]) : conv0;
71  if (isRepresentRange(conv0, conv1))
72  result.push_back({conv0.value(), conv1.value()});
73  }
74  }
75 
76  return result;
77 }
MVVM_MODEL_EXPORT std::optional< int > StringToInteger(const std::string &str)
Converts string to integer.

References ModelView ::Utils::SplitString(), and ModelView ::Utils::StringToInteger().

Referenced by CreateLineNumberPatternValidator().

Here is the call graph for this function:

◆ ExtractTwoColumns()

std::pair< std::vector< double >, std::vector< double > > gui2::Utils::ExtractTwoColumns ( const std::vector< std::vector< std::string >> &  text_data,
size_t  col1,
size_t  col2 
)

Extracts double values from two columns of a string array.

The row must be valid: string value must represent single double for both target columns, rows should have enough columns. If a row is invalid, it will be skipped, so resulting arrays have always the same length.

Definition at line 166 of file dataloader_utils.cpp.

168 {
169  std::vector<double> vec1, vec2;
170  for (const auto& row : text_data) {
171  if (col1 < row.size() && col2 < row.size()) {
172  auto val1 = ModelView::Utils::StringToDouble(row[col1]);
173  auto val2 = ModelView::Utils::StringToDouble(row[col2]);
174  if (val1.has_value() && val2.has_value()) {
175  vec1.push_back(val1.value());
176  vec2.push_back(val2.value());
177  }
178  }
179  }
180 
181  return std::make_pair(std::move(vec1), std::move(vec2));
182 }
MVVM_MODEL_EXPORT std::optional< double > StringToDouble(const std::string &str)
Converts string to double value using classc locale and returns it in the form of optional.

References ModelView ::Utils::StringToDouble().

Referenced by CreateData().

Here is the call graph for this function:

◆ FindProperty()

ModelView::ExternalProperty gui2::Utils::FindProperty ( const std::vector< ModelView::ExternalProperty > &  properties,
const std::string &  id 
)

Finds the property with the same id in given vector and returns it.

Definition at line 56 of file modelutils.cpp.

58 {
59  for (const auto& prop : properties)
60  if (prop.identifier() == id)
61  return prop;
62 
64 }
static ExternalProperty undefined()

References ModelView::ExternalProperty::undefined().

Referenced by gui2::ExperimentalDataController::update_all().

Here is the call graph for this function:

◆ LoadASCIIFile()

std::vector< std::string > gui2::Utils::LoadASCIIFile ( const std::string &  file_name)

Loads ASCII file, returns it in the form of vector of strings.

Definition at line 46 of file dataloader_utils.cpp.

47 {
48  std::vector<std::string> result;
49 
50  std::ifstream file(file_name);
51  if (!file.is_open())
52  throw std::ios_base::failure("Unable to open file '" + file_name + "'");
53  for (std::string line; getline(file, line);)
54  result.emplace_back(line);
55  return result;
56 }

Referenced by gui2::DataHandler::loadFile().

◆ SetDifference()

void gui2::Utils::SetDifference ( const ModelView::Data1DItem data1,
const ModelView::Data1DItem data2,
ModelView::Data1DItem target 
)

Make target item represent difference of two Data1DItems. Target will get an axis as in data1.

Definition at line 78 of file modelutils.cpp.

80 {
81  if (!areCompatibleAxes(*data1, *data2) || !areCompatibleAxes(*data1, *target))
82  return;
83 
84  // We expect same number of points to caclulate the difference graph.
85  if (data1->binCenters().size() != data2->binCenters().size())
86  return;
87 
88  if (data1->binCenters() != target->binCenters()) {
90  ->setParameters(data1->binCenters());
91  }
92  target->setValues(CreateDiffVector(data1->binValues(), data2->binValues()));
93 }
static const std::string T_AXIS
Definition: data1ditem.h:34
std::vector< double > binValues() const
Returns values stored in bins.
Definition: data1ditem.cpp:76
void setValues(const std::vector< double > &data)
Sets internal data buffer to given data.
Definition: data1ditem.cpp:66
std::vector< double > binCenters() const
Sets axis. Bin content will be set to zero.
Definition: data1ditem.cpp:57
Item to represent pointwise axis.
Definition: axisitems.h:94
T * item(const std::string &tag) const
Returns first item under given tag casted to a specified type.
Definition: sessionitem.h:156
DAREFLCORE_EXPORT std::vector< double > CreateDiffVector(const std::vector< double > &a, const std::vector< double > &b)
Returns vector representing elementwise 2*(a-b)/(a+b) difference over two vectors.
Definition: modelutils.cpp:66

References ModelView::Data1DItem::binCenters(), ModelView::Data1DItem::binValues(), CreateDiffVector(), ModelView::SessionItem::item(), ModelView::Data1DItem::setValues(), and ModelView::Data1DItem::T_AXIS.

Referenced by gui2::JobItem::updateDifferenceData().

Here is the call graph for this function: