RDKit
Open-source cheminformatics and machine learning.
MetricMatrixCalc.h
Go to the documentation of this file.
1 // $Id$
2 //
3 // Copyright (C) 2003-2006 Rational Discovery LLC
4 //
5 // @@ All Rights Reserved @@
6 // This file is part of the RDKit.
7 // The contents are covered by the terms of the BSD license
8 // which is included in the file license.txt, found at the root
9 // of the RDKit source tree.
10 //
11 #ifndef __RD_METRICMATRIXCAL_H__
12 #define __RD_METRICMATRIXCAL_H__
13 
14 #include "MetricFuncs.h"
15 #include <RDGeneral/Invariant.h>
16 
17 namespace RDDataManip {
18 
19  /*! \brief A generic metric matrix calculator (e.g similarity matrix or
20  * distance matrix)
21  *
22  * This templated class needs some explanation
23  * vectType is a container that can support [] operator
24  * entryType is the type of entry that is returned by the [] operator
25  * Examples of the container include PySequenceHolder which is wrapper around
26  * a python sequence objects like lists and tuples.
27  * Examples of the entryType include a sequence of double, floats, and ExplicitBitVects
28  *
29  */
30  template <class vectType, class entryType> class MetricMatrixCalc {
31  public:
32  /*! \brief Default Constructor
33  *
34  */
36 
37  /*! \brief Set the metric function
38  *
39  * Set the pointer to the mertic funvtion to be used by the metric calculator
40  *
41  * ARGUMENTS:
42  *
43  * mFunc - pointer to the metric funtion
44  */
45  void setMetricFunc(double (*mFunc)(const entryType &, const entryType &, unsigned int)) {
46  dp_metricFunc = mFunc;
47  }
48 
49  /*! \brief The calculator function
50  *
51  * ARGUMENTS:
52  *
53  * descrips - vectType container with a entryType for each item
54  * nItems - the number of item in the descripts.
55  * In several cases this argument is irrelvant since vectType probably supports
56  * a size() member function, But we would like this interface to take for example
57  * a double** and correctly parse the row and columns.
58  * dim - the dimension of the sequences
59  * distMat - pointer to an array to write the distance matrix to
60  * it is assumed that the right sized array has already be allocated.
61  *
62  * FIX: we can probably make this function create the correct sized distMat and return
63  * it to the caller, but when pushing he result out to a python array not sure how to
64  * avoid copy the entire distance matrix in that case
65  *
66  * RETURNS:
67  *
68  * pointer to a 1D array of doubles. Only the lower triangle elements are
69  * included in the array
70  */
71  void calcMetricMatrix(const vectType &descripts, unsigned int nItems, unsigned int dim,
72  double *distMat) {
73  CHECK_INVARIANT(distMat, "invalid pointer to a distance matix");
74 
75  for (unsigned int i = 1; i < nItems; i++) {
76  unsigned int itab = i*(i-1)/2;
77  for (unsigned int j = 0; j < i; j++) {
78  distMat[itab+j] = dp_metricFunc(descripts[i], descripts[j], dim);
79  }
80  }
81  };
82 
83  private:
84  // pointer to the metric function
85  /*! \brief pointer to the metric function
86  *
87  * In several cases the last argument 'dim' should be irrelevant,
88  * For example when entryType is a bit vector the size is of the vector
89  * or the dimension can be obtained by asking the bit vector itself. However
90  * we woul like this interface to support other containers lines double*
91  * in which case the 'dim' value is useful in cumputing the metric.
92  */
93  double (*dp_metricFunc)(const entryType &, const entryType &, unsigned int);
94 
95  };
96 };
97 
98 #endif
void calcMetricMatrix(const vectType &descripts, unsigned int nItems, unsigned int dim, double *distMat)
The calculator function.
#define CHECK_INVARIANT(expr, mess)
Definition: Invariant.h:114
void setMetricFunc(double(*mFunc)(const entryType &, const entryType &, unsigned int))
Set the metric function.
A generic metric matrix calculator (e.g similarity matrix or distance matrix)
MetricMatrixCalc()
Default Constructor.