Sfoglia il codice sorgente

changes to store and restore functions of Codebook and FPCRandomForests

Johannes Ruehle 11 anni fa
parent
commit
d836756391

+ 17 - 9
classifier/fpclassifier/randomforest/FPCRandomForests.cpp

@@ -226,7 +226,7 @@ int FPCRandomForests::classify_optimize(Example & pce)
 
 void FPCRandomForests::train(FeaturePool & fp, Examples & examples)
 {
-	cerr << "FPCRandomForests::train()" << endl;
+    fprintf (stderr,"FPCRandomForests::train()\n");
 	assert(builder != NULL);
 
 	if (maxClassNo < 0)
@@ -397,20 +397,26 @@ void FPCRandomForests::restore(istream & is, int format)
     std::string tag;
     int index;
 
-    while ( (is >> tag) && (tag == "TREE") )
+    if( (is >> tag) && (tag == "FOREST") )
     {
-		is >> index;
-		DecisionTree *dt = new DecisionTree ( conf, maxClassNo );
-		dt->restore ( is );
-		if ( minimum_entropy != 0.0 )
-			dt->pruneTreeEntropy ( minimum_entropy );
 
-		forest.push_back(dt);
-	}
+        while ( (is >> tag) && (tag == "TREE") )
+        {
+            is >> index;
+            DecisionTree *dt = new DecisionTree ( conf, maxClassNo );
+            dt->restore ( is );
+            if ( minimum_entropy != 0.0 )
+                dt->pruneTreeEntropy ( minimum_entropy );
+
+            forest.push_back(dt);
+        }
+
+    }
 }
 
 void FPCRandomForests::store(ostream & os, int format) const
 {
+    os << "FOREST " << endl;
     int index = 0;
     for ( vector<DecisionTree *>::const_iterator i = forest.begin();
 					    i != forest.end();
@@ -421,6 +427,8 @@ void FPCRandomForests::store(ostream & os, int format) const
 		dt.store ( os, format );
 		os << "ENDTREE ";
     }
+    os << endl;
+    os << "ENDFOREST " << endl;
 }
 
 void FPCRandomForests::clear()

+ 68 - 6
features/simplefeatures/Codebook.cpp

@@ -89,8 +89,53 @@ void Codebook::clear ()
 
 void Codebook::restore ( istream & is, int format )
 {
-   is >> thresholds;
-   is >> informativeMeasure;
+    if (is.good())
+    {
+        std::string tmp;
+        is >> tmp; //class name
+
+        if ( ! this->isStartTag( tmp, "Codebook" ) )
+        {
+            std::cerr << " WARNING - attempt to restore Codebook, but start flag " << tmp << " does not match! Aborting... " << std::endl;
+            throw;
+        }
+
+        bool b_endOfBlock = false;
+
+        while ( !b_endOfBlock )
+        {
+            is >> tmp; // start of block
+
+            if ( this->isEndTag( tmp, "Codebook" ) )
+            {
+                b_endOfBlock = true;
+                continue;
+            }
+
+            tmp = this->removeStartTag ( tmp );
+            if ( tmp.compare("thresholds") == 0 )
+            {
+                is >> thresholds;
+                is >> tmp; // end of block
+                tmp = this->removeEndTag ( tmp );
+            }
+            else if ( tmp.compare("informativeMeasure") == 0 )
+            {
+                is >> informativeMeasure;
+                is >> tmp; // end of block
+                tmp = this->removeEndTag ( tmp );
+            }
+            else if ( tmp.compare("classnos") == 0 )
+            {
+                is >> classnos;
+                is >> tmp; // end of block
+                tmp = this->removeEndTag ( tmp );
+            }
+
+
+        }
+    }
+
 //    is >> classnos;
    
    //TODO use a flag for compatibility with old systems?
@@ -111,9 +156,26 @@ void Codebook::restore ( istream & is, int format )
 
 void Codebook::store ( ostream & os, int format ) const
 {
-    os << this->thresholds << endl;
-    os << this->informativeMeasure << endl;
-    os << this->classnos << endl;
+    if (os.good())
+    {
+        // show starting point
+        os << this->createStartTag( "Codebook" ) << std::endl;
+
+        os << this->createStartTag( "thresholds" ) << std::endl;
+        os << this->thresholds << endl;
+        os << this->createEndTag( "thresholds" ) << std::endl;
+
+        os << this->createStartTag( "informativeMeasure" ) << std::endl;
+        os << this->informativeMeasure << endl;
+        os << this->createEndTag( "informativeMeasure" ) << std::endl;
+
+        os << this->createStartTag( "classnos" ) << std::endl;
+        os << this->classnos << endl;
+        os << this->createEndTag( "classnos" ) << std::endl;
+
+        // done
+        os << this->createEndTag( "Codebook" ) << std::endl;
+    }
 //     os << this->i_noOfNearestClustersToConsidere << endl;
 //     os << this->s_section << endl;
 //     
@@ -148,4 +210,4 @@ void Codebook::setHardAssignment( const bool & _hardAssignment )
 bool Codebook::getHardAssignment ( ) const
 {
   return this->b_hardAssignment;
-}
+}