MNE-CPP  0.1.9
A Framework for Electrophysiology
ioutils.h
Go to the documentation of this file.
1 //=============================================================================================================
36 #ifndef IOUTILS_H
37 #define IOUTILS_H
38 
39 //=============================================================================================================
40 // INCLUDES
41 //=============================================================================================================
42 
43 #include "utils_global.h"
44 #include <ios>
45 #include <string>
46 #include <sstream>
47 #include <vector>
48 #include <fstream>
49 
50 //=============================================================================================================
51 // QT INCLUDES
52 //=============================================================================================================
53 
54 #include <QSharedPointer>
55 #include <QTextStream>
56 #include <QFile>
57 #include <QDebug>
58 #include <QStringList>
59 
60 //=============================================================================================================
61 // QT INCLUDES
62 //=============================================================================================================
63 
64 #include <Eigen/Core>
65 
66 //=============================================================================================================
67 // DEFINE NAMESPACE UTILSLIB
68 //=============================================================================================================
69 
70 namespace UTILSLIB
71 {
72 
73 //=============================================================================================================
74 // FORWARD DECLARATIONS
75 //=============================================================================================================
76 
78 {
79  int i = 9;
80 };
81 
83 {
84  int i = 4;
85 };
86 
87 //=============================================================================================================
94 {
95 public:
96  typedef QSharedPointer<IOUtils> SPtr;
97  typedef QSharedPointer<const IOUtils> ConstSPtr;
99  //=========================================================================================================
103  ~IOUtils(){};
104 
105  //=========================================================================================================
115  static qint32 fread3(QDataStream &p_qStream);
116 
117  //=========================================================================================================
127  static qint32 fread3(std::iostream& stream);
128 
129  //=========================================================================================================
140  static Eigen::VectorXi fread3_many(QDataStream &p_qStream, qint32 count);
141 
142  //=========================================================================================================
153  static Eigen::VectorXi fread3_many(std::iostream &stream, qint32 count);
154 
155  //=========================================================================================================
163  static qint16 swap_short (qint16 source);
164 
165  //=========================================================================================================
173  static qint32 swap_int (qint32 source);
174 
175  //=========================================================================================================
181  static void swap_intp (qint32 *source);
182 
183  //=========================================================================================================
191  static qint64 swap_long (qint64 source);
192 
193  //=========================================================================================================
199  static void swap_longp (qint64 *source);
200 
201  //=========================================================================================================
209  static float swap_float (float source);
210 
211  //=========================================================================================================
217  static void swap_floatp (float *source);
218 
219  //=========================================================================================================
225  static void swap_doublep(double *source);
226 
227  //=========================================================================================================
234  template<typename T>
235  static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription = QString());
236  template<typename T>
237  static bool write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription = QString());
238  template<typename T>
239  static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const QString& sPath, const QString& sDescription = QString());
240 
241  //=========================================================================================================
248  template<typename T>
249  static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription = std::string());
250  template<typename T>
251  static bool write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription = std::string());
252  template<typename T>
253  static bool write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const std::string& sPath, const std::string& sDescription = std::string());
254 
255  //=========================================================================================================
262  template<typename T>
263  static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const QString& path);
264  template<typename T>
265  static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const QString& path);
266  template<typename T>
267  static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const QString& path);
268 
269  //=========================================================================================================
276  template<typename T>
277  static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const std::string& path);
278  template<typename T>
279  static bool read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const std::string& path);
280  template<typename T>
281  static bool read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const std::string& path);
282 
283  //=========================================================================================================
291  static QStringList get_new_chnames_conventions(const QStringList& chNames);
292 
293  //=========================================================================================================
301  static std::vector<std::string> get_new_chnames_conventions(const std::vector<std::string>& chNames);
302 
303  //=========================================================================================================
311  static QStringList get_old_chnames_conventions(const QStringList& chNames);
312 
313  //=========================================================================================================
321  static std::vector<std::string> get_old_chnames_conventions(const std::vector<std::string>& chNames);
322 
323  //=========================================================================================================
333  static bool check_matching_chnames_conventions(const QStringList& chNamesA, const QStringList& chNamesB, bool bCheckForNewNamingConvention = false);
334 
335  //=========================================================================================================
345  static bool check_matching_chnames_conventions(const std::vector<std::string>& chNamesA, const std::vector<std::string>& chNamesB, bool bCheckForNewNamingConvention = false);
346 };
347 
348 //=============================================================================================================
349 // INLINE DEFINITIONS
350 //=============================================================================================================
351 
352 template<typename T>
353 bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription)
354 {
355  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1,in.cols());
356  matrixName.row(0)= in;
357  return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
358 }
359 
360 //=============================================================================================================
361 
362 template<typename T>
363 bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const QString& sPath, const QString& sDescription)
364 {
365  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(),1);
366  matrixName.col(0)= in;
367  return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
368 }
369 
370 //=============================================================================================================
371 
372 template<typename T>
373 bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const QString& sPath, const QString& sDescription)
374 {
375  QFile file(sPath);
376  if(file.open(QIODevice::WriteOnly|QIODevice::Truncate))
377  {
378  QTextStream stream(&file);
379  if(!sDescription.isEmpty()) {
380  stream<<"# Dimensions (rows x cols): "<<in.rows()<<" x "<<in.cols()<<"\n";
381  stream<<"# Description: "<<sDescription<<"\n";
382  }
383 
384  for(int row = 0; row<in.rows(); row++) {
385  for(int col = 0; col<in.cols(); col++)
386  stream << in(row, col)<<" ";
387  stream<<"\n";
388  }
389  } else {
390  qWarning()<<"Could not write Eigen element to file! Path does not exist!";
391  return false;
392  }
393 
394  file.close();
395 
396  return true;
397 }
398 
399 //=============================================================================================================
400 
401 template<typename T>
402 bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, 1, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription)
403 {
404  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1,in.cols());
405  matrixName.row(0)= in;
406  return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
407 }
408 
409 //=============================================================================================================
410 
411 template<typename T>
412 bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, 1>& in, const std::string& sPath, const std::string& sDescription)
413 {
414  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(in.rows(),1);
415  matrixName.col(0)= in;
416  return IOUtils::write_eigen_matrix(matrixName, sPath, sDescription);
417 }
418 
419 //=============================================================================================================
420 
421 template<typename T>
422 bool IOUtils::write_eigen_matrix(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& in, const std::string& sPath, const std::string& sDescription)
423 {
424  std::ofstream outputFile(sPath);
425  if(outputFile.is_open())
426  {
427  if(!sDescription.empty()) {
428  outputFile<<"# Dimensions (rows x cols): "<<in.rows()<<" x "<<in.cols()<<"\n";
429  outputFile<<"# Description: "<<sDescription<<"\n";
430  }
431 
432  for(int row = 0; row<in.rows(); row++) {
433  for(int col = 0; col<in.cols(); col++)
434  outputFile << in(row, col)<<" ";
435  outputFile<<"\n";
436  }
437  } else {
438  qWarning()<<"Could not write Eigen element to file! Path does not exist!";
439  return false;
440  }
441 
442  return true;
443 }
444 
445 //=============================================================================================================
446 
447 template<typename T>
448 bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const QString& path)
449 {
450  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
451  bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
452 
453  if(matrixName.rows() > 0)
454  {
455  out = matrixName.row(0);
456  }
457 
458  return bStatus;
459 }
460 
461 //=============================================================================================================
462 
463 template<typename T>
464 bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const QString& path)
465 {
466  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
467  bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
468 
469  if(matrixName.cols() > 0)
470  {
471  out = matrixName.col(0);
472  }
473 
474  return bStatus;
475 }
476 
477 //=============================================================================================================
478 
479 template<typename T>
480 bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const QString& path)
481 {
482  QFile file(path);
483 
484  if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
485  //Start reading from file
486  QTextStream in(&file);
487  QList<Eigen::VectorXd> help;
488 
489  while(!in.atEnd())
490  {
491  QString line = in.readLine();
492  if(!line.contains("#")) {
493  QStringList fields = line.split(QRegExp("\\s+"));
494 
495  //Delete last element if it is a blank character
496  if(fields.at(fields.size()-1) == "")
497  fields.removeLast();
498 
499  Eigen::VectorXd x (fields.size());
500 
501  for (int j = 0; j<fields.size(); j++) {
502  x(j) = fields.at(j).toDouble();
503  }
504 
505  help.append(x);
506  }
507  }
508 
509  int rows = help.size();
510  int cols = rows <= 0 ? 0 : help.at(0).rows();
511 
512  out.resize(rows, cols);
513 
514  for (int i=0; i < help.length(); i++) {
515  out.row(i) = help[i].transpose();
516  }
517  } else {
518  qWarning()<<"IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
519  return false;
520  }
521 
522  return true;
523 }
524 
525 //=============================================================================================================
526 
527 template<typename T>
528 bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, 1, Eigen::Dynamic>& out, const std::string& path)
529 {
530  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
531  bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
532 
533  if(matrixName.rows() > 0)
534  {
535  out = matrixName.row(0);
536  }
537 
538  return bStatus;
539 }
540 
541 //=============================================================================================================
542 
543 template<typename T>
544 bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, 1>& out, const std::string& path)
545 {
546  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName;
547  bool bStatus = IOUtils::read_eigen_matrix(matrixName, path);
548 
549  if(matrixName.cols() > 0)
550  {
551  out = matrixName.col(0);
552  }
553 
554  return bStatus;
555 }
556 
557 //=============================================================================================================
558 
559 template<typename T>
560 bool IOUtils::read_eigen_matrix(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& out, const std::string& path)
561 {
562  std::ifstream inputFile(path);
563 
564  if(inputFile.is_open()) {
565  //Start reading from file
566  std::vector<Eigen::VectorXd> help;
567 
568  std::string line;
569 
570  while(std::getline(inputFile, line)){
571  if(line.find('#') == std::string::npos){
572  std::vector<double> elements;
573  std::stringstream stream{line};
574  std::string element;
575 
576  stream >> std::ws;
577  while(stream >> element){
578  elements.push_back(std::stod(element));
579  stream >> std::ws;
580  }
581 
582  Eigen::VectorXd x (elements.size());
583 
584  for(size_t i = 0; i < elements.size(); ++i){
585  x(i) = elements.at(i);
586  }
587 
588  help.push_back(std::move(x));
589  }
590  }
591 
592  int rows = help.size();
593  int cols = rows <= 0 ? 0 : help.at(0).rows();
594 
595  out.resize(rows, cols);
596 
597  for (size_t i = 0; i < help.size(); i++) {
598  out.row(i) = help[i].transpose();
599  }
600  } else {
601  qWarning()<<"IOUtils::read_eigen_matrix - Could not read Eigen element from file! Path does not exist!";
602  return false;
603  }
604 
605  return true;
606 }
607 } // NAMESPACE
608 
609 #endif // IOUTILS_H
610 
UTILSLIB::IOUtils::read_eigen_matrix
static bool read_eigen_matrix(Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &out, const QString &path)
Definition: ioutils.h:480
utils_global.h
utils library export/import macros.
UTILSLIB::IOUtils::ConstSPtr
QSharedPointer< const IOUtils > ConstSPtr
Definition: ioutils.h:97
UTILSLIB::IOUtils::SPtr
QSharedPointer< IOUtils > SPtr
Definition: ioutils.h:96
UTILSLIB::IOUtils::~IOUtils
~IOUtils()
Definition: ioutils.h:103
UTILSSHARED_EXPORT
#define UTILSSHARED_EXPORT
Definition: utils_global.h:58
UTILSLIB::IOUtils::write_eigen_matrix
static bool write_eigen_matrix(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &in, const QString &sPath, const QString &sDescription=QString())
Definition: ioutils.h:373
UTILSLIB::IOUtils
IO utilitie routines.
Definition: ioutils.h:93
UTILSLIB::aaaa
Definition: ioutils.h:77
UTILSLIB::b
Definition: ioutils.h:82