[393] | 1 | #include <stat/merger.h> |
---|
| 2 | |
---|
[407] | 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 | |
---|
[463] | 10 | /*! \file |
---|
[411] | 11 | \brief Merging static pdfs |
---|
| 12 | Merger is a program/mex-function for static merging of given pdfs on given support. |
---|
| 13 | |
---|
| 14 | In command line, it expected config file with the following structure: |
---|
| 15 | \code |
---|
| 16 | Sources = (... any epdf or mpdf ...); |
---|
| 17 | Support = {grid=([low1d,high1d],[low2d,high2d],...); |
---|
[463] | 18 | nbins=[bins1d,bins2d,... ]}; |
---|
[411] | 19 | // OR |
---|
| 20 | Support = {pdf={class='epdf',...}; |
---|
[463] | 21 | nsamples=2}; |
---|
[411] | 22 | Merger = {class="merger_base",...} |
---|
| 23 | \endcode |
---|
| 24 | |
---|
| 25 | In mex-file, the above structures (Sources,Support,Merger) are input arguments. Type >>merger for help. |
---|
| 26 | */ |
---|
| 27 | |
---|
[393] | 28 | using namespace bdm; |
---|
| 29 | |
---|
[463] | 30 | #ifdef MEX |
---|
| 31 | void mexFunction (int n_output, mxArray *output[], int n_input, const mxArray *input[]) |
---|
[393] | 32 | { |
---|
[463] | 33 | // Check the number of inputs and output arguments |
---|
| 34 | if (n_input != 3) mexErrMsgTxt ("Usage:\n" |
---|
| 35 | "result=merger(sources, support, merger)\n" |
---|
| 36 | " sources= { struct('class','epdf'),... }; % cell of pdfs (epdfs or mpdfs) to be merged,\n" |
---|
| 37 | " support= struct(\n" |
---|
| 38 | " grid = {[dim1_start,dim1_end], [dim2_start, dim2_end]...} %support boundary \n" |
---|
| 39 | " nbins = [bins_in_dim1, bins_in_dim2,...] %fixed \n" |
---|
| 40 | " === OR ==\n" |
---|
| 41 | " pdf = struct('class','epdf'); % pdf to draw samples from\n" |
---|
| 42 | " nsamples= 100; % number of samples\n" |
---|
| 43 | " );\n" |
---|
| 44 | " If all elements are present, (grid,nbins) is used;\n" |
---|
| 45 | " merger = struct('class','merger_*'); % object to be used for merging,\n\n" |
---|
| 46 | "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM."); |
---|
[407] | 47 | |
---|
| 48 | // LOAD CONFIG |
---|
| 49 | UImxArray Cfg; |
---|
[463] | 50 | Cfg.addList (input[0], "Sources"); |
---|
| 51 | Cfg.addGroup (input[1], "Support"); |
---|
| 52 | Cfg.addGroup (input[2], "Merger"); |
---|
[407] | 53 | |
---|
| 54 | //DBG |
---|
[463] | 55 | Cfg.writeFile ("merger.cfg"); |
---|
[407] | 56 | #else |
---|
[463] | 57 | int main() { |
---|
| 58 | UIFile Cfg ("merger.cfg"); |
---|
| 59 | #endif |
---|
[393] | 60 | // Sources |
---|
| 61 | Array<mpdf*> Sources; |
---|
| 62 | //abuse Mer to store sources |
---|
[463] | 63 | Setting& _Sources = Cfg.lookup ("Sources"); |
---|
| 64 | int Slen = _Sources.getLength(); |
---|
| 65 | Sources.set_size (Slen); |
---|
| 66 | for (int i = 0; i < Slen; i++) { |
---|
| 67 | try { |
---|
| 68 | mpdf* mtmp = UI::build<mpdf> (_Sources, i); |
---|
| 69 | Sources (i) = mtmp; |
---|
| 70 | } catch (UIException) { |
---|
[393] | 71 | // it is not mpdf - see if it is epdf |
---|
| 72 | try { |
---|
[463] | 73 | shared_ptr<epdf> etmp = UI::build<epdf> (_Sources, i); |
---|
| 74 | if (etmp) { |
---|
| 75 | Sources (i) = new mepdf (etmp); |
---|
| 76 | } |
---|
| 77 | } catch (UIException e) { |
---|
| 78 | it_error ("No mpdfs or epdfs found! " + string (e.what())); |
---|
| 79 | } catch (std::exception e) { |
---|
| 80 | it_error ("Error in UI at " + _Sources[i].getPath()); |
---|
[393] | 81 | } |
---|
[463] | 82 | } catch (std::exception e) { |
---|
| 83 | it_error ("Error in UI at " + _Sources[i].getPath()); |
---|
[393] | 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
[463] | 87 | merger_base* Merger = UI::build<merger_base> (Cfg, "Merger"); |
---|
[393] | 88 | |
---|
| 89 | // Support |
---|
[463] | 90 | Setting & _Supp = Cfg.lookup ("Support"); |
---|
| 91 | |
---|
| 92 | if (_Supp.exists ("grid") && _Supp.exists ("nbins")) { |
---|
| 93 | Array<vec> bounds (0); |
---|
| 94 | UI::get (bounds, _Supp, "grid"); |
---|
| 95 | ivec nbins (0); |
---|
| 96 | UI::get (nbins, _Supp, "nbins"); |
---|
| 97 | Merger->set_support (bounds, nbins); |
---|
| 98 | |
---|
| 99 | } else { |
---|
| 100 | if (_Supp.exists ("pdf") && _Supp.exists ("nsamples")) { |
---|
| 101 | epdf *g0 = UI::build<epdf> (_Supp, "pdf"); |
---|
| 102 | int npoints = 100; |
---|
| 103 | _Supp.lookupValue ("nsamples", npoints); |
---|
| 104 | Merger->set_support (*g0, npoints); |
---|
| 105 | delete g0; |
---|
| 106 | } else it_error ("Use either [grid,nbins] or [pdf,nsamples]."); |
---|
[393] | 107 | } |
---|
| 108 | // COMPUTE RESULTS |
---|
[463] | 109 | Merger->set_sources (Sources, true); // takes care of deletion of sources |
---|
[393] | 110 | Merger->merge(); |
---|
[463] | 111 | |
---|
[407] | 112 | // save results |
---|
[463] | 113 | Array<vec> source_vals (Sources.length()); |
---|
| 114 | for (int i = 0;i < Sources.length();i++) { |
---|
| 115 | datalink_m2e dl; |
---|
| 116 | dl.set_connection (Sources (i)->_rv(), Sources (i)->_rvc(), Merger->_rv()); |
---|
[429] | 117 | |
---|
[463] | 118 | vec ll (Merger->_Smp()._samples().length()); |
---|
| 119 | for (int j = 0; j < Merger->_Smp()._samples().length(); j++) { |
---|
| 120 | vec dt = dl.pushdown (Merger->_Smp()._samples() (j)); |
---|
| 121 | vec dtc = dl.get_cond (Merger->_Smp()._samples() (j)); |
---|
| 122 | ll (j) = Sources (i)->evallogcond (dt, dtc); |
---|
| 123 | } |
---|
[429] | 124 | |
---|
[463] | 125 | vec sll = exp (ll); |
---|
[429] | 126 | |
---|
[463] | 127 | source_vals (i) = sll / sum (sll); |
---|
| 128 | } |
---|
| 129 | |
---|
[464] | 130 | merger_mix* MerMix=dynamic_cast<merger_mix*>(Merger); |
---|
| 131 | vec mix_val; |
---|
| 132 | |
---|
| 133 | if (MerMix){ |
---|
| 134 | vec ll (Merger->_Smp()._samples().length()); |
---|
| 135 | for (int j = 0; j < Merger->_Smp()._samples().length(); j++) { |
---|
| 136 | ll (j) = Merger->evallog (Merger->_Smp()._samples() (j)); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | vec sll = exp (ll); |
---|
| 140 | |
---|
| 141 | mix_val = sll / sum (sll); |
---|
[463] | 142 | } |
---|
| 143 | |
---|
[407] | 144 | #ifdef MEX |
---|
| 145 | mxArray* tmp ; |
---|
| 146 | // Save results |
---|
[463] | 147 | if (n_output > 0) { |
---|
| 148 | tmp = mxCreateStructMatrix (1, 1, 0, NULL); |
---|
[407] | 149 | //support |
---|
[463] | 150 | Array<vec> &samples = Merger->_Smp()._samples(); |
---|
| 151 | if (samples.size() > 0) { |
---|
| 152 | mxArray* fld = mxCreateDoubleMatrix (samples (0).length(), samples.size(), mxREAL); |
---|
| 153 | Arrayvec2mxArray (samples, fld); |
---|
| 154 | mxReplaceFieldNM (tmp, "support", fld); |
---|
[407] | 155 | } |
---|
| 156 | |
---|
| 157 | //weights |
---|
| 158 | vec &w = Merger->_Smp()._w(); |
---|
[463] | 159 | mxArray* fldw = mxCreateDoubleMatrix (1, w.length(), mxREAL); |
---|
| 160 | vec2mxArray (w, fldw); |
---|
| 161 | mxReplaceFieldNM (tmp, "weights", fldw); |
---|
| 162 | |
---|
[429] | 163 | //mixture values |
---|
[464] | 164 | if (mix_val.length()>0){ |
---|
| 165 | mxArray* fldm = mxCreateDoubleMatrix (1, w.length(), mxREAL); |
---|
| 166 | vec2mxArray (mix_val, fldm); |
---|
| 167 | mxReplaceFieldNM (tmp, "mix", fldm); |
---|
| 168 | } |
---|
[407] | 169 | // sources |
---|
| 170 | char srcstr[20]; |
---|
[463] | 171 | for (int i = 0;i < Sources.length();i++) { |
---|
| 172 | sprintf (srcstr, "source%d", i + 1); |
---|
| 173 | mxArray* fldw = mxCreateDoubleMatrix (1, source_vals (i).length(), mxREAL); |
---|
| 174 | vec2mxArray (source_vals (i), fldw); |
---|
| 175 | mxReplaceFieldNM (tmp, srcstr, fldw); |
---|
| 176 | } |
---|
[407] | 177 | |
---|
| 178 | output[0] = tmp; |
---|
| 179 | } |
---|
| 180 | #endif |
---|
[393] | 181 | } |
---|