root/applications/bdmtoolbox/mex/merger.cpp @ 760

Revision 760, 5.1 kB (checked in by smidl, 14 years ago)

cleanups & stuff for SYSID like estimation

  • Property svn:eol-style set to native
Line 
1#include <stat/merger.h>
2
3#ifdef MEX
4#include <itpp/itmex.h>
5#include <mex/mex_logger.h>
6#include <mex/mex_parser.h>
7#endif
8
9/*! \file
10\brief Merging static pdfs
11Merger is a program/mex-function for static merging of given pdfs on given support.
12
13In command line, it expected config file with the following structure:
14\code
15Sources = (... any epdf or mpdf  ...);
16Support = {grid=([low1d,high1d],[low2d,high2d],...);
17                   nbins=[bins1d,bins2d,... ]};
18// OR
19Support = {pdf={class='epdf',...};
20                   nsamples=2};
21Merger = {class="merger_base",...}
22\endcode
23
24In mex-file, the above structures (Sources,Support,Merger) are input arguments. Type >>merger for help.
25*/
26
27using namespace bdm;
28
29#ifdef MEX
30void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) {
31        // Check the number of inputs and output arguments
32        if ( n_input != 3 ) mexErrMsgTxt ( "Usage:\n"
33                                                   "result=merger(sources, support, merger)\n"
34                                                   "  sources= { struct('class','epdf'),... };  % cell of pdfs (epdfs or mpdfs) to be merged,\n"
35                                                   "  support= struct(\n"
36                                                   "           grid    = {[dim1_start,dim1_end], [dim2_start, dim2_end]...}  %support boundary \n"
37                                                   "           nbins   = [bins_in_dim1, bins_in_dim2,...]                    %fixed \n"
38                                                   "         === OR ==\n"
39                                                   "           pdf     = struct('class','epdf'); % pdf to draw samples from\n"
40                                                   "           nsamples= 100;                    % number of samples\n"
41                                                   "           );\n"
42                                                   "        If all elements are present,  (grid,nbins) is used;\n"
43                                                   "  merger = struct('class','merger_*');       % object to be used for merging,\n\n"
44                                                   "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM." );
45        RV::clear_all();
46        // LOAD CONFIG
47        UImxArray Cfg;
48        Cfg.addList ( input[0], "Sources" );
49        Cfg.addGroup ( input[1], "Support" );
50        Cfg.addGroup ( input[2], "Merger" );
51
52        //DBG
53        Cfg.writeFile ( "merger.cfg" );
54#else
55int main() {
56        UIFile Cfg ( "merger.cfg" );
57#endif
58        // Sources
59        Array<shared_ptr<pdf> >  Sources;
60        UI::get(Sources, Cfg, "Sources", UI::compulsory);
61        shared_ptr<merger_base> Merger = UI::build<merger_base> ( Cfg, "Merger" );
62
63        // Support
64        try{
65        shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> ( Cfg, "Support" ,UI::optional);
66        if(RecSup){
67                Merger->set_support ( *RecSup );
68        } 
69        } catch (UIException &e) {
70                shared_ptr<discrete_support> DisSup = UI::build<discrete_support> ( Cfg, "Support", UI::optional );
71                if (DisSup){
72                        Merger->set_support ( *DisSup );
73                } else{bdm_error("Support not defined");}
74        }
75// COMPUTE RESULTS
76        Merger->set_sources ( Sources );
77        Merger->merge();
78
79// save results
80        Array<vec> source_vals ( Sources.length() );
81        for ( int i = 0;i < Sources.length();i++ ) {
82                datalink_m2e dl;
83                dl.set_connection ( Sources ( i )->_rv(), Sources ( i )->_rvc(), Merger->_rv() );
84
85                vec ll ( Merger->_Smp()._samples().length() );
86                for ( int j = 0; j < Merger->_Smp()._samples().length(); j++ ) {
87                        vec dt = dl.pushdown ( Merger->_Smp()._samples() ( j ) );
88                        vec dtc = dl.get_cond ( Merger->_Smp()._samples() ( j ) );
89                        ll ( j ) = Sources ( i )->evallogcond ( dt, dtc );
90                }
91
92                vec sll = exp ( ll );
93
94                source_vals ( i ) = sll / sum ( sll );
95        }
96
97        merger_mix* MerMix=dynamic_cast<merger_mix*> ( Merger.get() );
98        vec mix_val;
99
100        if ( MerMix ) {
101                vec ll ( Merger->_Smp()._samples().length() );
102                for ( int j = 0; j < Merger->_Smp()._samples().length(); j++ ) {
103                        ll ( j ) = Merger->evallog ( Merger->_Smp()._samples() ( j ) );
104                }
105
106                vec sll = exp ( ll );
107
108                mix_val = sll / sum ( sll );
109        }
110
111#ifdef MEX
112        mxArray* tmp ;
113        // Save results
114        if ( n_output > 0 ) {
115                tmp = mxCreateStructMatrix ( 1, 1, 0, NULL );
116                //support
117                Array<vec> &samples = Merger->_Smp()._samples();
118                if ( samples.size() > 0 ) {
119                        mxArray* fld = mxCreateDoubleMatrix ( samples ( 0 ).length(), samples.size(), mxREAL );
120                        Arrayvec2mxArray ( samples, fld );
121                        mxReplaceFieldNM ( tmp, "support", fld );
122                }
123
124                //weights
125                vec &w = Merger->_Smp()._w();
126                mxArray* fldw = mxCreateDoubleMatrix ( 1, w.length(), mxREAL );
127                vec2mxArray ( w, fldw );
128                mxReplaceFieldNM ( tmp, "weights", fldw );
129
130                //mixture values
131                if ( mix_val.length() >0 ) {
132                        mxArray* fldm = mxCreateDoubleMatrix ( 1, w.length(), mxREAL );
133                        vec2mxArray ( mix_val, fldm );
134                        mxReplaceFieldNM ( tmp, "mix", fldm );
135                }
136                // sources
137                char srcstr[20];
138                for ( int i = 0;i < Sources.length();i++ ) {
139                        sprintf ( srcstr, "source%d", i + 1 );
140                        mxArray* fldw = mxCreateDoubleMatrix ( 1, source_vals ( i ).length(), mxREAL );
141                        vec2mxArray ( source_vals ( i ), fldw );
142                        mxReplaceFieldNM ( tmp, srcstr, fldw );
143                }
144
145                output[0] = tmp;
146        }
147        if (n_output>1){
148                Config C;
149                C.setAutoConvert(true);
150                UI::save(&(Merger->_Smp()),C.getRoot());
151                output[1]= UImxArray::create_mxArray(C.getRoot());
152        }
153#endif
154}
Note: See TracBrowser for help on using the browser.