2 ***********************************************************
3 $SNARK_Header: S N A R K 1 4 - A PICTURE RECONSTRUCTION PROGRAM $
4 $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/analyze.c $
5 $LastChangedRevision: 137 $
6 $Date: 2014-07-15 16:38:33 -0400 (Tue, 15 Jul 2014) $
8 ***********************************************************
10 Previously part of SuperSNARK
17 #include "experimenter.h"
20 #include "hit_ratio.h"
21 #include "imagewise_roi.h"
24 #include "fom_weighted_squared_distance.h"
25 #include "user_fom1.h"
26 #include "user_fom2.h"
27 #include "user_fom3.h"
28 #include "user_fom4.h"
29 #include "user_fom5.h"
34 void printout(FILE* fomfl, char* fom, int* itr1, int* itr2, int niters, char* keywrd1, char* keywrd2, double* fom1, double* fom2);
36 /*--------------------------- analyze.c ---------------------------
37 * This function reads the information in the file containing the comparisons
38 * to be made and coordinates the calculation and storage of the appropriate
39 * figures of merit (FOM).
42 * exp - experiment number.
44 * numpairs - for PAIRed structures: number of pairs of structures.
45 * pairs - array of length 2*numpairs containing the indices of all paired
47 * anaflname - name of file containing comparisons to be made.
48 * fomfl - file pointer to the file which stores foms.
52 * ------------------------------------------------------------------------*/
54 void analyze(int exp, int run, int numpairs, int* pairs, char* anaflname, FILE* fomfl)
56 int *itr1, *itr2, niters, stru, poin, hitr, usr1, usr2, usr3, usr4, usr5, iroi, klds, wsqd, num_mode = 1, compare = 0, mode_flag = 0;
58 char string[MAXLINESIZE], key1[50], line[MAXLINESIZE], key2[50], keywrd1[50], keywrd2[50];
62 strcpy(line, "In analyze.c: error in file");
63 sprintf(line, "%s %s at line ", line, anaflname);
65 if ((anafl = fopen(anaflname, "r")) == NULL)
67 errorc("in analyze.c: error in opening file ", anaflname);
70 fgets(string, sizeof(string), anafl); /* don't need first line */
71 while (isSkip(string)) //jump over comment lines and blank lines
73 fgets(string, sizeof(string), anafl);
76 fgets(string, sizeof(string), anafl); /* second line is either a MODE * command or a COMPARE command */
77 while (isSkip(string)) //jump over comment lines and blank lines
79 fgets(string, sizeof(string), anafl);
82 if (strncasecmp(string, "mode", 4) == 0)
83 { /* set mode_flag to 1 if */
84 mode_flag = 1; /* second line is MODE */
86 else if (strncasecmp(string, "comp", 4) == 0)
96 { /* if second line is MODE */
97 fgets(string, sizeof(string), anafl); /* third line is COMPARE */
98 while (isSkip(string)) //jump over comment lines and blank lines
100 fgets(string, sizeof(string), anafl);
104 if (strncasecmp(string, "comp", 4) != 0)
106 errorc(line, string);
109 /* at this point string contains the COMPARE command */
111 while (strncasecmp(string, "end", 3) != 0)
113 /* beginning of while loop */
115 if (strncasecmp(string, "comp", 4) == 0)
117 /* If the string just read in has the COMPARE command:
118 (1) increment compare counter and read in the two keywords.
119 (2) get flags for the figures of merit (FOMs)
120 (3) get the iteration numbers to be compared
121 (4) compute the FOMs for these iteration numbers.
122 (5) free arrays for next set of iteration numbers. */
124 compare++; /* keeps count of number of COMPAREs read in */
125 sscanf(string, "%*s%s %s", key1, key2);
126 strncpy(keywrd1, key1, 4);
127 strncpy(keywrd2, key2, 4);
130 getfom(string, &stru, &poin, &hitr, &usr1, &usr2, &usr3, &usr4, &usr5, &iroi, &klds, &wsqd);
132 getiters(anafl, &itr1, &itr2, &niters, string);
134 fprintf(fomfl, " \n experiment= %d run= %d compare= %d\n", exp, run,
137 computefom(itr1, itr2, niters, numpairs, pairs, num_mode, keywrd1, keywrd2, stru, poin, hitr, usr1, usr2, usr3, usr4, usr5, iroi, klds, wsqd, fomfl);
142 if (strncasecmp(string, "mode", 4) == 0)
144 /* If string just read in has the MODE command then increment
145 mode counter and read the next string (which has the COMPARE
149 fgets(string, sizeof(string), anafl);
150 while (isSkip(string)) //jump over comment lines and blank lines
152 fgets(string, sizeof(string), anafl);
155 } /* end of while loop */
161 * This function coordinates the calculation and storage of the FOMs.
164 * itr1 - array of length niters containing the iteration numbers to be
165 * compared for the first algorithm.
166 * itr2 - array of length niters containing the iteration numbers to be
167 * compared for the second algorithm.
168 * niters - number of iterations to be compared.
169 * numpairs - for PAIRed structures: number of pairs of structures
170 * pairs - array of length 2*numpairs containing the indices of all paired
172 * num_mode - number of MODE commands read in so far.
173 * keywrd1 - keyword which defines the first algorithm.
174 * keywrd2 - keyword which defines the second algorithm.
175 * stru - equals 1 if structural accuracy is to be computed, else equals 0.
176 * poin - equals 1 if pointwise accuracy is to be computed, else equals 0.
177 * hitr - equals 1 if hit-ratio is to be computed, else equals 0.
178 * user - equals 1 if user-defined FOM is to be computed, else equals 0.
179 * fomfl - file pointer to the file which stores the FOMs.
185 void computefom(int* itr1, int* itr2, int niters, int numpairs, int* pairs, int num_mode, char* keywrd1, char* keywrd2, int stru, int poin, int hitr, int usr1, int usr2, int usr3, int usr4, int usr5, int iroi, int klds, int wsqd, FILE* fomfl)
189 /* allocate memory for the FOMs */
190 fom1 = (double *) malloc(niters * sizeof(double));
191 fom2 = (double *) malloc(niters * sizeof(double));
193 if ((fom1 == NULL) || (fom2 == NULL))
195 errorc("in analyze.c: memory allocation failed in ", "computefom");
198 /* Depending on the values of the FOM flags compute and print the
203 stru_acc(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
204 printout(fomfl, "Structural Accuracy", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
209 pointwse(num_mode, itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
210 printout(fomfl, "Point-wise Accuracy", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
215 user_fom1(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
216 printout(fomfl, "User-defined-1 ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
221 user_fom2(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
222 printout(fomfl, "User-defined-2 ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
227 user_fom3(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
228 printout(fomfl, "User-defined-3 ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
233 user_fom4(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
234 printout(fomfl, "User-defined-4 ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
239 user_fom5(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
240 printout(fomfl, "User-defined-5 ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
244 { /* hit-ratio is valid only if there
245 * exists paired structures */
249 hit_ratio(itr1, itr2, niters, pairs, numpairs, keywrd1, keywrd2, fom1, fom2);
251 printout(fomfl, "Hit-ratio ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
255 fprintf(fomfl, "\n %s \t \t \t %s\n", keywrd1, keywrd2);
257 "***** No paired structure in phantom for this run\n");
262 /* imagewise_roi is valid only if the number of paired structures is >= 2*/
265 imagewise_roi(itr1, itr2, niters, pairs, numpairs, keywrd1, keywrd2,
267 printout(fomfl, "Imagewise-ROI ", itr1, itr2, niters, keywrd1,
268 keywrd2, fom1, fom2);
272 fprintf(fomfl, "\n %s \t \t \t %s\n", keywrd1, keywrd2);
273 fprintf(fomfl, "***** Not enough paired structure "
274 "in phantom for this run\n");
275 fprintf(fomfl, "******** The minimum number of paired structures "
276 "should be >= 2 \n");
281 fom_klds(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
282 printout(fomfl, "Kullback-Leibler distance ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
286 fom_weighted_squared_distance(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
287 printout(fomfl, "Weighted squared distance ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
296 * This function prints out the computed FOMs
299 * see the above routine.
305 void printout(FILE* fomfl, char* fom, int* itr1, int* itr2, int niters, char* keywrd1, char* keywrd2, double* fom1, double* fom2)
307 fprintf(fomfl, "\n %s \t\t\t\t %s\n iter \t %s \t\t iter \t %s\n", keywrd1, keywrd2, fom, fom);
310 for (i = 0; i < niters; i++)
312 fprintf(fomfl, " %d \t %.25f\t %d\t %.25f\n", itr1[i], fom1[i], itr2[i], fom2[i]);