[pktools] 195/375: commit from branch

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:13 UTC 2014


This is an automated email from the git hooks/post-receive script.

sebastic-guest pushed a commit to branch upstream-master
in repository pktools.

commit 97a280093300af4108b79e808014b4b5f892f56b
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Fri Mar 21 11:55:48 2014 +0100

    commit from branch
---
 src/apps/pkclassify_nn.cc  |  10 ++---
 src/apps/pkclassify_svm.cc |  10 ++---
 src/apps/pkextract.cc      |  12 ++----
 src/apps/pkfs_nn.cc        |  19 +-------
 src/apps/pkfs_svm.cc       |   6 +--
 src/base/Optionpk.h        | 105 ++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 118 insertions(+), 44 deletions(-)

diff --git a/src/apps/pkclassify_nn.cc b/src/apps/pkclassify_nn.cc
index 01337ad..c4eefd4 100644
--- a/src/apps/pkclassify_nn.cc
+++ b/src/apps/pkclassify_nn.cc
@@ -18,10 +18,6 @@ You should have received a copy of the GNU General Public License
 along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************/
 #include <stdlib.h>
-#ifdef WIN32
-#define random rand
-#define srandom srand
-#endif
 #include <vector>
 #include <map>
 #include <algorithm>
@@ -46,6 +42,7 @@ int main(int argc, char *argv[])
   Optionpk<string> training_opt("t", "training", "training shape file. A single shape file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option). Use multiple training files for bootstrap aggregation (alternative to the bag and bsize options, where a random subset is taken from a single training file)"); 
   Optionpk<string> label_opt("label", "label", "identifier for class label in training shape file.","label"); 
   Optionpk<unsigned int> balance_opt("bal", "balance", "balance the input data to this number of samples for each class", 0);
+  Optionpk<bool> random_opt("random", "random", "in case of balance, randomize input data", true);
   Optionpk<int> minSize_opt("min", "min", "if number of training pixels is less then min, do not take this class into account (0: consider all classes)", 0);
   Optionpk<double> start_opt("s", "start", "start band sequence number (set to 0)",0); 
   Optionpk<double> end_opt("e", "end", "end band sequence number (set to 0 for all bands)", 0); 
@@ -88,6 +85,7 @@ int main(int argc, char *argv[])
     training_opt.retrieveOption(argc,argv);
     label_opt.retrieveOption(argc,argv);
     balance_opt.retrieveOption(argc,argv);
+    random_opt.retrieveOption(argc,argv);
     minSize_opt.retrieveOption(argc,argv);
     start_opt.retrieveOption(argc,argv);
     end_opt.retrieveOption(argc,argv);
@@ -266,7 +264,7 @@ int main(int argc, char *argv[])
       if(balance_opt[0]>0){
         while(balance_opt.size()<nclass)
           balance_opt.push_back(balance_opt.back());
-        if(random)
+        if(random_opt[0])
           srand(time(NULL));
         totalSamples=0;
         for(int iclass=0;iclass<nclass;++iclass){
@@ -392,7 +390,7 @@ int main(int argc, char *argv[])
       int nctraining=0;
       if(verbose_opt[0]>=1)
         cout << "calculating features for class " << iclass << endl;
-      if(random)
+      if(random_opt[0])
         srand(time(NULL));
       nctraining=(bagSize_opt[iclass]<100)? trainingPixels[iclass].size()/100.0*bagSize_opt[iclass] : trainingPixels[iclass].size();//bagSize_opt[iclass] given in % of training size
       if(nctraining<=0)
diff --git a/src/apps/pkclassify_svm.cc b/src/apps/pkclassify_svm.cc
index e5d7896..b428f95 100644
--- a/src/apps/pkclassify_svm.cc
+++ b/src/apps/pkclassify_svm.cc
@@ -18,10 +18,6 @@ You should have received a copy of the GNU General Public License
 along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************/
 #include <stdlib.h>
-#ifdef WIN32
-#define random rand
-#define srandom srand
-#endif
 #include <vector>
 #include <map>
 #include <algorithm>
@@ -56,6 +52,7 @@ int main(int argc, char *argv[])
   Optionpk<string> training_opt("t", "training", "training shape file. A single shape file contains all training features (must be set as: B0, B1, B2,...) for all classes (class numbers identified by label option). Use multiple training files for bootstrap aggregation (alternative to the bag and bsize options, where a random subset is taken from a single training file)"); 
   Optionpk<string> label_opt("label", "label", "identifier for class label in training shape file.","label"); 
   Optionpk<unsigned int> balance_opt("bal", "balance", "balance the input data to this number of samples for each class", 0);
+  Optionpk<bool> random_opt("random", "random", "in case of balance, randomize input data", true);
   Optionpk<int> minSize_opt("min", "min", "if number of training pixels is less then min, do not take this class into account (0: consider all classes)", 0);
   Optionpk<double> start_opt("s", "start", "start band sequence number (set to 0)",0); 
   Optionpk<double> end_opt("e", "end", "end band sequence number (set to 0 for all bands)", 0); 
@@ -104,6 +101,7 @@ int main(int argc, char *argv[])
     training_opt.retrieveOption(argc,argv);
     label_opt.retrieveOption(argc,argv);
     balance_opt.retrieveOption(argc,argv);
+    random_opt.retrieveOption(argc,argv);
     minSize_opt.retrieveOption(argc,argv);
     start_opt.retrieveOption(argc,argv);
     end_opt.retrieveOption(argc,argv);
@@ -316,7 +314,7 @@ int main(int argc, char *argv[])
       if(balance_opt[0]>0){
         while(balance_opt.size()<nclass)
           balance_opt.push_back(balance_opt.back());
-        if(random)
+        if(random_opt[0])
           srand(time(NULL));
         totalSamples=0;
         for(short iclass=0;iclass<nclass;++iclass){
@@ -443,7 +441,7 @@ int main(int argc, char *argv[])
       int nctraining=0;
       if(verbose_opt[0]>=1)
         std::cout << "calculating features for class " << iclass << std::endl;
-      if(random)
+      if(random_opt[0])
         srand(time(NULL));
       nctraining=(bagSize_opt[iclass]<100)? trainingPixels[iclass].size()/100.0*bagSize_opt[iclass] : trainingPixels[iclass].size();//bagSize_opt[iclass] given in % of training size
       if(nctraining<=0)
diff --git a/src/apps/pkextract.cc b/src/apps/pkextract.cc
index d893ee2..889faf4 100644
--- a/src/apps/pkextract.cc
+++ b/src/apps/pkextract.cc
@@ -20,10 +20,6 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include <assert.h>
 #include <math.h>
 #include <stdlib.h>
-#ifdef WIN32
-#define random rand
-#define srandom srand
-#endif
 #include <sstream>
 #include <string>
 #include <algorithm>
@@ -246,7 +242,7 @@ int main(int argc, char *argv[])
   void* pProgressArg=NULL;
   GDALProgressFunc pfnProgress=GDALTermProgress;
   double progress=0;
-  srandom(time(NULL));
+  srand(time(NULL));
 
   bool sampleIsRaster=false;
   ImgReaderOgr sampleReaderOgr;
@@ -427,7 +423,7 @@ int main(int argc, char *argv[])
               }
               float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
               if(theThreshold>0){//percentual value
-                double p=static_cast<double>(random())/(RAND_MAX);
+                double p=static_cast<double>(rand())/(RAND_MAX);
                 p*=100.0;
                 if(p>theThreshold)
 		  continue;//do not select for now, go to next column
@@ -671,7 +667,7 @@ int main(int argc, char *argv[])
               }
               float theThreshold=(threshold_opt.size()>1)?threshold_opt[processClass]:threshold_opt[0];
               if(theThreshold>0){//percentual value
-                double p=static_cast<double>(random())/(RAND_MAX);
+                double p=static_cast<double>(rand())/(RAND_MAX);
                 p*=100.0;
                 if(p>theThreshold)
                   continue;//do not select for now, go to next column
@@ -891,7 +887,7 @@ int main(int argc, char *argv[])
 	if(verbose_opt[0]>0)
 	  std::cout << "reading feature " << readFeature->GetFID() << std::endl;
 	if(threshold_opt[0]>0){//percentual value
-	  double p=static_cast<double>(random())/(RAND_MAX);
+	  double p=static_cast<double>(rand())/(RAND_MAX);
 	  p*=100.0;
 	  if(p>threshold_opt[0]){
 	    if(test_opt.size())
diff --git a/src/apps/pkfs_nn.cc b/src/apps/pkfs_nn.cc
index 6ac56b2..4a8c939 100644
--- a/src/apps/pkfs_nn.cc
+++ b/src/apps/pkfs_nn.cc
@@ -18,10 +18,6 @@ You should have received a copy of the GNU General Public License
 along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************/
 #include <stdlib.h>
-#ifdef WIN32
-#define random rand
-#define srandom srand
-#endif
 #include <vector>
 #include <string>
 #include <map>
@@ -162,11 +158,6 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
   else{//not working yet. please repair...
     assert(cv_opt[0]>0);
     bool initWeights=true;
-    //test
-    cout << "tempFeatures.size(): " << tmpFeatures.size() << endl;
-    cout << "ntraining: " << ntraining << endl;
-    cout << "initWeights: " << initWeights << endl;
-    cout << "maxit_opt.size(): " << maxit_opt.size() << endl;
     net.train_on_data(tmpFeatures,ntraining,initWeights, maxit_opt[0],
                       iterations_between_reports, desired_error);
     vector<Vector2d<float> > testFeatures(nclass);
@@ -178,8 +169,6 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
 	for(int ifeature=0;ifeature<nFeatures;++ifeature){
           testFeatures[iclass][isample][ifeature]=trainingFeatures[iclass][nctraining[iclass]+isample][ifeature];
         }
-	//test
-	cout << "isample:" << isample<< endl;
         result=net.run(testFeatures[iclass][isample]);
         string refClassName=nameVector[iclass];
         float maxP=-1;
@@ -190,19 +179,13 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
             maxClass=ic;
           }
         }
-	//test
-	cout << "maxClass:" << maxClass << "(" << nameVector.size() << ")" << endl;
         string className=nameVector[maxClass];
-	//test
-	cout << "className:" << nameVector[maxClass] << endl;
         if(classValueMap.size())
           cm.incrementResult(type2string<short>(classValueMap[refClassName]),type2string<short>(classValueMap[className]),1.0);
         else
           cm.incrementResult(cm.getClass(referenceVector[isample]),cm.getClass(outputVector[isample]),1.0);
       }
     }
-    //test
-    cout << "debug12" << endl;
   }
   assert(cm.nReference());
   return(cm.kappa());
@@ -237,7 +220,7 @@ int main(int argc, char *argv[])
     maxFeatures_opt.retrieveOption(argc,argv);
     label_opt.retrieveOption(argc,argv);
     balance_opt.retrieveOption(argc,argv);
-	random_opt.retrieveOption(argc,argv);
+    random_opt.retrieveOption(argc,argv);
     minSize_opt.retrieveOption(argc,argv);
     start_opt.retrieveOption(argc,argv);
     end_opt.retrieveOption(argc,argv);
diff --git a/src/apps/pkfs_svm.cc b/src/apps/pkfs_svm.cc
index 45f8996..65c6cab 100644
--- a/src/apps/pkfs_svm.cc
+++ b/src/apps/pkfs_svm.cc
@@ -18,10 +18,6 @@ You should have received a copy of the GNU General Public License
 along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************/
 #include <stdlib.h>
-#ifdef WIN32
-#define random rand
-#define srandom srand
-#endif
 #include <vector>
 #include <string>
 #include <map>
@@ -246,7 +242,7 @@ int main(int argc, char *argv[])
     maxFeatures_opt.retrieveOption(argc,argv);
     label_opt.retrieveOption(argc,argv);
     balance_opt.retrieveOption(argc,argv);
-	random_opt.retrieveOption(argc,argv);
+    random_opt.retrieveOption(argc,argv);
     minSize_opt.retrieveOption(argc,argv);
     start_opt.retrieveOption(argc,argv);
     end_opt.retrieveOption(argc,argv);
diff --git a/src/base/Optionpk.h b/src/base/Optionpk.h
index 9c07f43..1905978 100644
--- a/src/base/Optionpk.h
+++ b/src/base/Optionpk.h
@@ -31,6 +31,9 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include <typeinfo>
 #ifndef WIN32
 #include <cxxabi.h>
+#define mytypeid(T) abi::__cxa_demangle(typeid(T).name(),0,0,&status)
+#else
+#define mytypeid(T) typeid(T).name()
 #endif
 #include "ogr_feature.h"
 
@@ -236,7 +239,7 @@ template<class T> inline std::string Optionpk<T>::usageDoxygen() const
   else
     helpss << std::setiosflags(std::ios::left) << "                     | ";
   int status;
-  helpss << std::setiosflags(std::ios::left) << std::setw(4) << typeid(T).name() << " | ";
+  helpss << std::setiosflags(std::ios::left) << std::setw(4) << mytypeid(T) << " | ";
   //helpss << std::setiosflags(std::ios::left) << std::setw(4) << abi::__cxa_demangle(typeid(T).name(),0,0,&status) << " | ";
   if(m_hasDefault)
     helpss <<std::setiosflags(std::ios::left) << std::setw(5) << type2string<T>(m_defaultValue) << " |";
@@ -345,4 +348,104 @@ template<class T> inline bool Optionpk<T>::retrieveOption(int argc, char **argv)
 
 //template<class T> typename std::vector<T>::const_iterator Optionpk<T>::findSubstring(const T& argument) const {std::string errorString="Error: findSubstring only defined for options of type std::string"; throw(errorString);}
 
+//todo: to be put in .cc file
+/////////////////// Specializations /////////////////
+
+///specialization for string
+template<> inline std::string string2type(std::string const& s){
+  return s;
+}
+
+///specialization for OGRFieldType
+template<> inline OGRFieldType string2type(std::string const& s){
+  OGRFieldType ftype;
+  int ogr_typecount=11;//hard coded for now!
+  for(int iType = 0; iType < ogr_typecount; ++iType){
+    if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL
+        && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),s.c_str()))
+      ftype=(OGRFieldType) iType;
+  }
+  return ftype;
+}
+
+///specialization for bool
+template<> inline std::string type2string(bool const& value){
+  if(value)
+    return("true");
+  else
+    return("false");
+}
+
+///specialization for string
+template<> inline std::string type2string(std::string const& value){
+  // if(value.empty())
+  //   return("<empty string>");
+  // else
+    return(value);
+}
+
+///specialization for float
+template<> inline std::string type2string(float const& value){
+  std::ostringstream oss;
+  // oss.precision(1);
+  // oss.setf(ios::fixed);
+  oss << value;
+  return oss.str();
+}
+
+///specialization for double
+template<> inline std::string type2string(double const& value){
+  std::ostringstream oss;
+  // oss.precision(1);
+  //  oss.setf(ios::fixed);
+  oss << value;
+  return oss.str();
+}
+
+///specialization for bool
+template<> inline void Optionpk<bool>::setAll(const std::string& shortName, const std::string& longName, const std::string& helpInfo)
+{
+  m_shortName=shortName;
+  m_longName=longName;
+  m_hasArgument=false;
+  m_help=helpInfo;
+  m_hide=0;
+}
+
+///specialization for bool
+template<> inline void Optionpk<bool>::setAll(const std::string& shortName, const std::string& longName, const std::string& helpInfo,const bool& defaultValue, short hide)
+{
+  m_shortName=shortName;
+  m_longName=longName;
+  m_hasArgument=false;
+  m_help=helpInfo;
+  m_defaultValue=defaultValue;
+  m_hasDefault=true;
+  m_hide=hide;
+}
+
+///specialization for bool
+template<> inline Optionpk<bool>::Optionpk(const std::string& shortName, const std::string& longName, const std::string& helpInfo)
+{
+  setAll(shortName,longName,helpInfo);
+}
+
+///specialization for bool
+template<> inline Optionpk<bool>::Optionpk(const std::string& shortName, const std::string& longName, const std::string& helpInfo,const bool& defaultValue, short hide)
+{
+  setAll(shortName,longName,helpInfo,defaultValue, hide);
+}
+
+//specialization (only makes sense for T=std::string), generic function throws exception
+//find a substring in string option (e.g., option is of type -co INTERLEAVE=BAND)
+template<> inline std::vector<std::string>::const_iterator Optionpk<std::string>::findSubstring(const std::string& argument) const{
+  std::vector<std::string>::const_iterator opit=this->begin();
+  while(opit!=this->end()){
+    if(opit->find(argument)!=std::string::npos)
+      break;
+    ++opit;
+  }
+  return opit;
+}
+
 #endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/pktools.git



More information about the Pkg-grass-devel mailing list