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

Revision 393, 3.0 kB (checked in by smidl, 15 years ago)

merger_mx works!

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 (UIbuildException e){
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                        }
55                        catch (...) {it_error("no mpdfs or epdfs found!");}
56                }
57
58        }
59
60        merger_base* Merger=UI::build<merger_base>(Cfg,"Merger");
61
62        // Support
63        Setting & _Supp=Cfg.lookup("Support");
64       
65        if (_Supp.exists("grid") &&  _Supp.exists("nbins")) {
66        Array<vec> bounds (0);
67        UI::get (bounds, _Supp, "grid");
68        ivec nbins(0);
69        UI::get (nbins, _Supp, "nbins");
70        Merger->set_support (bounds,nbins);
71       
72        }else {
73                if (_Supp.exists("pdf") &&  _Supp.exists("nsamples")){
74                        epdf *g0=UI::build<epdf> (_Supp, "pdf");
75                        int npoints=100;
76                        _Supp.lookupValue("nsamples",npoints);
77                        Merger->set_support (*g0,npoints);
78                        delete g0;     
79                }
80                else it_error("Use either [grid,nbins] or [pdf,nsamples].");
81        }
82// COMPUTE RESULTS
83        Merger->set_sources(Sources,true); // takes care of deletion of sources
84        Merger->merge();
85       
86        // Save results
87        if (n_output>0){
88                mxArray* tmp = mxCreateStructMatrix(1,1,0,NULL);
89                //support
90                Array<vec> &samples=Merger->_Smp()._samples();
91                if (samples.size()>0){
92                        mxArray* fld=mxCreateDoubleMatrix(samples(0).length(), samples.size(), mxREAL);
93                Arrayvec2mxArray(samples,fld);
94                mxReplaceFieldNM(tmp, "support", fld);
95                }
96
97                //weights
98                vec &w = Merger->_Smp()._w();
99                mxArray* fldw=mxCreateDoubleMatrix(1, w.length(), mxREAL);
100                vec2mxArray(w,fldw);
101                mxReplaceFieldNM(tmp, "weights", fldw);
102               
103                output[0] = tmp;
104        }
105}
Note: See TracBrowser for help on using the browser.