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

Revision 622, 5.6 kB (checked in by smidl, 15 years ago)

astyle

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        //abuse Mer to store sources
62        Setting& _Sources = Cfg.lookup ( "Sources" );
63        int Slen = _Sources.getLength();
64        Sources.set_size ( Slen );
65        for ( int i = 0; i < Slen; i++ ) {
66                try {
67                        shared_ptr<mpdf> mtmp = UI::build<mpdf> ( _Sources, i );
68                        Sources ( i ) = mtmp;
69                } catch ( UIException ) {
70                        // it is not mpdf - see if it is epdf
71                        try {
72                                shared_ptr<epdf> etmp = UI::build<epdf> ( _Sources, i );
73                                if ( etmp ) {
74                                        Sources ( i ) = new mepdf ( etmp ); // hopefully OK
75                                }
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() );
80                        }
81                } catch ( std::exception &e ) {
82                        it_error ( "Error in UI at " + _Sources[i].getPath() );
83                }
84        }
85
86        shared_ptr<merger_base> Merger = UI::build<merger_base> ( Cfg, "Merger" );
87
88        // Support
89        try {
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 );
95        }
96// COMPUTE RESULTS
97        Merger->set_sources ( Sources );
98        Merger->merge();
99
100// save results
101        Array<vec> source_vals ( Sources.length() );
102        for ( int i = 0;i < Sources.length();i++ ) {
103                datalink_m2e dl;
104                dl.set_connection ( Sources ( i )->_rv(), Sources ( i )->_rvc(), Merger->_rv() );
105
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 );
111                }
112
113                vec sll = exp ( ll );
114
115                source_vals ( i ) = sll / sum ( sll );
116        }
117
118        merger_mix* MerMix=dynamic_cast<merger_mix*> ( Merger.get() );
119        vec mix_val;
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 ) );
125                }
126
127                vec sll = exp ( ll );
128
129                mix_val = sll / sum ( sll );
130        }
131
132#ifdef MEX
133        mxArray* tmp ;
134        // Save results
135        if ( n_output > 0 ) {
136                tmp = mxCreateStructMatrix ( 1, 1, 0, NULL );
137                //support
138                Array<vec> &samples = Merger->_Smp()._samples();
139                if ( samples.size() > 0 ) {
140                        mxArray* fld = mxCreateDoubleMatrix ( samples ( 0 ).length(), samples.size(), mxREAL );
141                        Arrayvec2mxArray ( samples, fld );
142                        mxReplaceFieldNM ( tmp, "support", fld );
143                }
144
145                //weights
146                vec &w = Merger->_Smp()._w();
147                mxArray* fldw = mxCreateDoubleMatrix ( 1, w.length(), mxREAL );
148                vec2mxArray ( w, fldw );
149                mxReplaceFieldNM ( tmp, "weights", fldw );
150
151                //mixture values
152                if ( mix_val.length() >0 ) {
153                        mxArray* fldm = mxCreateDoubleMatrix ( 1, w.length(), mxREAL );
154                        vec2mxArray ( mix_val, fldm );
155                        mxReplaceFieldNM ( tmp, "mix", fldm );
156                }
157                // sources
158                char srcstr[20];
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 );
164                }
165
166                output[0] = tmp;
167        }
168#endif
169}
Note: See TracBrowser for help on using the browser.