-
Notifications
You must be signed in to change notification settings - Fork 48
/
meas_init.c
502 lines (444 loc) · 16.2 KB
/
meas_init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
Copyright (c) 2008 - Chris Buckley.
Permission is granted for use and modification of this file for
research, non-commercial purposes.
*/
#include "common.h"
#include "sysfunc.h"
#include "trec_eval.h"
#include "functions.h"
/* Procedures for initialization of several default formats of measure values.
Used in init_meas procedure description of TREC_MEAS in trec_eval.h:
typedef struct trec_meas {
...
* Store parameters for measure in meas_params. Reserve space in
TREC_EVAL.values for results of measure. Store individual measure
names (possibly altered by parameters) in TREC_EVAL.values.
Set tm->eval_index to start of reserved space *
...
} TREC_MEAS;
Measures are defined in measures.c.
*/
/* Static utility functions defined later */
static int get_long_cutoffs(PARAMS * params, char *param_string);
static int get_double_cutoffs(PARAMS * params, char *param_string);
static int get_double_params(PARAMS * params, char *param_string);
static int get_param_pairs(PARAMS * params, char *param_string);
static int comp_long();
static int comp_double();
static char *append_long(char *name, long value);
static char *append_double(char *name, double value);
static char *append_string(char *name, char *value);
/* ---------------- Init measure generic procedures -------------- */
/* Measure does not require initialization or storage (eg num_q) */
int te_init_meas_empty(EPI * epi, TREC_MEAS * tm, TREC_EVAL * eval)
{
tm->eval_index = 0;
return (1);
}
/* Measure is a single double measure with no parameters */
int te_init_meas_s_double(EPI * epi, TREC_MEAS * tm, TREC_EVAL * eval)
{
/* Make sure enough space */
if (NULL == (eval->values =
te_chk_and_realloc(eval->values, &eval->max_num_values,
eval->num_values + 1,
sizeof(TREC_EVAL_VALUE))))
return (UNDEF);
/* Set location of value of measure, zero it, and increment
space used for values */
tm->eval_index = eval->num_values;
eval->values[tm->eval_index] = (TREC_EVAL_VALUE) {
tm->name, 0.0};
eval->num_values++;
return (1);
}
/* Measure is a single long measure with no parameters */
int te_init_meas_s_long(EPI * epi, TREC_MEAS * tm, TREC_EVAL * eval)
{
/* Make sure enough space */
if (NULL == (eval->values =
te_chk_and_realloc(eval->values, &eval->max_num_values,
eval->num_values + 1,
sizeof(TREC_EVAL_VALUE))))
return (UNDEF);
/* Set location of value of measure, zero it, and increment
space used for values */
tm->eval_index = eval->num_values;
eval->values[tm->eval_index] = (TREC_EVAL_VALUE) {
tm->name, 0.0};
eval->num_values++;
return (1);
}
/* Measure is a double array with long cutoffs */
int te_init_meas_a_double_cut_long(EPI * epi, TREC_MEAS * tm, TREC_EVAL * eval)
{
long *cutoffs;
long i;
/* See if there are command line parameters for this measure.
Use those if given, otherwise use default cutoffs */
if (epi->meas_arg) {
MEAS_ARG *meas_arg_ptr = epi->meas_arg;
while (meas_arg_ptr->measure_name) {
if (0 == strcmp(meas_arg_ptr->measure_name, tm->name)) {
if (UNDEF == get_long_cutoffs(tm->meas_params,
meas_arg_ptr->parameters))
return (UNDEF);
break;
}
meas_arg_ptr++;
}
}
cutoffs = (long *) tm->meas_params->param_values;
/* Make sure enough space */
if (NULL == (eval->values =
te_chk_and_realloc(eval->values, &eval->max_num_values,
eval->num_values +
tm->meas_params->num_params,
sizeof(TREC_EVAL_VALUE))))
return (UNDEF);
/* Initialize full measure name and value for each cutoff */
for (i = 0; i < tm->meas_params->num_params; i++) {
eval->values[eval->num_values + i] = (TREC_EVAL_VALUE) {
append_long(tm->name, cutoffs[i]), 0.0};
if (NULL == eval->values[eval->num_values + i].name)
return (UNDEF);
}
/* Set location of value of measure, and increment space used for values */
tm->eval_index = eval->num_values;
eval->num_values += tm->meas_params->num_params;
return (1);
}
/* Measure is a double array with double cutoffs */
int te_init_meas_a_double_cut_double(EPI * epi, TREC_MEAS * tm, TREC_EVAL * eval)
{
double *cutoffs;
long i;
/* See if there are command line parameters for this measure.
Use those if given, otherwise use default cutoffs */
if (epi->meas_arg) {
MEAS_ARG *meas_arg_ptr = epi->meas_arg;
while (meas_arg_ptr->measure_name) {
if (0 == strcmp(meas_arg_ptr->measure_name, tm->name)) {
if (UNDEF == get_double_cutoffs(tm->meas_params,
meas_arg_ptr->parameters))
return (UNDEF);
break;
}
meas_arg_ptr++;
}
}
cutoffs = (double *) tm->meas_params->param_values;
/* Make sure enough space */
if (NULL == (eval->values =
te_chk_and_realloc(eval->values, &eval->max_num_values,
eval->num_values +
tm->meas_params->num_params,
sizeof(TREC_EVAL_VALUE))))
return (UNDEF);
/* Initialize full measure name and value for each cutoff */
for (i = 0; i < tm->meas_params->num_params; i++) {
eval->values[eval->num_values + i] = (TREC_EVAL_VALUE) {
append_double(tm->name, cutoffs[i]), 0.0};
if (NULL == eval->values[eval->num_values + i].name)
return (UNDEF);
}
/* Set location of value of measure, and increment space used for values */
tm->eval_index = eval->num_values;
eval->num_values += tm->meas_params->num_params;
return (1);
}
/* Note difference between double cutoffs and double params is that
cutoff values are unordered, assumed unique, and one eval value is
allocated to each cutoff. Param values are ordered and can be anything,
and number of params means nothing about number of values*/
/* Measure is a single double with double params */
int te_init_meas_s_double_p_double(EPI * epi, TREC_MEAS * tm, TREC_EVAL * eval)
{
/* See if there are command line parameters for this measure.
Use those if given, otherwise use default cutoffs */
if (epi->meas_arg) {
MEAS_ARG *meas_arg_ptr = epi->meas_arg;
while (meas_arg_ptr->measure_name) {
if (0 == strcmp(meas_arg_ptr->measure_name, tm->name)) {
if (UNDEF == get_double_params(tm->meas_params,
meas_arg_ptr->parameters))
return (UNDEF);
break;
}
meas_arg_ptr++;
}
}
/* Make sure enough space */
if (NULL == (eval->values =
te_chk_and_realloc(eval->values, &eval->max_num_values,
eval->num_values + 1,
sizeof(TREC_EVAL_VALUE))))
return (UNDEF);
/* Set location of value of measure, zero it, and increment
space used for values */
tm->eval_index = eval->num_values;
if (tm->meas_params->printable_params)
eval->values[tm->eval_index] = (TREC_EVAL_VALUE) {
append_string(tm->name, tm->meas_params->printable_params), 0.0};
else
eval->values[tm->eval_index] = (TREC_EVAL_VALUE) {
tm->name, 0.0};
if (NULL == eval->values[eval->num_values].name)
return (UNDEF);
eval->num_values++;
return (1);
}
/* Measure is a single double with paired name=double params */
int te_init_meas_s_double_p_pair(EPI * epi, TREC_MEAS * tm, TREC_EVAL * eval)
{
if (epi->meas_arg) {
MEAS_ARG *meas_arg_ptr = epi->meas_arg;
while (meas_arg_ptr->measure_name) {
if (0 == strcmp(meas_arg_ptr->measure_name, tm->name)) {
if (UNDEF == get_param_pairs(tm->meas_params,
meas_arg_ptr->parameters))
return (UNDEF);
break;
}
meas_arg_ptr++;
}
}
/* Make sure enough space */
if (NULL == (eval->values =
te_chk_and_realloc(eval->values, &eval->max_num_values,
eval->num_values + 1,
sizeof(TREC_EVAL_VALUE))))
return (UNDEF);
/* Set location of value of measure, zero it, and increment
space used for values */
tm->eval_index = eval->num_values;
if (tm->meas_params->printable_params)
eval->values[tm->eval_index] = (TREC_EVAL_VALUE) {
append_string(tm->name, tm->meas_params->printable_params), 0.0};
else
eval->values[tm->eval_index] = (TREC_EVAL_VALUE) {
tm->name, 0.0};
if (NULL == eval->values[eval->num_values].name)
return (UNDEF);
eval->num_values++;
return (1);
}
/* ----------------- Utility procedures for initialization -------------- */
static int get_long_cutoffs(PARAMS * params, char *param_string)
{
long num_cutoffs;
char *ptr, *start_ptr;
long *cutoffs;
long i;
/* Count number of parameters in param_string (comma separated) */
num_cutoffs = 1;
for (ptr = param_string; *ptr; ptr++) {
if (*ptr == ',')
num_cutoffs++;
}
/* Reserve space for cutoffs */
if (NULL == (params->printable_params =
Malloc(strlen(param_string) + 1, char)) ||
NULL == (cutoffs = Malloc(num_cutoffs, long)))
return (UNDEF);
(void) strncpy(params->printable_params,
param_string, strlen(param_string) + 1);
params->num_params = num_cutoffs;
params->param_values = cutoffs;
start_ptr = param_string;
num_cutoffs = 0;
for (ptr = param_string; *ptr; ptr++) {
if (*ptr == ',') {
*ptr = '\0';
cutoffs[num_cutoffs++] = atol(start_ptr);
start_ptr = ptr + 1;
}
}
cutoffs[num_cutoffs++] = atol(start_ptr);
/* Sort cutoffs in increasing order */
qsort((char *) cutoffs, (int) num_cutoffs, sizeof(long), comp_long);
/* Sanity checking: cutoffs > 0 and non-duplicates */
if (cutoffs[0] <= 0) {
fprintf(stderr, "trec_eval: Negative cutoff detected\n");
return (UNDEF);
}
for (i = 1; i < num_cutoffs; i++) {
if (cutoffs[i - 1] == cutoffs[i]) {
fprintf(stderr, "trec_eval: duplicate cutoffs detected\n");
return (UNDEF);
}
}
return (1);
}
static int get_double_cutoffs(PARAMS * params, char *param_string)
{
long num_cutoffs;
char *ptr, *start_ptr;
double *cutoffs;
long i;
/* Count number of parameters in param_string (comma separated) */
num_cutoffs = 1;
for (ptr = param_string; *ptr; ptr++) {
if (*ptr == ',')
num_cutoffs++;
}
/* Reserve space for cutoffs */
if (NULL == (params->printable_params =
Malloc(strlen(param_string) + 1, char)) ||
NULL == (cutoffs = Malloc(num_cutoffs, double)))
return (UNDEF);
(void) strncpy(params->printable_params,
param_string, strlen(param_string) + 1);
params->num_params = num_cutoffs;
params->param_values = cutoffs;
start_ptr = param_string;
num_cutoffs = 0;
for (ptr = param_string; *ptr; ptr++) {
if (*ptr == ',') {
*ptr = '\0';
cutoffs[num_cutoffs++] = atof(start_ptr);
start_ptr = ptr + 1;
}
}
cutoffs[num_cutoffs++] = atof(start_ptr);
/* Sort cutoffs in increasing order */
qsort((char *) cutoffs, (int) num_cutoffs, sizeof(double), comp_double);
/* Sanity checking: non-duplicates */
for (i = 1; i < num_cutoffs; i++) {
if (cutoffs[i - 1] == cutoffs[i]) {
fprintf(stderr, "trec_eval: duplicate cutoffs detected\n");
return (UNDEF);
}
}
return (1);
}
static int get_double_params(PARAMS * params, char *param_string)
{
long num_params;
char *ptr, *start_ptr;
double *values;
/* Count number of parameters in param_string (comma separated) */
num_params = 1;
for (ptr = param_string; *ptr; ptr++) {
if (*ptr == ',')
num_params++;
}
/* Reserve space for params */
if (NULL == (params->printable_params =
Malloc(strlen(param_string) + 1, char)) ||
NULL == (values = Malloc(num_params, double)))
return (UNDEF);
(void) strncpy(params->printable_params,
param_string, strlen(param_string) + 1);
start_ptr = param_string;
num_params = 0;
for (ptr = param_string; *ptr; ptr++) {
if (*ptr == ',') {
*ptr = '\0';
values[num_params++] = atof(start_ptr);
start_ptr = ptr + 1;
}
}
values[num_params++] = atof(start_ptr);
params->param_values = values;
params->num_params = num_params;
return (1);
}
/* Params are in comma separated form name=double. Eg, -m ndcg_p.1=4.0,2=8.0 */
static int get_param_pairs(PARAMS * params, char *param_string)
{
long num_params;
char last_seen;
char *ptr, *start_ptr;
DOUBLE_PARAM_PAIR *values;
/* Count number of parameters in param_string (comma separated), all
of form name=value. Return error if not of right form */
num_params = 1;
last_seen = ',';
for (ptr = param_string; *ptr; ptr++) {
if (*ptr == ',') {
num_params++;
if (*ptr == last_seen) /* Error */
break;
last_seen = ',';
} else if (*ptr == '=') {
if (*ptr == last_seen) { /* Flag as error */
last_seen = ',';
break;
}
last_seen = '=';
}
}
if (last_seen != '=') {
fprintf(stderr, "trec_eval: malformed pair parameters in '%s'\n",
param_string);
return (UNDEF);
}
/* Reserve space for params */
if (NULL == (params->printable_params =
Malloc(strlen(param_string) + 1, char)) ||
NULL == (values = Malloc(num_params, DOUBLE_PARAM_PAIR)))
return (UNDEF);
(void) strncpy(params->printable_params,
param_string, strlen(param_string) + 1);
start_ptr = param_string;
num_params = 0;
for (ptr = param_string; *ptr; ptr++) {
if (*ptr == '=') {
*ptr = '\0';
values[num_params].name = start_ptr;
start_ptr = ptr + 1;
} else if (*ptr == ',') {
*ptr = '\0';
values[num_params++].value = atof(start_ptr);
start_ptr = ptr + 1;
}
}
values[num_params++].value = atof(start_ptr);
params->param_values = values;
params->num_params = num_params;
return (1);
}
static int comp_long(long *ptr1, long *ptr2)
{
return (*ptr1 - *ptr2);
}
static int comp_double(double *ptr1, double *ptr2)
{
if (*ptr1 < *ptr2)
return (-1);
if (*ptr1 > *ptr2)
return (1);
return (0);
}
static char *append_long(char *name, long value)
{
long length_required = strlen(name) + 20 + 2;
char *full_name;
if (NULL == (full_name = Malloc(length_required, char)))
return (NULL);
snprintf(full_name, length_required, "%s_%ld", name, value);
return (full_name);
}
static char *append_double(char *name, double value)
{
long length_required = strlen(name) + 8 + 2;
char *full_name;
if (NULL == (full_name = Malloc(length_required, char)))
return (NULL);
snprintf(full_name, length_required, "%s_%3.2f", name, value);
return (full_name);
}
static char *append_string(char *name, char *value)
{
long length_required;
char *full_name;
if (NULL == value)
return (name);
length_required = strlen(name) + strlen(value) + 2;
if (NULL == (full_name = Malloc(length_required, char)))
return (NULL);
snprintf(full_name, length_required, "%s_%s", name, value);
return (full_name);
}