RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
InfoBitRanker.h
Go to the documentation of this file.
1//
2// Copyright (C) 2003-2007 Greg Landrum and Rational Discovery LLC
3// @@ All Rights Reserved @@
4// This file is part of the RDKit.
5// The contents are covered by the terms of the BSD license
6// which is included in the file license.txt, found at the root
7// of the RDKit source tree.
8//
9
10#include <RDGeneral/export.h>
11#ifndef _RD_INFORANKER_H_
12#define _RD_INFORANKER_H_
13
14#include <RDGeneral/types.h>
16
17/*! \brief Class used to rank bits based on a specified measure of information
18 *
19 * Basically a primitive mimic of the CombiChem "signal" functionality
20 * To use:
21 * - create an instance of this class
22 * - loop over the fingerprints in the dataset by calling accumulateVotes
23 *method
24 * - call getTopN to get the top n ranked bits
25 *
26 * Sample usage and results from the python wrapper:
27 * Here's a small set of vectors:
28 * >>> for i,bv in enumerate(bvs): print bv.ToBitString(),acts[i]
29 * ...
30 * 0001 0
31 * 0101 0
32 * 0010 1
33 * 1110 1
34 *
35 * Default ranker, using infogain:
36 * >>> ranker = InfoBitRanker(4,2)
37 * >>> for i,bv in enumerate(bvs): ranker.AccumulateVotes(bv,acts[i])
38 * ...
39 * >>> for bit,gain,n0,n1 in ranker.GetTopN(3): print
40 *int(bit),'%.3f'%gain,int(n0),int(n1)
41 * ...
42 * 3 1.000 2 0
43 * 2 1.000 0 2
44 * 0 0.311 0 1
45 *
46 * Using the biased infogain:
47 * >>> ranker = InfoBitRanker(4,2,InfoTheory.InfoType.BIASENTROPY)
48 * >>> ranker.SetBiasList((1,))
49 * >>> for i,bv in enumerate(bvs): ranker.AccumulateVotes(bv,acts[i])
50 * ...
51 * >>> for bit,gain,n0,n1 in ranker.GetTopN(3): print
52 *int(bit),'%.3f'%gain,int(n0),int(n1)
53 * ...
54 * 2 1.000 0 2
55 * 0 0.311 0 1
56 * 1 0.000 1 1
57 *
58 * A chi squared ranker is also available:
59 * >>> ranker = InfoBitRanker(4,2,InfoTheory.InfoType.CHISQUARE)
60 * >>> for i,bv in enumerate(bvs): ranker.AccumulateVotes(bv,acts[i])
61 * ...
62 * >>> for bit,gain,n0,n1 in ranker.GetTopN(3): print
63 *int(bit),'%.3f'%gain,int(n0),int(n1)
64 * ...
65 * 3 4.000 2 0
66 * 2 4.000 0 2
67 * 0 1.333 0 1
68 *
69 * As is a biased chi squared:
70 * >>> ranker = InfoBitRanker(4,2,InfoTheory.InfoType.BIASCHISQUARE)
71 * >>> ranker.SetBiasList((1,))
72 * >>> for i,bv in enumerate(bvs): ranker.AccumulateVotes(bv,acts[i])
73 * ...
74 * >>> for bit,gain,n0,n1 in ranker.GetTopN(3): print
75 *int(bit),'%.3f'%gain,int(n0),int(n1)
76 * ...
77 * 2 4.000 0 2
78 * 0 1.333 0 1
79 * 1 0.000 1 1
80 */
81namespace RDInfoTheory {
82typedef std::vector<RDKit::USHORT> USHORT_VECT;
83typedef std::vector<USHORT_VECT> VECT_USHORT_VECT;
84
86 public:
87 /*! \brief the type of measure for information
88 *
89 */
90 typedef enum {
95 } InfoType;
96
97 /*! \brief Constructor
98 *
99 * ARGUMENTS:
100 *
101 * - nBits: the dimension of the bit vectors or the fingerprint length
102 * - nClasses: the number of classes used in the classification problem
103 *(e.g. active,
104 * moderately active, inactive etc.). It is assumed that the
105 *classes are
106 * numbered from 0 to (nClasses - 1)
107 * - infoType: the type of information metric
108 */
109 InfoBitRanker(unsigned int nBits, unsigned int nClasses,
111 : d_dims(nBits), d_classes(nClasses), d_type(infoType) {
112 d_counts.resize(0);
113 for (unsigned int i = 0; i < nClasses; i++) {
114 USHORT_VECT cCount;
115 cCount.resize(d_dims, 0);
116 d_counts.push_back(cCount);
117 }
118 d_clsCount.resize(d_classes, 0);
119 d_nInst = 0;
120 d_top = 0;
121 dp_topBits = nullptr;
122 d_biasList.resize(0);
123 dp_maskBits = nullptr;
124 }
125
127 if (dp_topBits) {
128 delete[] dp_topBits;
129 }
130 if (dp_maskBits) {
131 delete dp_maskBits;
132 }
133 }
134
135 /*! \brief Accumulate the votes for all the bits turned on in a bit vector
136 *
137 * ARGUMENTS:
138 *
139 * - bv : bit vector that supports [] operator
140 * - label : the class label for the bit vector. It is assumed that 0 <=
141 *class < nClasses
142 */
143 void accumulateVotes(const ExplicitBitVect &bv, unsigned int label);
144 void accumulateVotes(const SparseBitVect &bv, unsigned int label);
145
146 /*! \brief Returns the top n bits ranked by the information metric
147 *
148 * This is actually the function where most of the work of ranking is
149 *happening
150 *
151 * \param num the number of top ranked bits that are required
152 *
153 * \return a pointer to an information array. The client should *not*
154 * delete this
155 */
156 double *getTopN(unsigned int num);
157
158 /*! \brief return the number of labelled instances(examples) or fingerprints
159 *seen so far
160 *
161 */
162 unsigned int getNumInstances() const { return d_nInst; }
163
164 /*! \brief return the number of classes
165 *
166 */
167 unsigned int getNumClasses() const { return d_classes; }
168
169 /*! \brief Set the classes to which the entropy calculation should be biased
170 *
171 * This list contains a set of class ids used when in the BIASENTROPY mode of
172 *ranking bits.
173 * In this mode, a bit must be correllated higher with one of the biased
174 *classes than all the
175 * other classes. For example, in a two class problem with actives and
176 *inactives, the fraction of
177 * actives that hit the bit has to be greater than the fraction of inactives
178 *that hit the bit
179 *
180 * ARGUMENTS:
181 * classList - list of class ids that we want a bias towards
182 */
183 void setBiasList(RDKit::INT_VECT &classList);
184
185 /*! \brief Set the bits to be used as a mask
186 *
187 * If this function is called, only the bits which are present in the
188 * maskBits list will be used.
189 *
190 * ARGUMENTS:
191 * maskBits - the bits to be considered
192 */
194
195 /*! \brief Write the top N bits to a stream
196 *
197 */
198 void writeTopBitsToStream(std::ostream *outStream) const;
199
200 /*! \brief Write the top bits to a file
201 *
202 */
203 void writeTopBitsToFile(const std::string &fileName) const;
204
205 private:
206 /*! \brief check if we want to compute the info content for a bit based on the
207 *bias list
208 *
209 * This what happens here:
210 * - the fraction of items in each class that hit a particular bit are
211 *computed
212 * - the maximum of these fractions for classes that are not in the
213 *biasList are computed
214 * - If this maximum is less than the fraction for at least one of the
215 * classes in the biaslist, the bit is considered good
216 * ARGUMENTS:
217 * - resMat : the result matrix, one dimensional matrix of dimension (2*(num
218 *of classes))
219 * a 2D structure is assumed with the first row containing number
220 *of items of each class
221 * with the bit set and the second row to entires of each class
222 *with the bit turned off
223 */
224 bool BiasCheckBit(RDKit::USHORT *resMat) const;
225
226 /*! \brief Compute the biased info entropy gain based on the bias list
227 *
228 * This what happens here:
229 * - we call BiasCheckBit to see if the bit qualifies to compute the
230 *infocontent
231 * - If this bit is ok then we call InfoEntropyGain otherwise we return 0.0
232 *
233 * ARGUMENTS:
234 * - resMat : the result matrix, one dimensional matrix of dimension (2*(num
235 *of classes))
236 * a 2D structure is assumed with the first row containing number
237 *of items of each class
238 * with the bit set and the second row to entires of each class
239 *with the bit turned off
240 */
241 double BiasInfoEntropyGain(RDKit::USHORT *resMat) const;
242
243 /*! \brief Compute the biased chi qsure value based on the bias list
244 *
245 * This what happens here:
246 * - we call BiasCheckBit to see if the bit qualifies to compute the
247 *infocontent
248 * - If this bit is ok then we call InfoEntropyGain otherwise we return 0.0
249 *
250 * ARGUMENTS:
251 * - resMat : the result matrix, one dimensional matrix of dimension (2*(num
252 *of classes))
253 * a 2D structure is assumed with the first row containing number
254 *of items of each class
255 * with the bit set and the second row to entires of each class
256 *with the bit turned off
257 */
258 double BiasChiSquareGain(RDKit::USHORT *resMat) const;
259
260 unsigned int d_dims; // the number of bits in the fingerprints
261 unsigned int d_classes; // the number of classes (active, inactive,
262 // moderately active etc.)
263 InfoType d_type; // the type of information measure - currently we support
264 // only entropy
265 VECT_USHORT_VECT d_counts; // place holder of counting the number of hits for
266 // each bit for each class
267 USHORT_VECT d_clsCount; // counter for the number of instances of each class
268 double *dp_topBits; // storage for the top ranked bits and the corresponding
269 // statistics
270 unsigned int d_top; // the number of bits that have been ranked
271 unsigned int d_nInst; // total number of instances or fingerprints used
272 // accumulate votes
274 d_biasList; // if we want a bias towards certain classes in ranking bits
275 ExplicitBitVect *dp_maskBits; // allows only certain bits to be considered
276};
277} // namespace RDInfoTheory
278#endif
Pulls in all the BitVect classes.
a class for bit vectors that are densely occupied
void accumulateVotes(const ExplicitBitVect &bv, unsigned int label)
Accumulate the votes for all the bits turned on in a bit vector.
InfoType
the type of measure for information
void setMaskBits(RDKit::INT_VECT &maskBits)
Set the bits to be used as a mask.
void writeTopBitsToFile(const std::string &fileName) const
Write the top bits to a file.
InfoBitRanker(unsigned int nBits, unsigned int nClasses, InfoType infoType=InfoBitRanker::ENTROPY)
Constructor.
unsigned int getNumClasses() const
return the number of classes
void accumulateVotes(const SparseBitVect &bv, unsigned int label)
double * getTopN(unsigned int num)
Returns the top n bits ranked by the information metric.
unsigned int getNumInstances() const
return the number of labelled instances(examples) or fingerprints seen so far
void writeTopBitsToStream(std::ostream *outStream) const
Write the top N bits to a stream.
void setBiasList(RDKit::INT_VECT &classList)
Set the classes to which the entropy calculation should be biased.
a class for bit vectors that are sparsely occupied.
#define RDKIT_INFOTHEORY_EXPORT
Definition export.h:273
Class used to rank bits based on a specified measure of information.
std::vector< RDKit::USHORT > USHORT_VECT
std::vector< USHORT_VECT > VECT_USHORT_VECT
std::vector< int > INT_VECT
Definition types.h:224
unsigned short USHORT
Definition types.h:221