Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
CSVReader.cpp
Go to the documentation of this file.
1#include "CSVReader.h"
2#include <fstream>
3#include <sstream>
4#include <stdexcept>
5#include <typeindex>
6#include <memory>
7#include <iostream>
8#include <limits>
9#include <cctype>
10
11std::string trim_str(const std::string& str) {
12 size_t first = str.find_first_not_of(' ');
13 size_t last = str.find_last_not_of(' ');
14 return (first == std::string::npos || last == std::string::npos) ? "" : str.substr(first, last - first + 1);
15}
16
17template <typename T>
18T convertValue(const std::string& value) {
19 std::string cleaned_value = trim_str(value);
20
21 if constexpr (std::is_same<T, int>::value) {
22 return std::stoi(cleaned_value);
23 } else if constexpr (std::is_same<T, double>::value) {
24 try {
25 return std::stod(cleaned_value);
26 } catch (const std::invalid_argument& e) {
27 std::cerr << "Error converting '" << cleaned_value << "' to double" << std::endl;
28 return std::numeric_limits<double>::quiet_NaN();
29 } catch (const std::out_of_range& e) {
30 std::cerr << "Error: value out of range for double: " << cleaned_value << std::endl;
31 return std::numeric_limits<double>::quiet_NaN();
32 }
33 } else if constexpr (std::is_same<T, std::string>::value) {
34 return cleaned_value;
35 } else {
36 throw std::invalid_argument("Unsupported type");
37 }
38}
39
40template <typename T>
41void addValueToDataFrame(DataFrame& df, const std::string& colName, const std::string& value) {
42 df.addValueToColumn<T>(colName, convertValue<T>(value));
43}
44
45DataFrame CSVReader::read_csv(const std::string& filename, CSVOptions options) {
46 std::ifstream file(filename);
47 if (!file.is_open()) {
48 std::cout << "bite" << std::endl;
49 throw std::runtime_error("Could not open file");
50 }
51
52 DataFrame df;
53 std::string line;
54 bool header = true;
55 std::vector<std::string> headers;
56 int index_id = 0;
57 std::vector<std::string> index{};
58
59 while (std::getline(file, line)) {
60 std::stringstream ss(line);
61 std::string value;
62
63 if (header) {
64 while (std::getline(ss, value, ',')) {
65 headers.push_back(trim_str(value));
66
67 const auto& colName = headers.back();
68
69 auto it = options.columnTypes.find(colName);
70 if (it != options.columnTypes.end()) {
71 const auto& colType = it->second;
72 if (colType == typeid(int)) {
73 df.addColumn<int>(colName);
74 } else if (colType == typeid(double)) {
75 df.addColumn<double>(colName);
76 } else if (colType == typeid(std::string)) {
77 df.addColumn<std::string>(colName);
78 } else {
79 throw std::invalid_argument("Unsupported column type");
80 }
81 } else {
82 df.addColumn<double>(colName);
83 options.columnTypes.emplace(colName, std::type_index(typeid(double)));
84 }
85 }
86 header = false;
87 } else {
88 size_t colIdx = 0;
89 while (std::getline(ss, value, ',')) {
90 if (!options.hasIndex && colIdx == 0) {
91 index.emplace_back(std::to_string(index_id++));
92 }
93 if (options.hasIndex && colIdx == 0) {
94 index.emplace_back(trim_str(value));
95 } else {
96 const auto& colName = headers[colIdx];
97
98 auto it = options.columnTypes.find(colName);
99 if (it != options.columnTypes.end()) {
100 const auto& colType = it->second;
101 if (colType == typeid(int)) {
102 addValueToDataFrame<int>(df, colName, value);
103 } else if (colType == typeid(double)) {
104 addValueToDataFrame<double>(df, colName, value);
105 } else if (colType == typeid(std::string)) {
106 addValueToDataFrame<std::string>(df, colName, value);
107 } else {
108 throw std::invalid_argument("Unsupported column type");
109 }
110 } else {
111 addValueToDataFrame<double>(df, colName, value);
112 }
113 }
114 colIdx++;
115 }
116 }
117 }
118
119 df.setIndex(index);
120 file.close();
121 df._set_csv_options(options);
122
123 return df;
124}
125
void addValueToDataFrame(DataFrame &df, const std::string &colName, const std::string &value)
Definition CSVReader.cpp:41
T convertValue(const std::string &value)
Definition CSVReader.cpp:18
std::string trim_str(const std::string &str)
Definition CSVReader.cpp:11
Helper for loading CSV files into a DataFrame.
DataFrame read_csv(const std::string &filename, CSVOptions options=CSVOptions())
Read a CSV file and construct a DataFrame.
Definition CSVReader.cpp:45
Simple column-oriented data structure with basic analysis utilities.
Definition DataFrame.h:61
void addValueToColumn(const std::string &colName, const T &value)
Appends a value to an existing column.
void _set_csv_options(const CSVOptions &options)
Internal helper to set CSV options from an external source.
void addColumn(const std::string &colName)
Adds a new empty column of type T.
void setIndex(const std::vector< std::string > &newIndex)
Sets the index labels for all rows.
double T(double x)
Wilson coefficient T(x).
Options describing how to interpret or write CSV data.
Definition CSVOptions.h:31
std::unordered_map< std::string, std::type_index > columnTypes
Mapping from column name to its declared C++ type.
Definition CSVOptions.h:42
bool hasIndex
True if the CSV representation contains a separate index column.
Definition CSVOptions.h:33