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

Revision 568, 5.3 kB (checked in by smidl, 15 years ago)

bdmtoolbox adapted to shared_ptr, merger_grid (partially) working,

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{
33        // Check the number of inputs and output arguments
34        if (n_input != 3) mexErrMsgTxt ("Usage:\n"
35                                            "result=merger(sources, support, merger)\n"
36                                            "  sources= { struct('class','epdf'),... };  % cell of pdfs (epdfs or mpdfs) to be merged,\n"
37                                            "  support= struct(\n"
38                                            "           grid    = {[dim1_start,dim1_end], [dim2_start, dim2_end]...}  %support boundary \n"
39                                            "           nbins   = [bins_in_dim1, bins_in_dim2,...]                    %fixed \n"
40                                            "         === OR ==\n"
41                                            "           pdf     = struct('class','epdf'); % pdf to draw samples from\n"
42                                            "           nsamples= 100;                    % number of samples\n"
43                                            "           );\n"
44                                            "        If all elements are present,  (grid,nbins) is used;\n"
45                                            "  merger = struct('class','merger_*');       % object to be used for merging,\n\n"
46                                            "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM.");
47        RV::clear_all();
48        // LOAD CONFIG
49        UImxArray Cfg;
50        Cfg.addList (input[0], "Sources");
51        Cfg.addGroup (input[1], "Support");
52        Cfg.addGroup (input[2], "Merger");
53
54        //DBG
55        Cfg.writeFile ("merger.cfg");
56#else
57int main() {
58        UIFile Cfg ("merger.cfg");
59#endif
60        // Sources
61        Array<shared_ptr<mpdf> > Sources;
62        //abuse Mer to store sources
63        Setting& _Sources = Cfg.lookup ("Sources");
64        int Slen = _Sources.getLength();
65        Sources.set_size (Slen);
66        for (int i = 0; i < Slen; i++) {
67                try {
68                        shared_ptr<mpdf> mtmp = UI::build<mpdf> (_Sources, i);
69                        Sources (i) = mtmp;
70                } catch (UIException) {
71                        // it is not mpdf - see if it is epdf
72                        try {
73                                shared_ptr<epdf> etmp = UI::build<epdf> (_Sources, i);
74                                if (etmp) {
75                                        Sources (i) = new mepdf (etmp); // hopefully OK
76                                }
77                        } catch (UIException &e) {
78                                it_error ("No mpdfs or epdfs found! " + string (e.what()));
79                        } catch (std::exception e) {
80                                it_error ("Error in UI at " + _Sources[i].getPath());
81                        }
82                } catch (std::exception &e) {
83                        it_error ("Error in UI at " + _Sources[i].getPath());
84                }
85        }
86
87        shared_ptr<merger_base> Merger = UI::build<merger_base> (Cfg, "Merger");
88
89        // Support
90        try {
91                shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> (Cfg, "Support");
92                Merger->set_support(*RecSup);
93        }
94        catch (UIException &e) {
95                shared_ptr<epdf> g0 = UI::build<epdf> (Cfg, "Support");
96                Merger->set_support (*g0, 100);
97        }
98// COMPUTE RESULTS
99        Merger->set_sources (Sources); 
100        Merger->merge();
101
102// save results
103        Array<vec> source_vals (Sources.length());
104        for (int i = 0;i < Sources.length();i++) {
105                datalink_m2e dl;
106                dl.set_connection (Sources (i)->_rv(), Sources (i)->_rvc(), Merger->_rv());
107
108                vec ll (Merger->_Smp()._samples().length());
109                for (int j = 0; j < Merger->_Smp()._samples().length(); j++) {
110                        vec dt = dl.pushdown (Merger->_Smp()._samples() (j));
111                        vec dtc = dl.get_cond (Merger->_Smp()._samples() (j));
112                        ll (j) = Sources (i)->evallogcond (dt, dtc);
113                }
114
115                vec sll = exp (ll);
116
117                source_vals (i) = sll / sum (sll);
118        }
119
120        merger_mix* MerMix=dynamic_cast<merger_mix*>(Merger.get());
121        vec mix_val;
122                       
123        if (MerMix){
124                vec ll (Merger->_Smp()._samples().length());
125                for (int j = 0; j < Merger->_Smp()._samples().length(); j++) {
126                        ll (j) = Merger->evallog (Merger->_Smp()._samples() (j));
127                }
128       
129                vec sll = exp (ll);
130       
131                mix_val = sll / sum (sll);
132        }
133
134#ifdef MEX
135        mxArray* tmp ;
136        // Save results
137        if (n_output > 0) {
138                tmp = mxCreateStructMatrix (1, 1, 0, NULL);
139                //support
140                Array<vec> &samples = Merger->_Smp()._samples();
141                if (samples.size() > 0) {
142                        mxArray* fld = mxCreateDoubleMatrix (samples (0).length(), samples.size(), mxREAL);
143                        Arrayvec2mxArray (samples, fld);
144                        mxReplaceFieldNM (tmp, "support", fld);
145                }
146
147                //weights
148                vec &w = Merger->_Smp()._w();
149                mxArray* fldw = mxCreateDoubleMatrix (1, w.length(), mxREAL);
150                vec2mxArray (w, fldw);
151                mxReplaceFieldNM (tmp, "weights", fldw);
152
153                //mixture values
154                if (mix_val.length()>0){
155                        mxArray* fldm = mxCreateDoubleMatrix (1, w.length(), mxREAL);
156                        vec2mxArray (mix_val, fldm);
157                        mxReplaceFieldNM (tmp, "mix", fldm);
158                }
159                // sources
160                char srcstr[20];
161                for (int i = 0;i < Sources.length();i++) {
162                        sprintf (srcstr, "source%d", i + 1);
163                        mxArray* fldw = mxCreateDoubleMatrix (1, source_vals (i).length(), mxREAL);
164                        vec2mxArray (source_vals (i), fldw);
165                        mxReplaceFieldNM (tmp, srcstr, fldw);
166                }
167
168                output[0] = tmp;
169        }
170#endif
171}
Note: See TracBrowser for help on using the browser.