1+ /*
2+
3+ * File: PDFMainMatlab.cpp
4+ * Author: jenny
5+ *
6+ * Created on December 3, 2018, 8:32 AM
7+ */
8+
9+
10+ #include " EstimatePDF.h"
11+ #include " callPDF.h"
12+
13+
14+
15+ void MexFunction::operator ()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
16+
17+ int nParameters = inputs.size ();
18+ matlabPtr = getEngine ();
19+ out.debug = true ;
20+
21+ matlab::data::ArrayFactory factory;
22+ if (nParameters < 1 ) {
23+ out.displayError (matlabPtr, " Data sample required" );
24+ }
25+ if (nParameters > 2 ) {
26+ out.displayError (matlabPtr, " Only two input arguements allowed: data sample and parameter structure" );
27+ }
28+
29+ TypedArray<double > doubleArray = std::move (inputs[0 ]);
30+ int sampleSize = doubleArray.getNumberOfElements ();
31+ if (sampleSize < 3 ) {
32+ out.displayError (matlabPtr, " Must have at least three data points in sample" );
33+ }
34+ double sampleData[sampleSize];
35+ int i = 0 ;
36+ for (auto & elem : doubleArray) {
37+ sampleData[i++] = elem;
38+ }
39+
40+ callPDF *pd = new callPDF (matlabPtr);
41+
42+ if (nParameters > 1 ) {
43+ StructArray const matlabStructArray = inputs[1 ];
44+ auto fields = matlabStructArray.getFieldNames ();
45+ std::vector<std::string> fieldNames (fields.begin (), fields.end ());
46+ int count = 0 ;
47+ Array structField;
48+ for (std::vector<string>::iterator iter = fieldNames.begin (); iter != fieldNames.end (); ++iter) {
49+ string field = *iter;
50+ if (strcmp (field.c_str (), " SURDtarget" ) == 0 ) {
51+ structField = matlabStructArray[0 ][fieldNames[count++]];
52+ pd->setSURDtarget (structField[0 ]);
53+ } else if (strcmp (field.c_str (), " SURDmin" ) == 0 ) {
54+ structField = matlabStructArray[0 ][fieldNames[count++]];
55+ pd->setSURDmin (structField[0 ]);
56+ } else if (strcmp (field.c_str (), " SURDmax" ) == 0 ) {
57+ structField = matlabStructArray[0 ][fieldNames[count++]];
58+ pd->setSURDmax (structField[0 ]);
59+ } else if (strcmp (field.c_str (), " scoreType" ) == 0 ) {
60+ structField = matlabStructArray[0 ][fieldNames[count++]];
61+ pd->setScoreType ((CharArray (structField)).toAscii ());
62+ } else if (strcmp (field.c_str (), " LagrangeMin" ) == 0 ) {
63+ structField = matlabStructArray[0 ][fieldNames[count++]];
64+ pd->setLagrangeMin (structField[0 ]);
65+ } else if (strcmp (field.c_str (), " LagrangeMax" ) == 0 ) {
66+ structField = matlabStructArray[0 ][fieldNames[count++]];
67+ pd->setLagrangeMax (structField[0 ]);
68+ } else if (strcmp (field.c_str (), " integrationPoints" ) == 0 ) {
69+ structField = matlabStructArray[0 ][fieldNames[count++]];
70+ pd->setPoints (structField[0 ]);
71+ } else if (strcmp (field.c_str (), " lowBound" ) == 0 ) {
72+ structField = matlabStructArray[0 ][fieldNames[count++]];
73+ pd->setLow (structField[0 ]);
74+ } else if (strcmp (field.c_str (), " highBound" ) == 0 ) {
75+ structField = matlabStructArray[0 ][fieldNames[count++]];
76+ pd->setHigh (structField[0 ]);
77+ } else if (strcmp (field.c_str (), " outlierCutoff" ) == 0 ) {
78+ structField = matlabStructArray[0 ][fieldNames[count++]];
79+ pd->setOutlierCutoff (structField[0 ]);
80+ } else if (strcmp (field.c_str (), " partition" ) == 0 ) {
81+ structField = matlabStructArray[0 ][fieldNames[count++]];
82+ pd->setPartitionSize (structField[0 ]);
83+ } else if (strcmp (field.c_str (), " debug" ) == 0 ) {
84+ structField = matlabStructArray[0 ][fieldNames[count++]];
85+ pd->setDebug (structField[0 ]);
86+ } else if (strcmp (field.c_str (), " minVariance" ) == 0 ) {
87+ structField = matlabStructArray[0 ][fieldNames[count++]];
88+ pd->setVariance (structField[0 ]);
89+ } else if (strcmp (field.c_str (), " adaptiveDx" ) == 0 ) {
90+ structField = matlabStructArray[0 ][fieldNames[count++]];
91+ pd->setAdaptiveDx (structField[0 ]);
92+ } else {
93+ string unknown = " Unknown parameter: " + field;
94+ out.displayError (matlabPtr, unknown);
95+ }
96+ }
97+ }
98+ pd->makeCall (sampleSize, sampleData);
99+
100+ vector <int > failed;
101+ failed.push_back (pd->solutionFailed );
102+ ArrayDimensions sz = {failed.size (), 1 , 1 };
103+ TypedArray<int > MsolutionFailed = factory.createArray (sz, failed.begin (), failed.end ());
104+ outputs[0 ] = MsolutionFailed;
105+
106+ if (!pd->solutionFailed ) {
107+ sz = {pd->Vx .size (), 1 , 1 };
108+ TypedArray<double > Mx = factory.createArray (sz, pd->Vx .begin (), pd->Vx .end ());
109+ outputs[1 ] = Mx;
110+ sz = {pd->Vpdf .size (), 1 , 1 };
111+ TypedArray<double > Mpdf = factory.createArray (sz, pd->Vpdf .begin (), pd->Vpdf .end ());
112+ outputs[2 ] = Mpdf;
113+ sz = {pd->Vcdf .size (), 1 , 1 };
114+ TypedArray<double > Mcdf = factory.createArray (sz, pd->Vcdf .begin (), pd->Vcdf .end ());
115+ outputs[3 ] = Mcdf;
116+ sz = {pd->Vsqr .size (), 1 , 1 };
117+ TypedArray<double > Msqr = factory.createArray (sz, pd->Vsqr .begin (), pd->Vsqr .end ());
118+ outputs[4 ] = Msqr;
119+ sz = {pd->Vlagrange .size (), 1 , 1 };
120+ TypedArray<double > Mlagrange = factory.createArray (sz, pd->Vlagrange .begin (), pd->Vlagrange .end ());
121+ outputs[5 ] = Mlagrange;
122+
123+ vector <double > threshold;
124+ threshold.push_back (pd->thresholdScore );
125+ sz = {threshold.size (), 1 , 1 };
126+ TypedArray<double > Mthreshold = factory.createArray (sz, threshold.begin (), threshold.end ());
127+ outputs[6 ] = Mthreshold;
128+
129+ vector <double > confidence;
130+ confidence.push_back (pd->confidenceScore );
131+ sz = {confidence.size (), 1 , 1 };
132+ TypedArray<double > Mconfidence = factory.createArray (sz, confidence.begin (), confidence.end ());
133+ outputs[7 ] = Mconfidence;
134+
135+ sz = {pd->Vr .size (), 1 , 1 };
136+ TypedArray<double > Mr = factory.createArray (sz, pd->Vr .begin (), pd->Vr .end ());
137+ outputs[8 ] = Mr;
138+ }
139+ delete pd;
140+
141+ }
0 commit comments