BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
QREDataLoader.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Loaders/QREDataLoader.h
6 //! @brief Defines class QREDataLoader
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef BORNAGAIN_GUI_VIEW_LOADERS_QREDATALOADER_H
16 #define BORNAGAIN_GUI_VIEW_LOADERS_QREDATALOADER_H
17 
19 #include <QVector>
20 #include <variant>
21 
22 class QString;
24 
25 //! Loader for experimental reflectometry CSV files that contain
26 //! three columns with Q,R,E, namely
27 //! Q = wavenumber, R = reflectivity, E = error of R.
28 
30 public:
31  QREDataLoader();
32  QString name() const override;
33  QString persistentClassName() const override;
34  void populateImportSettingsWidget(QWidget* parent) override;
35  void initWithDefaultImportSettings() override;
36  void applyImportSettings() override;
37  QByteArray serialize() const override;
38  void deserialize(const QByteArray& data) override;
39  AbstractDataLoader* clone() const override;
40  QByteArray fileContent() const override;
41  void setFileContents(const QByteArray& fileContent) override;
42  void guessSettings() override;
43  void processContents() override;
44  int numErrors() const override;
45  int numLineRelatedErrors() const override;
46  QStringList lineUnrelatedErrors() const override;
48 
49 private:
50  void parseFileContent() const;
51  void calculateFromParseResult() const;
52  void datafieldFromParsingResult(RealDataItem* item) const;
53 
54 private:
56 
58  bool enabled; //!< shall data type be used
59  int column; //!< read the value from this raw column
60  UnitInFile unit; //!< the unit of the data in the file
61  double factor; //!< in case the raw data shall be multiplied
62 
63  bool operator==(const ColumnDefinition& other) const;
64  };
65 
66  enum class DataType { Q, R, dR };
67 
68  //! Settings for importing the file
69  struct ImportSettings {
70  QString separator; //!< column separator
71  QString headerPrefix; //!< prefix denoting header line
72  QString linesToSkip; //!< pattern denoting line to skip (i.e. '1,10-12,42')
73  bool allowValuesGreaterOne = true; //!< always true for now (see ##94); could
74  //!< be made configurable later on
75  QMap<DataType, ColumnDefinition> columnDefinitions;
76 
77  bool operator!=(const ImportSettings& other) const;
78  QByteArray serialize() const;
79  void deserialize(const QByteArray& data);
81 
82  //! Contains a line related error (stored in the import result). Used for showing
83  //! line related errors in the import data table.
84  struct ErrorDefinition {
85  // Attention: numbers are serialized! Do not change them!
86  enum Type {
87  none = 0,
91  RLessZero = 4
92  };
93 
94  ErrorDefinition(Type t = none, int d = 0);
95  ErrorDefinition(Type t, double d);
96 
97  //! Human readable error text
98  QString toString() const;
99 
101  std::variant<int, double> data; //!< Additional data; meaning depends on the error type (see
102  //!< implementation of toString() for more information)
103  };
104 
105  //! Result of the file import. Some of the contained data is only relevant for showing the
106  //! results in the result table. The usage of vectors which cover also invalid or skipped
107  //! lines has its reason also in this result showing (to improve presentation performance).
108  struct ImportResult {
109  void clear();
110  void clearCalculatedValues();
111  void addError(int line, ErrorDefinition::Type type, int data = 0);
112  void addError(int line, ErrorDefinition::Type type, double data);
113  QString errorText(int line) const;
114 
115  QVector<QPair<bool, QString>> lines; //!< bool describes whether line is skipped
116  //!< index is 0-based line number
117  QVector<QVector<double>> rawValues; //!< index is 0-based line number
118  QVector<double> qValues; //!< index is 0-based line number
119  QVector<double> rValues; //!< index is 0-based line number
120  QVector<double> eValues; //!< index is 0-based line number
121  int validCalculatedLines; //!< number of valid resulting data rows
122  int maxColumnCount; //!< max found columns in raw data
123  QMap<int, ErrorDefinition>
124  calculationErrors; //!< calculation error per line; line is 0-based
125  QString error; //!< error unrelated to lines
126  ImportSettings importSettings; //!< Settings used for the import
127  };
129  QByteArray m_fileContent;
130 
131  QPointer<QREDataLoaderProperties> m_propertiesWidget;
132 
133  friend QDataStream& operator<<(QDataStream& stream, const QREDataLoader::ImportSettings& s);
134  friend QDataStream& operator>>(QDataStream& stream, QREDataLoader::ImportSettings& s);
135  friend QDataStream& operator<<(QDataStream& stream, const QREDataLoader::ErrorDefinition& s);
136  friend QDataStream& operator>>(QDataStream& stream, QREDataLoader::ErrorDefinition& s);
138 };
139 
140 QDataStream& operator<<(QDataStream& stream, const QREDataLoader::ImportSettings& s);
141 QDataStream& operator>>(QDataStream& stream, QREDataLoader::ImportSettings& s);
142 
143 #endif // BORNAGAIN_GUI_VIEW_LOADERS_QREDATALOADER_H
Defines class AbstractDataLoader1D.
QDataStream & operator<<(QDataStream &stream, const QREDataLoader::ImportSettings &s)
QDataStream & operator>>(QDataStream &stream, QREDataLoader::ImportSettings &s)
Abstract base class for reflectometry data loaders.
Base class for result tables of data loaders. Derive from this class and return an instance in YourDa...
Abstract base class for all data loaders (classes to import real data).
Properties widget for the QREDataLoader.
The result model of a QREDataLoader (for showing the import results in a table view).
Loader for experimental reflectometry CSV files that contain three columns with Q,...
Definition: QREDataLoader.h:29
QByteArray fileContent() const override
Returns the original file content. If not available any more (like for legacy project file import),...
QString persistentClassName() const override
A name which can be used for save/load purposes (which will not change ever more)
friend QDataStream & operator<<(QDataStream &stream, const QREDataLoader::ImportSettings &s)
void guessSettings() override
Guess appropriate settings (for example the separator in a CSV file). Is called only once,...
int numLineRelatedErrors() const override
Number of errors related to a specific line. Such an error means that a particular content (line) can...
int numErrors() const override
Number of errors found while processing the content. An error means that either a particular content ...
void initWithDefaultImportSettings() override
Set import settings to defaults.
void setFileContents(const QByteArray &fileContent) override
Sets the file contents to be imported. If the file was a compressed file, then the decompressed conte...
QByteArray serialize() const override
Returns every internal setting so it can be restored completely.
void calculateFromParseResult() const
void deserialize(const QByteArray &data) override
Initialize from serialization data. If any error occurred, then a DeserializationException has to be ...
void applyImportSettings() override
Read all values from the properties UI into the internal variables.
QByteArray m_fileContent
AbstractDataLoader * clone() const override
Create a complete clone, including all internal states.
AbstractDataLoaderResultModel * createResultModel() const override
Create a table model which contains the import information like original file content,...
void datafieldFromParsingResult(RealDataItem *item) const
void populateImportSettingsWidget(QWidget *parent) override
Fills the widget on the import dialog pane. The implementation here in the base class does nothing (m...
QStringList lineUnrelatedErrors() const override
Errors not related to a particular line.
QString name() const override
The name shown in the format selection combo.
QPointer< QREDataLoaderProperties > m_propertiesWidget
struct QREDataLoader::ImportSettings m_importSettings
ImportResult m_importResult
void parseFileContent() const
void processContents() override
Process the file contents. Can be called more than once, e.g. if the import settings have changed....
friend QDataStream & operator>>(QDataStream &stream, QREDataLoader::ImportSettings &s)
Provides access to experimental data, for display and fitting. Owns an AbstractDataLoader.
Definition: RealDataItem.h:33
UnitInFile unit
the unit of the data in the file
Definition: QREDataLoader.h:60
double factor
in case the raw data shall be multiplied
Definition: QREDataLoader.h:61
bool enabled
shall data type be used
Definition: QREDataLoader.h:58
bool operator==(const ColumnDefinition &other) const
int column
read the value from this raw column
Definition: QREDataLoader.h:59
Contains a line related error (stored in the import result). Used for showing line related errors in ...
Definition: QREDataLoader.h:84
std::variant< int, double > data
Additional data; meaning depends on the error type (see implementation of toString() for more informa...
ErrorDefinition(Type t=none, int d=0)
QString toString() const
Human readable error text.
Result of the file import. Some of the contained data is only relevant for showing the results in the...
QVector< double > rValues
index is 0-based line number
int maxColumnCount
max found columns in raw data
QString error
error unrelated to lines
void addError(int line, ErrorDefinition::Type type, int data=0)
int validCalculatedLines
number of valid resulting data rows
QVector< QVector< double > > rawValues
index is 0-based line number
QMap< int, ErrorDefinition > calculationErrors
calculation error per line; line is 0-based
QVector< double > eValues
index is 0-based line number
QString errorText(int line) const
ImportSettings importSettings
Settings used for the import.
QVector< double > qValues
index is 0-based line number
QVector< QPair< bool, QString > > lines
bool describes whether line is skipped index is 0-based line number
Settings for importing the file.
Definition: QREDataLoader.h:69
QString separator
column separator
Definition: QREDataLoader.h:70
void deserialize(const QByteArray &data)
bool allowValuesGreaterOne
always true for now (see ##94); could be made configurable later on
Definition: QREDataLoader.h:73
QMap< DataType, ColumnDefinition > columnDefinitions
Definition: QREDataLoader.h:75
QString headerPrefix
prefix denoting header line
Definition: QREDataLoader.h:71
bool operator!=(const ImportSettings &other) const
QString linesToSkip
pattern denoting line to skip (i.e. '1,10-12,42')
Definition: QREDataLoader.h:72