RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
MetricMatrixCalc.h
Go to the documentation of this file.
1//
2// Copyright (C) 2003-2006 Rational Discovery LLC
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10#include <RDGeneral/export.h>
11#ifndef __RD_METRICMATRIXCAL_H__
12#define __RD_METRICMATRIXCAL_H__
13
14#include "MetricFuncs.h"
15#include <RDGeneral/Invariant.h>
16
17namespace 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 */
31template <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 metric function to be used by the metric calculator
42 *
43 * ARGUMENTS:
44 *
45 * mFunc - pointer to the metric function
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 descrips.
58 * In several cases this argument is irrelevant since vectType
59 * probably supports a size() member function, But we would like
60 * this interface to take for example a double** and correctly
61 * parse the row and columns.
62 * dim - the dimension of the sequences
63 * distMat - pointer to an array to write the distance matrix to
64 * it is assumed that the right sized array has already be
65 * allocated.
66 *
67 * FIX: we can probably make this function create the correct sized distMat
68 * and return it to the caller, but when pushing he result out to a
69 * python array not sure how to avoid copy the entire distance matrix
70 * in that case
71 *
72 * RETURNS:
73 *
74 * pointer to a 1D array of doubles. Only the lower triangle elements are
75 * included in the array
76 */
77 void calcMetricMatrix(const vectType &descripts, unsigned int nItems,
78 unsigned int dim, double *distMat) {
79 CHECK_INVARIANT(distMat, "invalid pointer to a distance matix");
80 CHECK_INVARIANT(dp_metricFunc, "metric function has not been set");
81
82 for (unsigned int i = 1; i < nItems; i++) {
83 unsigned int itab = i * (i - 1) / 2;
84 for (unsigned int j = 0; j < i; j++) {
85 distMat[itab + j] = dp_metricFunc(descripts[i], descripts[j], dim);
86 }
87 }
88 }
89
90 private:
91 // pointer to the metric function
92 /*! \brief pointer to the metric function
93 *
94 * In several cases the last argument 'dim' should be irrelevant,
95 * For example when entryType is a bit vector the size is of the vector
96 * or the dimension can be obtained by asking the bit vector itself. However
97 * we would like this interface to support other containers lines double*
98 * in which case the 'dim' value is useful in computing the metric.
99 */
100 double (*dp_metricFunc)(const entryType &, const entryType &,
101 unsigned int){nullptr};
102};
103}; // namespace RDDataManip
104
105#endif
#define CHECK_INVARIANT(expr, mess)
Definition Invariant.h:100
void setMetricFunc(double(*mFunc)(const entryType &, const entryType &, unsigned int))
Set the metric function.
void calcMetricMatrix(const vectType &descripts, unsigned int nItems, unsigned int dim, double *distMat)
The calculator function.
MetricMatrixCalc()
Default Constructor.