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

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

changes to dist identific example + wrapper for variance

  • 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->validate();
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        if (n_output>1){
149                Config C;
150                C.setAutoConvert(true);
151                UI::save(&(Merger->_Smp()),C.getRoot());
152                output[1]= UImxArray::create_mxArray(C.getRoot());
153        }
154#endif
155}
Note: See TracBrowser for help on using the browser.