BayesGlobalOperation.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/sa/core/BayesGlobalOperation.cpp
22 
23  \brief This file contains a class that represents the bayes global operation.
24 
25  \reference Adapted from TerraLib4.
26 
27  \reference Mapping Disease and Mortality Rates Using Empirical Bayes Estimators
28  Roger J. Marshall
29 */
30 
31 //TerraLib
32 #include "../../common/Exception.h"
33 #include "../../core/translator/Translator.h"
34 #include "../../common/progress/TaskProgress.h"
35 #include "../../dataaccess/datasource/DataSource.h"
36 #include "../../dataaccess/datasource/DataSourceManager.h"
37 #include "../../dataaccess/utils/Utils.h"
38 #include "../../datatype/SimpleProperty.h"
39 #include "../../geometry/Geometry.h"
40 #include "../../geometry/GeometryProperty.h"
41 #include "../../memory/DataSet.h"
42 #include "../../memory/DataSetItem.h"
43 #include "BayesGlobalOperation.h"
44 #include "Utils.h"
45 
46 //STL
47 #include <cassert>
48 
50 
52 
54 {
55  assert(m_inputParams->m_ds.get());
56  assert(m_inputParams->m_dsType.get());
57 
58  //create output data
59  std::unique_ptr<te::da::DataSetType> outDsType = createDataSetType(m_inputParams->m_dsType.get());
60 
61  std::unique_ptr<te::mem::DataSet> outDs = createDataSet(m_inputParams->m_ds.get(), outDsType.get());
62 
63  //run bayes
64  std::size_t eventIdx = outDsType->getPropertyPosition(m_inputParams->m_eventAttrName);
65  std::size_t popIdx = outDsType->getPropertyPosition(m_inputParams->m_populationAttrName);
66  std::size_t bayesIdx = outDsType->getPropertyPosition(TE_SA_BAYES_ATTR_NAME);
67 
68  runBayesGlobal(outDs.get(), eventIdx, popIdx, bayesIdx);
69 
70  //save data
71  saveDataSet(outDs.get(), outDsType.get());
72 }
73 
75 {
76  m_inputParams.reset(inParams);
77  m_outputParams.reset(outParams);
78 }
79 
81 {
82  //save dataset
83  dataSet->moveBeforeFirst();
84 
85  std::map<std::string, std::string> options;
86 
87  m_outputParams->m_ds->createDataSet(dsType, options);
88 
89  m_outputParams->m_ds->add(m_outputParams->m_outputDataSetName, dataSet, options);
90 }
91 
92 std::unique_ptr<te::da::DataSetType> te::sa::BayesGlobalOperation::createDataSetType(te::da::DataSetType* dsType)
93 {
94  std::unique_ptr<te::da::DataSetType> dataSetType(new te::da::DataSetType(m_outputParams->m_outputDataSetName));
95 
96  //create all input dataset properties
97  std::vector<te::dt::Property*> propertyVec = dsType->getProperties();
98 
99  for(std::size_t t = 0; t < propertyVec.size(); ++t)
100  {
101  te::dt::Property* prop = propertyVec[t];
102 
103  te::dt::Property* newProp = prop->clone();
104  newProp->setId(0);
105  newProp->setParent(nullptr);
106 
107  dataSetType->add(newProp);
108  }
109 
110  //create bayes property
112  dataSetType->add(bayesProperty);
113 
114  return dataSetType;
115 }
116 
117 std::unique_ptr<te::mem::DataSet> te::sa::BayesGlobalOperation::createDataSet(te::da::DataSet* inputDataSet, te::da::DataSetType* dsType)
118 {
119  std::unique_ptr<te::mem::DataSet> outDataset(new te::mem::DataSet(dsType));
120 
121  std::size_t nProp = inputDataSet->getNumProperties();
122 
123  inputDataSet->moveBeforeFirst();
124 
125  while(inputDataSet->moveNext())
126  {
127  //create dataset item
128  te::mem::DataSetItem* outDSetItem = new te::mem::DataSetItem(outDataset.get());
129 
130  for(std::size_t t = 0; t < nProp; ++t)
131  {
132  te::dt::AbstractData* ad = inputDataSet->getValue(t).release();
133 
134  outDSetItem->setValue(t, ad);
135  }
136 
137  //set kernel default value
138  outDSetItem->setDouble(TE_SA_BAYES_ATTR_NAME, 0.);
139 
140  //add item into dataset
141  outDataset->add(outDSetItem);
142  }
143 
144  return outDataset;
145 }
146 
147 void te::sa::BayesGlobalOperation::runBayesGlobal(te::mem::DataSet* ds, std::size_t eventIdx, std::size_t popIdx, std::size_t bayesIdx)
148 {
149  std::vector<double> eventVecY;
150  std::vector<double> popVecN;
151  std::vector<double> rateVec;
152  double sumPopN = 0.;
153  double sumEventY = 0.;
154  int count = 0;
155 
156  ds->moveBeforeFirst();
157 
158  {
159  //create task
161 
162  task.setTotalSteps(static_cast<int>(ds->size()));
163  task.setMessage(TE_TR("Getting Event/Population information."));
164 
165  while(ds->moveNext())
166  {
167  ++ count;
168 
169  double eventAttr = ds->getDouble(eventIdx);
170  eventVecY.push_back(eventAttr);
171 
172  double popAttr = ds->getDouble(popIdx);
173  popVecN.push_back(popAttr);
174 
175  if(popAttr <= 0.)
176  throw te::common::Exception(TE_TR("Invalid value (0) for Population Attribute."));
177 
178  rateVec.push_back(eventVecY[count-1] / popVecN[count-1]);
179 
180  sumEventY += eventVecY[count-1];
181  sumPopN += popVecN[count-1];
182 
183  if(!task.isActive())
184  {
185  throw te::common::Exception(TE_TR("Operation canceled by the user."));
186  }
187 
188  task.pulse();
189  }
190  }
191 
192  double mean = sumEventY / sumPopN;
193 
194  double meanPop = sumPopN / (double)count;
195 
196  double variance = 0.;
197 
198  for(int i = 0; i < count; ++i)
199  {
200  variance += popVecN[i] * pow((rateVec[i] - mean), 2);
201  }
202 
203  variance /= sumPopN;
204 
205  double aux = variance - (mean / meanPop);
206 
207  if(aux < 0.)
208  aux = 0.;
209 
210  //calculate bayes value
211  ds->moveBeforeFirst();
212 
213  {
214  //create task
216 
217  task.setTotalSteps(static_cast<int>(ds->size()));
218  task.setMessage(TE_TR("Calculating Bayes Value."));
219 
220  while(ds->moveNext())
221  {
222  double eventAttr = ds->getDouble(eventIdx);
223  double popAttr = ds->getDouble(popIdx);
224 
225  double wI = 1.;
226 
227  if(aux != 0. || mean != 0.)
228  wI = aux / (aux + (mean / popAttr));
229 
230  double thetaI = wI * (eventAttr / popAttr) + ( 1 - wI) * mean;
231 
232  ds->setDouble(bayesIdx, thetaI * m_inputParams->m_rate);
233 
234  if(!task.isActive())
235  {
236  throw te::common::Exception(TE_TR("Operation canceled by the user."));
237  }
238 
239  task.pulse();
240  }
241  }
242 }
std::size_t size() const
It returns the collection size, if it is known.
Class that represents the Bayes output parameters.
Definition: BayesParams.h:94
void setMessage(const std::string &message)
Set the task message.
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
An atomic property like an integer or double.
std::unique_ptr< te::mem::DataSet > createDataSet(te::da::DataSet *inputDataSet, te::da::DataSetType *dsType)
A class that models the description of a dataset.
Definition: DataSetType.h:72
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
void execute()
Function to execute the bayes operation.
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
virtual Property * clone() const =0
It returns a clone of the object.
This file contains a class that represents the bayes global operation.
It models a property definition.
Definition: Property.h:59
bool isActive() const
Verify if the task is active.
void setTotalSteps(int value)
Set the task total stepes.
void setId(unsigned int id)
It sets the property identifier.
Definition: Property.h:118
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
void saveDataSet(te::da::DataSet *dataSet, te::da::DataSetType *dsType)
#define TE_SA_BAYES_ATTR_NAME
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
~BayesGlobalOperation()
Virtual destructor.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
Utility functions for the data access module.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.
bool moveNext()
It moves the internal pointer to the next item of the collection.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A dataset is the unit of information manipulated by the data access module of TerraLib.
Class that represents the Bayes input parameters.
Definition: BayesParams.h:56
double getDouble(std::size_t i) const
Method for retrieving a double attribute value.
virtual std::unique_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
BayesGlobalOperation()
Default constructor.
void setParameters(te::sa::BayesInputParams *inParams, te::sa::BayesOutputParams *outParams)
void runBayesGlobal(te::mem::DataSet *ds, std::size_t eventIdx, std::size_t popIdx, std::size_t bayesIdx)
std::unique_ptr< te::da::DataSetType > createDataSetType(te::da::DataSetType *dsType)
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
std::unique_ptr< te::sa::BayesOutputParams > m_outputParams
Attribute with the bayes output parameters.
std::unique_ptr< te::sa::BayesInputParams > m_inputParams
Attribute with the bayes input parameters.
void setDouble(std::size_t i, double value)
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177