All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BayesLocalOperation.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/BayesLocalOperation.cpp
22 
23  \brief This file contains a class that represents the bayes local operation.
24 
25  \reference Adapted from TerraLib4.
26 */
27 
28 //TerraLib
29 #include "../../common/Exception.h"
30 #include "../../common/Translator.h"
31 #include "../../common/progress/TaskProgress.h"
32 #include "../../dataaccess/datasource/DataSource.h"
33 #include "../../dataaccess/datasource/DataSourceManager.h"
34 #include "../../dataaccess/utils/Utils.h"
35 #include "../../datatype/SimpleProperty.h"
36 #include "../../geometry/Geometry.h"
37 #include "../../geometry/GeometryProperty.h"
38 #include "../../graph/core/AbstractGraph.h"
39 #include "../../graph/core/Edge.h"
40 #include "../../graph/core/Vertex.h"
41 #include "../../memory/DataSet.h"
42 #include "../../memory/DataSetItem.h"
43 #include "BayesLocalOperation.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  //associate gpm event attribute
63  std::size_t eventIdx = m_inputParams->m_dsType->getPropertyPosition(m_inputParams->m_eventAttrName);
64  int eventType = m_inputParams->m_ds->getPropertyDataType(eventIdx);
65  int gpmEventIdx = te::sa::AssociateGPMVertexAttribute(m_inputParams->m_gpm.get(), m_inputParams->m_ds.get(), m_inputParams->m_gpmAttrLink, m_inputParams->m_eventAttrName, eventType);
66 
67  //associate gpm population attribute
68  std::size_t popIdx = m_inputParams->m_dsType->getPropertyPosition(m_inputParams->m_populationAttrName);
69  int popType = m_inputParams->m_ds->getPropertyDataType(popIdx);
70  int gpmPopIdx = te::sa::AssociateGPMVertexAttribute(m_inputParams->m_gpm.get(), m_inputParams->m_ds.get(), m_inputParams->m_gpmAttrLink, m_inputParams->m_populationAttrName, popType);
71 
72  //create output data
73  std::auto_ptr<te::da::DataSetType> outDsType = createDataSetType(m_inputParams->m_dsType.get());
74  std::auto_ptr<te::mem::DataSet> outDs = createDataSet(m_inputParams->m_ds.get(), outDsType.get());
75 
76  //run bayes
77  eventIdx = outDsType->getPropertyPosition(m_inputParams->m_eventAttrName);
78  popIdx = outDsType->getPropertyPosition(m_inputParams->m_populationAttrName);
79 
80  std::size_t idIdx = outDsType->getPropertyPosition(m_inputParams->m_gpmAttrLink);
81  std::size_t neighEventIdx = outDsType->getPropertyPosition(TE_SA_BAYESEVENT_ATTR_NAME);
82  std::size_t neighPopIdx = outDsType->getPropertyPosition(TE_SA_BAYESPOP_ATTR_NAME);
83  std::size_t bayesIdx = outDsType->getPropertyPosition(TE_SA_BAYES_ATTR_NAME);
84 
85  runBayesLocal(outDs.get(), idIdx, eventIdx, popIdx, neighEventIdx, neighPopIdx, bayesIdx, gpmEventIdx, gpmPopIdx);
86 
87  //save data
88  saveDataSet(outDs.get(), outDsType.get());
89 }
90 
92 {
93  m_inputParams.reset(inParams);
94  m_outputParams.reset(outParams);
95 }
96 
98 {
99  //save dataset
100  dataSet->moveBeforeFirst();
101 
102  std::map<std::string, std::string> options;
103 
104  m_outputParams->m_ds->createDataSet(dsType, options);
105 
106  m_outputParams->m_ds->add(m_outputParams->m_outputDataSetName, dataSet, options);
107 }
108 
109 std::auto_ptr<te::da::DataSetType> te::sa::BayesLocalOperation::createDataSetType(te::da::DataSetType* dsType)
110 {
111  std::auto_ptr<te::da::DataSetType> dataSetType(new te::da::DataSetType(m_outputParams->m_outputDataSetName));
112 
113  //create all input dataset properties
114  std::vector<te::dt::Property*> propertyVec = dsType->getProperties();
115 
116  for(std::size_t t = 0; t < propertyVec.size(); ++t)
117  {
118  te::dt::Property* prop = propertyVec[t];
119 
120  te::dt::Property* newProp = prop->clone();
121  newProp->setId(0);
122  newProp->setParent(0);
123 
124  dataSetType->add(newProp);
125  }
126 
127  //create neighbour event property
129  dataSetType->add(neighEvent);
130 
131  //create neighbour population property
133  dataSetType->add(neighPop);
134 
135  //create bayes property
137  dataSetType->add(bayesProperty);
138 
139  return dataSetType;
140 }
141 
142 std::auto_ptr<te::mem::DataSet> te::sa::BayesLocalOperation::createDataSet(te::da::DataSet* inputDataSet, te::da::DataSetType* dsType)
143 {
144  std::auto_ptr<te::mem::DataSet> outDataset(new te::mem::DataSet(dsType));
145 
146  std::size_t nProp = inputDataSet->getNumProperties();
147 
148  inputDataSet->moveBeforeFirst();
149 
150  while(inputDataSet->moveNext())
151  {
152  //create dataset item
153  te::mem::DataSetItem* outDSetItem = new te::mem::DataSetItem(outDataset.get());
154 
155  for(std::size_t t = 0; t < nProp; ++t)
156  {
157  te::dt::AbstractData* ad = inputDataSet->getValue(t).release();
158 
159  outDSetItem->setValue(t, ad);
160  }
161 
162  //set kernel default value
163  outDSetItem->setDouble(TE_SA_BAYESEVENT_ATTR_NAME, 0.);
164 
165  //set kernel default value
166  outDSetItem->setDouble(TE_SA_BAYESPOP_ATTR_NAME, 0.);
167 
168  //set kernel default value
169  outDSetItem->setDouble(TE_SA_BAYES_ATTR_NAME, 0.);
170 
171  //add item into dataset
172  outDataset->add(outDSetItem);
173  }
174 
175  return outDataset;
176 }
177 
178 void te::sa::BayesLocalOperation::runBayesLocal(te::mem::DataSet* ds, std::size_t idIdx, std::size_t eventIdx, std::size_t popIdx,
179  std::size_t neighEventIdx, std::size_t neighPopIdx, std::size_t bayesIdx, std::size_t gpmEventIdx, std::size_t gpmPopIdx)
180 {
181  assert(m_inputParams->m_gpm.get());
182 
183  te::graph::AbstractGraph* graph = m_inputParams->m_gpm->getGraph();
184 
185  {
186  //create task
188 
189  task.setTotalSteps(ds->size());
190  task.setMessage(TE_TR("Calculating events and population for each element."));
191 
192  //calculate neighbour events and population for each element
193  ds->moveBeforeFirst();
194 
195  while(ds->moveNext())
196  {
197  std::string strId = ds->getAsString(idIdx);
198 
199  int id = atoi(strId.c_str());
200 
201  double totEvent = ds->getDouble(eventIdx);
202  double totPop = ds->getDouble(popIdx);
203 
204  te::graph::Vertex* v = graph->getVertex(id);
205 
206  if(v)
207  {
208  std::set<int> neighbours = v->getSuccessors();
209  std::set<int>::iterator itNeighbours = neighbours.begin();
210  while(itNeighbours != neighbours.end())
211  {
212  te::graph::Edge* e = graph->getEdge(*itNeighbours);
213  te::graph::Vertex* vTo = 0;
214 
215  if(e)
216  {
217  if(e->getIdFrom() == id)
218  vTo = graph->getVertex(e->getIdTo());
219  else
220  vTo = graph->getVertex(e->getIdFrom());
221  }
222 
223  if(vTo)
224  {
225  totEvent += te::sa::GetDataValue(vTo->getAttributes()[gpmEventIdx]);
226  totPop += te::sa::GetDataValue(vTo->getAttributes()[gpmPopIdx]);
227  }
228 
229  ++itNeighbours;
230  }
231  }
232 
233  ds->setDouble(neighEventIdx, totEvent);
234  ds->setDouble(neighPopIdx, totPop);
235  }
236 
237  if(!task.isActive())
238  {
239  throw te::common::Exception(TE_TR("Operation canceled by the user."));
240  }
241 
242  task.pulse();
243  }
244 
245 
246  {
247  //create task
249 
250  task.setTotalSteps(ds->size());
251  task.setMessage(TE_TR("Calculating Local Bayes."));
252 
253  //calculate local bayes values
254  ds->moveBeforeFirst();
255 
256  while(ds->moveNext())
257  {
258  std::string strId = ds->getAsString(idIdx);
259 
260  int id = atoi(strId.c_str());
261 
262  double myEvent = ds->getDouble(eventIdx);
263  double myPop = ds->getDouble(popIdx);
264 
265  double totEvent = ds->getDouble(neighEventIdx);
266  double totPop = ds->getDouble(neighPopIdx);
267 
268  if(totPop <= 0.)
269  throw;
270 
271  double mean = totEvent / totPop;
272 
273  double thetaI = (myPop > 0) ? myEvent / myPop : 0.0;
274 
275  te::graph::Vertex* v = graph->getVertex(id);
276 
277  if(v)
278  {
279  std::set<int> neighbours = v->getSuccessors();
280  std::set<int>::iterator itNeighbours = neighbours.begin();
281  int nNeighbours = (int)neighbours.size();
282 
283  if(nNeighbours != 0)
284  {
285  double variance = myPop * pow((myEvent / myPop) - mean, 2);
286 
287  while(itNeighbours != neighbours.end())
288  {
289  te::graph::Edge* e = graph->getEdge(*itNeighbours);
290  te::graph::Vertex* vTo = 0;
291 
292  if(e)
293  {
294  if(e->getIdFrom() == id)
295  vTo = graph->getVertex(e->getIdTo());
296  else
297  vTo = graph->getVertex(e->getIdFrom());
298  }
299 
300  if(vTo)
301  {
302  double toEvent = te::sa::GetDataValue(vTo->getAttributes()[gpmEventIdx]);
303  double toPop = te::sa::GetDataValue(vTo->getAttributes()[gpmPopIdx]);
304 
305  variance += toPop * pow((toEvent / toPop) - mean, 2);
306  }
307 
308  ++itNeighbours;
309  }
310 
311  variance /= totPop;
312 
313  double aux = variance - (mean * ((double)nNeighbours + 1.) / totPop);
314 
315  if(aux < 0.)
316  aux = 0.;
317 
318  double wI = 1.;
319 
320  if(aux != 0. || mean != 0.)
321  wI = aux / (aux + (mean / myPop));
322 
323  thetaI = wI * (myEvent / myPop) + (1 - wI) * mean;
324  }
325  }
326 
327  ds->setDouble(bayesIdx, thetaI * m_inputParams->m_rate);
328  }
329 
330  if(!task.isActive())
331  {
332  throw te::common::Exception(TE_TR("Operation canceled by the user."));
333  }
334 
335  task.pulse();
336  }
337 }
std::size_t size() const
It returns the collection size, if it is known.
Definition: DataSet.cpp:311
#define TE_SA_BAYESPOP_ATTR_NAME
Definition: Config.h:86
Class that represents the Bayes output parameters.
Definition: BayesParams.h:94
This file contains a class that represents the bayes global operation.
void setMessage(const std::string &message)
Set the task message.
Utility functions for the data access module.
void saveDataSet(te::da::DataSet *dataSet, te::da::DataSetType *dsType)
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.
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.
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
It models a property definition.
Definition: Property.h:59
bool isActive() const
Verify if the task is active.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
void setTotalSteps(int value)
Set the task total stepes.
void setId(unsigned int id)
It sets the property identifier.
Definition: Property.h:118
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
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
~BayesLocalOperation()
Virtual destructor.
BayesLocalOperation()
Default constructor.
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Definition: DataSet.cpp:218
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
std::set< int > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
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
#define TE_SA_BAYESEVENT_ATTR_NAME
Definition: Config.h:83
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
void runBayesLocal(te::mem::DataSet *ds, std::size_t idIdx, std::size_t eventIdx, std::size_t popIdx, std::size_t neighEventIdx, std::size_t neighPopIdx, std::size_t bayesIdx, std::size_t gpmEventIdx, std::size_t gpmPopIdx)
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
Definition: Utils.cpp:200
TESAEXPORT int AssociateGPMVertexAttribute(te::sa::GeneralizedProximityMatrix *gpm, te::da::DataSource *ds, std::string dataSetName, std::string attrLink, std::string attr, int dataType, int srid=TE_UNKNOWN_SRS, int subType=te::gm::UnknownGeometryType)
Function used to set a an attribute valeu from a dataset to the vertex objects from a gpm...
Definition: Utils.cpp:58
void setParameters(te::sa::BayesInputParams *inParams, te::sa::BayesOutputParams *outParams)
std::auto_ptr< te::mem::DataSet > createDataSet(te::da::DataSet *inputDataSet, te::da::DataSetType *dsType)
#define TE_SA_BAYES_ATTR_NAME
Definition: Config.h:80
void setDouble(std::size_t i, double value)
Definition: DataSet.cpp:482
std::auto_ptr< te::da::DataSetType > createDataSetType(te::da::DataSetType *dsType)
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 execute()
Function to execute the bayes operation.
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177