64b47aec4775183a3eb1002a5e757cfa1ba64138
[snark14.git] / src / snark / analyze2.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/analyze2.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 <malloc.h>
14 #include <string.h>
15 #include <strings.h>
16 #include <stdio.h>
17 #include "experimenter.h"
18 #include "getfom.h"
19 #include "getiters.h"
20 #include "errorc.h"
21 #include "signif1.h"
22 #include "analyze2.h"
23
24 /*
25  * This function reads the information in the file containing the comparisons
26  * to be made and coordinates the calculation and storage of the significance
27  * levels.
28  *
29  * INPUTS:
30  * iexp - experiment number.
31  * nrun - number of runs in a given experiment.
32  * tot_PAIRS - number of phantoms with PAIRed structures used for a given
33  * experiment.
34  * anaflname - name of file containing comparisons to be made.
35  *
36  * OUTPUTS:
37  * none.
38  */
39 void analyze2(int iexp, int nrun, int tot_PAIRS, char* anaflname)
40 {
41         int *itr1, *itr2, niters, stru, poin, hitr, usr1, usr2, usr3, usr4, usr5, iroi, klds, wsqd, compare = 0, mode_flag = 0;
42         int i;
43
44         char string[MAXLINESIZE], key1[50], sigflname[MAXLINESIZE], key2[50],
45                         keywrd1[50], keywrd2[50];
46
47         float lowden = 0.0, highden = 0.0;
48
49         FILE *anafl, *sigfl;
50
51         if ((anafl = fopen(anaflname, "r")) == NULL)
52         {
53                 errorc("in analyze2.c: error in opening file ", anaflname);
54         }
55
56         fgets(string, sizeof(string), anafl); // get name of file to store significance levels
57         while (isSkip(string))     //jump over comment lines and blank lines
58         {
59                 fgets(string, sizeof(string), anafl);
60         }
61
62         for (i = strlen(string) - 1; i >= 0; i--)
63         {
64                 if (iscntrl(string[i]) || string[i] == ' ')
65                         string[i] = '\0';
66         }
67         if (strlen(string) == 0)
68                 errorc("in analyze2.c: error in opening file ", sigflname);
69
70         strcpy(sigflname, string);
71         sprintf(sigflname, "%s.%d", sigflname, iexp);
72
73         if ((sigfl = fopen(sigflname, "w")) == NULL)
74         {
75                 errorc("in analyze2.c: error in opening file ", sigflname);
76         }
77
78         fgets(string, sizeof(string), anafl); /* second line is either a MODE
79          * command or a COMPARE command */
80         while (isSkip(string))     //jump over comment lines and blank lines
81         {
82                 fgets(string, sizeof(string), anafl);
83         }
84
85         if (strncasecmp(string, "mode", 4) == 0)
86         {
87                 /* set mode_flag to 1 if second line is MODE and read in
88                  * MODE values */
89                 mode_flag = 1;
90                 sscanf(string, "%*s %f %f", &lowden, &highden);
91         }
92
93         if (mode_flag == 1)
94         { /* if second line is MODE */
95                 fgets(string, sizeof(string), anafl); /* third line is COMPARE */
96                 while (isSkip(string))     //jump over comment lines and blank lines
97                 {
98                         fgets(string, sizeof(string), anafl);
99                 }
100         }
101
102         /* at this point string contains the COMPARE command */
103
104         while (strncasecmp(string, "end", 3) != 0)
105         {
106                 /* beginning of while loop */
107
108                 if (strncasecmp(string, "comp", 4) == 0)
109                 {
110                         /* If the string just read in has the COMPARE command:
111                          * (1) increment compare counter and read in the two keywords.
112                          * (2) get flags for the figures of merit (FOMs)
113                          * (3) get the iteration numbers to be compared
114                          * (4) compute significance for these iteration numbers.
115                          * (5) free arrays for next set of iteration numbers. */
116
117                         compare++; /* keeps count of number of COMPAREs read in */
118                         sscanf(string, "%*s%s %s", key1, key2);
119                         strncpy(keywrd1, key1, 4);
120                         strncpy(keywrd2, key2, 4);
121                         keywrd1[4] = '\0';
122                         keywrd2[4] = '\0';
123                         getfom(string, &stru, &poin, &hitr, &usr1, &usr2, &usr3, &usr4, &usr5, &iroi, &klds, &wsqd);
124                         getiters(anafl, &itr1, &itr2, &niters, string);
125                         computesig(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, keywrd1, keywrd2, stru, poin, hitr, iroi, klds, wsqd, usr1, usr2, usr3, usr4, usr5, lowden, highden);
126                         free(itr1);
127                         free(itr2);
128                 }
129
130                 if (strncasecmp(string, "mode", 4) == 0)
131                 {
132                         /* If string just read in has the MODE command then read in
133                          * clip values and read the next string (which has the COMPARE
134                          * command). */
135
136                         sscanf(string, "%*s %f %f", &lowden, &highden);
137                         fgets(string, sizeof(string), anafl);
138                         while (isSkip(string))     //jump over comment lines and blank lines
139                         {
140                                 fgets(string, sizeof(string), anafl);
141                         }
142                 }
143         } /* end of while loop */
144
145         fclose(anafl);
146         fclose(sigfl);
147 }
148
149 /*
150  * This function coordinates the calculation and storage of the significance
151  *
152  * INPUTS:
153  * iexp - experiment number.
154  * nrun - number of runs for experiment iexp.
155  * tot_PAIRS - number of phantoms with PAIRed structures for experiment iexp.
156  * compare - number of COMPARE commands read in so far.
157  * niters - number of iterations to be compared.
158  * itr1 - array of length niters containing the iteration numbers to be
159  * compared for the first algorithm.
160  * itr2 - array of length niters containing the iteration numbers to be
161  * compared for the second algorithm.
162  * sigfl - file pointer to the file which stores the significance levels
163  * keywrd1 - keyword which defines the first algorithm.
164  * keywrd2 - keyword which defines the second algorithm.
165  * stru - equals 1 if structural accuracy is to be computed, else equals 0.
166  * poin - equals 1 if pointwise accuracy is to be computed, else equals 0.
167  * hitr - equals 1 if hit-ratio is to be computed, else equals 0.
168  * user - equals 1 if user-defined FOM is to be computed, else equals 0.
169  * lowden - lower clip value of MODE command.
170  * highden - higher clip value of MODE command.
171  */
172
173 void computesig(int iexp, int nrun, int tot_PAIRS, int compare, int niters, int* itr1, int* itr2, FILE* sigfl, char* keywrd1, char* keywrd2, int stru, int poin, int hitr, int iroi, int klds, int wsqd, int usr1, int usr2, int usr3, int usr4, int usr5, float lowden, float highden)
174 {
175         //  Depending on the values of the FOM flags compute and print the corresponding significance levels
176
177         if (stru == 1)
178         {
179                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "Structural Accuracy", keywrd1, keywrd2, lowden, highden);
180         }
181
182         if (poin == 1)
183         {
184                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "Pointwise Accuracy", keywrd1, keywrd2, lowden, highden);
185         }
186
187         if (hitr == 1)
188         {
189                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "Hit-ratio", keywrd1, keywrd2, lowden, highden);
190         }
191
192         if (iroi == 1)
193         {
194                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "Imagewise-ROI", keywrd1, keywrd2, lowden, highden);
195         }
196
197         if (klds == 1)
198         {
199                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "Kullback-Leibler distance", keywrd1, keywrd2, lowden, highden);
200         }
201
202         if (wsqd == 1)
203         {
204                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "Weighted squared distance", keywrd1, keywrd2, lowden, highden);
205         }
206
207         if (usr1 == 1)
208         {
209                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "User-defined-1", keywrd1, keywrd2, lowden, highden);
210         }
211
212         if (usr2 == 1)
213         {
214                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "User-defined-2", keywrd1, keywrd2, lowden, highden);
215         }
216
217         if (usr3 == 1)
218         {
219                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "User-defined-3", keywrd1, keywrd2, lowden, highden);
220         }
221
222         if (usr4 == 1)
223         {
224                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "User-defined-4", keywrd1, keywrd2, lowden, highden);
225         }
226
227         if (usr5 == 1)
228         {
229                 signif(iexp, nrun, tot_PAIRS, compare, niters, itr1, itr2, sigfl, "User-defined-5", keywrd1, keywrd2, lowden, highden);
230         }
231 }