All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../common/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 {
51 }
52 
54 {
55 }
56 
58 {
59  assert(m_inputParams->m_ds.get());
60  assert(m_inputParams->m_dsType.get());
61 
62  //create output data
63  std::auto_ptr<te::da::DataSetType> outDsType = createDataSetType(m_inputParams->m_dsType.get());
64 
65  std::auto_ptr<te::mem::DataSet> outDs = createDataSet(m_inputParams->m_ds.get(), outDsType.get());
66 
67  //run bayes
68  std::size_t eventIdx = outDsType->getPropertyPosition(m_inputParams->m_eventAttrName);
69  std::size_t popIdx = outDsType->getPropertyPosition(m_inputParams->m_populationAttrName);
70  std::size_t bayesIdx = outDsType->getPropertyPosition(TE_SA_BAYES_ATTR_NAME);
71 
72  runBayesGlobal(outDs.get(), eventIdx, popIdx, bayesIdx);
73 
74  //save data
75  saveDataSet(outDs.get(), outDsType.get());
76 }
77 
79 {
80  m_inputParams.reset(inParams);
81  m_outputParams.reset(outParams);
82 }
83 
85 {
86  //save dataset
87  dataSet->moveBeforeFirst();
88 
89  std::map<std::string, std::string> options;
90 
91  m_outputParams->m_ds->createDataSet(dsType, options);
92 
93  m_outputParams->m_ds->add(m_outputParams->m_outputDataSetName, dataSet, options);
94 }
95 
96 std::auto_ptr<te::da::DataSetType> te::sa::BayesGlobalOperation::createDataSetType(te::da::DataSetType* dsType)
97 {
98  std::auto_ptr<te::da::DataSetType> dataSetType(new te::da::DataSetType(m_outputParams->m_outputDataSetName));
99 
100  //create all input dataset properties
101  std::vector<te::dt::Property*> propertyVec = dsType->getProperties();
102 
103  for(std::size_t t = 0; t < propertyVec.size(); ++t)
104  {
105  te::dt::Property* prop = propertyVec[t];
106 
107  te::dt::Property* newProp = prop->clone();
108  newProp->setId(0);
109  newProp->setParent(0);
110 
111  dataSetType->add(newProp);
112  }
113 
114  //create bayes property
116  dataSetType->add(bayesProperty);
117 
118  return dataSetType;
119 }
120 
121 std::auto_ptr<te::mem::DataSet> te::sa::BayesGlobalOperation::createDataSet(te::da::DataSet* inputDataSet, te::da::DataSetType* dsType)
122 {
123  std::auto_ptr<te::mem::DataSet> outDataset(new te::mem::DataSet(dsType));
124 
125  std::size_t nProp = inputDataSet->getNumProperties();
126 
127  inputDataSet->moveBeforeFirst();
128 
129  while(inputDataSet->moveNext())
130  {
131  //create dataset item
132  te::mem::DataSetItem* outDSetItem = new te::mem::DataSetItem(outDataset.get());
133 
134  for(std::size_t t = 0; t < nProp; ++t)
135  {
136  te::dt::AbstractData* ad = inputDataSet->getValue(t).release();
137 
138  outDSetItem->setValue(t, ad);
139  }
140 
141  //set kernel default value
142  outDSetItem->setDouble(TE_SA_BAYES_ATTR_NAME, 0.);
143 
144  //add item into dataset
145  outDataset->add(outDSetItem);
146  }
147 
148  return outDataset;
149 }
150 
151 void te::sa::BayesGlobalOperation::runBayesGlobal(te::mem::DataSet* ds, std::size_t eventIdx, std::size_t popIdx, std::size_t bayesIdx)
152 {
153  std::vector<double> eventVecY;
154  std::vector<double> popVecN;
155  std::vector<double> rateVec;
156  double sumPopN = 0.;
157  double sumEventY = 0.;
158  int count = 0;
159 
160  ds->moveBeforeFirst();
161 
162  {
163  //create task
165 
166  task.setTotalSteps(ds->size());
167  task.setMessage(TE_TR("Getting Event/Population information."));
168 
169  while(ds->moveNext())
170  {
171  ++ count;
172 
173  double eventAttr = ds->getDouble(eventIdx);
174  eventVecY.push_back(eventAttr);
175 
176  double popAttr = ds->getDouble(popIdx);
177  popVecN.push_back(popAttr);
178 
179  if(popAttr <= 0.)
180  throw te::common::Exception(TE_TR("Invalid value (0) for Population Attribute."));
181 
182  rateVec.push_back(eventVecY[count-1] / popVecN[count-1]);
183 
184  sumEventY += eventVecY[count-1];
185  sumPopN += popVecN[count-1];
186 
187  if(!task.isActive())
188  {
189  throw te::common::Exception(TE_TR("Operation canceled by the user."));
190  }
191 
192  task.pulse();
193  }
194  }
195 
196  double mean = sumEventY / sumPopN;
197 
198  double meanPop = sumPopN / (double)count;
199 
200  double variance = 0.;
201 
202  for(int i = 0; i < count; ++i)
203  {
204  variance += popVecN[i] * pow((rateVec[i] - mean), 2);
205  }
206 
207  variance /= sumPopN;
208 
209  double aux = variance - (mean / meanPop);
210 
211  if(aux < 0.)
212  aux = 0.;
213 
214  //calculate bayes value
215  ds->moveBeforeFirst();
216 
217  {
218  //create task
220 
221  task.setTotalSteps(ds->size());
222  task.setMessage(TE_TR("Calculating Bayes Value."));
223 
224  while(ds->moveNext())
225  {
226  double eventAttr = ds->getDouble(eventIdx);
227  double popAttr = ds->getDouble(popIdx);
228 
229  double wI = 1.;
230 
231  if(aux != 0. || mean != 0.)
232  wI = aux / (aux + (mean / popAttr));
233 
234  double thetaI = wI * (eventAttr / popAttr) + ( 1 - wI) * mean;
235 
236  ds->setDouble(bayesIdx, thetaI * m_inputParams->m_rate);
237 
238  if(!task.isActive())
239  {
240  throw te::common::Exception(TE_TR("Operation canceled by the user."));
241  }
242 
243  task.pulse();
244  }
245  }
246 }
std::size_t size() const
It returns the collection size, if it is known.
Definition: DataSet.cpp:311
Class that represents the Bayes output parameters.
Definition: BayesParams.h:94
void setMessage(const std::string &message)
Set the task message.
Utility functions for the data access module.
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::auto_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
virtual Property * clone() const =0
It returns a clone of the object.
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.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
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...
Definition: DataSet.h:65
void saveDataSet(te::da::DataSet *dataSet, te::da::DataSetType *dsType)
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
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.
Definition: DataSet.cpp:328
bool moveNext()
It moves the internal pointer to the next item of the collection.
Definition: DataSet.cpp:316
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
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.
Definition: DataSet.cpp:477
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)
#define TE_SA_BAYES_ATTR_NAME
Definition: Config.h:80
std::auto_ptr< te::da::DataSetType > createDataSetType(te::da::DataSetType *dsType)
void setDouble(std::size_t i, double value)
Definition: DataSet.cpp:482
virtual std::auto_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
Definition: DataSet.cpp:151
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177