vector<double> GestureHMM::train(vector< Gesture* > train_set, int max_iter) {

	this->_trainset = train_set;

	field<rowvec> data(train_set.size(),1);
	vector< Gesture* >::const_iterator iter;
	int r = 0;
	for ( iter = train_set.begin(); iter != train_set.end(); ++iter ) {
		Gesture * g = *iter;
		vector<int> x = g->labels();
		data(r++,0) = conv_to< rowvec >::from(g->labels());
	}

	return HMM::train(data, max_iter);
}
// TODO : move to an external class (kind of serialization factory)
void GestureHMM::save(string filename) {
	Gesture *g;
	stringstream ss;
	fix_stream(ss);
	ofxXmlSettings XML;

	XML.clear();

	XML.addValue("name", name);

	XML.addValue("alphabet", alphabet);
	XML.addValue("hiddenStates", hiddenStates);

	XML.addValue( "prior", matrix_to_string(prior) );
	XML.addValue( "transmat", matrix_to_string(transmat) );
	XML.addValue( "obsmat", matrix_to_string(obsmat) );

	XML.addTag("gestures");
	XML.pushTag("gestures");

	int trainset_size = _trainset.size();
	for (int i = 0; i < trainset_size; ++i) {
		g = _trainset[i];

		XML.addTag("train_gestures");
		XML.addAttribute("train_gestures","num",i, i);

		XML.pushTag("train_gestures", i);
		{
			XML.addValue("labeled", to_string(g->labels()));
			XML.addValue("raw", to_string( g->points()) );
		}
		XML.popTag();
	}
	XML.popTag();

	XML.saveFile(filename);
}