43 using std::ostringstream;
92 config->addNameValue(
"Label",
label() );
97 config->addSubsection( cs );
99 config->addNameValue(
"Count",
int(
occur_.size()) );
104 while ( oc != end ) {
107 std::string
id = (*oc)->id();
112 cfg->addNameValue(
"Id",
id );
113 cfg->addNameValue(
"X", x, precision );
114 cfg->addNameValue(
"Y", y, precision );
115 if ( (*oc)->hasEnvironment() ) {
117 cfg->addNameValue(
"Sample", (*oc)->originalEnvironment() );
119 config->addSubsection( cfg );
130 label_ = config->getAttribute(
"Label");
136 Log::instance()->
warn(
"Occurrences has no Coordinate System. Assuming LatLong WSG84\n" );
141 cs_ = cs_config->getValue();
148 Configuration::subsection_list::iterator
begin = subs.begin();
149 Configuration::subsection_list::iterator
end = subs.end();
151 std::vector<Scalar> attrs;
155 if ( (*begin)->getName() !=
"Point" ) {
160 std::string
id = (*begin)->getAttribute(
"Id");
161 Scalar x = (*begin)->getAttributeAsDouble(
"X", 0.0 );
162 Scalar y = (*begin)->getAttributeAsDouble(
"Y", 0.0 );
168 std::vector<Scalar> unnormenv = (*begin)->getAttributeAsVecDouble(
"Sample" );
191 while ( oc != fin ) {
193 Sample sample = env->getUnnormalized( (*oc)->x(), (*oc)->y() );
195 if ( sample.
size() == 0 ) {
197 Log::instance()->
warn(
"%s Point \"%s\" at (%f,%f) has no environment. It will be discarded.\n", type, ((*oc)->id()).c_str(), (*oc)->x(), (*oc)->y() );
204 (*oc)->setUnnormalizedEnvironment( sample );
205 (*oc)->setNormalizedEnvironment(
Sample() );
217 if ( ! normalizerPtr ) {
226 while ( occ != end ) {
228 (*occ)->normalize( normalizerPtr, categoricalThreshold );
241 while ( occ != end ) {
243 (*occ)->setNormalizedEnvironment( (*occ)->originalEnvironment() );
256 *min =
Sample( (*occ)->environment() );
257 *max =
Sample( (*occ)->environment() );
260 while ( occ != end ) {
262 Sample sample = (*occ)->environment();
276 int num_attributes,
Scalar *attributes,
277 int num_env,
Scalar *env )
284 num_attributes, attributes,
293 std::vector<double> attributes,
294 std::vector<double> env)
340 return (*it)->hasEnvironment();
350 return (*it)->environment().size();
366 return occur_[ selected ];
389 while ( it != end ) {
399 std::vector<ScalarVector>
402 std::vector<ScalarVector> matrix(
dimension() );
405 for (
unsigned int i = 0; i < matrix.size(); i++ ) {
418 Sample const& sample = (*c)->environment();
421 for (
unsigned int i = 0; i < matrix.size(); i++ ) {
424 matrix[i][j] = sample[i];
459 while ( attr != end ) {
485 int n = occurrences->numOccurrences();
486 int k = (int) (n * propTrain);
487 std::vector<int> goToTrainSet(n);
490 for ( i = 0; i < k; i++ ) {
496 for ( ; i < n; i++ ) {
504 std::random_shuffle( goToTrainSet.begin(), goToTrainSet.end() );
514 if ( goToTrainSet[i] ) {
533 double dist, distLimit=8.0, x, y, xmin, xmax, ymin, ymax, deltax, deltay;
534 unsigned int flag = 0, i = 0, itrain=0, ktrain=0, ioccur=0, flagOk=0;
535 std::vector<double> occurTransformx( occurrences->numOccurrences() );
536 std::vector<double> occurTransformy( occurrences->numOccurrences() );
537 std::vector<int> testId( occurrences->numOccurrences() );
538 int n = occurrences->numOccurrences(), icont=0;
539 int nptTeste = (int) (n * 0.40) + 2;
544 xmin = xmax = (*it)->x();
545 ymin = ymax = (*it)->y();
549 if ( (*it)->x() < xmin ) xmin = (*it)->x();
550 else if ( (*it)->x() > xmax) xmax = (*it)->x();
551 if ( (*it)->y() < ymin) ymin = (*it)->y();
552 else if ( (*it)->y() > ymax) ymax = (*it)->y();
555 deltax = xmax - xmin;
556 deltay = ymax - ymin;
558 it = occurrences->begin();
560 occurTransformx[i] = 100 * ( (*it)->x() - xmin ) / deltax;
561 occurTransformy[i] = 100 * ( (*it)->y() - ymin ) / deltay;
566 flagOk=0, flag = 0, itrain=0, ktrain=0, ioccur=0, icont=0;
568 it = occurrences->begin();
574 testId[ktrain] = ioccur;
580 for ( i = 0; i < ktrain; i++ ) {
582 x = occurTransformx[ioccur] - occurTransformx[itrain];
583 y = occurTransformy[ioccur] - occurTransformy[itrain];
584 dist = sqrt( (x*x) + (y*y) );
586 if ( dist < distLimit) {
594 if (icont > nptTeste){
598 while ( it != last ) {
599 it = testOccurrences->erase(it);
600 last = testOccurrences->end();
603 it = testOccurrences->erase(it);
608 while ( itt != lastt ) {
609 itt = trainOccurrences->erase(itt);
610 lastt = trainOccurrences->end();
613 itt = trainOccurrences->erase(itt);
615 if (distLimit > 1.0) distLimit = distLimit - 1.0;
616 else if (distLimit > 0.2) distLimit = distLimit - 0.2;
617 else distLimit = distLimit - 0.02;
623 testId[ktrain] = ioccur;
std::vector< OccurrencePtr >::iterator iterator
void warn(const char *format,...)
'Warn' level.
void swap(ReferenceCountedPointer< T > &lhs, ReferenceCountedPointer< T > &rhs)
std::vector< ConfigurationPtr > subsection_list
void createOccurrence(const std::string &id, Coord longitude, Coord latitude, Scalar error, Scalar abundance, int num_attributes=0, Scalar *attributes=0, int num_env=0, Scalar *env=0)
double Scalar
Type of map values.
int initRandom(unsigned int new_seed)
const_iterator begin() const
void setCoordinateSystem(const std::string &cs)
static Log * instance()
Returns the instance pointer, creating the object on the first call.
OccurrencesImpl * clone() const
OccurrencesImpl(double default_abundance)
ConstOccurrencePtr getRandom() const
std::vector< OccurrencePtr > occur_
virtual void setConfiguration(const ConstConfigurationPtr &)
void normalize(Normalizer *normalizerPtr, size_t categoricalThreshold=0)
const_iterator end() const
double default_abundance_
void getMinMax(Sample *min, Sample *max) const
void setLabel(const std::string &label)
std::vector< ScalarVector > getEnvironmentMatrix()
bool hasEnvironment() const
void splitOccurrences(const OccurrencesPtr &occurrences, OccurrencesPtr &trainOccurrences, OccurrencesPtr &testOccurrences, double propTrain)
std::vector< Scalar > ScalarVector
void insert(const OccurrencePtr &)
void resetNormalization()
Scalar const * const_iterator
virtual ConfigurationPtr getConfiguration() const
void setEnvironment(const EnvironmentPtr &env, const char *type="Sample")
iterator erase(const iterator &it)
void info(const char *format,...)
'Info' level.
void dump(std::string msg="") const
double Coord
Type of map coordinates.
std::vector< OccurrencePtr >::const_iterator const_iterator
char const * coordSystem() const
char const * label() const
int numOccurrences() const
void appendFrom(const OccurrencesPtr &source)