Changeset 1056 for library

Show
Ignore:
Timestamp:
06/07/10 22:00:36 (14 years ago)
Author:
smidl
Message:

Cleanup in datasources

Location:
library/bdm
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/datasources.h

    r944 r1056  
    304304UIREGISTER ( PdfDS ); 
    305305 
    306 //! State-space data source simulating two densities 
    307 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 storage 
    316         vec dt; 
    317         //! state storage 
    318         vec xt; 
    319         //! input storage 
    320         vec ut; 
    321  
    322         //! datalink from ut to IM.rvc 
    323         datalink_part u2imc; 
    324         //! datalink from ut to OM.rvc 
    325         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 parameters 
    341         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 stateDS 
    352  
    353         The DS is constructed from a structure with fields: 
    354         \code 
    355         class = "stateDS"; 
    356         //Internal model 
    357         IM = { type = "pdf-offspring"; }; 
    358         //Observation model 
    359         OM = { type = "pdf-offspring"; } 
    360         //initial state 
    361         x0 = [...]; //vector of data 
    362         \endcode 
    363         Both models must have defined \c rv. and \c rvc 
    364         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  
    376306}; //namespace 
    377307 
  • library/bdm/mex/mex_datasource.h

    r904 r1056  
    99namespace bdm { 
    1010/*! 
    11 * \brief Memory storage of off-line data column-wise 
     11* \brief DataSource reading data columns from a Matlab matrix  
    1212 
    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. 
     13The data are stored in a arbitrary Matlab matrix. Each column of the matrix corresponds to one discrete time observation \f$t\f$.  
     14The DataSource needs to know the name of the matrix and possibly decription of its contents (via RV)_ 
    1415 
    15 The data can be loaded from a file. 
    1616*/ 
    17 class mxArrayDS : public MemDS { 
     17class 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; 
    1825public: 
     26         
    1927        //!Default constructor 
    20         mxArrayDS () : MemDS() {}; 
     28        mxArrayDS () : DS() {}; 
    2129 
    22         /*! \brief Create memory data source from mxArray 
     30        /*! \brief Create data source from mxArray 
    2331 
    2432        \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 
    3036        \endcode 
    31  
    32         MemDS with the above fields will be created; 
    33  
     37         
    3438        */ 
    3539        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); 
    4649                utsize = 0; 
    4750 
    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); 
    5561                } 
    5662        } 
    5763 
    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        } 
    6075}; 
    6176 
     
    6378SHAREDPTR ( mxArrayDS ); 
    6479 
    65 /*! 
    66 * \brief Matlab wrapper for DS mapping functions step() to a matlab function 
    67  
    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.m 
    71 \li workspace variable to write input to: name_input 
    72 */ 
    73 class mexDS : public DS { 
    74 protected: 
    75         //! identifier of matlab function 
    76         string step_name; 
    77         //! identifier of matlab input variabel 
    78         string input_name; 
    79         //! cache of results from name_step 
    80         vec dt; 
    81         //! cache of inputs 
    82         vec ut; 
    83 public: 
    84         //!Default constructor 
    85         mexDS () : DS() {}; 
    86  
    87         /*! \brief Data source computed by matlab function 
    88  
    89         \code 
    90         system={ 
    91                 class="mexDS"; 
    92                 step_name="";              // name of function to call 
    93                 input_name="";             // name of workspace variable where inputs are written 
    94                 drv = {class='RV',...}  // identification of outputs 
    95                 urv = {class='RV',...}   // identification of inputs 
    96                 }; 
    97         \endcode 
    98  
    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_name 
    125                 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 one 
    129                                 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_name 
    139                 mexCallMATLAB ( 1, &tmp, 0, ( mxArray ** ) &dummy, step_name.c_str() ); 
    140                 // save its results 
    141                 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  y 
    144                 dt.set_subvector ( 0, mxArray2vec ( tmp ) ); 
    145                 //write u 
    146         } 
    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  
    16780} 
    16881#endif //MXDS_H