Changeset 1056
- Timestamp:
- 06/07/10 22:00:36 (15 years ago)
- Location:
- library/bdm
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/datasources.h
r944 r1056 304 304 UIREGISTER ( PdfDS ); 305 305 306 //! State-space data source simulating two densities307 class StateDS : public DS {308 protected:309 //!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$310 shared_ptr<pdf> IM;311 312 //!conditional pdf of the observations \f$ f(d_t|x_t) \f$313 shared_ptr<pdf> OM;314 315 //! result storage316 vec dt;317 //! state storage318 vec xt;319 //! input storage320 vec ut;321 322 //! datalink from ut to IM.rvc323 datalink_part u2imc;324 //! datalink from ut to OM.rvc325 datalink_part u2omc;326 public:327 void getdata ( vec &dt0 ) const {328 dt0 = dt;329 }330 void write ( const vec &ut0 ) {331 ut = ut0;332 }333 334 void getdata ( vec &dt0, const ivec &indices ) {335 dt0 = dt ( indices );336 }337 338 virtual void step();339 340 //! set parameters341 void set_parameters ( shared_ptr<pdf> IM0, shared_ptr<pdf> OM0 ) {342 IM = IM0;343 OM = OM0;344 }345 void set_initx ( const vec &x0 ) {346 xt = x0;347 }348 349 virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;350 351 /*! UI for stateDS352 353 The DS is constructed from a structure with fields:354 \code355 class = "stateDS";356 //Internal model357 IM = { type = "pdf-offspring"; };358 //Observation model359 OM = { type = "pdf-offspring"; }360 //initial state361 x0 = [...]; //vector of data362 \endcode363 Both models must have defined \c rv. and \c rvc364 Random variables not found in any rv are considered to be inputs.365 */366 void from_setting ( const Setting &set );367 368 // TODO dodelat void to_setting( Setting &set ) const;369 370 void validate();371 };372 373 UIREGISTER ( StateDS );374 SHAREDPTR ( StateDS );375 376 306 }; //namespace 377 307 -
library/bdm/mex/mex_datasource.h
r904 r1056 9 9 namespace bdm { 10 10 /*! 11 * \brief Memory storage of off-line data column-wise11 * \brief DataSource reading data columns from a Matlab matrix 12 12 13 The data are stored in an internal matrix \c Data . Each column of Data corresponds to one discrete time observation \f$t\f$. Access to this matrix is via indices \c rowid and \c delays. 13 The data are stored in a arbitrary Matlab matrix. Each column of the matrix corresponds to one discrete time observation \f$t\f$. 14 The DataSource needs to know the name of the matrix and possibly decription of its contents (via RV)_ 14 15 15 The data can be loaded from a file.16 16 */ 17 class mxArrayDS : public MemDS { 17 class mxArrayDS : public DS { 18 protected: 19 //! raw pointer to data 20 double *data; 21 //! maximum length of data 22 int max_len; 23 //! active column 24 int column; 18 25 public: 26 19 27 //!Default constructor 20 mxArrayDS () : MemDS() {};28 mxArrayDS () : DS() {}; 21 29 22 /*! \brief Create memorydata source from mxArray30 /*! \brief Create data source from mxArray 23 31 24 32 \code 25 system={ 26 type="mexDS"; 27 varname=""; // name of workspace variable 28 row_rv = {class='RV',...} // definition of 29 }; 33 class = 'mxArrayDS'; 34 varname = 'data_matrix'; // name of a workspace variable 35 drv = {class='RV',...} // names of data in columns 30 36 \endcode 31 32 MemDS with the above fields will be created; 33 37 34 38 */ 35 39 void from_setting ( const Setting &set ) { 36 Data = mxArray2mat ( mexGetVariable ( "base", set["varname"] ) ); 37 /* UI::get ( rowid, set, "rids" , UI::compulsory ); 38 bdm_assert_debug ( max ( rowid ) <= Data.rows(), "MemDS rowid is too high for given Dat." ); 39 40 UI::get ( delays, set, "tds", UI::compulsory ); 41 time = max ( delays ); 42 bdm_assert_debug ( time < Data.cols(), "MemDS delays are too high." ); 43 */ 44 //set MemDS 45 dtsize = Data.rows(); 40 string name; 41 UI::get(name, set, "varname", UI::compulsory); 42 mxArray *mxAr = mexGetVariable ( "base", name.c_str() ); 43 44 bdm_assert(mxIsNumeric(mxAr),"Matlab variable: "+name +" is not a matrix"); 45 46 dtsize = mxGetM(mxAr); 47 max_len= mxGetN(mxAr); 48 data = (double*)mxGetPr(mxAr); 46 49 utsize = 0; 47 50 48 shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 49 RV ru = RV(); 50 if ( r ) { 51 set_drv ( *r, ru ); 52 } else { 53 RV def ( ( const char* ) set["varname"], Data.rows() ); 54 set_drv ( def, ru ); 51 DS::from_setting(set); //read drv 52 53 if (Drv._dsize()!=dtsize){ 54 RV def ( ( const char* ) set["varname"], dtsize ); 55 set_drv ( def, Urv ); 56 } 57 column = 0; 58 59 if (max_length()>0){ 60 dt = vec(&data[column*dtsize],dtsize); 55 61 } 56 62 } 57 63 58 59 // TODO dodelat void to_setting( Setting &set ) const; 64 int max_length() {return max_len;} 65 66 void step(){ 67 if (column<max_length()){ 68 column++; 69 70 dt = vec(&data[column*dtsize],dtsize); 71 } else { 72 bdm_error("DS: trying to read after max_length()"); 73 } 74 } 60 75 }; 61 76 … … 63 78 SHAREDPTR ( mxArrayDS ); 64 79 65 /*!66 * \brief Matlab wrapper for DS mapping functions step() to a matlab function67 68 The identifier of matlab function is stored in attribute \c name.69 This identifier defines:70 \li function to call to do a step(): name_step.m71 \li workspace variable to write input to: name_input72 */73 class mexDS : public DS {74 protected:75 //! identifier of matlab function76 string step_name;77 //! identifier of matlab input variabel78 string input_name;79 //! cache of results from name_step80 vec dt;81 //! cache of inputs82 vec ut;83 public:84 //!Default constructor85 mexDS () : DS() {};86 87 /*! \brief Data source computed by matlab function88 89 \code90 system={91 class="mexDS";92 step_name=""; // name of function to call93 input_name=""; // name of workspace variable where inputs are written94 drv = {class='RV',...} // identification of outputs95 urv = {class='RV',...} // identification of inputs96 };97 \endcode98 99 MemDS with the above fields will be created;100 101 */102 void from_setting ( const Setting &set ) {103 UI::get ( step_name, set, "step_name", UI::compulsory );104 UI::get ( input_name, set, "input_name", UI::compulsory );105 106 shared_ptr<RV> rd = UI::build<RV> ( set, "drv", UI::compulsory );107 shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory );108 109 dtsize = rd->_dsize();110 utsize = ru->_dsize();111 112 set_drv ( *rd, *ru );113 validate();114 }115 116 virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID;117 118 virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;119 120 121 void step() {122 mxArray* tmp;123 mxArray* dummy = NULL;124 // write inputs to variable input_name125 mxArray* mxinp = mexGetVariable ( "global", input_name.c_str() ) ;126 if ( mxinp ) {127 if ( ( int ) mxGetM ( mxinp ) != utsize || ( int ) mxGetN ( mxinp ) != utsize ) {128 // mxinp is invalid - create new one129 mxDestroyArray ( mxinp );130 mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL );131 }132 133 } else {134 mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL );135 }136 vec2mxArray ( ut, mxinp );137 mexPutVariable ( "global", input_name.c_str(), mxinp );138 // call function step_name139 mexCallMATLAB ( 1, &tmp, 0, ( mxArray ** ) &dummy, step_name.c_str() );140 // save its results141 bdm_assert_debug ( ( int ) mxGetM ( tmp ) == dtsize || ( int ) mxGetN ( tmp ) == dtsize, "mexDS.step() expected return vector of length " + num2str ( dtsize ) +142 "got vector " + num2str ( ( int ) mxGetM ( tmp ) ) + "x" + num2str ( ( int ) mxGetN ( tmp ) ) );143 //write y144 dt.set_subvector ( 0, mxArray2vec ( tmp ) );145 //write u146 }147 void write ( const vec &ut0 ) {148 bdm_assert_debug ( ut0.length() == ut.length(), "mexDS: Incompatible input vector" );149 ut = ut0;150 }151 void getdata ( vec &dt_out ) const {152 bdm_assert_debug ( dt_out.length() == dt.length(), "mexDS: Incompatible output vector" );153 dt_out = dt;154 }155 156 void validate() {157 dt = zeros ( dtsize );158 ut = zeros ( utsize );159 }160 161 // TODO dodelat void to_setting( Setting &set ) const;162 };163 164 UIREGISTER ( mexDS );165 SHAREDPTR ( mexDS );166 167 80 } 168 81 #endif //MXDS_H