TPCCLIB
Loading...
Searching...
No Matches
imgftac.c
Go to the documentation of this file.
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <unistd.h>
14#include <string.h>
15#include <math.h>
16#include <time.h>
17/*****************************************************************************/
18#include "libtpcmisc.h"
19#include "libtpcmodel.h"
20#include "libtpccurveio.h"
21#include "libtpcimgio.h"
22#include "libtpcimgp.h"
23#include "libtpcmodext.h"
24/*****************************************************************************/
25
26/*****************************************************************************/
27static char *info[] = {
28 "Find those voxels in dynamic PET image which correlate best with",
29 "user-specified TAC. Program saves a static image containing the Pearson's",
30 "correlation coefficients.",
31 " ",
32 "Usage: @P [Options] image tac ccimage",
33 " ",
34 "Options:",
35 " -slope=<filename>",
36 " Save the slopes of lines fitted to the concentration values as",
37 " static image.",
38 " -ic=<filename>",
39 " Save the y axis intercepts of lines fitted to the concentrations as",
40 " static image.",
41 " -integral",
42 " Integrals from 0 to each sample time are used in the correlation.",
43 " -stdoptions", // List standard options like --help, -v, etc
44 " ",
45 "See also: imgthrs, imgqntls, imgslim, imgcalc, pxl2tac, imgdysmo",
46 " ",
47 "Keywords: image, TAC, correlation, mask, threshold",
48 0};
49/*****************************************************************************/
50
51/*****************************************************************************/
52/* Turn on the globbing of the command line, since it is disabled by default in
53 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
54 In Unix&Linux wildcard command line processing is enabled by default. */
55/*
56#undef _CRT_glob
57#define _CRT_glob -1
58*/
59int _dowildcard = -1;
60/*****************************************************************************/
61
62/*****************************************************************************/
66int main(int argc, char **argv)
67{
68 int ai, help=0, version=0, verbose=1;
69 char imgfile[FILENAME_MAX], tacfile[FILENAME_MAX], outfile[FILENAME_MAX];
70 char slopefile[FILENAME_MAX], icfile[FILENAME_MAX];
71 int useIntegralCurve=0;
72 char *cptr, tmp[128];
73 int ret;
74
75
76
77 /*
78 * Get arguments
79 */
80 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
81 imgfile[0]=tacfile[0]=outfile[0]=slopefile[0]=icfile[0]=(char)0;
82 /* Get options */
83 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
84 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
85 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
86 if(strncasecmp(cptr, "SLOPE=", 6)==0) {
87 strlcpy(slopefile, cptr+6, FILENAME_MAX); continue;
88 } else if(strncasecmp(cptr, "IC=", 3)==0) {
89 strlcpy(icfile, cptr+3, FILENAME_MAX); continue;
90 } else if(strncasecmp(cptr, "INTEGRAL", 1)==0) {
91 useIntegralCurve=1; continue;
92 }
93 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
94 return(1);
95 } else break;
96
97 /* Print help or version? */
98 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
99 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
100 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
101
102 /* Process other arguments, starting from the first non-option */
103 if(ai<argc) {strlcpy(imgfile, argv[ai++], FILENAME_MAX);}
104 if(ai<argc) {strlcpy(tacfile, argv[ai++], FILENAME_MAX);}
105 if(ai<argc) {strlcpy(outfile, argv[ai++], FILENAME_MAX);}
106 if(ai<argc) {
107 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
108 return(1);
109 }
110
111 /* Did we get all the information that we need? */
112 if(!outfile[0]) {
113 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
114 return(1);
115 }
116
117 /* In verbose mode print arguments and options */
118 if(verbose>1) {
119 printf("imgfile := %s\n", imgfile);
120 printf("tacfile := %s\n", tacfile);
121 printf("outfile := %s\n", outfile);
122 if(slopefile[0]) printf("slopefile := %s\n", slopefile);
123 if(icfile[0]) printf("icfile := %s\n", icfile);
124 printf("useIntegralCurve := %d\n", useIntegralCurve);
125 fflush(stdout);
126 }
127 if(verbose>9) IMG_TEST=verbose-9; else IMG_TEST=0;
128
129
130 /*
131 * Read PET image and TAC
132 */
133 if(verbose>0) printf("reading data files\n");
134 IMG img; DFT tac; dftInit(&tac); imgInit(&img);
135 double scanlen=1.0E+020;
136 int frameNr=0;
138 imgfile, NULL, tacfile, NULL, NULL, &scanlen, &frameNr, &img,
139 NULL, &tac, 0, stdout, verbose-2, tmp);
140 if(ret!=0) {
141 fprintf(stderr, "Error: cannot read the data.\n");
142 fprintf(stderr, "%s.\n", tmp);
143 if(verbose>1) printf(" ret := %d\n", ret);
144 return(2);
145 }
146 if(verbose>1) {
147 printf("scanlen := %g\n", scanlen);
148 printf("frameNr := %d\n", frameNr);
149 }
150 /* Check that image is dynamic */
151 if(frameNr<3) {
152 fprintf(stderr, "Error: too few time frames.\n");
153 imgEmpty(&img); dftEmpty(&tac); return(2);
154 }
155 /* Check that TAC data contains just one TAC */
156 if(tac.voiNr>1) {
157 fprintf(stderr, "Warning: only the first TAC is used.\n");
158 }
159
160
161 /*
162 * Allocate memory for output image data
163 */
164 if(verbose>1) fprintf(stdout, "allocating memory\n");
165 IMG ccimg; imgInit(&ccimg);
166 IMG kimg; imgInit(&kimg);
167 IMG bimg; imgInit(&bimg);
168 ret=imgAllocateWithHeader(&ccimg, img.dimz, img.dimy, img.dimx, 1, &img);
169 if(ret) {
170 fprintf(stderr, "Error: cannot allocate memory for new image.\n");
171 imgEmpty(&img);
172 return(3);
173 }
174 /* Copy data from the original image */
175 if(verbose>2) fprintf(stdout, "copying data\n");
176 ccimg.start[0]=img.start[0];
177 ccimg.end[0]=img.end[img.dimt-1];
178 ccimg.mid[0]=0.5*(ccimg.start[0]+ccimg.end[0]);
179 /* Slope data */
180 if(slopefile[0]) {
181 ret=imgAllocateWithHeader(&kimg, img.dimz, img.dimy, img.dimx, 1, &img);
182 if(ret) {
183 fprintf(stderr, "Error: cannot allocate memory for new image.\n");
184 imgEmpty(&img); imgEmpty(&ccimg);
185 return(4);
186 }
187 /* Copy data from the original image */
188 if(verbose>2) fprintf(stdout, "copying data\n");
189 kimg.start[0]=img.start[0];
190 kimg.end[0]=img.end[img.dimt-1];
191 kimg.mid[0]=0.5*(kimg.start[0]+kimg.end[0]);
192 }
193 /* Intercept data */
194 if(icfile[0]) {
195 ret=imgAllocateWithHeader(&bimg, img.dimz, img.dimy, img.dimx, 1, &img);
196 if(ret) {
197 fprintf(stderr, "Error: cannot allocate memory for new image.\n");
198 imgEmpty(&img); imgEmpty(&ccimg); imgEmpty(&kimg);
199 return(4);
200 }
201 /* Copy data from the original image */
202 if(verbose>2) fprintf(stdout, "copying data\n");
203 bimg.start[0]=img.start[0];
204 bimg.end[0]=img.end[img.dimt-1];
205 bimg.mid[0]=0.5*(bimg.start[0]+bimg.end[0]);
206 }
207
208
209 /*
210 * Compute correlation coefficients
211 */
212 int zi, yi, xi, ti;
213 long long okNr=0;
214 double *x, y[img.dimt], k, kSD, b, bSD, r, ySD;
215 for(zi=0; zi<img.dimz; zi++) {
216 for(yi=0; yi<img.dimy; yi++) for(xi=0; xi<img.dimx; xi++) {
217 /* Set data for correlation calculation */
218 if(useIntegralCurve==0) {
219 x=tac.voi[0].y;
220 for(ti=0; ti<img.dimt; ti++) y[ti]=img.m[zi][yi][xi][ti];
221 } else {
222 x=tac.voi[0].y2;
223 float ytemp[img.dimt];
224 ret=fpetintegral(img.start, img.end, img.m[zi][yi][xi], img.dimt, ytemp, NULL);
225 if(ret==0) for(ti=0; ti<img.dimt; ti++) y[ti]=ytemp[ti];
226 else for(ti=0; ti<img.dimt; ti++) y[ti]=0.0;
227 }
228 ret=pearson3(x, y, img.dimt, &k, &kSD, &b, &bSD, &r, &ySD);
229 if(ret==0) {
230 okNr++;
231 } else {
232 k=kSD=b=bSD=r=ySD=0.0;
233 }
234 ccimg.m[zi][yi][xi][0]=(float)r;
235 if(slopefile[0]) {
236 if(k>1.0E+03) kimg.m[zi][yi][xi][0]=1.0E+03;
237 else if(k<-1.0E+03) kimg.m[zi][yi][xi][0]=-1.0E+03;
238 else kimg.m[zi][yi][xi][0]=(float)k;
239 }
240 if(icfile[0]) bimg.m[zi][yi][xi][0]=(float)b;
241 }
242 }
243 imgEmpty(&img);
244
245 /*
246 * Write output image data
247 */
248 if(verbose>1) fprintf(stdout, "writing correlation image...\n");
249 ret=imgWrite(outfile, &ccimg);
250 if(ret) {
251 fprintf(stderr, "Error: %s\n", ccimg.statmsg);
252 imgEmpty(&ccimg); imgEmpty(&kimg); imgEmpty(&bimg); return(11);
253 }
254 if(verbose>0) fprintf(stdout, "%s written.\n", outfile);
255 imgEmpty(&ccimg);
256 /* Slope data */
257 if(slopefile[0]) {
258 if(verbose>1) fprintf(stdout, "writing slope image...\n");
259 ret=imgWrite(slopefile, &kimg);
260 if(ret) {
261 fprintf(stderr, "Error: %s\n", kimg.statmsg);
262 imgEmpty(&kimg); imgEmpty(&bimg); return(11);
263 }
264 if(verbose>0) fprintf(stdout, "%s written.\n", slopefile);
265 imgEmpty(&kimg);
266 }
267 /* Intercept data */
268 if(icfile[0]) {
269 if(verbose>1) fprintf(stdout, "writing intercept image...\n");
270 ret=imgWrite(icfile, &bimg);
271 if(ret) {
272 fprintf(stderr, "Error: %s\n", bimg.statmsg);
273 imgEmpty(&bimg); return(11);
274 }
275 if(verbose>0) fprintf(stdout, "%s written.\n", icfile);
276 imgEmpty(&bimg);
277 }
278
279 return(0);
280}
281/*****************************************************************************/
282
283/*****************************************************************************/
void dftInit(DFT *data)
Definition dft.c:38
void dftEmpty(DFT *data)
Definition dft.c:20
int IMG_TEST
Definition img.c:6
int imgAllocateWithHeader(IMG *image, int planes, int rows, int columns, int frames, IMG *image_from)
Definition img.c:279
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int imgWrite(const char *fname, IMG *img)
Definition imgfile.c:136
int imgReadModelingData(char *petfile, char *siffile, char *inputfile1, char *inputfile2, char *inputfile3, double *fitdur, int *fitframeNr, IMG *img, DFT *inp, DFT *iinp, int verifypeak, FILE *loginfo, int verbose, char *status)
Definition imginput.c:24
int fpetintegral(float *x1, float *x2, float *y, int nr, float *ie, float *iie)
Integrate PET TAC data to frame mid times. Float version of petintegral().
Definition integr.c:838
Header file for libtpccurveio.
Header file for libtpcimgio.
Header file for libtpcimgp.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
Header file for libtpcmodel.
int pearson3(double *x, double *y, int nr, double *k, double *kSD, double *b, double *bSD, double *r, double *ySD)
Definition pearson.c:154
Header file for libtpcmodext.
Voi * voi
int voiNr
unsigned short int dimx
float **** m
unsigned short int dimt
float * start
unsigned short int dimz
unsigned short int dimy
float * end
const char * statmsg
float * mid
double * y2
double * y