Changeset 35785 for trunk/psLib/src/math/psMixtureModels.c
- Timestamp:
- Jul 9, 2013, 6:36:20 PM (13 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psMixtureModels.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/math/psMixtureModels.c
r35469 r35785 112 112 } 113 113 114 psImage *psMMCensorData(psImage *in, 115 long *Ncensored, 116 psVector *offsets) { 117 // Allocate offset vector 118 offsets = psVectorRecycle(offsets,in->numRows,PS_TYPE_S32); 119 120 *Ncensored = 0; 121 122 for (int i = 0; i < in->numRows; i++) { 123 int isCensored = 0; 124 for (int j = 0; j < in->numCols; j++) { 125 if (!isfinite(in->data.F32[i][j])) { 126 isCensored = 1; 127 } 128 } 129 if (isCensored) { 130 *Ncensored = *Ncensored + 1; 131 } 132 offsets->data.S32[i] = *Ncensored; 133 } 134 psImage *out; 135 if (*Ncensored == 0) { 136 out = psMemIncrRefCounter(in); 137 } 138 else { 139 out = psImageAlloc(in->numCols,in->numRows - *Ncensored,PS_TYPE_F32); 140 for (int i = 0; i < out->numRows; i++) { 141 for (int j = 0; j < out->numCols; j++) { 142 out->data.F32[i][j] = in->data.F32[i + offsets->data.S32[i]][j]; 143 } 144 } 145 } 146 return(out); 147 } 114 148 115 149 ///////////////////////////////////////////////////////////////////////////////// … … 117 151 ///////////////////////////////////////////////////////////////////////////////// 118 152 119 120 153 #define MAX_ITERATIONS 100 121 154 #define THRESHOLD 1e-3 122 155 123 124 bool psMMkmeans(psImage *D, 125 int dim, 126 long N, 127 psVector *modes, 128 psImage *means, 129 int m, 130 int *iterations, 131 double *V) { 156 bool psMMkmeans(psImage *D, //< Input data to fit. 157 int dim, //< dimension of the data 158 long N, //< number of data points 159 psVector *modes, //< Output vector of which mode each data point belongs to 160 psImage *means, //< Matrix containing mode mean values for each dimension 161 int m, //< Number of modes to fit 162 int *iterations, //< Number of iterations to perform 163 double *V) { //< Deviation value. Pseudo chi^2 164 PS_ASSERT_IMAGE_NON_NULL(D,false); 165 PS_ASSERT_IMAGE_TYPE(D,PS_TYPE_F32,false); 166 // We need to scan the input for invalid d 167 long Ncensored; 168 psVector *offsets = psVectorAlloc(N,PS_TYPE_S32); 169 psImage *Dcensored = psMMCensorData(D,&Ncensored,offsets); 170 171 psVector *tempModes = psVectorAlloc(N - Ncensored,PS_TYPE_F32); 172 bool success = psMMkmeansUncensored(Dcensored, 173 dim, 174 N - Ncensored, 175 tempModes, 176 means, 177 m, 178 iterations, 179 V); 180 if (Ncensored == 0) { // We can directly return the modes and means calculated 181 if (modes) { 182 psFree(modes); 183 } 184 modes = psMemIncrRefCounter(tempModes); 185 } 186 else { 187 if (!modes) { 188 modes = psVectorAlloc(N,PS_TYPE_F32); 189 } 190 191 psVectorInit(modes,NAN); 192 for (int i = 0; i < N - Ncensored; i++) { 193 modes->data.F32[i - offsets->data.S32[i]] = tempModes->data.F32[i]; 194 } 195 } 196 psFree(offsets); 197 psFree(Dcensored); 198 psFree(tempModes); 199 return(success); 200 } 201 202 203 bool psMMkmeansUncensored(psImage *D, //< Input data to fit. 204 int dim, //< dimension of the data 205 long N, //< number of data points 206 psVector *modes, //< Output vector of which mode each data point belongs to 207 psImage *means, //< Matrix containing mode mean values for each dimension 208 int m, //< Number of modes to fit 209 int *iterations, //< Number of iterations to perform 210 double *V) { //< Deviation value. Pseudo chi^2 132 211 // Assertions 133 212 PS_ASSERT_IMAGE_NON_NULL(D,false); … … 135 214 // PS_ASSERT_IMAGE_SIZE(D,N,dim,false); 136 215 216 137 217 // Allocate things we can allocate here if they're not already allocated 138 218 if (!modes) { 139 modes = psVectorAlloc( m,PS_TYPE_F32);219 modes = psVectorAlloc(N,PS_TYPE_F32); 140 220 } 141 221 if (!means) { … … 277 357 } 278 358 279 bool psMMGMM(psImage *D, 280 int dim, 281 long N, 282 psVector *modes, 283 psImage *means, 284 psArray *sigma, 285 psVector *pi, 286 psImage *P, 287 int m, 288 int *iterations, 289 double *V) { 359 bool psMMGMM(psImage *D, //< Input data to fit 360 int dim, //< Dimension of each data vector 361 long N, //< Number of data vectors 362 psVector *modes, //< Output vector assigning each data point to a mode 363 psImage *means, //< Output matrix containing vectors of the means for each mode 364 psArray *sigma, //< Output array containing covariance matrices for each mode 365 psVector *pi, //< Output vector contining population fractions for each mode 366 psImage *P, //< Output matrix containing the probabilities a data point is in each mode 367 int m, //< Number of modes to fit 368 int *iterations, //< Number of iterations performed 369 double *V) { //< Log-likelihood of final solution. 370 371 PS_ASSERT_IMAGE_NON_NULL(D,false); 372 PS_ASSERT_IMAGE_TYPE(D,PS_TYPE_F32,false); 373 // We need to scan the input for invalid d 374 long Ncensored; 375 psVector *offsets = psVectorAlloc(N,PS_TYPE_F32); 376 psImage *Dcensored = psMMCensorData(D,&Ncensored,offsets); 377 378 psVector *tempModes = psVectorAlloc(N - Ncensored,PS_TYPE_F32); 379 psImage *tempP = psImageAlloc(m,N - Ncensored,PS_TYPE_F32); 380 bool success = psMMGMMUncensored(Dcensored, 381 dim, 382 N - Ncensored, 383 tempModes, 384 means, 385 sigma, 386 pi, 387 tempP, 388 m, 389 iterations, 390 V); 391 if (Ncensored == 0) { // We can directly return the modes and means calculated 392 if (modes) { 393 psFree(modes); 394 } 395 if (P) { 396 psFree(P); 397 } 398 modes = psMemIncrRefCounter(tempModes); 399 P = psMemIncrRefCounter(tempP); 400 } 401 else { 402 if (!modes) { 403 modes = psVectorAlloc(N,PS_TYPE_F32); 404 } 405 if (!P) { 406 P = psImageAlloc(N,m,PS_TYPE_F32); 407 } 408 psVectorInit(modes,NAN); 409 psImageInit(P,NAN); 410 411 for (int i = 0; i < N - Ncensored; i++) { 412 modes->data.F32[i - offsets->data.S32[i]] = tempModes->data.F32[i]; 413 for (int j = 0; j < dim; j++) { 414 P->data.F32[i - offsets->data.S32[i]][j] = tempP->data.F32[i][j]; 415 } 416 } 417 } 418 psFree(offsets); 419 psFree(Dcensored); 420 psFree(tempModes); 421 psFree(tempP); 422 return(success); 423 } 424 425 426 bool psMMGMMUncensored(psImage *D, //< Input data to fit 427 int dim, //< Dimension of each data vector 428 long N, //< Number of data vectors 429 psVector *modes, //< Output vector assigning each data point to a mode 430 psImage *means, //< Output matrix containing vectors of the means for each mode 431 psArray *sigma, //< Output array containing covariance matrices for each mode 432 psVector *pi, //< Output vector contining population fractions for each mode 433 psImage *P, //< Output matrix containing the probabilities a data point is in each mode 434 int m, //< Number of modes to fit 435 int *iterations, //< Number of iterations performed 436 double *V) { //< Log-likelihood of final solution. 290 437 // Assertions 291 438 PS_ASSERT_IMAGE_NON_NULL(D,false);
Note:
See TracChangeset
for help on using the changeset viewer.
