CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Table.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2014 by Federico Marulli and Alfonso Veropalumbo *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
34 #include "Table.h"
35 
36 using namespace std;
37 
38 using namespace cbl;
39 using namespace data;
40 
41 
42 // ======================================================================================
43 
44 
45 void cbl::data::Table::m_set (const std::vector<std::string> names, const size_t nrows)
46 {
47  vector<vector<double>> values(names.size(), vector<double>(nrows, 0.));
48  m_set(names, values);
49 }
50 
51 
52 // ======================================================================================
53 
54 
55 void cbl::data::Table::m_set (const std::vector<std::string> names, const std::vector<std::vector<double>> values)
56 {
57  this->insert(names, values);
58 }
59 
60 
61 // ======================================================================================
62 
63 
64 vector<string> cbl::data::Table::m_keys ()
65 {
66  vector<string> keys;
67 
68  for(table_map::iterator it = m_table.begin(); it != m_table.end(); ++it)
69  keys.push_back(it->first);
70 
71  return keys;
72 }
73 
74 
75 // ======================================================================================
76 
77 
78 cbl::data::Table::Table (const std::vector<std::string> names, const std::vector<std::vector<double>> values)
79 {
80  m_set(names, values);
81 }
82 
83 
84 // ======================================================================================
85 
86 
87 cbl::data::Table::Table (const std::vector<std::string> names, const size_t nrows)
88 {
89  m_set(names, nrows);
90 }
91 
92 
93 // ======================================================================================
94 
95 
96 cbl::data::Table::Table (const std::string input_dir, const std::string input_file, const std::vector<std::string> names, const std::vector<size_t> use_cols, const size_t header_lines_to_skip)
97 {
98  this->read(input_dir, input_file, names, use_cols, header_lines_to_skip);
99 }
100 
101 
102 // ======================================================================================
103 
104 
105 vector<double>& cbl::data::Table::operator[] (const std::string name)
106 {
107  table_map::iterator it = m_table.find(name);
108 
109  if (it == m_table.end())
110  ErrorCBL("Column "+name+" has not been found, exiting.", "cbl::data::Table::operator[]", "Data/Table.cpp");
111 
112  return m_table.at(name);
113 }
114 
115 
116 // ======================================================================================
117 
118 
119 vector<vector<double>> cbl::data::Table::operator[] (const std::vector<std::string> names)
120 {
121  vector<vector<double>> columns(names.size());
122 
123  for (size_t i=0; i<names.size(); i++)
124  columns[i] = this->operator[](names[i]);
125 
126  return columns;
127 }
128 
129 
130 // ======================================================================================
131 
132 
133 void cbl::data::Table::insert (const std::string name, const std::vector<double> values, const bool replace)
134 {
135  table_map::iterator it = m_table.find(name);
136 
137  if (it == m_table.end())
138  m_table.emplace(name, values);
139  else {
140  if (replace) {
141  m_table.erase(name);
142  m_table.emplace(name, values);
143  }
144  else
145  cbl::ErrorCBL("Column "+name+" exists and replace is false. Check your inputs. Exiting.", "cbl::data::Table::insert", "Data/Table.cpp");
146  }
147 }
148 
149 
150 // ======================================================================================
151 
152 
153 void cbl::data::Table::insert (const std::vector<std::string> names, const std::vector<std::vector<double>> values, const bool replace)
154 {
155  for (size_t i=0; i<names.size(); i++)
156  this->insert(names[i], values[i], replace);
157 }
158 
159 
160 // ======================================================================================
161 
162 
163 void cbl::data::Table::read (const std::string input_dir, const std::string input_file, const std::vector<std::string> names, const vector<size_t> use_cols, const size_t header_lines_to_skip)
164 {
165  string file = input_dir+input_file;
166  coutCBL << "Reading the table " << file << endl;
167 
168  const size_t ncols = names.size();
169 
170  vector<vector<double>> values;
171 
172  if (names.size() != use_cols.size() && use_cols.size()>0)
173  ErrorCBL("Wrong number of columns, check your inputs!", "cbl::data::Table::read", "Data/Table.cpp");
174 
175  std::function<vector<double>(const vector<double> params)> assign;
176 
177  if (use_cols.size()==0) {
178  assign = [&] (const vector<double> params) {
179  return params;
180  };
181  }
182  else {
183  checkDim(use_cols, ncols, "columns");
184  assign = [&] (const vector<double> params) {
185  vector<double> pp(ncols);
186  for (size_t i=0; i<ncols; i++)
187  pp[i] = params[use_cols[i]];
188  return pp;
189  };
190  }
191 
192  ifstream fin(file.c_str()); checkIO(fin, file);
193 
194  string line;
195  for (size_t i=0; i<header_lines_to_skip; i++)
196  getline(fin, line);
197 
198  while (getline(fin, line)) {
199  stringstream ss(line);
200  double NUM;
201  vector<double> ll;
202 
203  while (ss>>NUM) ll.push_back(NUM);
204 
205  values.push_back(assign(ll));
206  }
207 
208  fin.clear(); fin.close();
209 
210  m_set(names, cbl::transpose(values));
211 
212  coutCBL << "Done!" << endl << endl;
213 }
214 
215 
216 // ======================================================================================
217 
218 
219 void cbl::data::Table::write (const std::string output_dir, const std::string output_file, const std::vector<std::string> names)
220 {
221  vector<string> keys = (names.size()==0) ? this->m_keys() : names;
222  vector<vector<double>> values = transpose(this->operator[](keys));
223 
224  string MK = "mkdir -p "+output_dir;
225  if (system(MK.c_str())) {}
226 
227  string file = output_dir+output_file;
228 
229  ofstream fout(file.c_str()); checkIO(fout, file);
230  fout.precision(10);
231 
232  // Write the header
233  fout << "#";
234  for (size_t i=0; i<keys.size(); i++)
235  fout << " " << keys[i];
236  fout << endl;
237 
238  for (size_t i=0; i<values.size(); i++) {
239  for (size_t j=0; j<values[i].size(); j++)
240  cbl::Print(values[i][j], 5, 14, "", " ", false, fout);
241  fout << endl;
242  }
243 
244  fout.clear(); fout.close();
245 
246  coutCBL << "I wrote the file: " << file << endl;
247 }
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class Table.
std::vector< std::string > m_keys()
get the map keys
Definition: Table.cpp:64
void m_set(const std::vector< std::string > names, const size_t nrows)
function to set internal members
Definition: Table.cpp:45
std::vector< double > & operator[](const std::string name)
function to get a column
Definition: Table.cpp:105
void write(const std::string output_dir, const std::string output_file, const std::vector< std::string > names={})
function to write columns
Definition: Table.cpp:219
void insert(const std::string name, const std::vector< double > values, const bool replace=false)
function to insert a column
Definition: Table.cpp:133
Table()=default
default constructor
void read(const std::string input_dir, const std::string input_file, const std::vector< std::string > names, const std::vector< size_t > use_cols={}, const size_t header_lines_to_skip=1)
function to read from an ascii file
Definition: Table.cpp:163
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void Print(const T value, const int prec, const int ww, const std::string header="", const std::string end="\n", const bool use_coutCBL=true, std::ostream &stream=std::cout, const std::string colour=cbl::par::col_default)
function to print values with a proper homegenised format
Definition: Kernel.h:1142
void checkDim(const std::vector< T > vect, const int val, const std::string vector, bool equal=true)
check if the dimension of a std::vector is equal/lower than an input value
Definition: Kernel.h:1532
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780
std::vector< std::vector< T > > transpose(std::vector< std::vector< T >> matrix)
transpose a matrix
Definition: Kernel.h:1729