00001
00013 #ifndef DATASOURCE_H
00014 #define DATASOURCE_H
00015
00016
00017 #include "../base/bdmbase.h"
00018 #include "../stat/exp_family.h"
00019 #include "../base/user_info.h"
00020
00021 namespace bdm {
00029 class MemDS : public DS {
00030 protected:
00032 mat Data;
00034 int time;
00036 ivec rowid;
00037
00038 public:
00039 int max_length() {return Data.cols();}
00040 void getdata ( vec &dt );
00041 void getdata ( vec &dt, const ivec &indeces );
00042 void set_drv (const RV &drv,const RV &urv );
00043
00044 void write ( const vec &ut ) {
00045 bdm_error ( "MemDS::write is not supported" );
00046 }
00047
00048 void write ( const vec &ut, const ivec &indices ) {
00049 bdm_error ( "MemDS::write is not supported" );
00050 }
00051
00052 void step();
00054 MemDS () {};
00056 MemDS ( mat &Dat, ivec &rowid0);
00081 void from_setting(const Setting &set){
00082 UI::get(Data, set, "Data", UI::compulsory);
00083 if(!UI::get(time, set,"time", UI::optional)) {time =0;}
00084 if(!UI::get(rowid, set, "rowid",UI::optional)) {rowid =linspace(0,Data.rows()-1);}
00085 shared_ptr<RV> r=UI::build<RV>(set,"drv",UI::optional);
00086 if (!r) {r=new RV();
00087 for (int i=0; i<rowid.length(); i++){ r->add(RV("ch"+num2str(rowid(i)), 1, 0));}
00088 }
00089 set_drv(*r,RV());
00090 dtsize=r->_dsize();
00091 utsize=0;
00092 }
00093 };
00094 UIREGISTER(MemDS);
00095
00101 class EpdfDS: public DS {
00102 protected:
00104 shared_ptr<epdf> iepdf;
00106 vec dt;
00107 public:
00108 void step() {
00109 dt=iepdf->sample();
00110 }
00111 void getdata ( vec &dt_out ) {
00112 dt_out = dt;
00113 }
00114 void getdata ( vec &dt_out, const ivec &ids ) {
00115 dt_out = dt ( ids );
00116 }
00117 const RV& _drv() const {
00118 return iepdf->_rv();
00119 }
00120
00128 void from_setting ( const Setting &set ) {
00129 iepdf=UI::build<epdf> ( set,"epdf",UI::compulsory );
00130 bdm_assert(iepdf->isnamed(), "Input epdf must be named, check if RV is given correctly");
00131 dt = zeros(iepdf->dimension());
00132 dtsize=dt.length();
00133 set_drv(iepdf->_rv(),RV());
00134 utsize =0;
00135 validate();
00136 }
00137 void validate() {
00138 dt = iepdf->sample();
00139 }
00140 };
00141 UIREGISTER ( EpdfDS );
00142
00146 class MpdfDS :public DS {
00147 protected:
00149 shared_ptr<mpdf> impdf;
00151 vec yt;
00153 vec ut;
00155 datalink_buffered ut2rgr;
00157 datalink_buffered yt2rgr;
00159 vec rgr;
00160
00161 public:
00162 void step() {
00163 yt2rgr.step(yt);
00164 ut2rgr.filldown ( ut,rgr );
00165 yt2rgr.filldown ( yt,rgr );
00166 yt=impdf->samplecond ( rgr );
00167 ut2rgr.step(ut);
00168 }
00169 void getdata ( vec &dt_out ) {
00170 bdm_assert_debug(dt_out.length()>=utsize+ytsize,"Short output vector");
00171 dt_out.set_subvector(0, yt);
00172 dt_out.set_subvector(ytsize, ut);
00173 }
00174 void write(const vec &ut0){ut=ut0;}
00175
00187 void from_setting ( const Setting &set ) {
00188 impdf=UI::build<mpdf> ( set,"mpdf",UI::compulsory );
00189
00190 Yrv = impdf->_rv();
00191
00192 RV rgrv0=impdf->_rvc().remove_time();
00193
00194 Urv=rgrv0.subt(Yrv);
00195 set_drv(Yrv, Urv);
00196
00197 ut2rgr.set_connection(impdf->_rvc(), Urv);
00198 yt2rgr.set_connection(impdf->_rvc(), Yrv);
00199
00200
00201 shared_ptr<RV> rv_ini=UI::build<RV>(set,"init_rv",UI::optional);
00202 if(rv_ini){
00203 vec val;
00204 UI::get(val, set, "init_values", UI::optional);
00205 if (val.length()!=rv_ini->_dsize()){
00206 bdm_error("init_rv and init_values fields have incompatible sizes");
00207 } else {
00208 ut2rgr.set_history(*rv_ini, val);
00209 yt2rgr.set_history(*rv_ini, val);
00210 }
00211 }
00212
00213 yt = zeros ( impdf->dimension() );
00214 rgr = zeros ( impdf->dimensionc() );
00215 ut = zeros(Urv._dsize());
00216
00217 ytsize=yt.length();
00218 utsize=ut.length();
00219 dtsize = ytsize+utsize;
00220 validate();
00221 }
00222 void validate() {
00223
00224 ut2rgr.filldown ( ut,rgr );
00225 yt2rgr.filldown ( yt,rgr );
00226 yt=impdf->samplecond ( rgr );
00227 }
00228 };
00229 UIREGISTER ( MpdfDS );
00230
00234 class FileDS: public MemDS {
00235
00236 public:
00237 void getdata ( vec &dt ) {
00238 dt = Data.get_col ( time );
00239 }
00240
00241 void getdata ( vec &dt, const ivec &indices ) {
00242 vec tmp = Data.get_col ( time );
00243 dt = tmp ( indices );
00244 }
00245
00247 int ndat() {
00248 return Data.cols();
00249 }
00251 void log_add ( logger &L ) {};
00253 void logit ( logger &L ) {};
00254 };
00255
00262 class ITppFileDS: public FileDS {
00263
00264 public:
00266 ITppFileDS ( const string &fname, const string &varname ) : FileDS() {
00267 it_file it ( fname );
00268 it << Name ( varname );
00269 it >> Data;
00270 time = 0;
00271
00272 };
00273
00274 ITppFileDS () : FileDS() {
00275 };
00276
00277 void from_setting ( const Setting &set );
00278
00279
00280
00281 };
00282
00283 UIREGISTER ( ITppFileDS );
00284 SHAREDPTR ( ITppFileDS );
00285
00293 class CsvFileDS: public FileDS {
00294
00295 public:
00297 CsvFileDS ( const string& fname, const string& orientation = "BY_COL" );
00298 };
00299
00300
00301
00302
00303
00305 class stateDS : public DS {
00306 private:
00308 shared_ptr<mpdf> IM;
00309
00311 shared_ptr<mpdf> OM;
00312
00313 protected:
00315 vec dt;
00317 vec xt;
00319 vec ut;
00321 int L_xt;
00322
00323 public:
00324 void getdata ( vec &dt0 ) {
00325 dt0 = dt;
00326 }
00327
00328 void getdata ( vec &dt0, const ivec &indices ) {
00329 dt0 = dt ( indices );
00330 }
00332 stateDS ( const shared_ptr<mpdf> &IM0, const shared_ptr<mpdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ),
00333 dt ( OM0->dimension() ), xt ( IM0->dimension() ),
00334 ut ( usize ), L_xt ( 0 ) { }
00335
00336 stateDS() : L_xt ( 0 ) { }
00337
00338 virtual void step() {
00339 xt = IM->samplecond ( concat ( xt, ut ) );
00340 dt = OM->samplecond ( concat ( xt, ut ) );
00341 }
00342
00343 virtual void log_add ( logger &L ) {
00344 DS::log_add ( L );
00345 L_xt = L.add ( IM->_rv(), "true" );
00346 }
00347 virtual void logit ( logger &L ) {
00348 DS::logit ( L );
00349 L.logit ( L_xt, xt );
00350 }
00351
00381 void from_setting ( const Setting &set );
00382
00383
00384
00385 };
00386
00387 UIREGISTER ( stateDS );
00388 SHAREDPTR ( stateDS );
00389
00390 };
00391
00392 #endif // DS_H