BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ImportDataUtils Namespace Reference

Provides utility methods to import data files. More...

Functions

bool Compatible (const InstrumentItem &instrumentItem, const RealDataItem &realDataItem)
 Check whether data item is compatible with instrument (same rank) More...
 
std::unique_ptr< OutputData< double > > CreateSimplifiedOutputData (const OutputData< double > &data)
 Creates OutputData with bin-valued axes. More...
 
QString Import1dData (RealDataItem *realDataItem, const AbstractDataLoader *selectedLoader)
 Import 1D data into the given item. More...
 
std::unique_ptr< OutputData< double > > Import2dData (const QString &baseNameOfLoadedFile)
 
std::unique_ptr< OutputData< double > > ImportKnownData (const QString &baseNameOfLoadedFile)
 
std::unique_ptr< OutputData< double > > ImportReflectometryData (const QString &baseNameOfLoadedFile)
 
QString printShapeMessage (const std::vector< int > &instrument_shape, const std::vector< int > &data_shape)
 Composes a message with the shapes of InstrumentItem and RealDataItem. More...
 

Detailed Description

Provides utility methods to import data files.

Function Documentation

◆ Compatible()

bool ImportDataUtils::Compatible ( const InstrumentItem instrumentItem,
const RealDataItem realDataItem 
)

Check whether data item is compatible with instrument (same rank)

Definition at line 157 of file ImportDataUtils.cpp.

159 {
160  return rank(instrumentItem) == rank(realDataItem);
161 }

Referenced by LinkInstrumentManager::canLinkDataToInstrument().

◆ CreateSimplifiedOutputData()

std::unique_ptr< OutputData< double > > ImportDataUtils::CreateSimplifiedOutputData ( const OutputData< double > &  data)

Creates OutputData with bin-valued axes.

Definition at line 164 of file ImportDataUtils.cpp.

165 {
166  const size_t data_rank = data.rank();
167  if (data_rank > 2 || data_rank < 1)
168  throw std::runtime_error("Error in ImportDataUtils::CreateSimplifiedOutputData: passed "
169  "array is neither 1D nor 2D");
170 
171  std::unique_ptr<OutputData<double>> result(new OutputData<double>);
172  for (size_t i = 0; i < data_rank; ++i) {
173  const IAxis& axis = data.axis(i);
174  const size_t axis_size = axis.size();
175  const double min = 0.0;
176  const double max = axis_size;
177  result->addAxis(FixedBinAxis(axis.getName(), axis_size, min, max));
178  }
179  result->setRawDataVector(data.getRawDataVector());
180 
181  return result;
182 }
Axis with fixed bin size.
Definition: FixedBinAxis.h:23
Interface for one-dimensional axes.
Definition: IAxis.h:25
virtual size_t size() const =0
retrieve the number of bins
std::string getName() const
retrieve the label of the axis
Definition: IAxis.h:40
size_t rank() const
Returns number of dimensions.
Definition: OutputData.h:56
std::vector< T > getRawDataVector() const
Returns copy of raw data vector.
Definition: OutputData.h:334
const IAxis & axis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:318

References OutputData< T >::axis(), IAxis::getName(), OutputData< T >::getRawDataVector(), OutputData< T >::rank(), and IAxis::size().

Referenced by ImportKnownData().

Here is the call graph for this function:

◆ Import1dData()

QString ImportDataUtils::Import1dData ( RealDataItem realDataItem,
const AbstractDataLoader selectedLoader 
)

Import 1D data into the given item.

Returns an error text if a fatal error occurred (discard item). "Empty string" means "no fatal error" => imported item should be kept. The imported item still can have errors, but they might be solvable by different import settings or by using a different data loader. selectedLoader is the one which was selected in the open-file-dialog (or null if none selected).

Definition at line 92 of file ImportDataUtils.cpp.

94 {
95  const QString fileName = realDataItem->nativeFileName();
96  const std::string fileNameStdString = fileName.toStdString();
97 
98  if (selectedLoader == nullptr) {
99  if (DataFormatUtils::isCompressed(fileNameStdString)
100  || DataFormatUtils::isIntFile(fileNameStdString)
101  || DataFormatUtils::isTiffFile(fileNameStdString)) {
102  try {
103  ImportDataInfo data(ImportKnownData(fileName), Axes::Units::QSPACE);
104  if (data) {
105  realDataItem->setImportData(std::move(data));
106  return QString();
107  }
108  } catch (std::exception& ex) {
109  // If it is not tiff but e.g. dat.gz, it could be tried with CSV import
110  const bool tryWithLoaders =
111  (DataFormatUtils::isIntFile(fileNameStdString)
113  fileNameStdString)); // #baimport support compressed
114 
115  if (!tryWithLoaders)
116  // import is not possible
117  return QString::fromLatin1(ex.what());
118  }
119  }
120  }
121 
122  // -- try with selected CSV loader. If none selected, try with QRE loader
123 
124  QFile file(fileName);
125  const bool fileCouldBeOpened = file.open(QFile::ReadOnly | QIODevice::Text);
126 
127  if (!fileCouldBeOpened)
128  return "File could not be opened.";
129 
130  QByteArray fileContent = file.readAll();
131  file.close();
132 
133  if (fileContent.isEmpty())
134  return "The imported file is empty.";
135 
136  if (DataFormatUtils::isCompressed(fileNameStdString)) {
137  // #baimport implement decompress
138  }
139 
140  AbstractDataLoader* loader = nullptr;
141  if (selectedLoader == nullptr)
142  loader = new QREDataLoader();
143  else
144  loader = selectedLoader->clone();
146  loader->setRealDataItem(realDataItem);
147  realDataItem->setDataLoader(loader);
148  QApplication::setOverrideCursor(Qt::WaitCursor);
149  loader->setFileContents(fileContent);
150  loader->guessSettings();
151  loader->processContents();
152  QApplication::restoreOverrideCursor();
153 
154  return QString();
155 }
Base class for all data loaders (classes which can import real data)
virtual void guessSettings()
Guess appropriate settings (for example the separator in a CSV file).
virtual void setFileContents(const QByteArray &fileContent)=0
Sets the file contents to be imported.
virtual void processContents()=0
Process the file contents.
virtual void initWithDefaultImportSettings()
Set import settings to defaults.
virtual AbstractDataLoader * clone() const =0
Create a complete clone, including all internal states.
void setRealDataItem(RealDataItem *item)
Define the real data item on which the import shall work.
Carries information about loaded data.
Real data loader for Q/R/E reflectometry CSV files.
Definition: QREDataLoader.h:27
void setImportData(ImportDataInfo data)
Sets imported data to underlying item.
QString nativeFileName() const
void setDataLoader(AbstractDataLoader *loader)
Takes ownership of loader.
bool isCompressed(const std::string &name)
Returns true if name contains *.gz extension.
bool isIntFile(const std::string &file_name)
returns true if file name corresponds to BornAgain native format (compressed or not)
bool isTiffFile(const std::string &file_name)
returns true if file name corresponds to tiff file (can be also compressed)
std::unique_ptr< OutputData< double > > ImportKnownData(const QString &baseNameOfLoadedFile)

References AbstractDataLoader::clone(), AbstractDataLoader::guessSettings(), ImportKnownData(), AbstractDataLoader::initWithDefaultImportSettings(), DataFormatUtils::isCompressed(), DataFormatUtils::isIntFile(), DataFormatUtils::isTiffFile(), RealDataItem::nativeFileName(), AbstractDataLoader::processContents(), RealDataItem::setDataLoader(), AbstractDataLoader::setFileContents(), RealDataItem::setImportData(), and AbstractDataLoader::setRealDataItem().

Referenced by RealDataSelectorWidget::importData().

Here is the call graph for this function:

◆ Import2dData()

std::unique_ptr< OutputData< double > > ImportDataUtils::Import2dData ( const QString &  baseNameOfLoadedFile)

Definition at line 87 of file ImportDataUtils.cpp.

88 {
89  return ImportKnownData(fileName);
90 }

References ImportKnownData().

Referenced by RealDataSelectorWidget::importData().

Here is the call graph for this function:

◆ ImportKnownData()

std::unique_ptr< OutputData< double > > ImportDataUtils::ImportKnownData ( const QString &  baseNameOfLoadedFile)

Definition at line 53 of file ImportDataUtils.cpp.

54 {
55  // Try to use the canonical tools for importing data
56  std::unique_ptr<OutputData<double>> result;
57  try {
58  std::unique_ptr<OutputData<double>> data(
59  IntensityDataIOFactory::readOutputData(fileName.toStdString()));
60  result = CreateSimplifiedOutputData(*data);
61  } catch (std::exception& ex) {
62  QString message = QString("Error while trying to read file\n\n'%1'\n\n%2")
63  .arg(fileName)
64  .arg(QString::fromStdString(std::string(ex.what())));
65  QMessageBox::warning(nullptr, "IO Problem", message);
66  }
67  return result;
68 }
static OutputData< double > * readOutputData(const std::string &file_name)
Reads file and returns newly created OutputData object.
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: GUIHelpers.cpp:74
std::unique_ptr< OutputData< double > > CreateSimplifiedOutputData(const OutputData< double > &data)
Creates OutputData with bin-valued axes.

References CreateSimplifiedOutputData(), IntensityDataIOFactory::readOutputData(), and GUIHelpers::warning().

Referenced by Import1dData(), and Import2dData().

Here is the call graph for this function:

◆ ImportReflectometryData()

std::unique_ptr< OutputData< double > > ImportDataUtils::ImportReflectometryData ( const QString &  baseNameOfLoadedFile)

Definition at line 71 of file ImportDataUtils.cpp.

72 {
73  std::unique_ptr<OutputData<double>> result;
74  try {
75  std::unique_ptr<OutputData<double>> data(
76  IntensityDataIOFactory::readReflectometryData(fileName.toStdString()));
77  result.swap(data);
78  } catch (std::exception& ex) {
79  QString message = QString("Error while trying to read file\n\n'%1'\n\n%2")
80  .arg(fileName)
81  .arg(QString::fromStdString(std::string(ex.what())));
82  QMessageBox::warning(nullptr, "IO Problem", message);
83  }
84  return result;
85 }
static OutputData< double > * readReflectometryData(const std::string &file_name)

References IntensityDataIOFactory::readReflectometryData(), and GUIHelpers::warning().

Here is the call graph for this function:

◆ printShapeMessage()

QString ImportDataUtils::printShapeMessage ( const std::vector< int > &  instrument_shape,
const std::vector< int > &  data_shape 
)

Composes a message with the shapes of InstrumentItem and RealDataItem.

Definition at line 184 of file ImportDataUtils.cpp.

186 {
187  auto to_str = [](const std::vector<int>& shape) {
188  std::string result;
189  for (size_t i = 0, size = shape.size(); i < size; ++i) {
190  result += std::to_string(shape[i]);
191  if (i + 1 != size)
192  result += ", ";
193  }
194  return result;
195  };
196 
197  std::string message_string = "instrument [";
198  message_string += to_str(instrument_shape);
199  message_string += "], data [";
200  message_string += to_str(data_shape);
201  message_string += "]";
202  return QString::fromStdString(std::move(message_string));
203 }

Referenced by LinkInstrumentManager::canLinkDataToInstrument().