CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Data1D.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 "Data1D.h"
35 
36 using namespace std;
37 
38 using namespace cbl;
39 using namespace data;
40 
41 
42 // ======================================================================================
43 
44 
45 void cbl::data::Data1D::set_xx (const vector<double> x)
46 {
47  checkDim(x, ndata(), "x");
48  m_x = x;
49  m_xsize = ndata();
50 }
51 
52 
53 // ======================================================================================
54 
55 
56 void cbl::data::Data1D::read (const string input_file, const int skip_nlines, const vector<int> column, const vector<int> column_data, const vector<int> column_errors, const vector<int> column_edges)
57 {
58  if (column_data.size()!=column_errors.size()) ErrorCBL("the sizes of column_data and columt_errors must be equal!", "read", "Data1D.cpp");
59 
60  // default column of input data values
61  int column_data_default = column[0]+1;
62 
63  // default column of input error values
64  int column_error_default = column[0]+2;
65 
66  // default first column of input edge values
67  int column_edges_default = column[0]+3;
68 
69  ifstream fin(input_file.c_str()); checkIO(fin, input_file);
70  string line;
71 
72  // loop on the data (and, possibly, error) vectors to be read; if
73  // more than one data vectors are read, the data (and, possibly,
74  // error) vectors will be added one after the other, in order to
75  // construct a single data object
76  const int cl_max = (column_data.size()<2) ? 1 : column_data.size();
77  for (int cl=0; cl<cl_max; ++cl) {
78 
79  // skip the first nlines (in case of header lines)
80  if (skip_nlines>0)
81  for (int i=0; i<skip_nlines; ++i)
82  getline(fin, line);
83 
84  // read the file lines
85  double last_bin_edge = par::defaultDouble;
86  while (getline(fin, line)) {
87 
88  stringstream ss(line);
89  vector<double> num; double NUM;
90  while (ss>>NUM) num.emplace_back(NUM);
91 
92  if (num.size()<2) ErrorCBL("there are less than 2 columns in the input file!", "read", "Data1D.cpp");
93 
94  // if column[0] is larger than 0, the column of x coordinates is
95  // the one specified in input
96  size_t ind_x = max(0, column[0]-1);
97  checkDim(num, ind_x+1, "num", false);
98  m_x.emplace_back(num[ind_x]);
99 
100  // if the size of the column_data vector is 0, the columns of
101  // data values are the ones specified in input
102  const size_t ind_data = (column_data.size()==0) ? column_data_default-1 : column_data[cl]-1;
103  checkDim(num, ind_data+1, "num", false);
104  m_data.emplace_back(num[ind_data]);
105 
106  // if the size of the column_errors vector is 0, the columns of
107  // error values are the ones specified in input; if the error
108  // column is not present, the errors will be set to 1, by
109  // default
110  const size_t ind_error = (column_errors.size()==0) ? column_error_default-1 : column_errors[cl]-1;
111  if (num.size()<ind_error+1 && column_errors.size()>1)
112  WarningMsgCBL("the errors cannot be retrieved from the provided input file, and will be set to 1", "read", "Data1D.cpp");
113  m_error.emplace_back((num.size()<ind_error+1) ? 1 : num[ind_error]);
114 
115  // if the size of the column_edges vector is 0, the columns of
116  // bin edge values are the ones specified in input
117  const size_t ind_edges = (column_edges.size()==0) ? column_edges_default-1 : column_edges[cl]-1;
118  if (num.size()>ind_edges+1) {
119  m_edges_xx.emplace_back(num[ind_edges]);
120  last_bin_edge = num[ind_edges+1];
121  }
122 
123  }
124 
125  // add the last bin edge to m_edges_xx
126  if (last_bin_edge>par::defaultDouble)
127  m_edges_xx.emplace_back(last_bin_edge);
128 
129  // go back at the beginning of the file
130  fin.clear(); fin.seekg(ios::beg);
131 
132  column_data_default += 2;
133  column_error_default += 2;
134  }
135 
136  fin.clear(); fin.close();
137 
138 
139  // set the data size and diagonal covariance
140 
141  m_xsize = m_data.size();
142  m_ndata = m_data.size();
143 
144  m_covariance.resize(m_ndata, vector<double>(m_ndata, 0));
145  for (int i=0; i<m_ndata; i++)
146  m_covariance[i][i] = pow(m_error[i], 2);
147 
148 }
149 
150 
151 // ======================================================================================
152 
153 
154 void cbl::data::Data1D::Print (const int precision) const
155 {
156  for (size_t i=0; i<m_x.size(); i++)
157  coutCBL << std::fixed << setprecision(precision) << setw(8) << right << m_x[i]
158  << " " << std::fixed << setprecision(precision) << setw(8) << right << m_data[i]
159  << " " << std::fixed << setprecision(precision) << setw(8) << right << m_error[i] << endl;
160 }
161 
162 // ======================================================================================
163 
164 
165 void cbl::data::Data1D::write (const string dir, const string file, const string header, const int prec, const int ww, int rank) const
166 {
167  (void)rank;
168 
169  string file_out = dir+file;
170  ofstream fout(file_out.c_str()); checkIO(fout, file_out);
171 
172  if (header!=par::defaultString)
173  fout << "### " << header << " ###" << endl;
174 
175  const int bp = std::cout.precision();
176 
177  for (size_t i=0; i<m_x.size(); i++) {
178  cbl::Print(m_x[i], prec, ww, "", " ", false, fout);
179  cbl::Print(m_data[i], prec, ww, "", " ", false, fout);
180  cbl::Print(m_error[i], prec, ww, "", "\n", false, fout);
181  }
182  cout.precision(bp);
183  fout.close(); cout << endl; coutCBL << "I wrote the file: " << file_out << endl;
184 }
185 
186 
187 // ======================================================================================
188 
189 
190 void cbl::data::Data1D::write_covariance (const string dir, const string file, const int precision) const
191 {
192  checkDim(m_covariance, m_ndata, m_ndata, "covariance", false);
193 
194  string file_out = dir+file;
195  ofstream fout(file_out.c_str()); checkIO(fout, file_out);
196 
197  fout << "### [1] r1 # [2] r2 # [3] covariance # [4] correlation # [5] index1 # [6] index2 ### " << endl;
198 
199  for (int i=0; i<m_ndata; ++i)
200  for (int j=0; j<m_ndata; ++j)
201  fout << setiosflags(ios::fixed) << setprecision(precision) << setw(15) << right << m_x[i]
202  << " " << setiosflags(ios::fixed) << setprecision(precision) << setw(15) << right << m_x[j]
203  << " " << setiosflags(ios::fixed) << setprecision(precision) << setw(15) << right << m_covariance[i][j]
204  << " " << setiosflags(ios::fixed) << setprecision(precision) << setw(15) << right << m_covariance[i][j]/sqrt(m_covariance[i][i]*m_covariance[j][j])
205  << " " << setiosflags(ios::fixed) << setprecision(precision) << setw(5) << right << i
206  << " " << setiosflags(ios::fixed) << setprecision(precision) << setw(5) << right << j << endl;
207 
208  fout.close(); cout << endl; coutCBL << "I wrote the file: " << file_out << endl;
209 }
210 
211 
212 // ======================================================================================
213 
214 
215 std::shared_ptr<Data> cbl::data::Data1D::cut (const std::vector<bool> mask) const
216 {
217  checkDim(mask, m_ndata, "mask");
218 
219  vector<double> xx, edges_xx;
220  int last_index = 0;
221  for (size_t i=0; i<mask.size(); i++){
222  if (mask[i]){
223  xx.push_back(m_x[i]);
224  if (m_edges_xx.size() > 0)
225  edges_xx.push_back(m_edges_xx[i]);
226  last_index = i;
227  }
228  }
229  if (m_edges_xx.size() > 0)
230  edges_xx.push_back(m_edges_xx[last_index+1]);
231 
232  vector<double> data, error;
233  vector<vector<double>> covariance;
234  Data::cut(mask, data, error, covariance);
235 
236  shared_ptr<Data> dd = make_shared<Data1D>(Data1D(xx, data, covariance, -1, edges_xx));
237 
238  return dd;
239 }
240 
241 
242 
243 // ======================================================================================
244 
245 
246 shared_ptr<Data> cbl::data::Data1D::cut (const double xmin, const double xmax) const
247 {
248  vector<bool> mask(m_ndata, true);
249  for (int i=0; i<m_ndata; i++)
250  if ((m_x[i] < xmin) || (m_x[i]>xmax))
251  mask[i] = false;
252 
253  return cut(mask);
254 }
255 
256 
257 // ======================================================================================
258 
259 
260 shared_ptr<Data> cbl::data::Data1D::as_factory ()
261 {
262  shared_ptr<Data> dd = make_shared<Data1D>(Data1D(m_x, m_data, m_covariance));
263 
264  return dd;
265 }
266 
The class Data1D.
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class Data1D.
Definition: Data1D.h:51
std::shared_ptr< Data > cut(const double xmin, const double xmax) const override
cut the data
Definition: Data1D.cpp:246
void write(const std::string dir, const std::string file, const std::string header, const int prec=4, const int ww=8, const int rank=0) const override
write the data
Definition: Data1D.cpp:165
std::shared_ptr< Data > as_factory()
static factory used to construct objects of class Data1D
Definition: Data1D.cpp:260
virtual void Print(const int precision=4) const override
print the data on screen
Definition: Data1D.cpp:154
void write_covariance(const std::string dir, const std::string file, const int precision=10) const override
write the covariance
Definition: Data1D.cpp:190
void read(const std::string input_file, const int skip_nlines=0, const std::vector< int > column={1}, const std::vector< int > column_data={}, const std::vector< int > column_errors={}, const std::vector< int > column_edges={}) override
read the data
Definition: Data1D.cpp:56
void set_xx(const std::vector< double > x)
set interval variable m_x
Definition: Data1D.cpp:45
static const std::string defaultString
default std::string value
Definition: Constants.h:336
static const double defaultDouble
default double value
Definition: Constants.h:348
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
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message
Definition: Kernel.h:747