Line data Source code
1 : /** 2 : * @file DataLoader.hpp 3 : * @brief Encapsulating DTWC data loading configurations in a class. 4 : * Uses method chaining for easier input taking. 5 : * @author Volkan Kumtepeli 6 : * @author Becky Perriment 7 : * @date 04 Dec 2022 8 : */ 9 : 10 : #pragma once 11 : 12 : #include "Data.hpp" //!< For Data class 13 : #include "fileOperations.hpp" //!< For load_batch_file(), load_folder() 14 : #include "settings.hpp" //!< For data_t type 15 : 16 : #include <cstddef> //!< For size_t 17 : #include <filesystem> //!< For filesystem objects like path 18 : #include <tuple> //!< For std::tie(), std::tuple 19 : #include <vector> //!< For std::vector 20 : 21 : namespace dtwc { 22 : /** 23 : * @brief Data loader class 24 : */ 25 : class DataLoader 26 : { 27 : int start_col{ 0 }; //!< Starting column for data extraction 28 : int start_row{ 0 }; //!< Starting row for data extraction 29 : int Ndata{ -1 }; //!< Number of data rows to load 30 : int verbose{ 1 }; //!< Verbosity level 31 : char delim{ ',' }; //!< Column delimiter character 32 : std::filesystem::path data_path{ "." }; //!< Path to data file or folder 33 : 34 : public: 35 : // Constructors 36 2 : DataLoader() = default; //!< Default constructor. 37 2 : DataLoader(const fs::path &path_) { this->path(path_); } //!< Constructor with path initialization. 38 3 : DataLoader(const fs::path &path_, int Ndata_) 39 3 : { 40 3 : this->path(path_); 41 3 : this->n_data(Ndata_); 42 3 : } 43 : 44 : // Accessor methods 45 6 : auto startColumn() { return start_col; } //!< Get the starting column for data loading. 46 6 : auto startRow() { return start_row; } //!< Get the starting row for data loading. 47 6 : auto n_data() { return Ndata; } //!< Get the number of data points to load. 48 6 : auto delimiter() { return delim; } //!< Get the delimiter used in data files. 49 6 : auto path() { return data_path; } //!< Get the path of the data file or directory. 50 1 : auto verbosity() { return verbose; } //!< Get the verbosity level for data loading. 51 : 52 : 53 : // Setters with chaining 54 : 55 : /** 56 : * @brief Set start column 57 : * @param N Starting column 58 : * @return Reference to self for chaining 59 : */ 60 2 : DataLoader &startColumn(int N) 61 : { 62 2 : start_col = N; 63 2 : return *this; 64 : } 65 : 66 : //!< Set start row 67 2 : DataLoader &startRow(int N) 68 : { 69 2 : start_row = N; 70 2 : return *this; 71 : } 72 : 73 : //!< Set number of data rows 74 4 : DataLoader &n_data(int N) 75 : { 76 4 : Ndata = N; 77 4 : return *this; 78 : } 79 : //!< Set delimiter 80 : DataLoader &delimiter(char delim_) 81 : { 82 : delim = delim_; 83 : return *this; 84 : } 85 : 86 : /** 87 : * @brief Set data path 88 : * 89 : * Sets delimiter based on file extension 90 : * 91 : * @param data_path_ Path to data 92 : * @return Reference to self for chaining 93 : */ 94 6 : DataLoader &path(const std::filesystem::path &data_path_) 95 : { 96 6 : data_path = data_path_; 97 6 : if (data_path_.extension() == ".csv") 98 3 : delim = ','; 99 3 : else if (data_path_.extension() == ".tsv") 100 2 : delim = '\t'; 101 6 : return *this; 102 : } 103 : 104 : //!< Set verbosity level 105 1 : DataLoader &verbosity(int N) 106 : { 107 1 : verbose = N; 108 1 : return *this; 109 : } 110 : 111 : /** 112 : * @brief Load data 113 : * @details Calls appropriate loader based on path being file or folder. 114 : * @return Loaded data 115 : */ 116 1 : Data load() 117 : { 118 1 : Data d; 119 1 : if (fs::is_directory(data_path)) 120 1 : std::tie(d.p_vec, d.p_names) = load_folder<data_t>(data_path, Ndata, verbose, start_row, start_col, delim); 121 : else 122 0 : std::tie(d.p_vec, d.p_names) = load_batch_file<data_t>(data_path, Ndata, verbose, start_row, start_col, delim); 123 : 124 1 : return d; 125 0 : } 126 : }; 127 : 128 : } // namespace dtwc