Removed the definition of basename, which confuses an existing system one.
[folded-ctf.git] / tools.cc
1 /*
2  *  folded-ctf is an implementation of the folded hierarchy of
3  *  classifiers for object detection, developed by Francois Fleuret
4  *  and Donald Geman.
5  *
6  *  Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of folded-ctf.
10  *
11  *  folded-ctf is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  folded-ctf is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "misc.h"
26 #include "tools.h"
27 #include "fusion_sort.h"
28
29 scalar_t robust_sampling(int nb, scalar_t *weights, int nb_to_sample, int *sampled) {
30   ASSERT(nb > 0);
31   if(nb == 1) {
32     for(int k = 0; k < nb_to_sample; k++) sampled[k] = 0;
33     return weights[0];
34   } else {
35     scalar_t *pair_weights = new scalar_t[(nb+1)/2];
36     for(int k = 0; k < nb/2; k++)
37       pair_weights[k] = weights[2 * k] + weights[2 * k + 1];
38     if(nb%2)
39       pair_weights[(nb+1)/2 - 1] = weights[nb-1];
40     scalar_t result = robust_sampling((nb+1)/2, pair_weights, nb_to_sample, sampled);
41     for(int k = 0; k < nb_to_sample; k++) {
42       int s = sampled[k];
43       // There is a bit of a trick for the isolated sample in the odd
44       // case. Since the corresponding pair weight is the same as the
45       // one sample alone, the test is always true and the isolated
46       // sample will be taken for sure.
47       if(drand48() * pair_weights[s] <= weights[2 * s])
48         sampled[k] = 2 * s;
49       else
50         sampled[k] = 2 * s + 1;
51     }
52     delete[] pair_weights;
53     return result;
54   }
55 }
56
57 void print_roc_small_pos(ostream *out,
58                          int nb_pos, scalar_t *pos_responses,
59                          int nb_neg, scalar_t *neg_responses,
60                          scalar_t fas_factor) {
61
62   scalar_t *sorted_pos_responses = new scalar_t[nb_pos];
63
64   fusion_sort(nb_pos, pos_responses, sorted_pos_responses);
65
66   int *bins = new int[nb_pos + 1];
67   for(int k = 0; k <= nb_pos; k++) bins[k] = 0;
68
69   for(int k = 0; k < nb_neg; k++) {
70     scalar_t r = neg_responses[k];
71
72     if(r < sorted_pos_responses[0])
73       bins[0]++;
74
75     else if(r >= sorted_pos_responses[nb_pos - 1])
76       bins[nb_pos]++;
77
78     else {
79       int a = 0;
80       int b = nb_pos - 1;
81       int c = 0;
82
83       while(a < b - 1) {
84         c = (a + b) / 2;
85         if(r < sorted_pos_responses[c])
86           b = c;
87         else
88           a = c;
89       }
90
91       // Beware of identical positive responses
92       while(c < nb_pos && r >= sorted_pos_responses[c])
93         c++;
94
95       bins[c]++;
96     }
97   }
98
99   int s = nb_neg;
100   for(int k = 0; k < nb_pos; k++) {
101     s -= bins[k];
102     if(k == 0 || sorted_pos_responses[k-1] < sorted_pos_responses[k]) {
103       (*out) << (scalar_t(s) / scalar_t(nb_neg)) * fas_factor
104              << " "
105              << scalar_t(nb_pos - k)/scalar_t(nb_pos)
106              << " "
107              << sorted_pos_responses[k]
108              << endl;
109     }
110   }
111
112   delete[] bins;
113   delete[] sorted_pos_responses;
114 }