00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifdef SPARKY
00014 #undef _POSIX_SOURCE
00015 #endif
00016 #include <sys/types.h>
00017
00018 #include <stdio.h>
00019 #include <math.h>
00020 #include <ctype.h>
00021 #include <sys/stat.h>
00022 #include <stdlib.h>
00023 #ifdef SYSV
00024 #include <sys/fcntl.h>
00025 #endif
00026
00027 #define min(a,b) ((a) < (b) ? (a) : (b))
00028 #define max(a,b) ((a) > (b) ? (a) : (b))
00029
00030 #define EPX1 64
00031 #define EPY1 64
00032 #define EPS1 (2*EPX1*EPY1)
00033 #define EPX2 128
00034 #define EPY2 128
00035 #define EPS2 (2*EPX2*EPY2)
00036
00037 #define NOISE_THR 10.
00038 #define SCALE 10000
00039 #define D_COEF 1.
00040 #define CORR ".CORR"
00041 #define IM_HEIGHT 256
00042 #define IM_ARR (IM_HEIGHT*IM_HEIGHT)
00043 #define OFFSET (28*IM_HEIGHT)
00044 #define H_SIZE (OFFSET+IM_ARR)
00045 #define IM_SIZE (2*H_SIZE)
00046
00047 #define SEQ_SIZE 1024
00048 #define NF_MAX 1050
00049 #define EXT_FILES 1
00050
00051 extern double strtod();
00052
00053 char *ProgName, *SQ_name, *f_name[NF_MAX], **ptr, *file_out;
00054 char *ort_name, file_CORR[256];
00055 int ORT_ar[EXT_FILES][SEQ_SIZE], ORT_Nr[EXT_FILES], idot, isq;
00056 float fORT[EXT_FILES][SEQ_SIZE], alpha, fdot, fsq, noise_thr;
00057 int SQ_Nr, SQ_arr[SEQ_SIZE], n_ort;
00058 int Im_frst = 0, npoints = 0, m_points;
00059 int N_im, fsize, isize, ar_size, itmp;
00060 short int *ar, tmp_ar[H_SIZE], corr_ar[H_SIZE];
00061 float fSQ[SEQ_SIZE], ft[SEQ_SIZE], fSQ1[SEQ_SIZE], dt[SEQ_SIZE];
00062 float fa, fb, fim[IM_ARR], amin, amax, ftmp, abmax, del_1, max_dev;
00063 float pct = 30., pcc, fSQ_vl, fll, coef = D_COEF, fzz;
00064 float fl_1ref, sq_1n, fcorr, max_cor, delr, next_cor = 0;;
00065 FILE *fp;
00066 int offs, normalize = 1, correl_im = 0, diff_im = 0;
00067 int n_ess = 0;
00068
00069
00070 main(argc,argv)
00071 int argc;
00072 char *argv[];
00073
00074 {
00075 int i, j, m;
00076
00077 ProgName = argv[0];
00078 if (argc < 2) Syntax();
00079
00080 noise_thr = NOISE_THR;
00081
00082 get_line_args(argc, argv);
00083
00084 if ( is_file(file_out) ) {
00085 fprintf(stderr, "\n !!! Output file: %s exist. !!!\n", file_out);
00086 Syntax();
00087 }
00088
00089
00090 for (i=0; i < IM_ARR; i++) fim[i] = 0.;
00091
00092 pcc = 1. - .01 * pct;
00093
00094 m_points = 0;
00095 idot = 0;
00096 for (j=0; j < N_im; j++) {
00097 if (SQ_arr[j] < 33333 ) {
00098 idot += SQ_arr[j];
00099 m_points++;
00100 }
00101 }
00102 if ( m_points == 0) {
00103 fprintf (stderr, "\n\n !!! Check reference file: %s !!!\n\n",SQ_name);
00104 exit(-1);
00105 }
00106 sq_1n = 1./sqrt((double) m_points);
00107
00108
00109 fa = (float) idot / (float) m_points;
00110 for (j=0; j < N_im; j++) {
00111 if ( SQ_arr[j] < 33333 )
00112 fSQ[j] = (float) SQ_arr[j] - fa;
00113 else
00114 fSQ[j] = 0.;
00115 }
00116
00117 for (m=0; m < n_ort; m++) {
00118 idot = 0;
00119 for (j=0; j < N_im; j++)
00120 if (SQ_arr[j] < 33333 ) idot += ORT_ar[m][j];
00121 fa = (float) idot / (float) m_points;
00122 for (j=0; j < N_im; j++) {
00123 if (SQ_arr[j] < 33333 )
00124 fORT[m][j] = (float) ORT_ar[m][j] - fa;
00125 else
00126 fORT[m][j] = 0;
00127 }
00128 }
00129
00130
00131
00132 for (m=0; m < n_ort; m++) {
00133 isq = 0;
00134 idot = 0;
00135 for (j=0; j < N_im; j++) {
00136 if (SQ_arr[j] < 33333 ) {
00137 fdot += fSQ[j]*fORT[m][j];
00138 fsq += fORT[m][j]*fORT[m][j];
00139 }
00140 }
00141 if ( fsq == 0. ) {
00142 fprintf (stderr, "\n\n !!! Orthogonal file #%d has zeros only !!!\n",
00143 m+1);
00144 exit(-3);
00145 }
00146 alpha = fdot / fsq;
00147 for (j=0; j < N_im; j++)
00148 if (SQ_arr[j] < 33333 ) fSQ[j] -= alpha * fORT[m][j];
00149 }
00150
00151
00152
00153 fSQ_vl = 0.;
00154 for (j=0; j < N_im; j++) fSQ_vl += fSQ[j] * fSQ[j];
00155 if (fSQ_vl < 1.e-10) {
00156 fprintf (stderr, "\n\n !!! Too small variartion in reference file !!!\n\n");
00157 exit(-1);
00158 }
00159 fl_1ref = 1./sqrt((double) fSQ_vl);
00160 for (j=0; j < N_im; j++) fSQ1[j] = fSQ[j] * fl_1ref;
00161
00162 amin = 1.e37;
00163 amax = -1.e37;
00164 for (j=0; j < N_im; j++) {
00165 if ( fSQ1[j] > amax ) amax = fSQ1[j];
00166 if ( fSQ1[j] < amin ) amin = fSQ1[j];
00167 }
00168 del_1 = amax - amin;
00169
00170
00171 max_dev = -1.e37;
00172 max_cor = 0.;
00173
00174
00175 for (i=0; i < ar_size; i++) {
00176
00177
00178 ftmp = 0;
00179 for (j=0; j < N_im; j++) {
00180 if ( SQ_arr[j] < 33333 ) {
00181 dt[j] = ar[j*ar_size+i];
00182 ftmp += dt[j];
00183 }
00184 }
00185 fa = ftmp /(float) m_points;
00186
00187 if ( ftmp < 0 ) diff_im = 1;
00188
00189 for (j=0; j < N_im; j++) {
00190 if ( SQ_arr[j] < 33333 )
00191 dt[j] -= fa;
00192 else
00193 dt[j] = 0.;
00194 }
00195
00196
00197 for (m=0; m < n_ort; m++) {
00198 fdot = 0;
00199 for (j=0; j < N_im; j++)
00200 if ( SQ_arr[j] < 33333 ) fdot += dt[j] * fORT[m][j];
00201 alpha = fdot / fsq;
00202 for (j=0; j < N_im; j++)
00203 if (SQ_arr[j] < 33333 ) dt[j] -= alpha * fORT[m][j];
00204 }
00205
00206
00207 fSQ_vl = 0.;
00208 for (j=0; j < N_im; j++)
00209 fSQ_vl += dt[j] * dt[j];
00210
00211 if (fSQ_vl < 1.e-10)
00212 for (j=0; j < N_im; j++) ft[j] = 0.;
00213 else {
00214 fll = 1./sqrt(fSQ_vl);
00215 for (j=0; j < N_im; j++)
00216 ft[j] = dt[j] * fll;
00217 }
00218
00219
00220 fcorr = 0.;
00221 for (j=0; j < N_im; j++) fcorr += ft[j] * fSQ1[j];
00222
00223
00224 corr_ar[i+offs] = fcorr * (float) SCALE;
00225
00226
00227
00228 if ( (fb = fabs((double) fcorr)) > pcc ) {
00229
00230 for (j=0; j < N_im; j++) fim[i] += dt[j] * fSQ1[j];
00231
00232 if ( fa > noise_thr ) delr = del_1 * fim[i] / fa;
00233 else delr = 0.;
00234
00235 if ( delr > max_dev ) max_dev = delr;
00236 if ( fb > max_cor ) {
00237 next_cor = max_cor;
00238 max_cor = fb;
00239 }
00240 fim[i] *= fl_1ref;
00241 n_ess++;
00242 }
00243 }
00244
00245
00246
00247 amin = 1.e37;
00248 amax = -1.e37;
00249 for (i=0; i < ar_size; i++) {
00250 if ( fim[i] > amax ) amax = fim[i];
00251 if ( fim[i] < amin ) amin = fim[i];
00252 }
00253 fa = fabs((double) amax);
00254 fb = fabs((double) amin);
00255 abmax = max(fa, fb);
00256 if (abmax < 1.e-10) {
00257 fprintf (stderr, "\n !!! Check you reference and data files !!!");
00258 fprintf (stderr, "\n !!! or increase value of -pcnt option. !!!");
00259 fprintf (stderr, "\n !!! No variation in functional image. !!!");
00260 fprintf (stderr, "\n !!! No output file created. !!!\n\n");
00261 exit(-2);
00262 }
00263
00264 if ( normalize ) fa = (float) SCALE / abmax;
00265 else fa = coef;
00266
00267 printf ("\n Output file: %s\n", file_out);
00268 printf (" Image data (relative to reference function):\n");
00269 printf (" max value : %g , scaled : %g\n", amax, amax*fa);
00270 printf (" min value : %g , scaled : %g\n", amin, amin*fa);
00271 printf (" max variation : %g %%\n", max_dev*100.);
00272 printf (" the best correlation: %g %%\n", max_cor*100.);
00273 printf (" next to the best : %g %%\n", next_cor*100.);
00274 printf (" number of essential pixels: %d\n", n_ess);
00275
00276 for (i=0; i < ar_size; i++) {
00277 fzz = fim[i]*fa;
00278 if ( fzz >= 0. ) tmp_ar[i+offs] = min((int) fzz, SCALE);
00279 if ( fzz < 0. ) tmp_ar[i+offs] = max((int) fzz, -SCALE);
00280 }
00281
00282
00283 for (i=0; i < offs; i++) corr_ar[i] = tmp_ar[i];
00284
00285 corr_ar[0+offs] = 0;
00286 corr_ar[1+offs] = -SCALE;
00287 corr_ar[2+offs] = SCALE;
00288
00289 tmp_ar[0+offs] = 0;
00290 tmp_ar[1+offs] = -SCALE;
00291 tmp_ar[2+offs] = SCALE;
00292
00293
00294 if (i = write_iqm(file_out, &fsize, tmp_ar) )
00295 fprintf (stderr, "\n\n Error writing output_file: %s (will not overwrite) %d\n\n", file_out, i);
00296
00297 if (correl_im) {
00298 strcpy(file_CORR, file_out);
00299 strcat(file_CORR, CORR);
00300 if (i = write_iqm(file_CORR, &fsize, corr_ar) )
00301 fprintf (stderr, "\n\n Error writing output_file: %s (will not overwrite) %d\n\n", file_CORR, i);
00302 }
00303 exit(0) ;
00304 }
00305
00306
00307 get_line_args(argc, argv)
00308 int argc;
00309 char *argv[];
00310
00311 {
00312 register int i, j, k, nopt, nnn;
00313 int sp;
00314 float ff;
00315
00316 n_ort = 0;
00317 nopt = 0;
00318 for (i = 1; i < argc; i++) {
00319 if (!strncmp(argv[i], "-h", 2)) {
00320 Syntax();
00321 }
00322 if (strncmp(argv[i], "-non", 4) == 0) {
00323 normalize = 0;
00324 nopt++;
00325 continue;
00326 }
00327 if (strncmp(argv[i], "-corr", 4) == 0) {
00328 correl_im = 1;
00329 nopt++;
00330 continue;
00331 }
00332 if (strncmp(argv [i], "-coe", 4) == 0) {
00333 if (++i >= argc) { Syntax(); exit(2); }
00334 ptr = argv;
00335 coef = strtod(argv[i], ptr);
00336 if ( **ptr ) {
00337 fprintf (stderr, "\n !!! Wrong value in option -coef: %g !!!\n\n",
00338 coef);
00339 exit(1);
00340 }
00341 nopt++; nopt++;
00342 continue;
00343 }
00344 if (strncmp(argv [i], "-im1", 4) == 0) {
00345 if (++i >= argc) { Syntax(); exit(2); }
00346 ptr = argv;
00347 Im_frst = strtod(argv[i], ptr) + .5;
00348 if ( **ptr || (Im_frst < 1)) {
00349 fprintf (stderr, "\n !!! First_image_# < 1 in -im1 !!!\n\n");
00350 exit(1);
00351 }
00352 nopt++; nopt++;
00353 continue;
00354 }
00355 if (strncmp(argv [i], "-ort", 4) == 0) {
00356 if (++i >= argc) { Syntax(); exit(2); }
00357 ort_name = argv[i];
00358 if ((fp = fopen(ort_name, "r")) == 0) {
00359 fprintf(stderr,"\n !!! Problem opening ort file: %s !!!\n",
00360 ort_name);
00361 Syntax();
00362 }
00363 for(k=0; k < SEQ_SIZE; k++)
00364 if (fscanf(fp, "%d", &ORT_ar[n_ort][k]) == EOF) break;
00365 ORT_Nr[n_ort] = k;
00366 if ( ORT_Nr[n_ort] == 0 ) {
00367 fprintf(stderr,"\n !!! Problem reading %s file !!!\n", ort_name);
00368 Syntax();
00369 }
00370 fclose(fp);
00371 n_ort++;
00372 nopt++; nopt++;
00373 continue;
00374 }
00375 if (strncmp(argv [i], "-num", 4) == 0) {
00376 if (++i >= argc) { Syntax(); exit(2); }
00377 ptr = argv;
00378 npoints = strtod(argv[i], ptr) + .5;
00379 if ( **ptr || (npoints < 1)) {
00380 fprintf (stderr, "\n !!! Too few images specified !!!\n\n");
00381 exit(1);
00382 }
00383 nopt++; nopt++;
00384 continue;
00385 }
00386 if (strncmp(argv [i], "-pcnt", 5) == 0) {
00387 if (++i >= argc) { Syntax(); exit(2); }
00388 ptr = argv;
00389 ff = strtod(argv[i], ptr);
00390 if ( **ptr || (ff < .0) || (ff > 100.)) {
00391 fprintf (stderr, "\n !!! %% accuracy in -pcnt out of range: %g !!!\n\n", ff);
00392 exit(1);
00393 }
00394 pct = ff;
00395 nopt++; nopt++;
00396 continue;
00397 }
00398 if (strncmp(argv [i], "-list", 5) == 0) {
00399 if (++i >= argc) { Syntax(); exit(2); }
00400 ptr = argv;
00401 ff = strtod(argv[i], ptr);
00402 if ( **ptr || (ff < .0) ) {
00403 fprintf (stderr, "\n !!! min_value in -list < 0 : %g !!!\n\n", ff);
00404 exit(1);
00405 }
00406 noise_thr = ff;
00407 nopt++; nopt++;
00408 continue;
00409 }
00410 }
00411 nopt++;
00412
00413 SQ_name = argv[nopt];
00414
00415 if ((fp = fopen(SQ_name, "r")) == 0) {
00416 fprintf(stderr,"\n !!! Problem opening sequence file: %s !!!\n", SQ_name);
00417 Syntax();
00418 }
00419 for(k=0; k < SEQ_SIZE; k++)
00420 if (fscanf(fp, "%d", &SQ_arr[k]) == EOF) break;
00421 SQ_Nr = k;
00422 if (SQ_Nr == 0) {
00423 fprintf(stderr,"\n !!! Problem reading %s file !!!\n", SQ_name);
00424 Syntax();
00425 }
00426 fclose(fp);
00427
00428 nopt++;
00429
00430 if ( nopt > (argc-1) || nopt < (argc - NF_MAX) ) {
00431 fprintf (stderr, "\n Wrong # of files. %d files entered :\n", argc-nopt);
00432 for(i=nopt, j=1; i < argc; i++, j++)
00433 fprintf (stderr, " %3d - %s\n", j, argv[i]);
00434 Syntax(); exit(2);
00435 }
00436
00437 file_out = argv[argc-1];
00438
00439 N_im = argc-nopt-1;
00440 if ( Im_frst > N_im ) {
00441 fprintf (stderr, "\n !!! First_im_# %d in -im1 is bigger then number of images (%d) !!!\n\n", Im_frst, N_im);
00442 Syntax(); exit(2);
00443 }
00444 for (i=0; i < N_im; i++) f_name[i] = argv[i+nopt];
00445 for (i=0; i < Im_frst-1; i++) f_name[i] = f_name[Im_frst-1];
00446
00447
00448 isize = 0;
00449 fprintf(stderr, "\n\n Reading file %s\n", f_name[0]);
00450 if (k = read_iqm(f_name[0], &isize, tmp_ar)) {
00451 fprintf (stderr, "\n Problem with file: %s\n", f_name[0]);
00452 Syntax(); exit(2);
00453 }
00454 if ( (isize != EPS1) && (isize != EPS2) && (isize != IM_SIZE) ) {
00455 fprintf (stderr, "\n\n !!! File %s has wrong format !!!\n", f_name[0]);
00456 Syntax(); exit(2);
00457 }
00458
00459 fsize = isize;
00460 if (fsize == IM_SIZE) {
00461 ar_size = IM_ARR;
00462 offs = OFFSET;
00463 }
00464 else {
00465 ar_size = fsize/2;
00466 offs = 0;
00467 }
00468
00469 ar = (short int *) malloc((unsigned) ((ar_size*N_im)*sizeof(short int)));
00470
00471 for (i=0; i < ar_size; i++) ar[i] = tmp_ar[offs+i];
00472
00473 for (i=1; i < N_im; i++) {
00474 fprintf(stderr, " Reading file %s\n", f_name[i]);
00475 isize = 0;
00476 if ( k = read_iqm(f_name[i], &isize, tmp_ar) ) {
00477 fprintf (stderr, "\n Problem with file: %s\n", f_name[i]);
00478 Syntax(); exit(2);
00479 }
00480 if ( isize != fsize) {
00481 fprintf (stderr, "\n\n !!! File %s has different format !!!\n",
00482 f_name[i]);
00483 Syntax(); exit(2);
00484 }
00485 k = i*ar_size;
00486 for (j=0; j < ar_size; j++) ar[k+j] = tmp_ar[offs+j];
00487 }
00488 if (!npoints || npoints > N_im) npoints = N_im;
00489 for (i=0; i < n_ort; i++) {
00490 if ( ORT_Nr[i] < N_im ) {
00491 fprintf(stderr,"\n !!! Orthogonal file # %d too short !!!\n", i);
00492 exit(-4);
00493 }
00494 }
00495 if ( SQ_Nr < N_im ) {
00496 fprintf(stderr, "\n\n !!! Reference file to short. Add %d lines !!!\n\n",
00497 N_im - SQ_Nr);
00498 exit(-1);
00499 }
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513 int read_iqm(fname,size,arr)
00514 int *size;
00515 char fname[],arr[];
00516
00517 {
00518 int isize = *size;
00519 int fp;
00520 struct stat file_stat;
00521
00522 if ((fp = open(fname, O_RDONLY)) <= 0)
00523 return(1);
00524
00525 fstat(fp, &file_stat);
00526
00527 if(file_stat.st_size > isize && isize)
00528 return(2);
00529
00530 *size = file_stat.st_size;
00531
00532 read(fp, arr, file_stat.st_size);
00533 close(fp);
00534 return(0);
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 int write_iqm(fname,size,arr)
00548 int *size;
00549 char fname[],arr[];
00550
00551 {
00552 int isize = *size;
00553 int fp;
00554
00555 if(isize < 0)
00556 return(2);
00557
00558 if ((fp = open(fname, O_WRONLY|O_CREAT|O_EXCL,0444)) <= 0)
00559 return(1);
00560
00561
00562 write(fp, arr, isize);
00563 close(fp);
00564 return(0);
00565 }
00566
00567
00568 int is_file(fname)
00569 char *fname;
00570
00571 {
00572 FILE *fp;
00573
00574 if ( (fp = fopen(fname, "r")) != NULL ) {
00575 fclose(fp);
00576 return(1);
00577 }
00578 else
00579 return(0);
00580 }
00581
00582
00583 Syntax()
00584
00585 {
00586 fprintf (stderr, "\n\n %s makes functional image from time series of images and", ProgName);
00587 fprintf (stderr, "\n reference (formated) functional-sequence file.");
00588 fprintf (stderr, "\n Reference function is normalized before any correlation is done, so");
00589 fprintf (stderr, "\n the results are proportional to the amplitude of functional changes.");
00590 fprintf (stderr, "\n Additionally, output values are scaled to be independent of the number of");
00591 fprintf (stderr, "\n used points in the reference functional-sequence file. Output data can");
00592 fprintf (stderr, "\n be orthogonalized to additional external function using option -ort.");
00593 fprintf (stderr, "\n Maximum number of external functios: %d", EXT_FILES);
00594 fprintf (stderr, "\n Image resolusion: up to 256x256\n");
00595 fprintf (stderr, "\n Usage: %s [options] func_seq_file file_1 [file_2 ... file_n] out_file\n", ProgName);
00596 fprintf (stderr, "\n Where options are:\n");
00597 fprintf (stderr, "\n -non - don't make normalization (def. normalize to %d)", SCALE);
00598 fprintf (stderr, "\n -coef value - coefficient for output data (def. = %g)", D_COEF);
00599 fprintf (stderr, "\n -im1 image_# - first image in time course. Previous images will be ");
00600 fprintf (stderr, "\n filled with this one for proper timing of others.");
00601 fprintf (stderr, "\n -num #_of_images - # of images in time course [2-%d].", NF_MAX);
00602 fprintf (stderr, "\n -pcnt # - accuracy in %% of functional fit (def. = %g%%).", pct);
00603 fprintf (stderr, "\n This value applies to the shape (not amplitude)");
00604 fprintf (stderr, "\n and 30%% means that all data having correlation");
00605 fprintf (stderr, "\n coefficient greater than .7 will be included.");
00606 fprintf (stderr, "\n -ort file - additional function files to which data will be");
00607 fprintf (stderr, "\n orthogonalized. Use one file with each option -ort .");
00608 fprintf (stderr, "\n Maximum number of options -ort: %d .", EXT_FILES);
00609 fprintf (stderr, "\n -corr - make image of correlation coefficients. Range -1 to 1");
00610 fprintf (stderr, "\n is scaled to -/+ %d. The program will create second", SCALE);
00611 fprintf (stderr, "\n output file with extention %s .", CORR);
00612 fprintf (stderr, "\n -list min_value - make report for pixels brighter than min_value.");
00613 fprintf (stderr, "\n Default min_value: %g", NOISE_THR);
00614 fprintf (stderr, "\n\n Reference functional-sequence file can contain zero in each line for image");
00615 fprintf (stderr, "\n with no action and value one for action. More complicated functions are OK.");
00616 fprintf (stderr, "\n All values should be in the range -32768 to 32767. Any image can");
00617 fprintf (stderr, "\n be discarded from computation by placing value 33,000 or more");
00618 fprintf (stderr, "\n in equivalent line. Plot file of center pixel, made by program FD (using <p>)");
00619 fprintf (stderr, "\n can be used as reference file too. AJ.");
00620 fprintf (stderr, "\n\n");
00621 exit(1);
00622 }