TPCCLIB
Loading...
Searching...
No Matches
inpstart.c File Reference

Verifies that peak is not missed in an input TAC file, and optionally adds a guess of first zero sample. More...

Go to the source code of this file.

Functions

int dftFixPeak (DFT *dft, int verbose)
 

Detailed Description

Verifies that peak is not missed in an input TAC file, and optionally adds a guess of first zero sample.

Author
Vesa Oikonen

Definition in file inpstart.c.

Function Documentation

◆ dftFixPeak()

int dftFixPeak ( DFT * dft,
int verbose )

Tries to fix the situation when specified (input) TAC does not seem to contain peak or it starts too late to get reliable estimate of AUC.

Returns
Returns 0 in case data could be fixed, < 0 if there still may be a problem, 1 in case of an error, and 2 if TAC can not be corrected automatically.
Parameters
dftPointer to TAC data which is fixed and verified.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 266 of file inpstart.c.

271 {
272 int ri, ret, mini, maxi, n, warn=0, fixnr=0;
273 double minx, maxx, miny, maxy, dif, d, slope, ic;
274 char tmp[256];
275
276 if(verbose>0) printf("dftFixPeak(dft, %d)\n", verbose);
277 /* Check the arguments */
278 if(dft==NULL || dft->frameNr<1 || dft->voiNr<1) return 1;
279
280 /* If less than 3 samples, then this can not be suitable as input TAC */
281 if(dft->frameNr<3) return 2;
282
283 /* Check if any correction is needed for any TAC */
284 if(dftVerifyPeak(dft, -1, 0, NULL)==0) {
285 if(verbose>1) printf("TAC does not need to be corrected\n");
286 return 0;
287 }
288
289 /* Check if problem was something else than missing initial rise */
290 if(dft->x[0]<=0.001) {
291 if(verbose>1) printf("TAC already has zero sample\n");
292 return 2;
293 }
294
295 /* Allocate memory for new sample points to be added */
296 double zx, zy[dft->voiNr];
297 zx=0.0; if(dft->timetype==DFT_TIME_STARTEND) zx=0.5*dft->x1[0];
298
299 /* Go through all TACs */
300 for(ri=0; ri<dft->voiNr; ri++) {
301 zy[ri]=0.0;
302 if(verbose>1 && dft->voiNr>1)
303 printf("checking region %d: %s\n", 1+ri, dft->voi[ri].name);
304
305 /* Check if any correction is needed for this TAC */
306 if(dft->voiNr>1 && dftVerifyPeak(dft, ri, 0, NULL)==0) continue; // next TAC
307
308 /* Get the TAC min and max */
309 ret=dftMinMaxTAC(dft, ri, &minx, &maxx, &miny, &maxy, NULL, NULL,
310 &mini, &maxi);
311 if(ret!=0) {
312 if(verbose>0) printf("Error %d in dftMinMaxTAC()\n", ret);
313 return 1;
314 }
315
316 /* If peak is not much higher than lowest value, that may indicate
317 a problem */
318 if(maxy<1.5*miny) {
319 if(verbose>0) printf("TAC does not have a clear peak.\n");
320 return 2;
321 }
322 if(maxy<5.0*miny) {
323 if(verbose>1) printf("TAC does not have a clear peak.\n");
324 warn++;
325 }
326
327 /* If the first sample is the peak:
328 then add zero sample unless sample time is not too late */
329 if(maxi==0) {
330 if(verbose>1) printf("peak at the first sample.\n");
331 if(dft->timetype==DFT_TIME_STARTEND) {
332 dif=dft->x1[maxi]/(dft->x2[maxi]-dft->x1[maxi]);
333 } else {
334 dif=dft->x[maxi]/(dft->x[maxi+1]-dft->x[maxi]);
335 }
336 if(verbose>2) printf("dif := %g\n", dif);
337 if(dif>5.0) {
338 if(verbose>1)
339 printf("peak at the first sample, which is too late.\n");
340 return 2;
341 } else if(dif>1.0 && maxy<=10.*miny) {
342 if(verbose>1)
343 printf("peak at the first sample and bad peak/tail ratio.\n");
344 return 2;
345 }
346 if(verbose>1) printf("zero sample added.\n");
347 // default zeroes are good for this TAC
348 fixnr++; continue;
349 }
350
351 /* If first sample is closer to peak and relatively high, then
352 it is not safe to guess the start sample */
353 if(dft->x[0]>0.75*maxx) {
354 if(dft->voi[ri].y[0]>0.50*maxy) {
355 if(verbose>1) printf("The first sample is relatively late and high.\n");
356 return 2;
357 }
358 }
359
360 /* Try to estimate zero point from ascending part */
361 n=maxi+1; if(n>6) n/=2; else if(n>3) n--;
362 ret=highest_slope(dft->x, dft->voi[ri].y, maxi+1, n,
363 &slope, &ic, &d, NULL);
364 if(ret!=0 || d>=maxx) {
365 if(verbose>0) printf("ascending part of TAC not available.\n");
366 if(verbose>1) printf("ret=%d\n", ret);
367 return 2;
368 }
369 if(verbose>2) {
370 printf("based on ascending part:\n");
371 printf(" slope: %g\n", slope);
372 printf(" ic: %g\n", ic);
373 printf(" new x intercept: %g\n", d);
374 }
375 if(dft->timetype==DFT_TIME_STARTEND) {
376 /* If start and end times, then set the y value */
377 zy[ri]=slope*zx+ic;
378 if(zy[ri]>0.5*dft->voi[ri].y[0]) zy[ri]=0.5*dft->voi[ri].y[0];
379 else if(zy[ri]<0.0) zy[ri]=0.0;
380 } else if(dft->voiNr>1) {
381 /* If more than one TAC, then only set the y value */
382 zy[ri]=slope*zx+ic;
383 if(zy[ri]>0.5*dft->voi[ri].y[0]) zy[ri]=0.5*dft->voi[ri].y[0];
384 else if(zy[ri]<0.0) zy[ri]=0.0;
385 } else {
386 /* Otherwise change the sample time */
387 if(d<0.0) {
388 if(ic<0.5*dft->voi[ri].y[0]) d=0.5*dft->x[0]; else d=0.0;
389 }
390 if(d>=dft->x[0]) d=0.5*dft->x[0];
391 zx=d;
392 }
393 fixnr++;
394
395 } // next TAC
396
397 /* If TAC(s) were fixed, only then add the zero sample */
398 if(fixnr>0) {
399 ret=dftFillInitialGap(dft); if(ret!=0) return 1;
400 dft->x[0]=zx;
401 for(ri=0; ri<dft->voiNr; ri++) {
402 dft->voi[ri].y[0]=zy[ri];
403 if(verbose>1) printf("zero sample added at (%g,%g).\n", zx, zy[ri]);
404 }
405 }
406
407 /* Check TACs are fine after corrections */
408 ret=dftVerifyPeak(dft, -1, 0, tmp);
409 if(verbose>0) {
410 if(verbose>2) printf("dftVerifyPeak()=%d\n", ret);
411 if(verbose>1) fprintf(stderr, "Note: %s\n", tmp);
412 if(ret>1) printf("correction was not successful\n");
413 }
414 return(ret);
415}
int dftMinMaxTAC(DFT *dft, int tacindex, double *minx, double *maxx, double *miny, double *maxy, int *mini, int *maxi, int *mins, int *maxs)
Definition dft.c:1024
int dftFillInitialGap(DFT *dft)
Definition dft.c:1385
int dftVerifyPeak(DFT *dft, int index, int verbose, char *status)
Definition dftinput.c:747
#define DFT_TIME_STARTEND
int highest_slope(double *x, double *y, int n, int slope_n, double *m, double *c, double *xi, double *xh)
Definition pearson.c:424
int timetype
Voi * voi
double * x1
int voiNr
double * x2
int frameNr
double * x
double * y
char name[MAX_REGIONNAME_LEN+1]