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

Revision 685, 4.9 kB (checked in by smidl, 15 years ago)

mex tutorial cleanup + working mexDS

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#include <mex/config2mxstruct.h>
8#endif
9
10/*! \file
11\brief Merging static pdfs
12Merger is a program/mex-function for static merging of given pdfs on given support.
13
14In command line, it expected config file with the following structure:
15\code
16Sources = (... any epdf or mpdf  ...);
17Support = {grid=([low1d,high1d],[low2d,high2d],...);
18                   nbins=[bins1d,bins2d,... ]};
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= { 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." );
46        RV::clear_all();
47        // LOAD CONFIG
48        UImxArray Cfg;
49        Cfg.addList ( input[0], "Sources" );
50        Cfg.addGroup ( input[1], "Support" );
51        Cfg.addGroup ( input[2], "Merger" );
52
53        //DBG
54        Cfg.writeFile ( "merger.cfg" );
55#else
56int main() {
57        UIFile Cfg ( "merger.cfg" );
58#endif
59        // Sources
60        Array<shared_ptr<mpdf> >  Sources;
61        UI::get(Sources, Cfg, "Sources", UI::compulsory);
62        shared_ptr<merger_base> Merger = UI::build<merger_base> ( Cfg, "Merger" );
63
64        // Support
65        try{
66        shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> ( Cfg, "Support" ,UI::optional);
67        if(RecSup){
68                Merger->set_support ( *RecSup );
69        } 
70        } catch (UIException &e) {
71                shared_ptr<discrete_support> DisSup = UI::build<discrete_support> ( Cfg, "Support", UI::optional );
72                if (DisSup){
73                        Merger->set_support ( *DisSup );
74                } else{bdm_error("Support not defined");}
75        }
76// COMPUTE RESULTS
77        Merger->set_sources ( Sources );
78        Merger->merge();
79
80// save results
81        Array<vec> source_vals ( Sources.length() );
82        for ( int i = 0;i < Sources.length();i++ ) {
83                datalink_m2e dl;
84                dl.set_connection ( Sources ( i )->_rv(), Sources ( i )->_rvc(), Merger->_rv() );
85
86                vec ll ( Merger->_Smp()._samples().length() );
87                for ( int j = 0; j < Merger->_Smp()._samples().length(); j++ ) {
88                        vec dt = dl.pushdown ( Merger->_Smp()._samples() ( j ) );
89                        vec dtc = dl.get_cond ( Merger->_Smp()._samples() ( j ) );
90                        ll ( j ) = Sources ( i )->evallogcond ( dt, dtc );
91                }
92
93                vec sll = exp ( ll );
94
95                source_vals ( i ) = sll / sum ( sll );
96        }
97
98        merger_mix* MerMix=dynamic_cast<merger_mix*> ( Merger.get() );
99        vec mix_val;
100
101        if ( MerMix ) {
102                vec ll ( Merger->_Smp()._samples().length() );
103                for ( int j = 0; j < Merger->_Smp()._samples().length(); j++ ) {
104                        ll ( j ) = Merger->evallog ( Merger->_Smp()._samples() ( j ) );
105                }
106
107                vec sll = exp ( ll );
108
109                mix_val = sll / sum ( sll );
110        }
111
112#ifdef MEX
113        mxArray* tmp ;
114        // Save results
115        if ( n_output > 0 ) {
116                tmp = mxCreateStructMatrix ( 1, 1, 0, NULL );
117                //support
118                Array<vec> &samples = Merger->_Smp()._samples();
119                if ( samples.size() > 0 ) {
120                        mxArray* fld = mxCreateDoubleMatrix ( samples ( 0 ).length(), samples.size(), mxREAL );
121                        Arrayvec2mxArray ( samples, fld );
122                        mxReplaceFieldNM ( tmp, "support", fld );
123                }
124
125                //weights
126                vec &w = Merger->_Smp()._w();
127                mxArray* fldw = mxCreateDoubleMatrix ( 1, w.length(), mxREAL );
128                vec2mxArray ( w, fldw );
129                mxReplaceFieldNM ( tmp, "weights", fldw );
130
131                //mixture values
132                if ( mix_val.length() >0 ) {
133                        mxArray* fldm = mxCreateDoubleMatrix ( 1, w.length(), mxREAL );
134                        vec2mxArray ( mix_val, fldm );
135                        mxReplaceFieldNM ( tmp, "mix", fldm );
136                }
137                // sources
138                char srcstr[20];
139                for ( int i = 0;i < Sources.length();i++ ) {
140                        sprintf ( srcstr, "source%d", i + 1 );
141                        mxArray* fldw = mxCreateDoubleMatrix ( 1, source_vals ( i ).length(), mxREAL );
142                        vec2mxArray ( source_vals ( i ), fldw );
143                        mxReplaceFieldNM ( tmp, srcstr, fldw );
144                }
145
146                output[0] = tmp;
147        }
148#endif
149}
Note: See TracBrowser for help on using the browser.