2f766a6978ee3b0cc14f1b6318306e587b848efb
[svrt.git] / misc.cc
1 /*
2  *  svrt is the ``Synthetic Visual Reasoning Test'', an image
3  *  generator for evaluating classification performance of machine
4  *  learning systems, humans and primates.
5  *
6  *  Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of svrt.
10  *
11  *  svrt is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  svrt 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 selector.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include <fstream>
26
27 using namespace std;
28
29 #include "misc.h"
30
31 char *basename(char *name) {
32   char *result = name;
33   while(*name) {
34     if(*name == '/') result = name + 1;
35     name++;
36   }
37   return result;
38 }
39
40 char *next_word(char *buffer, char *r, int buffer_size) {
41   char *s;
42   s = buffer;
43
44   if(r != 0) {
45     while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
46     if(*r == '"') {
47       r++;
48       while((*r != '"') && (*r != '\0') &&
49             (s<buffer+buffer_size-1))
50         *s++ = *r++;
51       if(*r == '"') r++;
52     } else {
53       while((*r != '\r') && (*r != '\n') && (*r != '\0') &&
54             (*r != '\t') && (*r != ' ') && (*r != ',')) {
55         if(s == buffer + buffer_size) {
56           cerr << "Buffer overflow in next_word." << endl;
57           exit(1);
58         }
59         *s++ = *r++;
60       }
61     }
62
63     while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
64     if((*r == '\0') || (*r=='\r') || (*r=='\n')) r = 0;
65   }
66   *s = '\0';
67
68   return r;
69 }
70
71 scalar_t discrete_entropy(int *n, int nb) {
72   scalar_t s = 0, t = 0;
73   for(int k = 0; k < nb; k++) if(n[k] > 0) {
74     s += n[k] * log(scalar_t(n[k]));
75     t += n[k];
76   }
77   return (log(t) - s/scalar_t(t))/log(2.0);
78 }
79
80 void random_permutation(int *val, int nb) {
81   for(int k = 0; k < nb; k++) val[k] = k;
82   int i, t;
83   for(int k = 0; k < nb - 1; k++) {
84     i = int(drand48() * (nb - k)) + k;
85     t = val[i];
86     val[i] = val[k];
87     val[k] = t;
88   }
89 }
90
91 void tag_subset(bool *val, int nb_total, int nb_to_tag) {
92   ASSERT(nb_to_tag <= nb_total);
93   int index[nb_total];
94   random_permutation(index, nb_total);
95   for(int n = 0; n < nb_total; n++) val[n] = false;
96   for(int n = 0; n < nb_to_tag; n++) val[index[n]] = true;
97 }
98
99 int compare_couple(const void *a, const void *b) {
100   if(((Couple *) a)->value < ((Couple *) b)->value) return -1;
101   else if(((Couple *) a)->value > ((Couple *) b)->value) return 1;
102   else return 0;
103 }