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

Revision 411, 4.2 kB (checked in by smidl, 15 years ago)

merger finalization

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
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
57        int main()
58        {
59                UIFile Cfg("merger.cfg");
60#endif 
61        // Sources
62        Array<mpdf*> Sources;
63        //abuse Mer to store sources
64        Setting& _Sources=Cfg.lookup("Sources");
65        int Slen=_Sources.getLength();
66        Sources.set_size(Slen);
67        for (int i=0; i<Slen; i++){
68                try{
69                        mpdf* mtmp = UI::build<mpdf>(_Sources,i);
70                        Sources(i)=mtmp;
71                }
72                catch (UIException){
73                        // it is not mpdf - see if it is epdf
74                        try {
75                                epdf* etmp = UI::build<epdf>(_Sources,i);
76                                if (etmp){
77                                        Sources(i) = new mepdf(etmp, true);
78                                }                               
79                        }
80                        catch (UIException e) 
81                        {
82                                it_error("No mpdfs or epdfs found! " + string(e.what()));
83                        }
84                        catch (std::exception e) {
85                                it_error("Error in UI at "+_Sources[i].getPath());
86                        }
87                }
88                catch (std::exception e) {
89                        it_error("Error in UI at "+_Sources[i].getPath());
90                }
91        }
92
93        merger_base* Merger=UI::build<merger_base>(Cfg,"Merger");
94
95        // Support
96        Setting & _Supp=Cfg.lookup("Support");
97       
98        if (_Supp.exists("grid") &&  _Supp.exists("nbins")) {
99        Array<vec> bounds (0);
100        UI::get (bounds, _Supp, "grid");
101        ivec nbins(0);
102        UI::get (nbins, _Supp, "nbins");
103        Merger->set_support (bounds,nbins);
104       
105        }else {
106                if (_Supp.exists("pdf") &&  _Supp.exists("nsamples")){
107                        epdf *g0=UI::build<epdf> (_Supp, "pdf");
108                        int npoints=100;
109                        _Supp.lookupValue("nsamples",npoints);
110                        Merger->set_support (*g0,npoints);
111                        delete g0;     
112                }
113                else it_error("Use either [grid,nbins] or [pdf,nsamples].");
114        }
115// COMPUTE RESULTS
116        Merger->set_sources(Sources,true); // takes care of deletion of sources
117        Merger->merge();
118       
119// save results
120       
121#ifdef MEX
122        mxArray* tmp ;
123        // Save results
124        if (n_output>0){
125                tmp = mxCreateStructMatrix(1,1,0,NULL);
126                //support
127                Array<vec> &samples=Merger->_Smp()._samples();
128                if (samples.size()>0){
129                        mxArray* fld=mxCreateDoubleMatrix(samples(0).length(), samples.size(), mxREAL);
130                Arrayvec2mxArray(samples,fld);
131                mxReplaceFieldNM(tmp, "support", fld);
132                }
133
134                //weights
135                vec &w = Merger->_Smp()._w();
136                mxArray* fldw=mxCreateDoubleMatrix(1, w.length(), mxREAL);
137                vec2mxArray(w,fldw);
138                mxReplaceFieldNM(tmp, "weights", fldw);
139
140                // sources
141                char srcstr[20];
142                for (int i=0;i<Sources.length();i++){
143                        sprintf(srcstr,"source%d",i+1);
144                        vec sll=exp(Sources(i)->evallogcond_m(Merger->_Smp()._samples(),vec(0)));
145
146                        mxArray* fldw=mxCreateDoubleMatrix(1, sll.length(), mxREAL);
147                        vec2mxArray(sll/sum(sll),fldw);
148                        mxReplaceFieldNM(tmp, srcstr, fldw);
149                }               
150
151                output[0] = tmp;
152        }
153#endif
154}
Note: See TracBrowser for help on using the browser.