6#ifndef THEORETICA_IO_CSV_H
7#define THEORETICA_IO_CSV_H
14#include "../algebra/vec.h"
15#include "../algebra/mat.h"
16#include "../statistics/histogram.h"
34 std::vector<std::string>
fields;
38 for (
size_t i = 0; i <
line.length(); ++i) {
47 }
else if(!std::isspace(c) ||
quoted) {
67 if (std::isspace(c)) {
74 return "\"" +
str +
"\"";
85 template<
typename Type,
unsigned int N>
89 std::ofstream
file (filename);
90 if (!
file.is_open()) {
95 for (
size_t i = 0; i < v.
size(); ++i)
105 template<
typename Type,
unsigned int N>
107 const std::string& filename,
const std::string& header,
110 std::ofstream
file (filename);
111 if (!
file.is_open()) {
117 file <<
"\"" << header <<
"\"" << std::endl;
118 for (
size_t i = 0; i < v.
size(); ++i)
130 template<
typename Type,
unsigned int N, enable_real<Type> = true>
133 std::ifstream
file (filename);
136 if (!
file.is_open()) {
148 std::vector<real> col;
152 std::replace(
line.begin(),
line.end(),
',',
'.');
154 col.emplace_back(
first);
155 }
catch (
const std::invalid_argument&
e) {
157 }
catch(
const std::out_of_range&
e) {
166 std::replace(
line.begin(),
line.end(),
',',
'.');
170 col.emplace_back(
val);
171 }
catch (
const std::exception&
e) {
172 col.emplace_back(
nan());
177 if (v.
size() > col.size()) {
179 for (
size_t i = 0; i < col.size(); i++)
182 for (
size_t i = col.size(); i < v.
size(); i++)
201 template<
typename Type,
unsigned int N, enable_real<Type> = true>
203 const std::string& filename,
const std::string&
col_name,
206 std::ifstream
file (filename);
209 if (!
file.is_open()) {
222 for (
size_t i = 0; i <
headers.size(); ++i) {
231 if (col_index == -1) {
238 for (
size_t i = 0; i < v.
size(); ++i)
245 std::vector<real> data;
246 std::vector<std::string>
cells;
252 if (
size_t(col_index) <
cells.size()) {
255 std::replace(
cell.begin(),
cell.end(),
',',
'.');
259 data.emplace_back(
val);
260 }
catch (
const std::exception&
e) {
261 data.emplace_back(
nan());
264 data.emplace_back(
nan());
299 template<
typename Type,
unsigned int N,
unsigned int M>
304 std::ofstream
file (filename);
306 if (!
file.is_open()) {
311 for (
size_t i = 0; i < A.
rows(); i++) {
312 for (
size_t j = 0;
j < A.
cols();
j++) {
316 if (
j != A.cols() - 1)
331 template<
unsigned int N,
unsigned int K>
334 std::ifstream
file (filename);
337 if (!
file.is_open()) {
342 std::vector<std::vector<real>> rows;
364 std::vector<real> row;
368 std::replace(
cell.begin(),
cell.end(),
',',
'.');
371 row.emplace_back(std::stod(
cell));
372 }
catch (
const std::exception&
e) {
373 row.emplace_back(
nan());
378 rows.emplace_back(row);
389 std::vector<real> row;
393 std::replace(
cell.begin(),
cell.end(),
',',
'.');
396 row.emplace_back(std::stod(
cell));
397 }
catch (
const std::exception&
e) {
398 row.emplace_back(
nan());
403 rows.emplace_back(row);
407 A.
resize(rows.size(), rows[0].size());
409 if (A.
rows() < rows[0].size() || A.
cols() < rows.size()) {
416 for (
size_t i = 0; i <
min(rows.size(), A.
rows()); ++i) {
418 for (
size_t j = 0;
j <
min(rows[i].size(), A.
cols()); ++
j)
419 A(i,
j) = rows[i][
j];
422 for (
size_t j = rows[i].size();
j < A.
cols(); ++
j)
427 for (
size_t i = rows.size(); i < A.
rows(); ++i)
428 for (
size_t j = 0;
j < A.
cols(); ++
j)
441 std::ofstream
file (filename);
443 if (!
file.is_open()) {
450 for (
const std::string& name :
table.header()) {
462 for (
size_t i = 0; i <
max_rows; ++i) {
465 for (
const auto& col :
table.data()) {
491 std::ifstream
file (filename);
494 if (!
file.is_open()) {
508 std::vector<std::string> column_names;
510 std::vector<vec<real>> columns (
num_cols);
529 std::replace(
cell.begin(),
cell.end(),
',',
'.');
532 columns[
j].append(std::stod(
cell));
533 }
catch (
const std::exception&
e) {
534 columns[
j].append(
nan());
540 column_names.emplace_back(
"col" + std::to_string(
j));
562 std::replace(
cell.begin(),
cell.end(),
',',
'.');
566 columns[
j].append(
val);
567 }
catch (
const std::exception&
e) {
568 columns[
j].append(
nan());
571 columns[
j].append(
nan());
577 table.insert(column_names[
j], columns[
j]);
598 std::ofstream
file (filename);
599 if (!
file.is_open()) {
604 const auto bin_counts =
hist.bins();
607 if (!bin_counts.size()) {
621 file <<
"bins, counts, number, average, tss, min, max" << std::endl;
622 if (!bin_counts.size())
633 for (
size_t i = 1; i < bin_counts.size(); i++) {
650 std::ifstream
file (filename);
651 if (!
file.is_open()) {
657 std::vector<std::string>
cells;
667 for (
size_t i = 0; i <
cells.size(); i++) {
706 real run_average, run_tss, value_min, value_max;
719 }
catch (
const std::invalid_argument&
e) {
739 }
catch (
const std::exception&
e) {
750 for (
size_t i = 0; i <
counts.size(); i++) {
759 std::vector<unsigned int> bin_counts (
counts.size());
760 for (
size_t i = 0; i <
counts.size(); i++) {
769 for (
size_t i = 1; i < bins.
size(); i++) {
770 if (
abs((bins[i] - bins[i - 1]) -
bin_dx) > 1
e-6) {
777 bin_counts,
vec2({range_min, range_max}),
778 N, run_average, run_tss,
793 template<
typename Type>
A data structure for holding labeled columns of data, where each column is a vector of real numbers.
Definition data_table.h:26
Histogram class with running statistics, can be constructed from the parameters of the bins or from a...
Definition histogram.h:28
A generic matrix with a fixed number of rows and columns.
Definition mat.h:136
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:639
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:646
mat< Type, N, K > resize(unsigned int n, unsigned int k)
Compatibility function to allow for allocation or resizing of dynamic matrices.
Definition mat.h:728
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:459
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition vec.h:449
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:225
Data table structure for holding labeled columns of data.
Error handling for IO operations.
Vector1 & vec_copy(Vector1 &dest, const Vector2 &src)
Copy a vector by overwriting another.
Definition algebra.h:195
Matrix & mat_error(Matrix &m)
Overwrite the given matrix with the error matrix with NaN values on the diagonal and zeroes everywher...
Definition algebra.h:40
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:58
bool is_number(const std::string &str)
Check if a given string could be correctly interpreted as a number.
Definition strings.h:16
std::string trim(const std::string &str)
Remove all leading and trailing whitespace from a string, returning the resulting string.
Definition strings.h:36
@ FileNotFound
File or directory not found.
@ FormatError
The file format is invalid or the data is corrupted.
@ ReadError
Error occurred while reading from the file or stream.
std::string quote_csv(const std::string &str)
Given a string entry, sanitize it for printing to a CSV file.
Definition csv.h:62
std::vector< std::string > parse_csv(const std::string &line, char delimiter=',')
Parse a CSV line handling quoted fields.
Definition csv.h:32
void read_csv(const std::string &filename, vec< Type, N > &v)
Read a vector from a file in the CSV format.
Definition csv.h:131
void write_csv(const std::string &filename, const vec< Type, N > &v, unsigned int precision=8)
Write a vector to file in the CSV format.
Definition csv.h:86
std::string unquote(const std::string &str)
Remove leading and trailing double quotes from a string, if both are present.
Definition strings.h:54
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:207
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition dataset.h:351
vec< real, 2 > vec2
A 2-dimensional vector with real elements.
Definition algebra_types.h:39
bool is_nan(const T &x)
Check whether a generic variable is (equivalent to) a NaN number.
Definition error.h:94
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
TH_CONSTEXPR Type make_error()
Create a number representing an error state, constructed from a NaN value.
Definition real_analysis.h:1322
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:78
@ OutOfRange
Result out of range.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
TH_CONSTEXPR int floor(real x)
Compute the floor of x, as the maximum integer number that is smaller than x.
Definition real_analysis.h:271