10#include "tpcclibConfig.h"
24static char *info[] = {
25 "Program for adding Gaussian noise to dynamic PET time-activity curve (TAC)",
26 "or TACs using equation",
27 " TAC_noisy(t) = TAC(t) + (CV/100)*TAC(t)*G(0,1) ,",
28 "G(0,1) is a pseudo random number from a Gaussian distribution",
29 "with zero mean and SD of one.",
31 "Usage: @P [Options] tacfile CV outputfile ",
34 " -minsd=<Percentage of maximal SD>",
35 " Based on CV only the time frames with little or no activity would have",
36 " very little or no noise. With this option the noise level (SD) in all",
37 " time frames will be at least the given percentage of the maximal SD",
38 " of all time frames and TACs.",
41 "Frame durations are not used, even if available in the TAC file.",
42 "If TAC file contains weight column, then the specified amount of variance is",
43 "added to frames with weight=1, and higher or lower variance to those",
44 "frames with lower or higher weight.",
47 " @P simulated.tac 10 noisy.tac",
49 "See also: svar4tac, fvar4tac, sim_3tcm, tacweigh, tacadd, avgttac",
51 "Keywords: TAC, simulation, noise",
70int main(
int argc,
char **argv)
72 int ai, help=0, version=0, verbose=1;
73 char tacfile[FILENAME_MAX], simfile[FILENAME_MAX];
81 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
82 tacfile[0]=simfile[0]=(char)0;
84 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
86 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
87 if(strncasecmp(cptr,
"MINSD=", 6)==0) {
89 if(isnan(minsd) || minsd<0.0 || minsd>100.0) {
90 fprintf(stderr,
"Error: invalid minimum SD percentage '%s'.\n", argv[ai]);
return(1);}
91 if(minsd<0.9) fprintf(stderr,
"Warning: minimum SD is set to %g%%.\n", minsd);
94 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
103 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
108 if(ai<argc) {
strlcpy(tacfile, argv[ai++], FILENAME_MAX);}
111 if(isnan(cv) || cv<0.0 || cv>1000.0) {
112 fprintf(stderr,
"Error: invalid CV%% '%s'.\n", argv[ai]);
return(1);}
113 if(cv<1.0) fprintf(stderr,
"Warning: CV is set to %g%%.\n", cv);
116 if(ai<argc) {
strlcpy(simfile, argv[ai++], FILENAME_MAX);}
118 fprintf(stderr,
"Error: invalid argument '%s'.\n", argv[ai]);
124 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
130 printf(
"tacfile := %s\n", tacfile);
131 printf(
"simfile := %s\n", simfile);
132 if(!isnan(minsd)) printf(
"minsd := %g\n", minsd);
140 if(verbose>1) fprintf(stdout,
"reading %s\n", tacfile);
143 fprintf(stderr,
"Error: %s (%s)\n",
errorMsg(status.
error), tacfile);
148 printf(
"tacNr := %d\n", tac.
tacNr);
149 printf(
"sampleNr := %d\n", tac.
sampleNr);
156 fprintf(stderr,
"Warning: missing frame times.\n");
160 fprintf(stderr,
"Warning: missing concentrations.\n");
170 for(
int fi=0; fi<tac.
sampleNr; fi++)
171 for(
int ri=0; ri<tac.
tacNr; ri++) {
174 if(tac.
c[ri].
y[fi]>ymax) ymax=tac.
c[ri].
y[fi];
176 if(verbose>1) printf(
"wymax := %g\n", ymax);
178 fprintf(stderr,
"Error: invalid concentration data.\n");
184 minsd*=(0.01*cv)*ymax;
185 if(verbose>1) printf(
"final_minsd := %g\n", minsd);
193 if(verbose>1) printf(
"adding noise\n");
196 for(
int fi=0; fi<tac.
sampleNr; fi++) {
197 for(
int ri=0; ri<tac.
tacNr; ri++) {
198 sd=0.01*cv*tac.
c[ri].
y[fi];
200 if(minsd>sd) sd=minsd;
209 if(verbose>1) printf(
"writing noisy data in %s\n", simfile);
210 FILE *fp; fp=fopen(simfile,
"w");
212 fprintf(stderr,
"Error: cannot open file for writing (%s)\n", simfile);
218 fprintf(stderr,
"Error (%d): %s\n", ret,
errorMsg(status.
error));
221 if(verbose>=0) printf(
"Noisy data saved in %s\n", simfile);
double atofVerified(const char *s)
void mertwiInitWithSeed64(MERTWI *mt, uint64_t seed)
Initialize the state vector mt[] inside data structure for Mersenne Twister MT19937 pseudo-random num...
double mertwiRandomGaussian(MERTWI *mt)
Generate a 64-bit double precision floating point pseudo-random number with normal (Gaussian) distrib...
uint64_t mertwiSeed64(void)
Make uint64_t seed for pseudo-random number generators.
void mertwiInit(MERTWI *mt)
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
void statusInit(TPCSTATUS *s)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
char * tacFormattxt(tacformat c)
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
int tacYNaNs(TAC *tac, const int i)
int tacIsWeighted(TAC *tac)
Header file for library libtpcextensions.
char * unitName(int unit_code)
Header file for library libtpcift.
Header file for libtpcrand.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.