Citlali
Loading...
Searching...
No Matches
ecsv_io.h
Go to the documentation of this file.
1#pragma once
2
3#include <tula/algorithm/ei_stats.h>
4#include <tula/algorithm/index.h>
5#include <tula/container.h>
6#include <tula/datatable.h>
7#include <tula/filename.h>
8
9#include <tula/ecsv/core.h>
10#include <csv_parser/parser.hpp>
11#include <sstream>
12#include <tula/ecsv/table.h>
13#include <tula/formatter/container.h>
14#include <tula/formatter/matrix.h>
15#include <yaml-cpp/node/emit.h>
16
17// create Eigen::Matrix from ecsv file
18inline auto to_matrix_from_ecsv(std::string filepath) {
19 namespace fs = std::filesystem;
20
21 // get logger
22 std::shared_ptr<spdlog::logger> logger = spdlog::get("citlali_logger");
23
24 std::vector<std::string> header;
25 Eigen::MatrixXd table;
26
27 YAML::Node meta_;
28
29 try {
30 table = datatable::read<double, datatable::Format::ecsv>(
31 filepath, &header, &meta_);
32
33 } catch (datatable::ParseError &e) {
34 logger->warn("unable to read apt table file as ECSV {}: {}", filepath,
35 e.what());
36 try {
37 table = datatable::read<double, datatable::Format::ascii>(filepath,
38 &header);
39 } catch (datatable::ParseError &e) {
40 logger->warn("unable to read apt table file as ASCII {}: {}",
41 filepath, e.what());
42 throw e;
43 }
44 }
45 return std::tuple {table, header, meta_};
46}
47
48// create ecsv file from Eigen::Matrix
49template <typename Derived>
50inline void to_ecsv_from_matrix(std::string filepath, Eigen::DenseBase<Derived> &table, std::vector<std::string> header, YAML::Node meta) {
51 namespace fs = std::filesystem;
52
53 // get logger
54 std::shared_ptr<spdlog::logger> logger = spdlog::get("citlali_logger");
55
56 try {
57 YAML::Node meta_;
58 datatable::write<datatable::Format::ecsv>(filepath + ".ecsv", table, header, std::vector<int>{}, meta);
59
60 } catch (datatable::ParseError &e) {
61 logger->warn("unable to read apt table file as ECSV {}: {}", filepath,
62 e.what());
63 try {
64 datatable::write<datatable::Format::ascii>(filepath + ".ascii", table, header, std::vector<int>{});
65
66 } catch (datatable::ParseError &e) {
67 logger->warn("unable to write apt table file as ASCII {}: {}",
68 filepath, e.what());
69 throw e;
70 }
71 }
72}
73
74inline auto to_map_from_ecsv_mixted_type(std::string filepath) {
75 using namespace tula::ecsv;
76
77 // get logger
78 std::shared_ptr<spdlog::logger> logger = spdlog::get("citlali_logger");
79
80 // vector to hold header
81 std::vector<std::string> header;
82
83 // std map for holding data
84 std::map<std::string, Eigen::VectorXd> table;
85
86 // hold str meta
87 std::map<std::string, std::string> map_with_strs;
88
89 // to hold meta data
90 YAML::Node meta{};
91
92 std::ifstream fo(filepath);
93 try {
94 // read in header
95 auto hdr = ECSVHeader::read(fo);
96 // create table
97 auto tbl = ECSVTable(hdr);
98 // parse the contents
99 auto parser = aria::csv::CsvParser(fo).delimiter(tbl.header().delimiter());
100 // load rows
101 tbl.load_rows(parser);
102
103 // get header colnames
104 for (Eigen::Index i=0; i<tbl.header().colnames().size(); i++) {
105 header.push_back(tbl.header().colnames()[i]);
106 }
107
108 const auto map_with_bools =
109 meta_to_map<std::string, bool>(hdr.meta(), &meta);
110
111 map_with_strs =
112 meta_to_map<std::string, std::string>(meta, &meta);
113
114 // get ints
115 auto int_colnames = tbl.array_data<int>().colnames();
116 for (auto & col : int_colnames) {
117 table[col] = tbl.col<int>(col).template cast<double> ();
118 }
119
120 // get int16
121 auto int16_colnames = tbl.array_data<int16_t>().colnames();
122 for (auto & col : int16_colnames) {
123 table[col] = tbl.col<int16_t>(col).template cast<double> ();
124 }
125
126 // get int64
127 auto int64_colnames = tbl.array_data<int64_t>().colnames();
128 for (auto & col : int64_colnames) {
129 table[col] = tbl.col<int64_t>(col).template cast<double> ();
130 }
131
132 // get bools
133 auto bool_colnames = tbl.array_data<bool>().colnames();
134 for (auto & col : bool_colnames) {
135 table[col] = tbl.col<bool>(col).template cast<double> ();
136 }
137
138 // get floats
139 auto float_colnames = tbl.array_data<float>().colnames();
140 for (auto & col : float_colnames) {
141 table[col] = tbl.col<float>(col).template cast<double> ();
142 }
143
144 // get doubles
145 auto dbl_colnames = tbl.array_data<double>().colnames();
146 for (auto & col : dbl_colnames) {
147 table[col] = tbl.col<double>(col);
148 }
149 }
150 catch(...) {
151 logger->error("cannot open input table");
152 std::exit(EXIT_FAILURE);
153 }
154
155 // return map and header
156 return std::tuple {table, header, map_with_strs};
157}
void to_ecsv_from_matrix(std::string filepath, Eigen::DenseBase< Derived > &table, std::vector< std::string > header, YAML::Node meta)
Definition ecsv_io.h:50
auto to_map_from_ecsv_mixted_type(std::string filepath)
Definition ecsv_io.h:74
auto to_matrix_from_ecsv(std::string filepath)
Definition ecsv_io.h:18