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

Revision 407, 3.7 kB (checked in by smidl, 15 years ago)

clean up of bdmdir mex - merger is now in one .cpp file

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
10using namespace bdm;
11
12#ifdef MEX
13void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[])
14{
15    // Check the number of inputs and output arguments
16        if(n_input!=3) mexErrMsgTxt("Usage:\n" 
17                "result=merger(sources, support, merger)\n"
18                "  sources= { struct('class','epdf'),... };  % cell of pdfs (epdfs or mpdfs) to be merged,\n"
19                "  support= struct(\n"
20                "           grid    = {[dim1_start,dim1_end], [dim2_start, dim2_end]...}  %support boundary \n"   
21                "           nbins   = [bins_in_dim1, bins_in_dim2,...]                    %fixed \n"   
22                "         === OR ==\n"
23                "           pdf     = struct('class','epdf'); % pdf to draw samples from\n"
24                "           nsamples= 100;                    % number of samples\n"
25                "           );\n"
26                "        If all elements are present,  (grid,nbins) is used;\n"
27                "  merger = struct('class','merger_*');       % object to be used for merging,\n\n"
28                "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM.");
29
30        // LOAD CONFIG
31        UImxArray Cfg;
32        Cfg.addList(input[0],"Sources");
33        Cfg.addGroup(input[1],"Support");
34        Cfg.addGroup(input[2],"Merger");
35
36        //DBG
37        Cfg.writeFile("merger.cfg");
38#else
39        int main()
40        {
41                UIFile Cfg("merger.cfg");
42#endif 
43        // Sources
44        Array<mpdf*> Sources;
45        //abuse Mer to store sources
46        Setting& _Sources=Cfg.lookup("Sources");
47        int Slen=_Sources.getLength();
48        Sources.set_size(Slen);
49        for (int i=0; i<Slen; i++){
50                try{
51                        mpdf* mtmp = UI::build<mpdf>(_Sources,i);
52                        Sources(i)=mtmp;
53                }
54                catch (UIException){
55                        // it is not mpdf - see if it is epdf
56                        try {
57                                epdf* etmp = UI::build<epdf>(_Sources,i);
58                                if (etmp){
59                                        Sources(i) = new mepdf(etmp, true);
60                                }                               
61                        }
62                        catch (UIException e) 
63                        {
64                                it_error("No mpdfs or epdfs found! " + string(e.what()));
65                        }
66                        catch (std::exception e) {
67                                it_error("Error in UI at "+_Sources[i].getPath());
68                        }
69                }
70                catch (std::exception e) {
71                        it_error("Error in UI at "+_Sources[i].getPath());
72                }
73        }
74
75        merger_base* Merger=UI::build<merger_base>(Cfg,"Merger");
76
77        // Support
78        Setting & _Supp=Cfg.lookup("Support");
79       
80        if (_Supp.exists("grid") &&  _Supp.exists("nbins")) {
81        Array<vec> bounds (0);
82        UI::get (bounds, _Supp, "grid");
83        ivec nbins(0);
84        UI::get (nbins, _Supp, "nbins");
85        Merger->set_support (bounds,nbins);
86       
87        }else {
88                if (_Supp.exists("pdf") &&  _Supp.exists("nsamples")){
89                        epdf *g0=UI::build<epdf> (_Supp, "pdf");
90                        int npoints=100;
91                        _Supp.lookupValue("nsamples",npoints);
92                        Merger->set_support (*g0,npoints);
93                        delete g0;     
94                }
95                else it_error("Use either [grid,nbins] or [pdf,nsamples].");
96        }
97// COMPUTE RESULTS
98        Merger->set_sources(Sources,true); // takes care of deletion of sources
99        Merger->merge();
100       
101// save results
102       
103#ifdef MEX
104        mxArray* tmp ;
105        // Save results
106        if (n_output>0){
107                tmp = mxCreateStructMatrix(1,1,0,NULL);
108                //support
109                Array<vec> &samples=Merger->_Smp()._samples();
110                if (samples.size()>0){
111                        mxArray* fld=mxCreateDoubleMatrix(samples(0).length(), samples.size(), mxREAL);
112                Arrayvec2mxArray(samples,fld);
113                mxReplaceFieldNM(tmp, "support", fld);
114                }
115
116                //weights
117                vec &w = Merger->_Smp()._w();
118                mxArray* fldw=mxCreateDoubleMatrix(1, w.length(), mxREAL);
119                vec2mxArray(w,fldw);
120                mxReplaceFieldNM(tmp, "weights", fldw);
121
122                // sources
123                char srcstr[20];
124                for (int i=0;i<Sources.length();i++){
125                        sprintf(srcstr,"source%d",i+1);
126                        vec sll=exp(Sources(i)->evallogcond_m(Merger->_Smp()._samples(),vec(0)));
127
128                        mxArray* fldw=mxCreateDoubleMatrix(1, sll.length(), mxREAL);
129                        vec2mxArray(sll/sum(sll),fldw);
130                        mxReplaceFieldNM(tmp, srcstr, fldw);
131                }               
132
133                output[0] = tmp;
134        }
135#endif
136}
Note: See TracBrowser for help on using the browser.