Changeset 622 for applications/bdmtoolbox/mex
- Timestamp:
- 09/16/09 22:52:57 (15 years ago)
- Location:
- applications/bdmtoolbox/mex
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/bdmtoolbox/mex/estimator.cpp
r611 r622 9 9 {rank="same"; "Data Source"; "Bayesian Model"} 10 10 "Data Source" -> "Bayesian Model" [label="data"]; 11 "Bayesian Model" -> "Result Logger" [label="estimated\n statistics"]; 11 "Bayesian Model" -> "Result Logger" [label="estimated\n statistics"]; 12 12 "Data Source" -> "Result Logger" [label="Simulated\n data"]; 13 13 } 14 14 \enddot 15 15 16 Here, 16 Here, 17 17 \li Data Source is an object (class DS) providing sequential data, \f$ [d_1, d_2, \ldots d_t] \f$. 18 18 \li Bayesian Model is an object (class BM) performing Bayesian filtering, … … 65 65 void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { 66 66 // Check the number of inputs and output arguments 67 if (n_input<2) mexErrMsgTxt("Usage:\n"68 "result=estimator(system, estimators, experiment, logger)\n"69 " system = struct('class','datasource',...); % Estimated system\n"70 " estimators = {struct('class','estimator',...), % Estimators\n"71 " struct('class','estimator',...),...} \n"72 " === optional ==="73 " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n"74 " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n"75 "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM.");76 67 if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" 68 "result=estimator(system, estimators, experiment, logger)\n" 69 " system = struct('class','datasource',...); % Estimated system\n" 70 " estimators = {struct('class','estimator',...), % Estimators\n" 71 " struct('class','estimator',...),...} \n" 72 " === optional ===" 73 " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n" 74 " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n" 75 "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); 76 77 77 RV::clear_all(); 78 //CONFIG 78 //CONFIG 79 79 UImxArray Cfg; 80 try {81 Cfg.addGroup(input[0],"system");82 Cfg.addList(input[1],"estimators");83 if (n_input>2){84 Cfg.addGroup(input[2],"experiment");85 }86 if (n_input>3){87 Cfg.addGroup(input[3],"logger");88 }/*else{80 try { 81 Cfg.addGroup ( input[0],"system" ); 82 Cfg.addList ( input[1],"estimators" ); 83 if ( n_input>2 ) { 84 Cfg.addGroup ( input[2],"experiment" ); 85 } 86 if ( n_input>3 ) { 87 Cfg.addGroup ( input[3],"logger" ); 88 }/*else{ 89 89 // define logger as mexlog 90 90 Setting &S=Cfg.getRoot(); … … 97 97 S["logger"]["maxlen"]=maxlen; 98 98 }*/ 99 } catch(SettingException e){it_error("error: "+string(e.getPath()));} 99 } catch ( SettingException e ) { 100 it_error ( "error: "+string ( e.getPath() ) ); 101 } 100 102 101 103 //DBG 102 Cfg.writeFile ("estimator.cfg");104 Cfg.writeFile ( "estimator.cfg" ); 103 105 104 106 #else … … 112 114 UIFile Cfg ( fname ); 113 115 #endif 114 115 shared_ptr<DS> Ds = UI::build<DS>( Cfg, "system" ); 116 Array<shared_ptr<BM> > Es; UI::get(Es,Cfg, "estimators" ); 116 117 shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" ); 118 Array<shared_ptr<BM> > Es; 119 UI::get ( Es,Cfg, "estimators" ); 117 120 long Ndat=10; 118 if ( Cfg.exists("experiment")) {119 if ( Cfg.lookupValue ( "experiment.ndat",Ndat )) {120 bdm_assert (Ndat<=Ds->max_length(), "Data source has less data then required");121 if ( Cfg.exists ( "experiment" ) ) { 122 if ( Cfg.lookupValue ( "experiment.ndat",Ndat ) ) { 123 bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" ); 121 124 }; 122 125 } else { 123 if ( Ds->max_length() < inf) {126 if ( Ds->max_length() < inf ) { 124 127 Ndat=Ds->max_length(); 125 };// else Ndat=10; 128 } 129 ;// else Ndat=10; 126 130 } 127 shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional);128 if ( !L) {129 131 shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional ); 132 if ( !L ) { 133 #ifdef MEX 130 134 //mex logger has only from_setting constructor - we have to fill it... 131 L=new mexlog (Ndat);132 135 L=new mexlog ( Ndat ); 136 #else 133 137 L=new stdlog(); 134 138 #endif 135 139 } 136 140 137 141 Ds->log_add ( *L ); 138 142 string Ename; 139 143 Setting &S=Cfg; 140 for ( int i=0; i<Es.length(); i++){141 try {142 UI::get (Ename, S["estimators"][i], "name");143 } catch ( UIException e){144 Ename="Est"+num2str (i);144 for ( int i=0; i<Es.length(); i++ ) { 145 try { 146 UI::get ( Ename, S["estimators"][i], "name" ); 147 } catch ( UIException e ) { 148 Ename="Est"+num2str ( i ); 145 149 } 146 147 Es (i)->log_add(*L,Ename); // estimate150 151 Es ( i )->log_add ( *L,Ename ); // estimate 148 152 } 149 153 L->init(); 150 154 151 155 vec dt=zeros ( Ds->_drv()._dsize() ); //data variable 152 Array<datalink*> Dls (Es.length());153 for ( int i=0; i<Es.length(); i++){154 Dls (i)=new datalink( Es(i)->_drv(),Ds->_drv() ); //datalink between a datasource and estimator156 Array<datalink*> Dls ( Es.length() ); 157 for ( int i=0; i<Es.length(); i++ ) { 158 Dls ( i ) =new datalink ( Es ( i )->_drv(),Ds->_drv() ); //datalink between a datasource and estimator 155 159 } 156 160 157 161 for ( int tK=0;tK<Ndat;tK++ ) { 158 162 Ds->getdata ( dt ); // read data 159 163 Ds->logit ( *L ); 160 161 for ( int i=0; i<Es.length(); i++){162 Es (i)->bayes ( Dls(i)->pushdown ( dt ) ); // update estimates163 Es (i)->logit (*L);164 165 for ( int i=0; i<Es.length(); i++ ) { 166 Es ( i )->bayes ( Dls ( i )->pushdown ( dt ) ); // update estimates 167 Es ( i )->logit ( *L ); 164 168 } 165 169 L->step(); … … 171 175 172 176 #ifdef MEX 173 mexlog* mL=dynamic_cast<mexlog*> (L.get());177 mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); 174 178 175 if ( mL) { // user wants output!!179 if ( mL ) { // user wants output!! 176 180 if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); 177 181 output[0] = mL->toCell(); -
applications/bdmtoolbox/mex/merger.cpp
r569 r622 29 29 30 30 #ifdef MEX 31 void mexFunction (int n_output, mxArray *output[], int n_input, const mxArray *input[]) 32 { 31 void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { 33 32 // Check the number of inputs and output arguments 34 if ( n_input != 3) mexErrMsgTxt ("Usage:\n"35 "result=merger(sources, support, merger)\n"36 " sources= { struct('class','epdf'),... }; % cell of pdfs (epdfs or mpdfs) to be merged,\n"37 " support= struct(\n"38 " grid = {[dim1_start,dim1_end], [dim2_start, dim2_end]...} %support boundary \n"39 " nbins = [bins_in_dim1, bins_in_dim2,...] %fixed \n"40 " === OR ==\n"41 " pdf = struct('class','epdf'); % pdf to draw samples from\n"42 " nsamples= 100; % number of samples\n"43 " );\n"44 " If all elements are present, (grid,nbins) is used;\n"45 " merger = struct('class','merger_*'); % object to be used for merging,\n\n"46 "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM.");33 if ( n_input != 3 ) mexErrMsgTxt ( "Usage:\n" 34 "result=merger(sources, support, merger)\n" 35 " sources= { struct('class','epdf'),... }; % cell of pdfs (epdfs or mpdfs) to be merged,\n" 36 " support= struct(\n" 37 " grid = {[dim1_start,dim1_end], [dim2_start, dim2_end]...} %support boundary \n" 38 " nbins = [bins_in_dim1, bins_in_dim2,...] %fixed \n" 39 " === OR ==\n" 40 " pdf = struct('class','epdf'); % pdf to draw samples from\n" 41 " nsamples= 100; % number of samples\n" 42 " );\n" 43 " If all elements are present, (grid,nbins) is used;\n" 44 " merger = struct('class','merger_*'); % object to be used for merging,\n\n" 45 "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM." ); 47 46 RV::clear_all(); 48 47 // LOAD CONFIG 49 48 UImxArray Cfg; 50 Cfg.addList ( input[0], "Sources");51 Cfg.addGroup ( input[1], "Support");52 Cfg.addGroup ( input[2], "Merger");49 Cfg.addList ( input[0], "Sources" ); 50 Cfg.addGroup ( input[1], "Support" ); 51 Cfg.addGroup ( input[2], "Merger" ); 53 52 54 53 //DBG 55 Cfg.writeFile ( "merger.cfg");54 Cfg.writeFile ( "merger.cfg" ); 56 55 #else 57 56 int main() { 58 UIFile Cfg ( "merger.cfg");57 UIFile Cfg ( "merger.cfg" ); 59 58 #endif 60 59 // Sources 61 60 Array<shared_ptr<mpdf> > Sources; 62 61 //abuse Mer to store sources 63 Setting& _Sources = Cfg.lookup ( "Sources");62 Setting& _Sources = Cfg.lookup ( "Sources" ); 64 63 int Slen = _Sources.getLength(); 65 Sources.set_size ( Slen);66 for ( int i = 0; i < Slen; i++) {64 Sources.set_size ( Slen ); 65 for ( int i = 0; i < Slen; i++ ) { 67 66 try { 68 shared_ptr<mpdf> mtmp = UI::build<mpdf> ( _Sources, i);69 Sources ( i) = mtmp;70 } catch ( UIException) {67 shared_ptr<mpdf> mtmp = UI::build<mpdf> ( _Sources, i ); 68 Sources ( i ) = mtmp; 69 } catch ( UIException ) { 71 70 // it is not mpdf - see if it is epdf 72 71 try { 73 shared_ptr<epdf> etmp = UI::build<epdf> ( _Sources, i);74 if ( etmp) {75 Sources ( i) = new mepdf (etmp); // hopefully OK72 shared_ptr<epdf> etmp = UI::build<epdf> ( _Sources, i ); 73 if ( etmp ) { 74 Sources ( i ) = new mepdf ( etmp ); // hopefully OK 76 75 } 77 } catch ( UIException &e) {78 it_error ( "No mpdfs or epdfs found! " + string (e.what()));79 } catch ( std::exception e) {80 it_error ( "Error in UI at " + _Sources[i].getPath());76 } catch ( UIException &e ) { 77 it_error ( "No mpdfs or epdfs found! " + string ( e.what() ) ); 78 } catch ( std::exception e ) { 79 it_error ( "Error in UI at " + _Sources[i].getPath() ); 81 80 } 82 } catch ( std::exception &e) {83 it_error ( "Error in UI at " + _Sources[i].getPath());81 } catch ( std::exception &e ) { 82 it_error ( "Error in UI at " + _Sources[i].getPath() ); 84 83 } 85 84 } 86 85 87 shared_ptr<merger_base> Merger = UI::build<merger_base> ( Cfg, "Merger");86 shared_ptr<merger_base> Merger = UI::build<merger_base> ( Cfg, "Merger" ); 88 87 89 88 // Support 90 89 try { 91 shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> (Cfg, "Support"); 92 Merger->set_support(*RecSup); 93 } 94 catch (UIException &e) { 95 shared_ptr<discrete_support> DisSup = UI::build<discrete_support> (Cfg, "Support"); 96 Merger->set_support (*DisSup); 90 shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> ( Cfg, "Support" ); 91 Merger->set_support ( *RecSup ); 92 } catch ( UIException &e ) { 93 shared_ptr<discrete_support> DisSup = UI::build<discrete_support> ( Cfg, "Support" ); 94 Merger->set_support ( *DisSup ); 97 95 } 98 96 // COMPUTE RESULTS 99 Merger->set_sources ( Sources);97 Merger->set_sources ( Sources ); 100 98 Merger->merge(); 101 99 102 100 // save results 103 Array<vec> source_vals ( Sources.length());104 for ( int i = 0;i < Sources.length();i++) {101 Array<vec> source_vals ( Sources.length() ); 102 for ( int i = 0;i < Sources.length();i++ ) { 105 103 datalink_m2e dl; 106 dl.set_connection ( Sources (i)->_rv(), Sources (i)->_rvc(), Merger->_rv());104 dl.set_connection ( Sources ( i )->_rv(), Sources ( i )->_rvc(), Merger->_rv() ); 107 105 108 vec ll ( Merger->_Smp()._samples().length());109 for ( int j = 0; j < Merger->_Smp()._samples().length(); j++) {110 vec dt = dl.pushdown ( Merger->_Smp()._samples() (j));111 vec dtc = dl.get_cond ( Merger->_Smp()._samples() (j));112 ll ( j) = Sources (i)->evallogcond (dt, dtc);106 vec ll ( Merger->_Smp()._samples().length() ); 107 for ( int j = 0; j < Merger->_Smp()._samples().length(); j++ ) { 108 vec dt = dl.pushdown ( Merger->_Smp()._samples() ( j ) ); 109 vec dtc = dl.get_cond ( Merger->_Smp()._samples() ( j ) ); 110 ll ( j ) = Sources ( i )->evallogcond ( dt, dtc ); 113 111 } 114 112 115 vec sll = exp ( ll);113 vec sll = exp ( ll ); 116 114 117 source_vals ( i) = sll / sum (sll);115 source_vals ( i ) = sll / sum ( sll ); 118 116 } 119 117 120 merger_mix* MerMix=dynamic_cast<merger_mix*> (Merger.get());118 merger_mix* MerMix=dynamic_cast<merger_mix*> ( Merger.get() ); 121 119 vec mix_val; 122 123 if ( MerMix){124 vec ll ( Merger->_Smp()._samples().length());125 for ( int j = 0; j < Merger->_Smp()._samples().length(); j++) {126 ll ( j) = Merger->evallog (Merger->_Smp()._samples() (j));120 121 if ( MerMix ) { 122 vec ll ( Merger->_Smp()._samples().length() ); 123 for ( int j = 0; j < Merger->_Smp()._samples().length(); j++ ) { 124 ll ( j ) = Merger->evallog ( Merger->_Smp()._samples() ( j ) ); 127 125 } 128 129 vec sll = exp ( ll);130 131 mix_val = sll / sum ( sll);126 127 vec sll = exp ( ll ); 128 129 mix_val = sll / sum ( sll ); 132 130 } 133 131 … … 135 133 mxArray* tmp ; 136 134 // Save results 137 if ( n_output > 0) {138 tmp = mxCreateStructMatrix ( 1, 1, 0, NULL);135 if ( n_output > 0 ) { 136 tmp = mxCreateStructMatrix ( 1, 1, 0, NULL ); 139 137 //support 140 138 Array<vec> &samples = Merger->_Smp()._samples(); 141 if ( samples.size() > 0) {142 mxArray* fld = mxCreateDoubleMatrix ( samples (0).length(), samples.size(), mxREAL);143 Arrayvec2mxArray ( samples, fld);144 mxReplaceFieldNM ( tmp, "support", fld);139 if ( samples.size() > 0 ) { 140 mxArray* fld = mxCreateDoubleMatrix ( samples ( 0 ).length(), samples.size(), mxREAL ); 141 Arrayvec2mxArray ( samples, fld ); 142 mxReplaceFieldNM ( tmp, "support", fld ); 145 143 } 146 144 147 145 //weights 148 146 vec &w = Merger->_Smp()._w(); 149 mxArray* fldw = mxCreateDoubleMatrix ( 1, w.length(), mxREAL);150 vec2mxArray ( w, fldw);151 mxReplaceFieldNM ( tmp, "weights", fldw);147 mxArray* fldw = mxCreateDoubleMatrix ( 1, w.length(), mxREAL ); 148 vec2mxArray ( w, fldw ); 149 mxReplaceFieldNM ( tmp, "weights", fldw ); 152 150 153 151 //mixture values 154 if ( mix_val.length()>0){155 mxArray* fldm = mxCreateDoubleMatrix ( 1, w.length(), mxREAL);156 vec2mxArray ( mix_val, fldm);157 mxReplaceFieldNM ( tmp, "mix", fldm);152 if ( mix_val.length() >0 ) { 153 mxArray* fldm = mxCreateDoubleMatrix ( 1, w.length(), mxREAL ); 154 vec2mxArray ( mix_val, fldm ); 155 mxReplaceFieldNM ( tmp, "mix", fldm ); 158 156 } 159 157 // sources 160 158 char srcstr[20]; 161 for ( int i = 0;i < Sources.length();i++) {162 sprintf ( srcstr, "source%d", i + 1);163 mxArray* fldw = mxCreateDoubleMatrix ( 1, source_vals (i).length(), mxREAL);164 vec2mxArray ( source_vals (i), fldw);165 mxReplaceFieldNM ( tmp, srcstr, fldw);159 for ( int i = 0;i < Sources.length();i++ ) { 160 sprintf ( srcstr, "source%d", i + 1 ); 161 mxArray* fldw = mxCreateDoubleMatrix ( 1, source_vals ( i ).length(), mxREAL ); 162 vec2mxArray ( source_vals ( i ), fldw ); 163 mxReplaceFieldNM ( tmp, srcstr, fldw ); 166 164 } 167 165 -
applications/bdmtoolbox/mex/mxstruct2config.cpp
r391 r622 2 2 3 3 void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { 4 UImxArray F ( input[0]);5 string filename = mxArray2string (input[1]);6 F.writeFile (filename.c_str());4 UImxArray F ( input[0] ); 5 string filename = mxArray2string ( input[1] ); 6 F.writeFile ( filename.c_str() ); 7 7 } -
applications/bdmtoolbox/mex/simulator.cpp
r617 r622 12 12 \enddot 13 13 14 Here, 14 Here, 15 15 \li Data Source is an object (class DS) providing sequential data, \f$ [d_1, d_2, \ldots d_t] \f$. 16 16 \li Result Logger is an object (class logger) dedicated to storing important data from the experiment. … … 56 56 void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { 57 57 // Check the number of inputs and output arguments 58 if (n_input<1) mexErrMsgTxt("Usage:\n"59 "result=simulator(system, experiment, logger)\n"60 " system = struct('class','datasource',...); % Estimated system\n"61 " === optional ==="62 " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n"63 " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n"64 "see documentation of classes datasource and mexlogger and their offsprings in BDM.");65 58 if ( n_input<1 ) mexErrMsgTxt ( "Usage:\n" 59 "result=simulator(system, experiment, logger)\n" 60 " system = struct('class','datasource',...); % Estimated system\n" 61 " === optional ===" 62 " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n" 63 " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n" 64 "see documentation of classes datasource and mexlogger and their offsprings in BDM." ); 65 66 66 RV::clear_all(); 67 //CONFIG 67 //CONFIG 68 68 UImxArray Cfg; 69 try {70 Cfg.addGroup(input[0],"system");71 if (n_input>2){72 Cfg.addGroup(input[1],"experiment");73 }74 if (n_input>3){75 Cfg.addGroup(input[2],"logger");76 }/*else{69 try { 70 Cfg.addGroup ( input[0],"system" ); 71 if ( n_input>2 ) { 72 Cfg.addGroup ( input[1],"experiment" ); 73 } 74 if ( n_input>3 ) { 75 Cfg.addGroup ( input[2],"logger" ); 76 }/*else{ 77 77 // define logger as mexlog 78 78 Setting &S=Cfg.getRoot(); … … 85 85 S["logger"]["maxlen"]=maxlen; 86 86 }*/ 87 } catch(SettingException e){it_error("error: "+string(e.getPath()));} 87 } catch ( SettingException e ) { 88 it_error ( "error: "+string ( e.getPath() ) ); 89 } 88 90 89 91 //DBG 90 Cfg.writeFile ("simulator.cfg");92 Cfg.writeFile ( "simulator.cfg" ); 91 93 92 94 #else … … 100 102 UIFile Cfg ( fname ); 101 103 #endif 102 103 shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" );104 105 shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" ); 104 106 long Ndat=10; 105 if ( Cfg.exists("experiment")) {106 if ( Cfg.lookupValue ( "experiment.ndat",Ndat )) {107 bdm_assert (Ndat<=Ds->max_length(), "Data source has less data then required");107 if ( Cfg.exists ( "experiment" ) ) { 108 if ( Cfg.lookupValue ( "experiment.ndat",Ndat ) ) { 109 bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" ); 108 110 }; 109 111 } else { 110 if ( Ds->max_length() < std::numeric_limits< int >::max()) {112 if ( Ds->max_length() < std::numeric_limits< int >::max() ) { 111 113 Ndat=Ds->max_length(); 112 };// else Ndat=10; 114 } 115 ;// else Ndat=10; 113 116 } 114 shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional);115 if ( !L) {116 117 shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional ); 118 if ( !L ) { 119 #ifdef MEX 117 120 //mex logger has only from_setting constructor - we have to fill it... 118 L=new mexlog (Ndat);119 121 L=new mexlog ( Ndat ); 122 #else 120 123 L=new stdlog(); 121 124 #endif 122 125 } 123 126 124 127 Ds->log_add ( *L ); 125 128 L->init(); … … 130 133 Ds->getdata ( dt ); // read data 131 134 Ds->logit ( *L ); 132 135 133 136 L->step(); 134 137 Ds->step(); // simulator step … … 139 142 140 143 #ifdef MEX 141 mexlog* mL=dynamic_cast<mexlog*> (L.get());144 mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); 142 145 143 if ( mL) { // user wants output!!146 if ( mL ) { // user wants output!! 144 147 if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); 145 148 output[0] = mL->toCell();