root/applications/bdmtoolbox/mex/merger.cpp

Revision 1083, 4.4 kB (checked in by smidl, 14 years ago)

Mergers + fixes

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