GPHIKRegressionMex.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. #ifdef NICE_USELIB_MEX
  2. /**
  3. * @file GPHIKRegressionMex.cpp
  4. * @author Alexander Freytag
  5. * @date 17-01-2014 (dd-mm-yyyy)
  6. * @brief Matlab-Interface of our GPHIKRegression, allowing for training, regression, optimization, variance prediction, incremental learning, and storing/re-storing.
  7. */
  8. // STL includes
  9. #include <math.h>
  10. #include <matrix.h>
  11. #include <mex.h>
  12. // NICE-core includes
  13. #include <core/basics/Config.h>
  14. #include <core/basics/Timer.h>
  15. #include <core/vector/MatrixT.h>
  16. #include <core/vector/VectorT.h>
  17. // gp-hik-core includes
  18. #include "gp-hik-core/GPHIKRegression.h"
  19. // Interface for conversion between Matlab and C objects
  20. #include "gp-hik-core/matlab/classHandleMtoC.h"
  21. #include "gp-hik-core/matlab/ConverterMatlabToNICE.h"
  22. #include "gp-hik-core/matlab/ConverterNICEToMatlab.h"
  23. using namespace std; //C basics
  24. using namespace NICE; // nice-core
  25. NICE::Config parseParametersGPHIKRegression(const mxArray *prhs[], int nrhs)
  26. {
  27. NICE::Config conf;
  28. // if first argument is the filename of an existing config file,
  29. // read the config accordingly
  30. int i_start ( 0 );
  31. std::string variable = MatlabConversion::convertMatlabToString(prhs[i_start]);
  32. if(variable == "conf")
  33. {
  34. conf = NICE::Config ( MatlabConversion::convertMatlabToString( prhs[i_start+1] ) );
  35. i_start = i_start+2;
  36. }
  37. // now run over all given parameter specifications
  38. // and add them to the config
  39. for( int i=i_start; i < nrhs; i+=2 )
  40. {
  41. std::string variable = MatlabConversion::convertMatlabToString(prhs[i]);
  42. /////////////////////////////////////////
  43. // READ STANDARD BOOLEAN VARIABLES
  44. /////////////////////////////////////////
  45. if( (variable == "verboseTime") ||
  46. (variable == "verbose") ||
  47. (variable == "optimize_noise") ||
  48. (variable == "uncertaintyPredictionForRegression") ||
  49. (variable == "use_quantization") ||
  50. (variable == "ils_verbose")
  51. )
  52. {
  53. if ( mxIsChar( prhs[i+1] ) )
  54. {
  55. string value = MatlabConversion::convertMatlabToString( prhs[i+1] );
  56. if ( (value != "true") && (value != "false") )
  57. {
  58. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. In string modus, \'true\' or \'false\' expected.";
  59. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  60. }
  61. if( value == "true" )
  62. conf.sB("GPHIKRegression", variable, true);
  63. else
  64. conf.sB("GPHIKRegression", variable, false);
  65. }
  66. else if ( mxIsLogical( prhs[i+1] ) )
  67. {
  68. bool value = MatlabConversion::convertMatlabToBool( prhs[i+1] );
  69. conf.sB("GPHIKRegression", variable, value);
  70. }
  71. else
  72. {
  73. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. \'true\', \'false\', or logical expected.";
  74. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  75. }
  76. }
  77. /////////////////////////////////////////
  78. // READ STANDARD INT VARIABLES
  79. /////////////////////////////////////////
  80. if ( (variable == "nrOfEigenvaluesToConsiderForVarApprox")
  81. )
  82. {
  83. if ( mxIsDouble( prhs[i+1] ) )
  84. {
  85. double value = MatlabConversion::convertMatlabToDouble(prhs[i+1]);
  86. conf.sI("GPHIKRegression", variable, (int) value);
  87. }
  88. else if ( mxIsInt32( prhs[i+1] ) )
  89. {
  90. int value = MatlabConversion::convertMatlabToInt32(prhs[i+1]);
  91. conf.sI("GPHIKRegression", variable, value);
  92. }
  93. else
  94. {
  95. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. Int32 or Double expected.";
  96. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  97. }
  98. }
  99. /////////////////////////////////////////
  100. // READ STRICT POSITIVE INT VARIABLES
  101. /////////////////////////////////////////
  102. if ( (variable == "num_bins") ||
  103. (variable == "ils_max_iterations")
  104. )
  105. {
  106. if ( mxIsDouble( prhs[i+1] ) )
  107. {
  108. double value = MatlabConversion::convertMatlabToDouble(prhs[i+1]);
  109. if( value < 1 )
  110. {
  111. std::string errorMsg = "Expected parameter value larger than 0 for \'" + variable + "\'.";
  112. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  113. }
  114. conf.sI("GPHIKRegression", variable, (int) value);
  115. }
  116. else if ( mxIsInt32( prhs[i+1] ) )
  117. {
  118. int value = MatlabConversion::convertMatlabToInt32(prhs[i+1]);
  119. if( value < 1 )
  120. {
  121. std::string errorMsg = "Expected parameter value larger than 0 for \'" + variable + "\'.";
  122. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  123. }
  124. conf.sI("GPHIKRegression", variable, value);
  125. }
  126. else
  127. {
  128. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. Int32 or Double expected.";
  129. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  130. }
  131. }
  132. /////////////////////////////////////////
  133. // READ STANDARD DOUBLE VARIABLES
  134. /////////////////////////////////////////
  135. if ( (variable == "parameter_upper_bound") ||
  136. (variable == "parameter_lower_bound")
  137. )
  138. {
  139. if ( mxIsDouble( prhs[i+1] ) )
  140. {
  141. double value = MatlabConversion::convertMatlabToDouble(prhs[i+1]);
  142. conf.sD("GPHIKRegression", variable, value);
  143. }
  144. else
  145. {
  146. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. Double expected.";
  147. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  148. }
  149. }
  150. /////////////////////////////////////////
  151. // READ POSITIVE DOUBLE VARIABLES
  152. /////////////////////////////////////////
  153. if ( (variable == "ils_min_delta") ||
  154. (variable == "ils_min_residual") ||
  155. (variable == "noise")
  156. )
  157. {
  158. if ( mxIsDouble( prhs[i+1] ) )
  159. {
  160. double value = MatlabConversion::convertMatlabToDouble(prhs[i+1]);
  161. if( value < 0.0 )
  162. {
  163. std::string errorMsg = "Expected parameter value larger than 0 for \'" + variable + "\'.";
  164. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  165. }
  166. conf.sD("GPHIKRegression", variable, value);
  167. }
  168. else
  169. {
  170. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. Double expected.";
  171. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  172. }
  173. }
  174. /////////////////////////////////////////
  175. // READ REMAINING SPECIFIC VARIABLES
  176. /////////////////////////////////////////
  177. if(variable == "ils_method")
  178. {
  179. string value = MatlabConversion::convertMatlabToString(prhs[i+1]);
  180. if(value != "CG" && value != "CGL" && value != "SYMMLQ" && value != "MINRES")
  181. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'ils_method\'. \'CG\', \'CGL\', \'SYMMLQ\' or \'MINRES\' expected.");
  182. conf.sS("GPHIKRegression", variable, value);
  183. }
  184. if(variable == "optimization_method")
  185. {
  186. string value = MatlabConversion::convertMatlabToString(prhs[i+1]);
  187. if(value != "greedy" && value != "downhillsimplex" && value != "none")
  188. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'optimization_method\'. \'greedy\', \'downhillsimplex\' or \'none\' expected.");
  189. conf.sS("GPHIKRegression", variable, value);
  190. }
  191. if(variable == "s_quantType")
  192. {
  193. string value = MatlabConversion::convertMatlabToString( prhs[i+1] );
  194. if( value != "1d-aequi-0-1" && value != "1d-aequi-0-max" && value != "nd-aequi-0-max" )
  195. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'s_quantType\'. \'1d-aequi-0-1\' , \'1d-aequi-0-max\' or \'nd-aequi-0-max\' expected.");
  196. conf.sS("GPHIKClassifier", variable, value);
  197. }
  198. if(variable == "transform")
  199. {
  200. string value = MatlabConversion::convertMatlabToString( prhs[i+1] );
  201. if( value != "identity" && value != "absexp" && value != "exp" && value != "MKL" && value != "WeightedDim")
  202. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'transform\'. \'identity\', \'absexp\', \'exp\' , \'MKL\' or \'WeightedDim\' expected.");
  203. conf.sS("GPHIKRegression", variable, value);
  204. }
  205. if(variable == "varianceApproximation")
  206. {
  207. string value = MatlabConversion::convertMatlabToString(prhs[i+1]);
  208. if(value != "approximate_fine" && value != "approximate_rough" && value != "exact" && value != "none")
  209. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'varianceApproximation\'. \'approximate_fine\', \'approximate_rough\', \'none\' or \'exact\' expected.");
  210. conf.sS("GPHIKRegression", variable, value);
  211. }
  212. }
  213. return conf;
  214. }
  215. // MAIN MATLAB FUNCTION
  216. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  217. {
  218. // get the command string specifying what to do
  219. if (nrhs < 1)
  220. mexErrMsgTxt("No commands and options passed... Aborting!");
  221. if( !mxIsChar( prhs[0] ) )
  222. mexErrMsgTxt("First argument needs to be the command, ie.e, the class method to call... Aborting!");
  223. std::string cmd = MatlabConversion::convertMatlabToString( prhs[0] );
  224. // create object
  225. if ( !strcmp("new", cmd.c_str() ) )
  226. {
  227. // check output variable
  228. if (nlhs != 1)
  229. mexErrMsgTxt("New: One output expected.");
  230. // read config settings
  231. NICE::Config conf = parseParametersGPHIKRegression(prhs+1,nrhs-1);
  232. // create class instance
  233. NICE::GPHIKRegression * regressor = new NICE::GPHIKRegression ( &conf, "GPHIKRegression" /*sectionName in config*/ );
  234. // handle to the C++ instance
  235. plhs[0] = MatlabConversion::convertPtr2Mat<NICE::GPHIKRegression>( regressor );
  236. return;
  237. }
  238. // in all other cases, there should be a second input,
  239. // which the be the class instance handle
  240. if (nrhs < 2)
  241. mexErrMsgTxt("Second input should be a class instance handle.");
  242. // delete object
  243. if ( !strcmp("delete", cmd.c_str() ) )
  244. {
  245. // Destroy the C++ object
  246. MatlabConversion::destroyObject<NICE::GPHIKRegression>(prhs[1]);
  247. return;
  248. }
  249. // get the class instance pointer from the second input
  250. // every following function needs the regressor object
  251. NICE::GPHIKRegression * regressor = MatlabConversion::convertMat2Ptr<NICE::GPHIKRegression>(prhs[1]);
  252. ////////////////////////////////////////
  253. // Check which class method to call //
  254. ////////////////////////////////////////
  255. // standard train - assumes initialized object
  256. if (!strcmp("train", cmd.c_str() ))
  257. {
  258. // Check parameters
  259. if (nlhs < 0 || nrhs < 4)
  260. {
  261. mexErrMsgTxt("Train: Unexpected arguments.");
  262. }
  263. //------------- read the data --------------
  264. std::vector< const NICE::SparseVector *> examplesTrain;
  265. NICE::Vector yValuesTrain;
  266. if ( mxIsSparse( prhs[2] ) )
  267. {
  268. examplesTrain = MatlabConversion::convertSparseMatrixToNice( prhs[2] );
  269. }
  270. else
  271. {
  272. NICE::Matrix dataTrain;
  273. dataTrain = MatlabConversion::convertDoubleMatrixToNice(prhs[2]);
  274. //----------------- convert data to sparse data structures ---------
  275. examplesTrain.resize( dataTrain.rows() );
  276. std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin();
  277. for (int i = 0; i < (int)dataTrain.rows(); i++, exTrainIt++)
  278. {
  279. *exTrainIt = new NICE::SparseVector( dataTrain.getRow(i) );
  280. }
  281. }
  282. yValuesTrain = MatlabConversion::convertDoubleVectorToNice(prhs[3]);
  283. //----------------- train our regressor -------------
  284. regressor->train ( examplesTrain , yValuesTrain );
  285. //----------------- clean up -------------
  286. for(int i=0;i<examplesTrain.size();i++)
  287. delete examplesTrain[i];
  288. return;
  289. }
  290. // perform regression
  291. if ( !strcmp("estimate", cmd.c_str() ) )
  292. {
  293. // Check parameters
  294. if ( (nlhs < 0) || (nrhs < 2) )
  295. {
  296. mexErrMsgTxt("Test: Unexpected arguments.");
  297. }
  298. //------------- read the data --------------
  299. double result;
  300. double uncertainty;
  301. if ( mxIsSparse( prhs[2] ) )
  302. {
  303. NICE::SparseVector * example;
  304. example = new NICE::SparseVector ( MatlabConversion::convertSparseVectorToNice( prhs[2] ) );
  305. regressor->estimate ( example, result, uncertainty );
  306. //----------------- clean up -------------
  307. delete example;
  308. }
  309. else
  310. {
  311. NICE::Vector * example;
  312. example = new NICE::Vector ( MatlabConversion::convertDoubleVectorToNice(prhs[2]) );
  313. regressor->estimate ( example, result, uncertainty );
  314. //----------------- clean up -------------
  315. delete example;
  316. }
  317. // output
  318. plhs[0] = mxCreateDoubleScalar( result );
  319. if(nlhs >= 2)
  320. {
  321. plhs[1] = mxCreateDoubleScalar( uncertainty );
  322. }
  323. return;
  324. }
  325. // Uncertainty prediction
  326. if ( !strcmp("uncertainty", cmd.c_str() ) )
  327. {
  328. // Check parameters
  329. if ( (nlhs < 0) || (nrhs < 2) )
  330. {
  331. mexErrMsgTxt("Test: Unexpected arguments.");
  332. }
  333. double uncertainty;
  334. //------------- read the data --------------
  335. if ( mxIsSparse( prhs[2] ) )
  336. {
  337. NICE::SparseVector * example;
  338. example = new NICE::SparseVector ( MatlabConversion::convertSparseVectorToNice( prhs[2] ) );
  339. regressor->predictUncertainty( example, uncertainty );
  340. //----------------- clean up -------------
  341. delete example;
  342. }
  343. else
  344. {
  345. NICE::Vector * example;
  346. example = new NICE::Vector ( MatlabConversion::convertDoubleVectorToNice(prhs[2]) );
  347. regressor->predictUncertainty( example, uncertainty );
  348. //----------------- clean up -------------
  349. delete example;
  350. }
  351. // output
  352. plhs[0] = mxCreateDoubleScalar( uncertainty );
  353. return;
  354. }
  355. // Test - evaluate regressor on whole test set
  356. if ( !strcmp("testL2loss", cmd.c_str() ) )
  357. {
  358. // Check parameters
  359. if (nlhs < 0 || nrhs < 3)
  360. mexErrMsgTxt("Test: Unexpected arguments.");
  361. //------------- read the data --------------
  362. bool dataIsSparse ( mxIsSparse( prhs[2] ) );
  363. std::vector< const NICE::SparseVector *> dataTest_sparse;
  364. NICE::Matrix dataTest_dense;
  365. if ( dataIsSparse )
  366. {
  367. dataTest_sparse = MatlabConversion::convertSparseMatrixToNice( prhs[2] );
  368. }
  369. else
  370. {
  371. dataTest_dense = MatlabConversion::convertDoubleMatrixToNice(prhs[2]);
  372. }
  373. NICE::Vector yValuesTest;
  374. yValuesTest = MatlabConversion::convertDoubleVectorToNice(prhs[3]);
  375. int i_numTestSamples ( yValuesTest.size() );
  376. double l2loss ( 0.0 );
  377. NICE::Vector scores;
  378. NICE::Vector::iterator itScores;
  379. if ( nlhs >= 2 )
  380. {
  381. scores.resize( i_numTestSamples );
  382. itScores = scores.begin();
  383. }
  384. // ------------------------------------------
  385. // ------------- REGRESSION --------------
  386. // ------------------------------------------
  387. NICE::Timer t;
  388. double testTime (0.0);
  389. for (int i = 0; i < i_numTestSamples; i++)
  390. {
  391. //----------------- convert data to sparse data structures ---------
  392. double result;
  393. if ( dataIsSparse )
  394. {
  395. // and perform regression
  396. t.start();
  397. regressor->estimate( dataTest_sparse[ i ], result);
  398. t.stop();
  399. testTime += t.getLast();
  400. }
  401. else
  402. {
  403. NICE::Vector example ( dataTest_dense.getRow(i) );
  404. // and perform regression
  405. t.start();
  406. regressor->estimate( &example, result );
  407. t.stop();
  408. testTime += t.getLast();
  409. }
  410. l2loss += pow ( yValuesTest[i] - result, 2);
  411. if ( nlhs >= 2 )
  412. {
  413. *itScores = result;
  414. itScores++;
  415. }
  416. }
  417. std::cerr << "Time for testing: " << testTime << std::endl;
  418. // clean up
  419. if ( dataIsSparse )
  420. {
  421. for ( std::vector<const NICE::SparseVector *>::iterator it = dataTest_sparse.begin(); it != dataTest_sparse.end(); it++)
  422. delete *it;
  423. }
  424. plhs[0] = mxCreateDoubleScalar( l2loss );
  425. if(nlhs >= 2)
  426. plhs[1] = MatlabConversion::convertVectorFromNice(scores);
  427. return;
  428. }
  429. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  430. // interface specific methods for incremental extensions
  431. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  432. // addExample
  433. if ( !strcmp("addExample", cmd.c_str() ) )
  434. {
  435. // Check parameters
  436. if ( (nlhs < 0) || (nrhs < 4) )
  437. {
  438. mexErrMsgTxt("Test: Unexpected arguments.");
  439. }
  440. //------------- read the data --------------
  441. NICE::SparseVector * newExample;
  442. double newLabel;
  443. if ( mxIsSparse( prhs[2] ) )
  444. {
  445. newExample = new NICE::SparseVector ( MatlabConversion::convertSparseVectorToNice( prhs[2] ) );
  446. }
  447. else
  448. {
  449. NICE::Vector * example;
  450. example = new NICE::Vector ( MatlabConversion::convertDoubleVectorToNice(prhs[2]) );
  451. newExample = new NICE::SparseVector ( *example );
  452. //----------------- clean up -------------
  453. delete example;
  454. }
  455. newLabel = MatlabConversion::convertMatlabToDouble( prhs[3] );
  456. // setting performOptimizationAfterIncrement is optional
  457. if ( nrhs > 4 )
  458. {
  459. bool performOptimizationAfterIncrement;
  460. performOptimizationAfterIncrement = MatlabConversion::convertMatlabToBool( prhs[4] );
  461. regressor->addExample ( newExample, newLabel, performOptimizationAfterIncrement );
  462. }
  463. else
  464. {
  465. regressor->addExample ( newExample, newLabel );
  466. }
  467. //----------------- clean up -------------
  468. delete newExample;
  469. return;
  470. }
  471. // addMultipleExamples
  472. if ( !strcmp("addMultipleExamples", cmd.c_str() ) )
  473. {
  474. // Check parameters
  475. if ( (nlhs < 0) || (nrhs < 4) )
  476. {
  477. mexErrMsgTxt("Test: Unexpected arguments.");
  478. }
  479. //------------- read the data --------------
  480. std::vector< const NICE::SparseVector *> newExamples;
  481. NICE::Vector newLabels;
  482. if ( mxIsSparse( prhs[2] ) )
  483. {
  484. newExamples = MatlabConversion::convertSparseMatrixToNice( prhs[2] );
  485. }
  486. else
  487. {
  488. NICE::Matrix newData;
  489. newData = MatlabConversion::convertDoubleMatrixToNice(prhs[2]);
  490. //----------------- convert data to sparse data structures ---------
  491. newExamples.resize( newData.rows() );
  492. std::vector< const NICE::SparseVector *>::iterator exTrainIt = newExamples.begin();
  493. for (int i = 0; i < (int)newData.rows(); i++, exTrainIt++)
  494. {
  495. *exTrainIt = new NICE::SparseVector( newData.getRow(i) );
  496. }
  497. }
  498. newLabels = MatlabConversion::convertDoubleVectorToNice(prhs[3]);
  499. // setting performOptimizationAfterIncrement is optional
  500. if ( nrhs > 4 )
  501. {
  502. bool performOptimizationAfterIncrement;
  503. performOptimizationAfterIncrement = MatlabConversion::convertMatlabToBool( prhs[4] );
  504. regressor->addMultipleExamples ( newExamples, newLabels, performOptimizationAfterIncrement );
  505. }
  506. else
  507. {
  508. regressor->addMultipleExamples ( newExamples, newLabels );
  509. }
  510. //----------------- clean up -------------
  511. for ( std::vector< const NICE::SparseVector *>::iterator exIt = newExamples.begin();
  512. exIt != newExamples.end(); exIt++
  513. )
  514. {
  515. delete *exIt;
  516. }
  517. return;
  518. }
  519. ///////////////////// INTERFACE PERSISTENT /////////////////////
  520. // interface specific methods for store and restore
  521. ///////////////////// INTERFACE PERSISTENT /////////////////////
  522. // store the regressor to an external file
  523. if ( !strcmp("store", cmd.c_str() ) || !strcmp("save", cmd.c_str() ) )
  524. {
  525. // Check parameters
  526. if ( nrhs < 3 )
  527. mexErrMsgTxt("store: no destination given.");
  528. std::string s_destination = MatlabConversion::convertMatlabToString( prhs[2] );
  529. std::filebuf fb;
  530. fb.open ( s_destination.c_str(), ios::out );
  531. std::ostream os(&fb);
  532. //
  533. regressor->store( os );
  534. //
  535. fb.close();
  536. return;
  537. }
  538. // load regressor from external file
  539. if ( !strcmp("restore", cmd.c_str() ) || !strcmp("load", cmd.c_str() ) )
  540. {
  541. // Check parameters
  542. if ( nrhs < 3 )
  543. mexErrMsgTxt("restore: no destination given.");
  544. std::string s_destination = MatlabConversion::convertMatlabToString( prhs[2] );
  545. std::cerr << " aim at restoring the regressor from " << s_destination << std::endl;
  546. std::filebuf fbIn;
  547. fbIn.open ( s_destination.c_str(), ios::in );
  548. std::istream is (&fbIn);
  549. //
  550. regressor->restore( is );
  551. //
  552. fbIn.close();
  553. return;
  554. }
  555. // Got here, so command not recognized
  556. std::string errorMsg (cmd.c_str() );
  557. errorMsg += " -- command not recognized.";
  558. mexErrMsgTxt( errorMsg.c_str() );
  559. }
  560. #endif