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);
19 std::string cleaned_value =
trim_str(value);
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) {
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();
33 }
else if constexpr (std::is_same<T, std::string>::value) {
36 throw std::invalid_argument(
"Unsupported type");
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");
55 std::vector<std::string> headers;
57 std::vector<std::string> index{};
59 while (std::getline(file, line)) {
60 std::stringstream ss(line);
64 while (std::getline(ss, value,
',')) {
67 const auto& colName = headers.back();
71 const auto& colType = it->second;
72 if (colType ==
typeid(
int)) {
74 }
else if (colType ==
typeid(
double)) {
76 }
else if (colType ==
typeid(std::string)) {
79 throw std::invalid_argument(
"Unsupported column type");
83 options.
columnTypes.emplace(colName, std::type_index(
typeid(
double)));
89 while (std::getline(ss, value,
',')) {
90 if (!options.
hasIndex && colIdx == 0) {
91 index.emplace_back(std::to_string(index_id++));
93 if (options.
hasIndex && colIdx == 0) {
96 const auto& colName = headers[colIdx];
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);
108 throw std::invalid_argument(
"Unsupported column type");
111 addValueToDataFrame<double>(df, colName, value);
void addValueToDataFrame(DataFrame &df, const std::string &colName, const std::string &value)
T convertValue(const std::string &value)
std::string trim_str(const std::string &str)
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.
Simple column-oriented data structure with basic analysis utilities.
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.
std::unordered_map< std::string, std::type_index > columnTypes
Mapping from column name to its declared C++ type.
bool hasIndex
True if the CSV representation contains a separate index column.