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