Skip to content
Snippets Groups Projects
Commit f3385095 authored by Juan Manuel Carmona Loaiza's avatar Juan Manuel Carmona Loaiza :ghost:
Browse files

Postponing CsvImportAssistant for next release

parent a488bb65
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,8 @@ IOutputDataReadStrategy* OutputDataReadFactory::getReadStrategy(const std::strin
IOutputDataReadStrategy* result(nullptr);
if(DataFormatUtils::isIntFile(file_name))
result = new OutputDataReadINTStrategy();
else if(DataFormatUtils::isTxtFile(file_name))
result = new OutputDataReadNumpyTXTStrategy();
#ifdef BORNAGAIN_TIFF_SUPPORT
else if(DataFormatUtils::isTiffFile(file_name))
result = new OutputDataReadTiffStrategy();
......
......@@ -36,6 +36,48 @@ OutputData<double>* OutputDataReadINTStrategy::readOutputData(std::istream& inpu
return result;
}
OutputData<double>* OutputDataReadNumpyTXTStrategy::readOutputData(std::istream& input_stream)
{
std::string line;
std::vector<std::vector<double>> data;
while( std::getline(input_stream, line) ) {
if(line.empty() || line[0] == '#')
continue;
std::vector<double> data_in_row = DataFormatUtils::parse_doubles(line);
data.push_back(data_in_row);
}
// validating
size_t nrows = data.size();
size_t ncols(0);
if(nrows) ncols = data[0].size();
if (ncols == 0)
throw std::runtime_error("OutputDataReadNumpyTXTStrategy::readOutputData() -> Error. "
"Can't parse file");
for(size_t row=0; row<nrows; row++) {
if(data[row].size() != ncols)
throw std::runtime_error("OutputDataReadNumpyTXTStrategy::readOutputData() -> Error. "
"Number of elements is different from row to row.");
}
OutputData<double>* result = new OutputData<double>;
result->addAxis("x", ncols, 0.0, double(ncols));
result->addAxis("y", nrows, 0.0, double(nrows));
std::vector<unsigned> axes_indices(2);
for(unsigned row=0; row<nrows; row++) {
for(unsigned col=0; col<ncols; col++) {
axes_indices[0] = col;
axes_indices[1] = static_cast<unsigned>(nrows) - 1 - row;
size_t global_index = result->toGlobalIndex(axes_indices);
(*result)[global_index] = data[row][col];
}
}
return result;
}
#ifdef BORNAGAIN_TIFF_SUPPORT
OutputDataReadTiffStrategy::OutputDataReadTiffStrategy()
......
......@@ -39,6 +39,16 @@ public:
OutputData<double>* readOutputData(std::istream& input_stream);
};
//! Strategy to read OutputData from simple ASCII file with the layout as in numpy.savetxt.
//! @ingroup input_output_internal
class OutputDataReadNumpyTXTStrategy : public IOutputDataReadStrategy
{
public:
OutputData<double>* readOutputData(std::istream& input_stream);
};
#ifdef BORNAGAIN_TIFF_SUPPORT
class TiffHandler;
......
......@@ -21,7 +21,6 @@
#include "IntensityDataIOFactory.h"
#include "IntensityDataItem.h"
#include "RealDataItem.h"
#include "CsvImportAssistant.h"
#include "projectmanager.h"
#include <QFileDialog>
#include <QFileInfo>
......@@ -29,7 +28,7 @@
namespace
{
const QString filter_string = "Intensity File (*.int *.gz *.tif *.tiff *.txt *.csv);;"
const QString filter_string = "Intensity File (*.int *.gz *.tif *.tiff *.txt);;"
"Other (*)";
int getRank(const RealDataItem& item)
......@@ -45,58 +44,35 @@ int getRank(const InstrumentItem& item) {
std::unique_ptr<OutputData<double>> ImportDataUtils::ImportData(QString& baseNameOfLoadedFile)
{
QString dirname = AppSvc::projectManager()->userImportDir();
QString fileName = QFileDialog::getOpenFileName(Q_NULLPTR, QStringLiteral("Open Intensity File"),
QString fileName = QFileDialog::getOpenFileName(0, QStringLiteral("Open Intensity File"),
dirname, filter_string);
std::unique_ptr<OutputData<double>> result;
QString newImportDir = GUIHelpers::fileDir(fileName);
if (newImportDir != dirname)
AppSvc::projectManager()->setImportDir(newImportDir);
if (fileName.isEmpty())
return nullptr;
QFileInfo info(fileName);
baseNameOfLoadedFile = info.baseName();
//Try to use the canonical tools for importing data
try {
std::unique_ptr<OutputData<double>> data(
IntensityDataIOFactory::readOutputData(fileName.toStdString()));
result = CreateSimplifiedOutputData(*data.get());
QString newImportDir = GUIHelpers::fileDir(fileName);
if (newImportDir != dirname)
AppSvc::projectManager()->setImportDir(newImportDir);
} catch(std::exception& e)
//Try to import data using the GUI importer
{
std::unique_ptr<OutputData<double>> data;
if(!UseImportAssistant(dirname, fileName, data))
return nullptr;
std::unique_ptr<OutputData<double>> result;
try {
std::unique_ptr<OutputData<double>> data(
IntensityDataIOFactory::readOutputData(fileName.toStdString()));
result = CreateSimplifiedOutputData(*data.get());
} catch (std::exception& ex) {
QString message = QString("Error while trying to read file\n\n'%1'\n\n%2")
.arg(fileName)
.arg(QString::fromStdString(std::string(ex.what())));
QMessageBox::warning(0, "IO Problem", message);
}
return result;
}
bool ImportDataUtils::UseImportAssistant(QString& dirname, QString& fileName, std::unique_ptr<OutputData<double>>& result){
try{
CsvImportAssistant assistant(dirname,fileName);
int res = assistant.exec();
if(res == assistant.Accepted){
result = assistant.getData();
return true;
}
}catch(std::exception& e){
QString message = QString("Unable to read file:\n\n'%1'\n\n%2\n\nCheck that the file exists and it is not being used by other program.\n\n")
.arg(fileName)
.arg(QString::fromStdString(std::string(e.what())));
QMessageBox::warning(nullptr, "IO Problem", message);
return false;
}
return false;
return result;
}
bool ImportDataUtils::Compatible(const InstrumentItem& instrumentItem,
const RealDataItem& realDataItem)
{
......
......@@ -31,8 +31,6 @@ namespace ImportDataUtils
{
BA_CORE_API_ std::unique_ptr<OutputData<double>> ImportData(QString& baseNameOfLoadedFile);
BA_CORE_API_ bool UseImportAssistant(QString& dirname, QString& fileName, std::unique_ptr<OutputData<double>>& result);
//! Creates OutputData with bin-valued axes.
BA_CORE_API_ std::unique_ptr<OutputData<double>>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment