forked from Craigacp/FEAST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSToolboxMex.c
310 lines (271 loc) · 12 KB
/
FSToolboxMex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*******************************************************************************
** FSToolboxMex.c is the entry point for the feature selection toolbox.
** It provides a MATLAB interface to the various selection algorithms.
**
** Initial Version - 27/06/2011
** Updated - 22/02/2014 - Moved increment of feature numbers here.
**
** Author - Adam Pocock
**
** Part of the FEAture Selection Toolbox (FEAST), please reference
** "Conditional Likelihood Maximisation: A Unifying Framework for Information
** Theoretic Feature Selection"
** G. Brown, A. Pocock, M.-J. Zhao, M. Lujan
** Journal of Machine Learning Research (JMLR), 2012
**
** Please check www.cs.manchester.ac.uk/~gbrown/fstoolbox for updates.
**
** Copyright (c) 2010-2014, A. Pocock, G. Brown, The University of Manchester
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without modification,
** are permitted provided that the following conditions are met:
**
** - Redistributions of source code must retain the above copyright notice, this
** list of conditions and the following disclaimer.
** - Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
** and/or other materials provided with the distribution.
** - Neither the name of The University of Manchester nor the names of its
** contributors may be used to endorse or promote products derived from this
** software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
** ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*******************************************************************************/
#include "FSToolbox.h"
#include "FSAlgorithms.h"
#include "ArrayOperations.h"
#include "Entropy.h"
/******************************************************************************
** entry point for the mex call
** nlhs - number of outputs
** plhs - pointer to array of outputs
** nrhs - number of inputs
** prhs - pointer to array of inputs
******************************************************************************/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/*************************************************************
** this function takes 4-6 arguments:
** flag = which algorithm to use,
** k = number of features to select,
** featureMatrix[][] = matrix of features,
** classColumn[] = targets,
** optionalParam1 = (path angle or beta value),
** optionalParam2 = (gamma value),
** the arguments should all be discrete integers.
** and has one output:
** selectedFeatures[] of size k
*************************************************************/
int flag, k;
double optionalParam1, optionalParam2;
int numberOfFeatures, numberOfSamples, numberOfTargets;
double *featureMatrix, *targets, *output, *outputFeatures;
double entropyTest;
int i,j;
/************************************************************
** number to function map
** 1 = MIFS
** 2 = mRMR
** 3 = CMIM
** 4 = JMI
** 5 = DISR
** 6 = CIFE
** 7 = ICAP
** 8 = CondRed
** 9 = BetaGamma
** 10 = CMI
*************************************************************/
if (nlhs > 1)
{
printf("Incorrect number of output arguments");
}/*if not 1 output*/
if ((nrhs < 4) || (nrhs > 6))
{
printf("Incorrect number of input arguments");
return;
}/*if not 4-6 inputs*/
/*get the flag for which algorithm*/
flag = (int) mxGetScalar(prhs[0]);
/*get the number of features to select, cast out as it is a double*/
k = (int) mxGetScalar(prhs[1]);
numberOfFeatures = mxGetN(prhs[2]);
numberOfSamples = mxGetM(prhs[2]);
numberOfTargets = mxGetM(prhs[3]);
if (nrhs == 6)
{
optionalParam1 = (double) mxGetScalar(prhs[4]);
optionalParam2 = (double) mxGetScalar(prhs[5]);
}
else if (nrhs == 5)
{
optionalParam1 = (double) mxGetScalar(prhs[4]);
optionalParam2 = 0.0;
}
if (numberOfTargets != numberOfSamples)
{
printf("Number of targets must match number of samples\n");
printf("Number of targets = %d, Number of Samples = %d, Number of Features = %d\n",numberOfTargets,numberOfSamples,numberOfFeatures);
plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL);
return;
}/*if size mismatch*/
else if ((k < 1) || (k > numberOfFeatures))
{
printf("You have requested k = %d features, which is not possible\n",k);
plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL);
return;
}
else
{
featureMatrix = mxGetPr(prhs[2]);
targets = mxGetPr(prhs[3]);
/*double calculateEntropy(double *dataVector, int vectorLength)*/
entropyTest = calculateEntropy(targets,numberOfSamples);
if (entropyTest < 0.0000001)
{
printf("The class label Y has entropy of 0, therefore all mutual informations containing Y will be 0. No feature selection is performed\n");
plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL);
return;
}
else
{
/*printf("Flag = %d, k = %d, numFeatures = %d, numSamples = %d\n",flag,k,numberOfFeatures,numberOfSamples);*/
switch (flag)
{
case 1: /* MIFS */
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
if (nrhs == 4)
{
/* MIFS is Beta = 1, Gamma = 0 */
optionalParam1 = 1.0;
optionalParam2 = 0.0;
}
/*void BetaGamma(int k, long noOfSamples, long noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures, double beta, double gamma)*/
BetaGamma(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output,optionalParam1,optionalParam2);
incrementVector(output,k);
break;
}
case 2: /* mRMR */
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
/*void mRMR_D(int k, int noOfSamples, int noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures)*/
mRMR_D(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output);
incrementVector(output,k);
break;
}
case 3: /* CMIM */
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
/*void CMIM(int k, int noOfSamples, int noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures)*/
CMIM(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output);
incrementVector(output,k);
break;
}
case 4: /* JMI */
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
/*void JMI(int k, int noOfSamples, int noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures)*/
JMI(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output);
incrementVector(output,k);
break;
}
case 5: /* DISR */
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
/*void DISR(int k, int noOfSamples, int noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures)*/
DISR(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output);
incrementVector(output,k);
break;
}
case 6: /* CIFE */
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
/* CIFE is Beta = 1, Gamma = 1 */
optionalParam1 = 1.0;
optionalParam2 = 1.0;
/*void BetaGamma(int k, long noOfSamples, long noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures, double beta, double gamma)*/
BetaGamma(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output,optionalParam1,optionalParam2);
incrementVector(output,k);
break;
}
case 7: /* ICAP */
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
/*void ICAP(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output);*/
ICAP(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output);
incrementVector(output,k);
break;
}
case 8: /* CondRed */
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
/* CondRed is Beta = 0, Gamma = 1 */
optionalParam1 = 0.0;
optionalParam2 = 1.0;
/*void BetaGamma(int k, long noOfSamples, long noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures, double beta, double gamma)*/
BetaGamma(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output,optionalParam1,optionalParam2);
incrementVector(output,k);
break;
}
case 9: /* BetaGamma */
{
if (nrhs != 6)
{
printf("Insufficient arguments specified for Beta Gamma FS\n");
plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL);
return;
}
else
{
plhs[0] = mxCreateDoubleMatrix(k,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
/*void BetaGamma(int k, long noOfSamples, long noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures, double beta, double gamma)*/
BetaGamma(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output,optionalParam1,optionalParam2);
incrementVector(output,k);
}
break;
}
case 10: /* CMI */
{
output = (double *)mxCalloc(k,sizeof(double));
/*void CondMI(int k, int noOfSamples, int noOfFeatures,double *featureMatrix, double *classColumn, double *outputFeatures)*/
CondMI(k,numberOfSamples,numberOfFeatures,featureMatrix,targets,output);
i = 0;
while((output[i] != -1) && (i < k))
{
i++;
}
plhs[0] = mxCreateDoubleMatrix(i,1,mxREAL);
outputFeatures = (double *)mxGetPr(plhs[0]);
for (j = 0; j < i; j++)
{
outputFeatures[j] = output[j] + 1; /*C indexes from 0 not 1*/
}/*for number of selected features*/
mxFree(output);
output = NULL;
break;
}
}/*switch on flag*/
return;
}
}
}/*mex function entry*/