root/applications/bdmtoolbox/mex/merger_mx.cpp @ 400

Revision 400, 3.6 kB (checked in by smidl, 15 years ago)

details in mergers

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