[383] | 1 | #include <itpp/itmex.h> |
---|
[391] | 2 | #include <stat/merger.h> |
---|
| 3 | #include <mex/mex_logger.h> |
---|
| 4 | #include <mex/mex_parser.h> |
---|
| 5 | #include <mex/config2mxstruct.h> |
---|
[383] | 6 | |
---|
| 7 | using namespace bdm; |
---|
| 8 | |
---|
| 9 | void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[]) |
---|
| 10 | { |
---|
| 11 | // Check the number of inputs and output arguments |
---|
[388] | 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" |
---|
[383] | 24 | "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM."); |
---|
| 25 | |
---|
| 26 | // LOAD CONFIG |
---|
[393] | 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"); |
---|
[388] | 34 | |
---|
| 35 | // Sources |
---|
[383] | 36 | Array<mpdf*> Sources; |
---|
[388] | 37 | //abuse Mer to store sources |
---|
[393] | 38 | Setting& _Sources=Cfg.lookup("Sources"); |
---|
[383] | 39 | int Slen=_Sources.getLength(); |
---|
| 40 | Sources.set_size(Slen); |
---|
| 41 | for (int i=0; i<Slen; i++){ |
---|
[393] | 42 | try{ |
---|
| 43 | mpdf* mtmp = UI::build<mpdf>(_Sources,i); |
---|
[383] | 44 | Sources(i)=mtmp; |
---|
[393] | 45 | } |
---|
[396] | 46 | catch (UIException){ |
---|
[393] | 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); |
---|
[396] | 52 | } |
---|
[383] | 53 | } |
---|
[396] | 54 | catch (UIException e) |
---|
| 55 | { |
---|
| 56 | it_error("No mpdfs or epdfs found! " + string(e.what())); |
---|
| 57 | } |
---|
[400] | 58 | catch (std::exception e) { |
---|
| 59 | it_error("Error in UI at "+_Sources[i].getPath()); |
---|
| 60 | } |
---|
[383] | 61 | } |
---|
[400] | 62 | catch (std::exception e) { |
---|
| 63 | it_error("Error in UI at "+_Sources[i].getPath()); |
---|
| 64 | } |
---|
[383] | 65 | } |
---|
[388] | 66 | |
---|
[393] | 67 | merger_base* Merger=UI::build<merger_base>(Cfg,"Merger"); |
---|
[388] | 68 | |
---|
| 69 | // Support |
---|
[393] | 70 | Setting & _Supp=Cfg.lookup("Support"); |
---|
[383] | 71 | |
---|
[393] | 72 | if (_Supp.exists("grid") && _Supp.exists("nbins")) { |
---|
[388] | 73 | Array<vec> bounds (0); |
---|
| 74 | UI::get (bounds, _Supp, "grid"); |
---|
| 75 | ivec nbins(0); |
---|
| 76 | UI::get (nbins, _Supp, "nbins"); |
---|
[393] | 77 | Merger->set_support (bounds,nbins); |
---|
[388] | 78 | |
---|
[393] | 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); |
---|
[388] | 84 | Merger->set_support (*g0,npoints); |
---|
[393] | 85 | delete g0; |
---|
[388] | 86 | } |
---|
[393] | 87 | else it_error("Use either [grid,nbins] or [pdf,nsamples]."); |
---|
[388] | 88 | } |
---|
[393] | 89 | // COMPUTE RESULTS |
---|
| 90 | Merger->set_sources(Sources,true); // takes care of deletion of sources |
---|
[383] | 91 | Merger->merge(); |
---|
[395] | 92 | |
---|
| 93 | mxArray* tmp ; |
---|
[388] | 94 | // Save results |
---|
[383] | 95 | if (n_output>0){ |
---|
[395] | 96 | tmp = mxCreateStructMatrix(1,1,0,NULL); |
---|
[383] | 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); |
---|
[395] | 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 | |
---|
[383] | 122 | output[0] = tmp; |
---|
| 123 | } |
---|
| 124 | } |
---|