From 9cb87753ef64283493a567b352268a7588b332e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <traizetc@cesbio.cnes.fr>
Date: Wed, 5 Jul 2017 10:05:25 +0200
Subject: [PATCH] SOM can now output the trained map in a txt file

---
 include/SOMModel.h     |  5 ++++-
 include/SOMModel.txx   | 18 ++++++++++++++++++
 include/cbTrainSOM.txx |  1 +
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/include/SOMModel.h b/include/SOMModel.h
index a5752ac5c9..c9553cd9c3 100644
--- a/include/SOMModel.h
+++ b/include/SOMModel.h
@@ -60,6 +60,8 @@ public:
 	itkGetMacro(NumberOfIterations, unsigned int);
     itkSetMacro(BetaInit, double);
 	itkGetMacro(BetaInit, double);
+	itkSetMacro(WriteMap, bool);
+	itkGetMacro(WriteMap, bool);
 	itkSetMacro(BetaEnd, double);
 	itkGetMacro(BetaEnd, double);
 	itkSetMacro(MinWeight, InputValueType);
@@ -121,7 +123,8 @@ private:
 	SOMLearningBehaviorFunctorType m_BetaFunctor;
 	/** Behavior of the Neighborhood extent */
 	SOMNeighborhoodBehaviorFunctorType m_NeighborhoodSizeFunctor;
-
+	/** Write the SOM Map vectors in a txt file */
+	bool m_WriteMap;
 };
 
 
diff --git a/include/SOMModel.txx b/include/SOMModel.txx
index b0b2229ac7..6195b9f961 100644
--- a/include/SOMModel.txx
+++ b/include/SOMModel.txx
@@ -112,6 +112,24 @@ void SOMModel<TInputValue, MapDimension>::Save(const std::string & filename, con
 	++inputIterator;
 	}
 	ofs.close();
+	
+	if (this->m_WriteMap == true)     // output the map vectors in a txt file
+	{
+		std::ofstream otxt(filename+".txt");
+		inputIterator.GoToBegin();
+		while(!inputIterator.IsAtEnd())
+		{
+			InputSampleType vect = inputIterator.Get();
+			for (size_t i=0;i<vect.GetNumberOfElements();i++)
+			{
+				
+				otxt << vect[i] << " ";
+			}	
+			otxt << std::endl;
+			++inputIterator;
+		}
+		otxt.close();
+	}
 }
 
 template <class TInputValue, unsigned int MapDimension>
diff --git a/include/cbTrainSOM.txx b/include/cbTrainSOM.txx
index 538ccea03c..fa9137b309 100644
--- a/include/cbTrainSOM.txx
+++ b/include/cbTrainSOM.txx
@@ -120,6 +120,7 @@ void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
 		std::cout << dim << std::endl;
 		dimredTrainer->SetNumberOfIterations(GetParameterInt("model.som.ni"));
 		dimredTrainer->SetBetaInit(GetParameterFloat("model.som.bi"));
+		dimredTrainer->SetWriteMap(true);
 		dimredTrainer->SetBetaEnd(GetParameterFloat("model.som.bf"));
 		dimredTrainer->SetMaxWeight(GetParameterFloat("model.som.iv"));
 		typename TemplateEstimatorType::SizeType size;
-- 
GitLab