Vespucci  1.0.0
vespucci.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2014-2016 Wright State University - All Rights Reserved
3  Daniel P. Foose - Maintainer/Lead Developer
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
20 
21 #include "Global/vespucci.h"
22 #include <QtSvg>
23 #include <cstdlib>
24 #include <QProcess>
25 #include <climits>
26 
27 
36 bool Vespucci::SaveOldVespucciBinary(std::string filename, const arma::mat &spectra, const arma::vec &x, const arma::vec &y, const arma::vec &abscissa)
37 {
38  bool success;
39  try{
40  arma::field<arma::mat> dataset(4);
41  dataset(0) = spectra;
42  dataset(1) = abscissa;
43  dataset(2) = x;
44  dataset(3) = y;
45  success = dataset.save(filename, arma::arma_binary);
46  }
47  catch(std::exception e){
48  std::cerr << "See armadillo exception" << std::endl;
49  std::string str = "Vespucci::SaveVespucciBinary: " + std::string(e.what());
50  throw std::runtime_error(str);
51  }
52  return success;
53 }
54 
55 
56 
57 
58 
59 bool Vespucci::SaveText(std::string basename, const arma::mat &spectra, const arma::vec &x, const arma::vec &y, const arma::vec &abscissa, arma::file_type type)
60 {
61  std::string extension;
62  switch (type){
63  case arma::csv_ascii:
64  extension = ".csv";
65  case arma::raw_ascii: default:
66  extension = ".txt";
67  }
68  bool x_success, y_success, spectra_success, abscissa_success;
69  spectra_success = spectra.save(basename + "_spectra" + extension, type);
70  abscissa_success = abscissa.save(basename + "_abscissa" + extension, type);
71  x_success = x.save(basename + "_x" + extension, type);
72  y_success = y.save(basename + "_y" + extension, type);
73  return (x_success && y_success && spectra_success && abscissa_success);
74 }
75 
113 
114 bool Vespucci::StitchDatasets(const arma::field<arma::field<arma::mat> > &datasets,
115  arma::mat &spectra, arma::vec &x, arma::vec &y,
116  arma::vec &abscissa)
117 {
118  Vespucci::ResetDataset(spectra, x, y, abscissa);
119  //set abscissa to be the first in the field
120  abscissa = datasets(0,0)(1);
121 
122  //determine spatial resolution.
123  //due to rounding errors, this may not be the same for all points in a dataset
124  //to armadillo's degree of precision, but will be close. If drastically different
125  //color maps will be rendered meaningless but spectral data will still be valid
126  arma::mat first_x = datasets(0, 0)(2);
127  arma::mat first_y = datasets(0, 0)(3);
128 
129 
130  arma::vec unique_x = arma::unique(first_x);
131  arma::vec unique_y = arma::unique(first_y);
132  double spatial_resolution;
133  if (unique_x.n_rows < 2){
134  if (unique_y.n_rows > 1){
135  spatial_resolution =
136  unique_y(unique_y.n_rows - 1)
137  - unique_y(unique_y.n_rows - 2);
138  }
139  else{return false;}
140  }
141  else{
142  spatial_resolution =
143  unique_x(unique_x.n_rows - 1) - unique_x(unique_x.n_rows - 2);
144  }
145 
146  arma::uword n_unique_y = 0;//per row block in new dataset
147  arma::uword row_block_n_unique_y = 0;
148  //building bottom to top, left to right
149  for (size_t i = datasets.n_rows - 1; i >= 0; --i){
150  row_block_n_unique_y = 0;
151  unique_x = arma::unique(datasets(i, 0)(2));
152  arma::uword n_unique_x = unique_x.n_rows;
153  arma::mat row_block_spc;
154  arma::vec row_block_x;
155  arma::vec row_block_y;
156  //iterate over datasets in this row block
157  for (arma::uword j = 0; j < datasets.n_cols; ++j){
158  arma::uword abs_ct = datasets(i,j)(1).n_rows;
159  //ensure validity of spectra for this abscissa
160  if (datasets(i,j)(0).n_rows != abs_ct){
161  Vespucci::ResetDataset(spectra, x, y, abscissa);
162  return false;
163  }
164  //check validity of spatial data
165  unique_x = arma::unique(datasets(i,j)(2));
166  unique_y = arma::unique(datasets(i,j)(3));
167  if (unique_x.n_rows != n_unique_x){
168  Vespucci::ResetDataset(spectra, x, y, abscissa);
169  return false;
170  }
171  row_block_n_unique_y += unique_y.n_rows;
172 
173 
174  //we're shifting values so that 0,0 is local origin
175  //lowest value of x and y globally will be zero
176  arma::vec new_x = datasets(i,j)(2) - datasets(i,j)(2).min()*arma::ones(datasets(i,j)(2).n_rows);
177  arma::vec new_y = datasets(i,j)(3) - datasets(i,j)(3).min()*arma::ones(datasets(i,j)(3).n_rows);
178 
179  //x offset is equal to the current_maximum
180  double x_offset = (row_block_x.n_elem ?
181  row_block_x.max() + spatial_resolution :
182  0);
183  new_x = datasets(i,j)(2) + x_offset*arma::ones(datasets(i,j)(2).n_rows);
184 
185  //add to row block
186  if (!row_block_x.n_elem){row_block_x = new_x;}
187  else{row_block_x = arma::join_vert(row_block_x, new_x);}
188  if (!row_block_y.n_elem){row_block_y = new_y;}
189  else{row_block_y = arma::join_vert(row_block_y, new_y);}
190  if (!row_block_spc.n_elem){row_block_spc = datasets(i,j)(0);}
191  else{row_block_spc = arma::join_horiz(row_block_spc, datasets(i,j)(0));}
192  }//datasets in row block
193  //offset y values in this row block;
194 
195  double y_offset = (y.n_elem ?
196  y.max() - row_block_y.min() + spatial_resolution :
197  0);
198  row_block_y = row_block_y + y_offset*arma::ones(row_block_y.n_rows);
199 
200  //if this is the first row block...
201  if (i == datasets.n_rows - 1){n_unique_y = row_block_n_unique_y;}
202 
203  //check validity of this row block relative to bottom block
204  if (row_block_n_unique_y != n_unique_y){
205  Vespucci::ResetDataset(spectra, x, y, abscissa);
206  return false;
207  }
208 
209  //add this row block to the new dataset
210  if (!spectra.n_cols){spectra = row_block_spc;}
211  else{spectra = arma::join_horiz(spectra, row_block_spc);}
212  if (!x.n_rows){x = row_block_x;}
213  else{x = arma::join_vert(x, row_block_x);}
214  if (!y.n_rows){y = row_block_y;}
215  else{y = arma::join_vert(y, row_block_y);}
216 
217  }//row blocks in new dataset
218 
219  //recenter spatial data to put origin at center of spatial data
220  //x = x - arma::median(x)*arma::ones(x.n_rows);
221  //y = y - arma::median(y)*arma::ones(y.n_rows);
222 
223  return true;
224 }
225 
226 void Vespucci::ResetDataset(arma::mat &spectra, arma::vec &x, arma::vec &y, arma::vec &abscissa)
227 {
228  spectra.reset();
229  x.reset();
230  y.reset();
231  abscissa.reset();
232 }
233 
240 std::string Vespucci::CleanString(const std::string &in)
241 {
242  std::string out;
243  std::string::const_iterator it = in.begin();
244  for (const char c : in){
245  if (c == '\t' || c == ','){out.append(" ");}
246  else{out.append(&c);}
247  }
248  return out;
249 }
VESPUCCI_EXPORT std::string CleanString(const std::string &in)
Vespucci::CleanString.
Definition: vespucci.cpp:240
VESPUCCI_EXPORT bool StitchDatasets(const arma::field< arma::field< arma::mat > > &datasets, arma::mat &spectra, arma::vec &x, arma::vec &y, arma::vec &abscissa)
Vespucci::StitchDatasets.
Definition: vespucci.cpp:114
VESPUCCI_EXPORT bool SaveOldVespucciBinary(std::string filename, const arma::mat &spectra, const arma::vec &x, const arma::vec &y, const arma::vec &abscissa)
Vespucci::SaveVespucciBinary.
Definition: vespucci.cpp:36
VESPUCCI_EXPORT arma::uword min(arma::uword a, arma::uword b)
Vespucci::Math::min.
Definition: accessory.cpp:249
VESPUCCI_EXPORT void ResetDataset(arma::mat &spectra, arma::vec &x, arma::vec &y, arma::vec &abscissa)
Definition: vespucci.cpp:226
VESPUCCI_EXPORT bool SaveText(std::string basename, const arma::mat &spectra, const arma::vec &x, const arma::vec &y, const arma::vec &abscissa, arma::file_type type)
Definition: vespucci.cpp:59