Using stdlib.h rather than malloc.h
[snark14.git] / src / snark / analyze.c
1 /*
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) $
7  $Author: bprommegger $
8  ***********************************************************
9
10  Previously part of SuperSNARK
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <strings.h>
17 #include "experimenter.h"
18 #include "getfom.h"
19 #include "stru_acc.h"
20 #include "hit_ratio.h"
21 #include "imagewise_roi.h"
22 #include "pointwse.h"
23 #include "fom_klds.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"
30 #include "getiters.h"
31 #include "errorc.h"
32 #include "analyze.h"
33
34 void printout(FILE* fomfl, char* fom, int* itr1, int* itr2, int niters, char* keywrd1, char* keywrd2, double* fom1, double* fom2);
35
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).
40  *
41  * INPUTS:
42  * exp - experiment number.
43  * run - run number.
44  * numpairs - for PAIRed structures: number of pairs of structures.
45  * pairs - array of length 2*numpairs containing the indices of all paired
46  * structures.
47  * anaflname - name of file containing comparisons to be made.
48  * fomfl - file pointer to the file which stores foms.
49  *
50  * OUTPUTS:
51  * none.
52  * ------------------------------------------------------------------------*/
53
54 void analyze(int exp, int run, int numpairs, int* pairs, char* anaflname, FILE* fomfl)
55 {
56         int *itr1, *itr2, niters, stru, poin, hitr, usr1, usr2, usr3, usr4, usr5, iroi, klds, wsqd, num_mode = 1, compare = 0, mode_flag = 0;
57
58         char string[MAXLINESIZE], key1[50], line[MAXLINESIZE], key2[50], keywrd1[50], keywrd2[50];
59
60         FILE *anafl;
61
62         strcpy(line, "In analyze.c: error in file");
63         sprintf(line, "%s %s at line ", line, anaflname);
64
65         if ((anafl = fopen(anaflname, "r")) == NULL)
66         {
67                 errorc("in analyze.c: error in opening file ", anaflname);
68         }
69
70         fgets(string, sizeof(string), anafl); /* don't need first line */
71         while (isSkip(string))     //jump over comment lines and blank lines
72         {
73                 fgets(string, sizeof(string), anafl);
74         }
75
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
78         {
79                 fgets(string, sizeof(string), anafl);
80         }
81
82         if (strncasecmp(string, "mode", 4) == 0)
83         { /* set mode_flag to 1 if */
84                 mode_flag = 1; /* second line is MODE */
85         }
86         else if (strncasecmp(string, "comp", 4) == 0)
87         {
88                 ;
89         }
90         else
91         {
92                 errorc(line, string);
93         }
94
95         if (mode_flag == 1)
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
99                 {
100                         fgets(string, sizeof(string), anafl);
101                 }
102         }
103
104         if (strncasecmp(string, "comp", 4) != 0)
105         {
106                 errorc(line, string);
107         }
108
109         /* at this point string contains the COMPARE command */
110
111         while (strncasecmp(string, "end", 3) != 0)
112         {
113                 /* beginning of while loop */
114
115                 if (strncasecmp(string, "comp", 4) == 0)
116                 {
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. */
123
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);
128                         keywrd1[4] = '\0';
129                         keywrd2[4] = '\0';
130                         getfom(string, &stru, &poin, &hitr, &usr1, &usr2, &usr3, &usr4, &usr5, &iroi, &klds, &wsqd);
131
132                         getiters(anafl, &itr1, &itr2, &niters, string);
133
134                         fprintf(fomfl, " \n experiment= %d run= %d compare= %d\n", exp, run,
135                                         compare);
136
137                         computefom(itr1, itr2, niters, numpairs, pairs, num_mode, keywrd1, keywrd2, stru, poin, hitr, usr1, usr2, usr3, usr4, usr5,     iroi, klds, wsqd, fomfl);
138
139                         free(itr1);
140                         free(itr2);
141                 }
142                 if (strncasecmp(string, "mode", 4) == 0)
143                 {
144                         /* If string just read in has the MODE command then increment
145                          mode counter and read the next string (which has the COMPARE
146                          command). */
147
148                         num_mode++;
149                         fgets(string, sizeof(string), anafl);
150                         while (isSkip(string))     //jump over comment lines and blank lines
151                         {
152                                 fgets(string, sizeof(string), anafl);
153                         }
154                 }
155         } /* end of while loop */
156
157         fclose(anafl);
158 }
159
160 /*
161  * This function coordinates the calculation and storage of the FOMs.
162  *
163  * INPUTS:
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
171  * structures.
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.
180  *
181  * OUTPUTS:
182  * none
183  */
184
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)
186 {
187         double *fom1, *fom2;
188
189         /* allocate memory for the FOMs */
190         fom1 = (double *) malloc(niters * sizeof(double));
191         fom2 = (double *) malloc(niters * sizeof(double));
192
193         if ((fom1 == NULL) || (fom2 == NULL))
194         {
195                 errorc("in analyze.c: memory allocation failed in ", "computefom");
196         }
197
198         /* Depending on the values of the FOM flags compute and print the
199          corresponding FOM */
200
201         if (stru == 1)
202         {
203                 stru_acc(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
204                 printout(fomfl, "Structural Accuracy", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
205         }
206
207         if (poin == 1)
208         {
209                 pointwse(num_mode, itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
210                 printout(fomfl, "Point-wise Accuracy", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
211         }
212
213         if (usr1 == 1)
214         {
215                 user_fom1(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
216                 printout(fomfl, "User-defined-1   ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
217         }
218
219         if (usr2 == 1)
220         {
221                 user_fom2(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
222                 printout(fomfl, "User-defined-2   ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
223         }
224
225         if (usr3 == 1)
226         {
227                 user_fom3(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
228                 printout(fomfl, "User-defined-3   ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
229         }
230
231         if (usr4 == 1)
232         {
233                 user_fom4(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
234                 printout(fomfl, "User-defined-4   ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
235         }
236
237         if (usr5 == 1)
238         {
239                 user_fom5(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
240                 printout(fomfl, "User-defined-5   ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
241         }
242
243         if (hitr == 1)
244         { /* hit-ratio is valid only if there
245          * exists paired structures */
246
247                 if (numpairs != 0)
248                 {
249                         hit_ratio(itr1, itr2, niters, pairs, numpairs, keywrd1, keywrd2, fom1, fom2);
250
251                         printout(fomfl, "Hit-ratio       ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
252                 }
253                 else
254                 {
255                         fprintf(fomfl, "\n    %s \t \t \t    %s\n", keywrd1, keywrd2);
256                         fprintf(fomfl,
257                                         "***** No paired structure in phantom for this run\n");
258                 }
259         }
260         if (iroi == 1)
261         {
262                 /* imagewise_roi is valid only if the number of paired structures is >= 2*/
263                 if (numpairs >= 2)
264                 {
265                         imagewise_roi(itr1, itr2, niters, pairs, numpairs, keywrd1, keywrd2,
266                                         fom1, fom2);
267                         printout(fomfl, "Imagewise-ROI       ", itr1, itr2, niters, keywrd1,
268                                         keywrd2, fom1, fom2);
269                 }
270                 else
271                 {
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");
277                 }
278         }
279         if (klds == 1)
280         {
281                 fom_klds(itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
282                 printout(fomfl, "Kullback-Leibler distance   ", itr1, itr2, niters, keywrd1, keywrd2, fom1, fom2);
283         }
284         if (wsqd == 1)
285         {
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);
288         }
289
290
291         free(fom1);
292         free(fom2);
293 }
294
295 /*
296  * This function prints out the computed FOMs
297  *
298  * INPUTS:
299  * see the above routine.
300  *
301  * OUTPUTS:
302  * none
303  */
304
305 void printout(FILE* fomfl, char* fom, int* itr1, int* itr2, int niters, char* keywrd1, char* keywrd2, double* fom1, double* fom2)
306 {
307         fprintf(fomfl, "\n    %s \t\t\t\t    %s\n iter \t %s \t\t iter \t %s\n", keywrd1, keywrd2, fom, fom);
308
309         int i = 0;
310         for (i = 0; i < niters; i++)
311         {
312                 fprintf(fomfl, " %d \t %.25f\t %d\t %.25f\n", itr1[i], fom1[i], itr2[i], fom2[i]);
313         }
314 }