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