Changeset 7931
 Timestamp:
 03/23/12 17:53:53 (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/projects/curvefitter/src/main/java/loci/curvefitter/SLIMCurveFitter.java
r7929 r7931 244 244 } 245 245 246 // use array to pass double by reference 247 double[] chiSquare = new double[1]; 248 249 if (FitAlgorithm.SLIMCURVE_RLD.equals(m_fitAlgorithm)  FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 250 // RLD or triple integral fit 251 252 // use arrays to pass double by reference 253 double[] z = new double[1]; 254 double[] a = new double[1]; 255 double[] tau = new double[1]; 256 257 for (ICurveFitData data: dataArray) { 258 // grab incoming parameters 259 a[0] = data.getParams()[2]; 260 tau[0] = data.getParams()[3]; 261 z[0] = data.getParams()[1]; 262 263 // get IRF curve, if any 264 double[] instrumentResponse = getInstrumentResponse(data.getPixels()); 265 int nInstrumentResponse = 0; 266 if (null != instrumentResponse) { 267 nInstrumentResponse = instrumentResponse.length; 268 } 269 270 // set start and stop 271 int start = data.getAdjustedDataStartIndex(); 272 if (FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 273 start = data.getTransEstimateStartIndex(); 274 } 275 int stop = data.getAdjustedTransEndIndex(); 276 277 int chiSquareAdjust = stop  start  numParamFree; 278 279 returnValue = doRLDFit( 280 m_xInc, 281 data.getAdjustedYCount(), 282 start, 283 stop, 284 instrumentResponse, 285 nInstrumentResponse, 286 noise, 287 data.getSig(), 288 z, 289 a, 290 tau, 291 data.getYFitted(), 292 chiSquare, 293 data.getChiSquareTarget() * chiSquareAdjust 294 ); 295 296 // set outgoing parameters, unless they are fixed 297 data.getParams()[0] = chiSquare[0] / chiSquareAdjust; 298 if (free[0]) { 299 data.getParams()[1] = z[0]; 300 } 301 if (free[1]) { 302 data.getParams()[2] = a[0]; 303 } 304 if (free[2]) { 305 data.getParams()[3] = tau[0]; 306 } 307 } 308 } 309 310 if (FitAlgorithm.SLIMCURVE_LMA.equals(m_fitAlgorithm)  FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 311 // LMA fit 312 for (ICurveFitData data: dataArray) { 313 int nInstrumentResponse = 0; 314 if (null != m_instrumentResponse) { 315 nInstrumentResponse = m_instrumentResponse.length; 316 } 317 318 // set start and stop 319 int start = data.getAdjustedDataStartIndex(); 320 int stop = data.getAdjustedTransEndIndex(); 321 322 int chiSquareAdjust = stop  start  numParamFree; 323 324 returnValue = doLMAFit( 325 m_xInc, 326 data.getAdjustedYCount(), 327 start, 328 stop, 329 m_instrumentResponse, 330 nInstrumentResponse, 331 noise, 332 data.getSig(), 333 data.getParams(), 334 toIntArray(m_free), 335 data.getParams().length  1, 336 data.getYFitted(), 337 chiSquare, 338 data.getChiSquareTarget() * chiSquareAdjust, 339 chiSquareDelta 340 ); 341 342 data.getParams()[0] /= chiSquareAdjust; 343 } 344 } 345 346 //TODO ARG error value deserves more thought; just returning last value 347 return returnValue; 348 349 } 350 351 /* 352 * Does the RLD fit according to whether the library is accessed via JNA or 353 * JNI. 354 */ 355 private int doRLDFit(double xInc, 356 double y[], 357 int fitStart, 358 int fitEnd, 359 double instr[], 360 int nInstr, 361 int noise, 362 double sig[], 363 double z[], 364 double a[], 365 double tau[], 366 double fitted[], 367 double chiSquare[], 368 double chiSquareTarget 369 ) 370 { 371 int returnValue = 0; 246 372 if (s_libraryOnPath) { 247 373 // JNA version 248 DoubleByReference chiSquare = new DoubleByReference(); 249 250 if (FitAlgorithm.SLIMCURVE_RLD.equals(m_fitAlgorithm)  FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 251 // RLD or triple integral fit 252 DoubleByReference z = new DoubleByReference(); 253 DoubleByReference a = new DoubleByReference(); 254 DoubleByReference tau = new DoubleByReference(); 255 256 for (ICurveFitData data: dataArray) { 257 // grab incoming parameters 258 a.setValue( data.getParams()[2]); 259 tau.setValue(data.getParams()[3]); 260 z.setValue( data.getParams()[1]); 261 262 // get IRF curve, if any 263 double[] instrumentResponse = m_instrumentResponse; //TODO ARG was scaling: getInstrumentResponse(data.getPixels()); 264 int nInstrumentResponse = 0; 265 if (null != instrumentResponse) { 266 nInstrumentResponse = instrumentResponse.length; 267 } 268 269 // set start and stop 270 int start = data.getAdjustedDataStartIndex(); 271 if (FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 272 start = data.getTransEstimateStartIndex(); 273 } 274 int stop = data.getAdjustedTransEndIndex(); 275 276 int chiSquareAdjust = stop  start  numParamFree; 277 278 returnValue = s_library.RLD_fit( 279 m_xInc, 280 data.getAdjustedYCount(), 281 start, 282 stop, 283 instrumentResponse, 284 nInstrumentResponse, 285 noise, 286 data.getSig(), 287 z, 288 a, 289 tau, 290 data.getYFitted(), 291 chiSquare, 292 data.getChiSquareTarget() * chiSquareAdjust 293 ); 294 // set outgoing parameters, unless they are fixed 295 data.getParams()[0] = chiSquare.getValue(); //TODO ARG TRI2 / chiSquareAdjust; take nonreduced chisquare target and return nonreduced chisquare; this s/b fixed elsewhere 296 if (free[0]) { 297 data.getParams()[1] = z.getValue(); 298 } 299 if (free[1]) { 300 data.getParams()[2] = a.getValue(); 301 } 302 if (free[2]) { 303 data.getParams()[3] = tau.getValue(); 304 } 305 } 306 } 307 308 if (FitAlgorithm.SLIMCURVE_LMA.equals(m_fitAlgorithm)  FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 309 // LMA fit 310 for (ICurveFitData data: dataArray) { 311 int nInstrumentResponse = 0; 312 if (null != m_instrumentResponse) { 313 nInstrumentResponse = m_instrumentResponse.length; 314 } 315 316 // set start and stop 317 int start = data.getAdjustedDataStartIndex(); 318 int stop = data.getAdjustedTransEndIndex(); 319 320 int chiSquareAdjust = stop  start  numParamFree; 321 322 returnValue = s_library.LMA_fit( 323 m_xInc, 324 data.getAdjustedYCount(), 325 start, 326 stop, 327 m_instrumentResponse, 328 nInstrumentResponse, 329 noise, 330 data.getSig(), 331 data.getParams(), 332 toIntArray(m_free), 333 data.getParams().length  1, 334 data.getYFitted(), 335 chiSquare, 336 data.getChiSquareTarget() * chiSquareAdjust, 337 data.getChiSquareDelta() 338 ); 339 } 340 } 374 375 DoubleByReference zRef = new DoubleByReference(z[0]); 376 DoubleByReference aRef = new DoubleByReference(a[0]); 377 DoubleByReference tauRef = new DoubleByReference(tau[0]); 378 DoubleByReference chiSquareRef = new DoubleByReference(chiSquare[0]); 379 380 returnValue = s_library.RLD_fit( 381 xInc, 382 y, 383 fitStart, 384 fitEnd, 385 instr, 386 nInstr, 387 noise, 388 sig, 389 zRef, 390 aRef, 391 tauRef, 392 fitted, 393 chiSquareRef, 394 chiSquareTarget); 395 396 z[0] = zRef.getValue(); 397 a[0] = aRef.getValue(); 398 tau[0] = tauRef.getValue(); 399 chiSquare[0] = chiSquareRef.getValue(); 341 400 } 342 401 else { 343 402 // JNI version 344 345 // use array to pass double by reference 346 double[] chiSquare = new double[1]; 347 348 if (FitAlgorithm.SLIMCURVE_RLD.equals(m_fitAlgorithm)  FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 349 // RLD or triple integral fit 350 351 // use arrays to pass double by reference 352 double[] z = new double[1]; 353 double[] a = new double[1]; 354 double[] tau = new double[1]; 355 356 for (ICurveFitData data: dataArray) { 357 // grab incoming parameters 358 a[0] = data.getParams()[2]; 359 tau[0] = data.getParams()[3]; 360 z[0] = data.getParams()[1]; 361 362 // get IRF curve, if any 363 double[] instrumentResponse = getInstrumentResponse(data.getPixels()); 364 int nInstrumentResponse = 0; 365 if (null != instrumentResponse) { 366 nInstrumentResponse = instrumentResponse.length; 367 } 368 369 // set start and stop 370 int start = data.getAdjustedDataStartIndex(); 371 if (FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 372 start = data.getTransEstimateStartIndex(); 373 } 374 int stop = data.getAdjustedTransEndIndex(); 375 376 int chiSquareAdjust = stop  start  numParamFree; 377 378 returnValue = RLD_fit(m_xInc, 379 data.getAdjustedYCount(), 380 start, 381 stop, 382 instrumentResponse, 383 nInstrumentResponse, 384 noise, 385 data.getSig(), 386 z, 387 a, 388 tau, 389 data.getYFitted(), 390 chiSquare, 391 data.getChiSquareTarget() * chiSquareAdjust 392 ); 393 394 // set outgoing parameters, unless they are fixed 395 data.getParams()[0] = chiSquare[0] / chiSquareAdjust; 396 if (free[0]) { 397 data.getParams()[1] = z[0]; 398 } 399 if (free[1]) { 400 data.getParams()[2] = a[0]; 401 } 402 if (free[2]) { 403 data.getParams()[3] = tau[0]; 404 } 405 } 406 } 407 408 if (FitAlgorithm.SLIMCURVE_LMA.equals(m_fitAlgorithm)  FitAlgorithm.SLIMCURVE_RLD_LMA.equals(m_fitAlgorithm)) { 409 // LMA fit 410 for (ICurveFitData data: dataArray) { 411 int nInstrumentResponse = 0; 412 if (null != m_instrumentResponse) { 413 nInstrumentResponse = m_instrumentResponse.length; 414 } 415 416 // set start and stop 417 int start = data.getAdjustedDataStartIndex(); 418 int stop = data.getAdjustedTransEndIndex(); 419 420 int chiSquareAdjust = stop  start  numParamFree; 421 422 returnValue = LMA_fit( 423 m_xInc, 424 data.getAdjustedYCount(), 425 start, 426 stop, 427 m_instrumentResponse, 428 nInstrumentResponse, 429 noise, 430 data.getSig(), 431 data.getParams(), 432 toIntArray(m_free), 433 data.getParams().length  1, 434 data.getYFitted(), 435 chiSquare, 436 data.getChiSquareTarget() * chiSquareAdjust, 437 chiSquareDelta 438 ); 439 440 data.getParams()[0] /= chiSquareAdjust; 441 } 442 } 443 } 444 445 //TODO error return deserves much more thought!! Just returning the last value here!! 403 404 returnValue = RLD_fit( 405 xInc, 406 y, 407 fitStart, 408 fitEnd, 409 instr, 410 nInstr, 411 noise, 412 sig, 413 z, 414 z, 415 tau, 416 fitted, 417 chiSquare, 418 chiSquareTarget); 419 } 446 420 return returnValue; 447 448 421 } 449 450 int[] toIntArray(boolean[] booleanArray) { 422 423 /* 424 * Does the LMA fit according to whether the library is accessed via JNA or 425 * JNI. 426 */ 427 private int doLMAFit(double xInc, 428 double y[], 429 int fitStart, 430 int fitEnd, 431 double instr[], 432 int n_instr, 433 int noise, 434 double sig[], 435 double param[], 436 int paramFree[], 437 int nParam, 438 double fitted[], 439 double chiSquare[], 440 double chiSquareTarget, 441 double chiSquareDelta 442 ) 443 { 444 int returnValue = 0; 445 446 if (s_libraryOnPath) { 447 // JNA version 448 449 DoubleByReference chiSquareRef = new DoubleByReference(chiSquare[0]); 450 451 returnValue = s_library.LMA_fit( 452 xInc, 453 y, 454 fitStart, 455 fitEnd, 456 instr, 457 n_instr, 458 noise, 459 sig, 460 param, 461 paramFree, 462 nParam, 463 fitted, 464 chiSquareRef, 465 chiSquareTarget, 466 chiSquareDelta); 467 468 chiSquare[0] = chiSquareRef.getValue(); 469 } 470 else { 471 // JNI version 472 473 returnValue = LMA_fit( 474 xInc, 475 y, 476 fitStart, 477 fitEnd, 478 instr, 479 n_instr, 480 noise, 481 sig, 482 param, 483 paramFree, 484 nParam, 485 fitted, 486 chiSquare, 487 chiSquareTarget, 488 chiSquareDelta); 489 } 490 return returnValue; 491 } 492 493 private int[] toIntArray(boolean[] booleanArray) { 451 494 int intArray[] = new int[booleanArray.length]; 452 495 for (int i = 0; i < booleanArray.length; ++i) { … … 455 498 return intArray; 456 499 } 457 458 double[] floatToDouble(float[] f) {459 double d[] = new double[f.length];460 for (int i = 0; i < f.length; ++i) {461 d[i] = f[i];462 }463 return d;464 }465 466 float[] doubleToFloat(double[] d) {467 float f[] = new float[d.length];468 for (int i = 0; i < d.length; ++i) {469 f[i] = (float) d[i];470 }471 return f;472 }473 474 500 }
Note: See TracChangeset
for help on using the changeset viewer.