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

Revision 396, 3.4 kB (checked in by mido, 15 years ago)

UI documentation almost finished; UIException redesigned and used instead of UI::ui_error function

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                }
59
60        }
61
62        merger_base* Merger=UI::build<merger_base>(Cfg,"Merger");
63
64        // Support
65        Setting & _Supp=Cfg.lookup("Support");
66       
67        if (_Supp.exists("grid") &&  _Supp.exists("nbins")) {
68        Array<vec> bounds (0);
69        UI::get (bounds, _Supp, "grid");
70        ivec nbins(0);
71        UI::get (nbins, _Supp, "nbins");
72        Merger->set_support (bounds,nbins);
73       
74        }else {
75                if (_Supp.exists("pdf") &&  _Supp.exists("nsamples")){
76                        epdf *g0=UI::build<epdf> (_Supp, "pdf");
77                        int npoints=100;
78                        _Supp.lookupValue("nsamples",npoints);
79                        Merger->set_support (*g0,npoints);
80                        delete g0;     
81                }
82                else it_error("Use either [grid,nbins] or [pdf,nsamples].");
83        }
84// COMPUTE RESULTS
85        Merger->set_sources(Sources,true); // takes care of deletion of sources
86        Merger->merge();
87
88        mxArray* tmp ;
89        // Save results
90        if (n_output>0){
91                tmp = mxCreateStructMatrix(1,1,0,NULL);
92                //support
93                Array<vec> &samples=Merger->_Smp()._samples();
94                if (samples.size()>0){
95                        mxArray* fld=mxCreateDoubleMatrix(samples(0).length(), samples.size(), mxREAL);
96                Arrayvec2mxArray(samples,fld);
97                mxReplaceFieldNM(tmp, "support", fld);
98                }
99
100                //weights
101                vec &w = Merger->_Smp()._w();
102                mxArray* fldw=mxCreateDoubleMatrix(1, w.length(), mxREAL);
103                vec2mxArray(w,fldw);
104                mxReplaceFieldNM(tmp, "weights", fldw);
105
106                // sources
107                                char srcstr[20];
108                for (int i=0;i<Sources.length();i++){
109                        sprintf(srcstr,"source%d",i+1);
110                        vec sll=exp(Sources(i)->evallogcond_m(Merger->_Smp()._samples(),vec(0)));
111
112                        mxArray* fldw=mxCreateDoubleMatrix(1, sll.length(), mxREAL);
113                        vec2mxArray(sll/sum(sll),fldw);
114                        mxReplaceFieldNM(tmp, srcstr, fldw);
115                }               
116
117                output[0] = tmp;
118        }
119}
Note: See TracBrowser for help on using the browser.