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
28  *ExplicitBitVects
29  *
30  */
31 template <class vectType, class entryType>
33  public:
34  /*! \brief Default Constructor
35  *
36  */
38 
39  /*! \brief Set the metric function
40  *
41  * Set the pointer to the mertic funvtion to be used by the metric calculator
42  *
43  * ARGUMENTS:
44  *
45  * mFunc - pointer to the metric funtion
46  */
47  void setMetricFunc(double (*mFunc)(const entryType &, const entryType &,
48  unsigned int)) {
49  dp_metricFunc = mFunc;
50  }
51 
52  /*! \brief The calculator function
53  *
54  * ARGUMENTS:
55  *
56  * descrips - vectType container with a entryType for each item
57  * nItems - the number of item in the descripts.
58  * In several cases this argument is irrelvant since vectType
59  *probably supports
60  * a size() member function, But we would like this interface to
61  *take for example
62  * a double** and correctly parse the row and columns.
63  * dim - the dimension of the sequences
64  * distMat - pointer to an array to write the distance matrix to
65  * it is assumed that the right sized array has already be
66  *allocated.
67  *
68  * FIX: we can probably make this function create the correct sized distMat
69  *and return
70  * it to the caller, but when pushing he result out to a python array not sure
71  *how to
72  * avoid copy the entire distance matrix in that case
73  *
74  * RETURNS:
75  *
76  * pointer to a 1D array of doubles. Only the lower triangle elements are
77  * included in the array
78  */
79  void calcMetricMatrix(const vectType &descripts, unsigned int nItems,
80  unsigned int dim, double *distMat) {
81  CHECK_INVARIANT(distMat, "invalid pointer to a distance matix");
82 
83  for (unsigned int i = 1; i < nItems; i++) {
84  unsigned int itab = i * (i - 1) / 2;
85  for (unsigned int j = 0; j < i; j++) {
86  distMat[itab + j] = dp_metricFunc(descripts[i], descripts[j], dim);
87  }
88  }
89  };
90 
91  private:
92  // pointer to the metric function
93  /*! \brief pointer to the metric function
94  *
95  * In several cases the last argument 'dim' should be irrelevant,
96  * For example when entryType is a bit vector the size is of the vector
97  * or the dimension can be obtained by asking the bit vector itself. However
98  * we woul like this interface to support other containers lines double*
99  * in which case the 'dim' value is useful in cumputing the metric.
100  */
101  double (*dp_metricFunc)(const entryType &, const entryType &, unsigned int);
102 };
103 };
104 
105 #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:99
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.