CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Data.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 "Data.h"
35 #include "Data1D.h"
36 #include "Data2D.h"
37 #include "Data1D_collection.h"
38 #include "Data1D_extra.h"
39 #include "Data2D_extra.h"
40 
41 using namespace std;
42 
43 using namespace cbl;
44 using namespace data;
45 
46 
47 // ======================================================================================
48 
49 
50 shared_ptr<Data> cbl::data::Data::Create (const DataType dataType)
51 {
52  if (dataType==DataType::_1D_) return move(unique_ptr<Data1D>(new Data1D()));
53  else if (dataType==DataType::_2D_) return move(unique_ptr<Data2D>(new Data2D()));
54  else if (dataType==DataType::_1D_collection_) return move(unique_ptr<Data1D_collection>(new Data1D_collection()));
55  else if (dataType==DataType::_1D_extra_) return move(unique_ptr<Data1D_extra>(new Data1D_extra()));
56  else if (dataType==DataType::_2D_extra_) return move(unique_ptr<Data2D_extra>(new Data2D_extra()));
57  else ErrorCBL("no such type of object, or error in the input parameters!", "Create", "Data.cpp");
58 
59  return NULL;
60 }
61 
62 
63 // ======================================================================================
64 
65 
66 void cbl::data::Data::reset (const int ndata)
67 {
68  m_ndata = ndata;
69  m_data.resize(m_ndata, 0);
70  m_error.resize(m_ndata, 0);
71  m_covariance.resize(m_ndata, vector<double>(m_ndata, 0));
72  m_inverse_covariance.resize(m_ndata, vector<double>(m_ndata, 0));
73 }
74 
75 
76 // ======================================================================================
77 
78 
79 cbl::data::Data::Data (const DataType dataType, const std::vector<double> data)
80 {
81  m_dataType = dataType;
82  reset(data.size());
83 
84  set_data(data);
85 }
86 
87 
88 // ======================================================================================
89 
90 
91 cbl::data::Data::Data (const DataType dataType, const std::vector<double> data, const std::vector<double> error)
92 {
93  m_dataType = dataType;
94  reset(data.size());
95 
96  set_data(data);
97  set_error(error);
98  set_covariance(error);
99 }
100 
101 
102 // ======================================================================================
103 
104 
105 cbl::data::Data::Data (const DataType dataType, const std::vector<double> data, const std::vector<std::vector<double>> covariance)
106 {
107  m_dataType = dataType;
108  reset(data.size());
109 
110  set_data(data);
111  set_covariance(covariance);
112  set_error(covariance);
113 }
114 
115 
116 // ======================================================================================
117 
118 
119 std::vector<std::vector<double>> cbl::data::Data::correlation() const
120 {
121  vector<vector<double>> corr(m_ndata, vector<double>(m_ndata,0));
122 
123  for (int i=0; i<m_ndata; i++)
124  for (int j=0; j<m_ndata; j++)
125  corr[i][j] = m_covariance[i][j]/sqrt(m_covariance[i][i]*m_covariance[j][j]);
126 
127  return corr;
128 }
129 
130 
131 // ======================================================================================
132 
133 
134 void cbl::data::Data::set_data(const std::vector<double> data)
135 {
136  checkDim(data, m_ndata, "data");
137  m_data = data;
138 }
139 
140 
141 // ======================================================================================
142 
143 
144 void cbl::data::Data::set_error(const std::vector<double> error)
145 {
146  checkDim(error, m_ndata, "error");
147  m_error = error;
148 }
149 
150 
151 // ======================================================================================
152 
153 
154 void cbl::data::Data::set_error(const std::vector<std::vector<double>> covariance)
155 {
156  checkDim(covariance, m_ndata, m_ndata, "covariance");
157 
158  for (int i=0; i<m_ndata; i++)
159  m_error[i] = sqrt(covariance[i][i]);
160 }
161 
162 
163 
164 // ======================================================================================
165 
166 
167 void cbl::data::Data::set_covariance (const std::vector<double> error)
168 {
169  checkDim(error, m_ndata, "error");
170 
171  for (int i=0; i<m_ndata; i++)
172  m_covariance[i][i] = pow(m_error[i], 2);
173 }
174 
175 
176 // ======================================================================================
177 
178 
179 void cbl::data::Data::set_covariance (const std::vector<std::vector<double>> covariance)
180 {
181  checkDim(covariance, m_ndata, m_ndata, "covariance");
182 
183  m_covariance = covariance;
184  set_error(covariance);
185 }
186 
187 
188 // ======================================================================================
189 
190 
191 void cbl::data::Data::set_covariance (const std::string filename, const int cov_col, const int skipped_lines)
192 {
193  ifstream fin(filename.c_str()); checkIO(fin, filename);
194  string line;
195 
196  for (int i=0; i<skipped_lines; ++i) getline(fin, line);
197 
198  vector<double> covariance;
199 
200  while (getline(fin, line)) {
201 
202  stringstream ss(line);
203  vector<double> num; double NN = par::defaultDouble;
204  while (ss>>NN) num.push_back(NN);
205 
206  if (int(num.size())>=cov_col && num[cov_col]>par::defaultDouble)
207  covariance.push_back(num[cov_col]);
208  }
209 
210  fin.clear(); fin.close();
211 
212  m_covariance = reshape(covariance, m_ndata, m_ndata);
213 
214  set_covariance(m_covariance);
215  set_error(m_covariance);
216 }
217 
218 
219 // ======================================================================================
220 
221 
222 void cbl::data::Data::cut (const std::vector<bool> mask, std::vector<double> &data, std::vector<double> &error, std::vector<std::vector<double>> &covariance_matrix) const
223 {
224  checkDim (mask, m_ndata, "mask");
225 
226  int ndata_eff = 0;
227 
228  for (int i=0; i<m_ndata; i++)
229  if (mask[i])
230  ndata_eff +=1;
231 
232  if (ndata_eff <1)
233  ErrorCBL("no elements left!", "cut", "Data.cpp");
234 
235  data.resize(ndata_eff, 0);
236  error.resize(ndata_eff, 0);
237  covariance_matrix.resize(ndata_eff, vector<double>(ndata_eff, 0));
238 
239  int index1 = 0;
240  for (int i=0; i<m_ndata; i++) {
241  if (mask[i]) {
242  data[index1] = m_data[i];
243  error[index1] = m_error[i];
244 
245  int index2 = 0;
246  for (int j=0; j<m_ndata; j++) {
247  if (mask[j]) {
248  covariance_matrix[index1][index2] = m_covariance[i][j];
249  index2++;
250  }
251  }
252  index1++;
253  }
254  }
255 }
256 
257 
258 // ============================================================================
259 
260 
261 shared_ptr<data::Data> cbl::data::join_dataset (std::vector<std::shared_ptr<data::Data>> dataset)
262 {
263  if (dataset.size()<2)
264  ErrorCBL("at least 2 dataset have to be provided!", "join_dataset", "Data.cpp");
265 
266  data::DataType dt = dataset[0]->dataType();
267 
268  for (size_t i=0; i<dataset.size(); i++)
269  if (dt!=dataset[i]->dataType())
270  ErrorCBL("the dataset types must be equal!", "join_dataset", "Data.cpp");
271 
272  switch(dt) {
273 
274  case data::DataType::_1D_:
275  return join_dataset_1D(dataset);
276  break;
277 
278  case data::DataType::_1D_extra_:
279  return join_dataset_1D_extra(dataset);
280  break;
281 
282  default:
283  ErrorCBL("", "join_dataset", "Data.cpp");
284 
285  }
286 
287  return NULL;
288 }
289 
290 
291 // ============================================================================
292 
293 
294 shared_ptr<data::Data> cbl::data::join_dataset_1D(std::vector<std::shared_ptr<data::Data>> dataset)
295 {
296  int ndataset = (int)dataset.size();
297  int ndata = 0;
298  vector<vector<int>> data_index;
299 
300  int nn = 0;
301  for (int i=0; i<ndataset; i++) {
302  ndata+=dataset[i]->ndata();
303  vector<int> _dd;
304  for (int j=0; j<dataset[i]->ndata(); j++) {
305  _dd.push_back(nn);
306  nn++;
307  }
308  data_index.push_back(_dd);
309  }
310 
311  vector<double> xx(ndata, 0), data(ndata, 0);
312  vector<vector<double>> covariance(ndata, vector<double>(ndata, 0));
313 
314  for (int i=0; i<ndataset;i++)
315  for (int j=0; j<dataset[i]->ndata(); j++) {
316  xx[data_index[i][j]] = dataset[i]->xx(j);
317  data[data_index[i][j]] = dataset[i]->data(j);
318 
319  for (int k=0; k<dataset[i]->ndata(); k++)
320  covariance[data_index[i][j]][data_index[i][k]] = dataset[i]->covariance(j, k);
321  }
322 
323  return move(unique_ptr<data::Data1D>(new data::Data1D(xx, data, covariance)));
324 }
325 
326 
327 // ============================================================================
328 
329 
330 shared_ptr<data::Data> data::join_dataset_1D_extra (std::vector<std::shared_ptr<data::Data>> dataset)
331 {
332  ErrorCBL("", "join_dataset_1D_extra", "Data.cpp", glob::ExitCode::_workInProgress_);
333  int ndataset = (int)dataset.size();
334  int ndata = 0;
335  vector<int> n_data(ndataset);
336  int nextra = dataset[0]->extra_info().size();
337 
338  for (int i=0; i<ndataset; i++)
339  ndata += dataset[i]->ndata();
340 
341  vector<double> xx(ndata, 0), data(ndata, 0);
342  vector<vector<double>> extra_info (nextra, vector<double>(ndata, 0));
343  vector<vector<double>> covariance(ndata, vector<double>(ndata, 0));
344 
345  auto merged_dataset = make_shared<data::Data1D_extra>(data::Data1D_extra(xx, data, covariance, extra_info));
346 
347  return merged_dataset;
348 }
The class Data1D.
The class Data1D_collection.
The class Data1D_extra.
The class Data2D.
The class Data2D_extra.
The class Data.
The class Data1D_collection.
The class Data1D_extra.
Definition: Data1D_extra.h:52
The class Data1D.
Definition: Data1D.h:51
The class Data2D_extra.
Definition: Data2D_extra.h:52
The class Data2D.
Definition: Data2D.h:51
std::vector< std::vector< double > > correlation() const
get the value of the data correlation at index i,j
Definition: Data.cpp:119
void cut(const std::vector< bool > mask, std::vector< double > &data, std::vector< double > &error, std::vector< std::vector< double >> &covariance_matrix) const
cut the dataset using a mask
Definition: Data.cpp:222
virtual void set_data(const std::vector< std::vector< double >> data)
set interval variable m_data, for Data1D_collection, Data2D
Definition: Data.h:535
void set_covariance(const std::string filename, const int cov_col=2, const int skipped_lines=0)
set the interval variable m_covariance, reading from an input file
Definition: Data.cpp:191
void set_error(const std::vector< double > error)
set interval variable m_error_fx
Definition: Data.cpp:144
Data()=default
default constructor
void reset(const int ndata)
reset data object with new empty arrays large enough to store ndata data
Definition: Data.cpp:66
static std::shared_ptr< Data > Create(const DataType dataType)
static factory used to construct objects of class Data1D
Definition: Data.cpp:50
static const double defaultDouble
default double value
Definition: Constants.h:348
std::shared_ptr< data::Data > join_dataset_1D(std::vector< std::shared_ptr< data::Data >> dataset)
merge dataset of type 1D
Definition: Data.cpp:294
std::shared_ptr< data::Data > join_dataset_1D_extra(std::vector< std::shared_ptr< data::Data >> dataset)
merge dataset of type 1D_extra
Definition: Data.cpp:330
DataType
the data type
Definition: Data.h:53
std::shared_ptr< data::Data > join_dataset(std::vector< std::shared_ptr< data::Data >> dataset)
merge dataset (only work for one dataset type)
Definition: Data.cpp:261
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
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 > > reshape(std::vector< T > vec, const int size1, const int size2)
reshape a vector into a matrix of given number of rows and columns
Definition: Kernel.h:1707
void covariance_matrix(const std::vector< std::vector< double >> mat, std::vector< std::vector< double >> &cov, const bool JK=false)
compute the covariance matrix from an input dataset
Definition: Func.cpp:761