24 #include <boost/tokenizer.hpp> 25 #include <boost/lexical_cast.hpp> 39 QFile inputfile(filename);
40 inputfile.open(QIODevice::ReadOnly);
41 QTextStream inputstream(&inputfile);
42 QString line = inputstream.readLine();
43 QStringList tab_list = line.split(
"\t");
44 QStringList comma_list = line.split(
",");
47 if(tab_list.size() <= 0 && comma_list.size() <= 0){
50 else if(tab_list.size() > 0 && comma_list.size() > 0){
54 comma_decimals =
true;
57 else if(tab_list.size() > 0 && comma_list.size() == 0){
58 comma_decimals =
false;
59 for (
int i = 0; i < tab_list.size(); ++i){
60 tab_list[i].toDouble(&valid);
66 else if(tab_list.size() == 0 && comma_list.size() > 0){
67 comma_decimals =
false;
71 for (
int i = 0; i < comma_list.size(); ++i){
72 comma_list[i].toDouble(&valid);
100 arma::vec &x, arma::vec &y,
105 std::ifstream file(filename);
106 std::string first_line;
107 std::getline(file, first_line);
108 first_line.erase(0, 2);
110 abscissa = mat(first_line).t();
112 ok = spectra.load(file, raw_ascii);
127 spectra.shed_cols(0, 1);
128 arma::inplace_trans(spectra);
131 arma::uvec sorted_indices = arma::stable_sort_index(abscissa);
132 abscissa = abscissa.rows(sorted_indices);
133 spectra = spectra.rows(sorted_indices);
156 arma::vec &x, arma::vec &y)
159 x.set_size(rows*cols);
160 y.set_size(rows*cols);
162 bool have_abscissa =
false;
163 mat current_spectrum;
164 std::pair<int,int> origin(0,0);
165 bool ok = current_spectrum.load(filenames[origin]);
166 int spec_rows = current_spectrum.n_rows;
167 spectra.set_size(spec_rows, x.n_rows);
171 typedef std::map<std::pair<int,int>, std::string>::iterator map_it;
173 std::string filename;
174 for (map_it it = filenames.begin(); it!=filenames.end(); ++it){
175 std::pair<int,int> keys = it->first;
176 x(i) = (double) keys.first;
177 y(i) = (double) keys.second;
178 filename = it->second;
179 ok = current_spectrum.load(filename);
180 if (!have_abscissa && current_spectrum.n_cols == 2){
181 if (current_spectrum.n_rows > current_spectrum.n_cols){
182 abscissa = current_spectrum.col(0);
183 current_spectrum.shed_col(0);
186 abscissa = current_spectrum.row(0).t();
187 current_spectrum.shed_row(0);
189 have_abscissa =
true;
192 if (current_spectrum.n_rows > current_spectrum.n_cols){
193 if (current_spectrum.n_cols > 1)
194 spectra.col(i) = current_spectrum.col(1);
196 spectra.col(i) = current_spectrum.col(0);
199 if (current_spectrum.n_rows > 1)
200 spectra.col(i) = current_spectrum.row(1).t();
202 spectra.col(i) = current_spectrum.row(0).t();
204 }
catch(std::exception e){
209 std::cerr <<
"Exception thrown. Spectra " << i <<
"." <<
"Type: " << e.what() <<
"." << std::endl;
214 std::cerr <<
"Could not load file " << filename <<
"." << std::endl;
220 arma::uvec sorted_indices = arma::stable_sort_index(abscissa);
221 abscissa = abscissa.rows(sorted_indices);
222 spectra = spectra.rows(sorted_indices);
224 return have_abscissa;
255 spectra.load(filename);
256 if (spectra.n_cols < 2)
return false;
257 abscissa = spectra.col(0);
259 if (x_count * y_count != spectra.n_cols){
267 if (!(x.n_rows == y.n_rows && x.n_rows == spectra.n_cols)){
286 void TextImport::GenerateSpatialData(
double x_start,
double y_start,
double x_end,
double y_end, arma::uword x_count, arma::uword y_count, arma::vec &x, arma::vec &y)
290 arma::vec unique_x = arma::linspace(x_start, x_end, x_count);
291 arma::vec unique_y = arma::linspace(y_start, y_end, y_count);
292 arma::uword vec_size = x_count * y_count;
293 x.set_size(vec_size);
294 y.set_size(vec_size);
295 for (arma::uword i = 0; i < unique_y.n_rows; ++i){
296 y.rows(i, i*x_count).fill(unique_y(i));
297 x.rows(i, i*x_count) = unique_x;
311 bool TextImport::ImportLongText(std::string filename, arma::mat &spectra, arma::mat &abscissa, arma::vec &x, arma::vec &y,
bool swap_spatial)
315 all_data.load(filename);
319 if (all_data.n_cols < 4)
return false;
320 arma::vec all_x = (swap_spatial ? all_data.col(1) : all_data.col(0));
321 arma::vec all_y = (swap_spatial ? all_data.col(0) : all_data.col(1));
322 arma::vec all_abscissa = all_data.col(2);
323 arma::vec all_spectra = all_data.col(3);
324 double abscissa_max = all_abscissa.max();
325 arma::uvec max_indices = arma::find(all_abscissa == abscissa_max);
326 x = all_x.elem(max_indices);
327 y = all_y.elem(max_indices);
328 abscissa = arma::unique(all_abscissa);
329 spectra = arma::mat(all_spectra.memptr(), abscissa.n_rows, x.n_rows);
331 arma::uvec sorted_indices = arma::stable_sort_index(abscissa);
332 abscissa = abscissa.rows(sorted_indices);
333 spectra = spectra.rows(sorted_indices);
VESPUCCI_EXPORT bool CheckFileValidity(QString filename, bool &comma_decimals)
TextImport::CheckFileValidity.
VESPUCCI_EXPORT bool ImportWitec(std::string filename, double x_start, double y_start, double x_end, double y_end, arma::uword x_count, arma::uword y_count, arma::mat &spectra, arma::vec &abscissa, arma::vec &x, arma::vec &y)
TextImport::ImportWitec.
VESPUCCI_EXPORT void GenerateSpatialData(double x_start, double y_start, double x_end, double y_end, arma::uword x_count, arma::uword y_count, arma::vec &x, arma::vec &y)
TextImport::GenerateSpatialData.
VESPUCCI_EXPORT bool ImportWideText(std::string filename, arma::mat &spectra, arma::vec &abscissa, arma::vec &x, arma::vec &y, bool swap_spatial)
TextImport::ImportWideText.
VESPUCCI_EXPORT bool ImportMultiplePoints(std::map< std::pair< int, int >, std::string > filenames, int rows, int cols, arma::mat &spectra, arma::vec &abscissa, arma::vec &x, arma::vec &y)
TextImport::ImportMultiplePoints.
VESPUCCI_EXPORT void ResetDataset(arma::mat &spectra, arma::vec &x, arma::vec &y, arma::vec &abscissa)
VESPUCCI_EXPORT bool ImportLongText(std::string filename, arma::mat &spectra, arma::mat &abscissa, arma::vec &x, arma::vec &y, bool swap_spatial)
TextImport::ImportLongText.