File: MaxMinPicker.cpp

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (295 lines) | stat: -rw-r--r-- 12,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
//  Copyright (C) 2003-2017  Greg Landrum and Rational Discovery LLC
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#define NO_IMPORT_ARRAY

#define PY_ARRAY_UNIQUE_SYMBOL rdpicker_array_API
#include <RDBoost/python.h>
#include <RDBoost/Wrap.h>
#include <RDBoost/boost_numpy.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
#include <map>

#include <DataStructs/BitVects.h>
#include <DataStructs/BitOps.h>
#include <SimDivPickers/DistPicker.h>
#include <SimDivPickers/MaxMinPicker.h>
#include <SimDivPickers/HierarchicalClusterPicker.h>
#include <iostream>
#include <utility>

namespace python = boost::python;
namespace RDPickers {

// REVIEW: the poolSize can be pulled from the numeric array
RDKit::INT_VECT MaxMinPicks(MaxMinPicker *picker, python::object distMat,
                            int poolSize, int pickSize,
                            python::object firstPicks, int seed) {
  if (pickSize >= poolSize) {
    throw ValueErrorException("pickSize must be less than poolSize");
  }

  if (!PyArray_Check(distMat.ptr())) {
    throw ValueErrorException("distance mat argument must be a numpy matrix");
  }

  PyArrayObject *copy;
  copy = (PyArrayObject *)PyArray_ContiguousFromObject(distMat.ptr(),
                                                       NPY_DOUBLE, 1, 1);
  double *dMat = (double *)PyArray_DATA(copy);

  RDKit::INT_VECT firstPickVect;
  for (unsigned int i = 0;
       i < python::extract<unsigned int>(firstPicks.attr("__len__")()); ++i) {
    firstPickVect.push_back(python::extract<int>(firstPicks[i]));
  }
  RDKit::INT_VECT res =
      picker->pick(dMat, poolSize, pickSize, firstPickVect, seed);
  Py_DECREF(copy);
  return res;
}

class pyobjFunctor {
 public:
  pyobjFunctor(python::object obj) : dp_obj(std::move(obj)) {}
  ~pyobjFunctor() {}
  double operator()(unsigned int i, unsigned int j) {
    return python::extract<double>(dp_obj(i, j));
  }

 private:
  python::object dp_obj;
};

namespace {
template <typename T>
void LazyMaxMinHelper(MaxMinPicker *picker, T functor, unsigned int poolSize,
                      unsigned int pickSize, python::object firstPicks,
                      int seed, RDKit::INT_VECT &res, double &threshold) {
  RDKit::INT_VECT firstPickVect;
  for (unsigned int i = 0;
       i < python::extract<unsigned int>(firstPicks.attr("__len__")()); ++i) {
    firstPickVect.push_back(python::extract<int>(firstPicks[i]));
  }
  res = picker->lazyPick(functor, poolSize, pickSize, firstPickVect, seed,
                         threshold);
}
}  // end of anonymous namespace

RDKit::INT_VECT LazyMaxMinPicks(MaxMinPicker *picker, python::object distFunc,
                                int poolSize, int pickSize,
                                python::object firstPicks, int seed,
                                python::object useCache) {
  if (useCache != python::object()) {
    BOOST_LOG(rdWarningLog) << "the useCache argument is deprecated and ignored"
                            << std::endl;
  }
  pyobjFunctor functor(distFunc);
  RDKit::INT_VECT res;
  double threshold = -1.;
  LazyMaxMinHelper(picker, functor, poolSize, pickSize, firstPicks, seed, res,
                   threshold);
  return res;
}
python::tuple LazyMaxMinPicksWithThreshold(
    MaxMinPicker *picker, python::object distFunc, int poolSize, int pickSize,
    double threshold, python::object firstPicks, int seed) {
  pyobjFunctor functor(distFunc);
  RDKit::INT_VECT res;
  LazyMaxMinHelper(picker, functor, poolSize, pickSize, firstPicks, seed, res,
                   threshold);
  return python::make_tuple(res, threshold);
}

// NOTE: TANIMOTO and DICE provably return the same results for the diversity
// picking this is still here just in case we ever later want to support other
//    methods.
typedef enum { TANIMOTO = 1, DICE } DistanceMethod;

template <typename BV>
class pyBVFunctor {
 public:
  pyBVFunctor(const std::vector<const BV *> &obj, DistanceMethod method)
      : d_obj(obj), d_method(method) {}
  ~pyBVFunctor() {}
  double operator()(unsigned int i, unsigned int j) {
    double res = 0.0;
    switch (d_method) {
      case TANIMOTO:
        res = 1. - TanimotoSimilarity(*d_obj[i], *d_obj[j]);
        break;
      case DICE:
        res = 1. - DiceSimilarity(*d_obj[i], *d_obj[j]);
        break;
      default:
        throw_value_error("unsupported similarity value");
    }
    return res;
  }

 private:
  const std::vector<const BV *> &d_obj;
  DistanceMethod d_method;
};

RDKit::INT_VECT LazyVectorMaxMinPicks(MaxMinPicker *picker, python::object objs,
                                      int poolSize, int pickSize,
                                      python::object firstPicks, int seed,
                                      python::object useCache) {
  if (useCache != python::object()) {
    BOOST_LOG(rdWarningLog) << "the useCache argument is deprecated and ignored"
                            << std::endl;
  }
  std::vector<const ExplicitBitVect *> bvs(poolSize);
  for (int i = 0; i < poolSize; ++i) {
    bvs[i] = python::extract<const ExplicitBitVect *>(objs[i]);
  }
  pyBVFunctor<ExplicitBitVect> functor(bvs, TANIMOTO);

  RDKit::INT_VECT res;
  double threshold = -1.;
  LazyMaxMinHelper(picker, functor, poolSize, pickSize, firstPicks, seed, res,
                   threshold);
  return res;
}

python::tuple LazyVectorMaxMinPicksWithThreshold(
    MaxMinPicker *picker, python::object objs, int poolSize, int pickSize,
    double threshold, python::object firstPicks, int seed) {
  std::vector<const ExplicitBitVect *> bvs(poolSize);
  for (int i = 0; i < poolSize; ++i) {
    bvs[i] = python::extract<const ExplicitBitVect *>(objs[i]);
  }
  pyBVFunctor<ExplicitBitVect> functor(bvs, TANIMOTO);

  RDKit::INT_VECT res;
  LazyMaxMinHelper(picker, functor, poolSize, pickSize, firstPicks, seed, res,
                   threshold);
  return python::make_tuple(res, threshold);
}

}  // end of namespace RDPickers

struct MaxMin_wrap {
  static void wrap() {
    python::class_<RDPickers::MaxMinPicker>(
        "MaxMinPicker",
        "A class for diversity picking of items using the MaxMin Algorithm\n")
        .def("Pick", RDPickers::MaxMinPicks,
             (python::arg("self"), python::arg("distMat"),
              python::arg("poolSize"), python::arg("pickSize"),
              python::arg("firstPicks") = python::tuple(),
              python::arg("seed") = -1),
             "Pick a subset of items from a pool of items using the MaxMin "
             "Algorithm\n"
             "Ashton, M. et. al., Quant. Struct.-Act. Relat., 21 (2002), "
             "598-604 \n\n"
             "ARGUMENTS:\n"
             "  - distMat: 1D distance matrix (only the lower triangle "
             "elements)\n"
             "  - poolSize: number of items in the pool\n"
             "  - pickSize: number of items to pick from the pool\n"
             "  - firstPicks: (optional) the first items to be picked (seeds "
             "the list)\n"
             "  - seed: (optional) seed for the random number generator\n")

        .def("LazyPick", RDPickers::LazyMaxMinPicks,
             (python::arg("self"), python::arg("distFunc"),
              python::arg("poolSize"), python::arg("pickSize"),
              python::arg("firstPicks") = python::tuple(),
              python::arg("seed") = -1,
              python::arg("useCache") = python::object()),
             "Pick a subset of items from a pool of items using the MaxMin "
             "Algorithm\n"
             "Ashton, M. et. al., Quant. Struct.-Act. Relat., 21 (2002), "
             "598-604 \n"
             "ARGUMENTS:\n\n"
             "  - distFunc: a function that should take two indices and return "
             "the\n"
             "              distance between those two points.\n"
             "              NOTE: the implementation caches distance values, "
             "so the\n"
             "              client code does not need to do so; indeed, it "
             "should not.\n"
             "  - poolSize: number of items in the pool\n"
             "  - pickSize: number of items to pick from the pool\n"
             "  - firstPicks: (optional) the first items to be picked (seeds "
             "the list)\n"
             "  - seed: (optional) seed for the random number generator\n"
             "  - useCache: IGNORED\n")
        .def("LazyBitVectorPick", RDPickers::LazyVectorMaxMinPicks,
             (python::arg("self"), python::arg("objects"),
              python::arg("poolSize"), python::arg("pickSize"),
              python::arg("firstPicks") = python::tuple(),
              python::arg("seed") = -1,
              python::arg("useCache") = python::object()),
             "Pick a subset of items from a pool of bit vectors using the "
             "MaxMin Algorithm\n"
             "Ashton, M. et. al., Quant. Struct.-Act. Relat., 21 (2002), "
             "598-604 \n"
             "ARGUMENTS:\n\n"
             "  - vectors: a sequence of the bit vectors that should be picked "
             "from.\n"
             "  - poolSize: number of items in the pool\n"
             "  - pickSize: number of items to pick from the pool\n"
             "  - firstPicks: (optional) the first items to be picked (seeds "
             "the list)\n"
             "  - seed: (optional) seed for the random number generator\n"
             "  - useCache: IGNORED.\n")

        .def("LazyPickWithThreshold", RDPickers::LazyMaxMinPicksWithThreshold,
             (python::arg("self"), python::arg("distFunc"),
              python::arg("poolSize"), python::arg("pickSize"),
              python::arg("threshold"),
              python::arg("firstPicks") = python::tuple(),
              python::arg("seed") = -1),
             "Pick a subset of items from a pool of items using the MaxMin "
             "Algorithm\n"
             "Ashton, M. et. al., Quant. Struct.-Act. Relat., 21 (2002), "
             "598-604 \n"
             "ARGUMENTS:\n\n"
             "  - distFunc: a function that should take two indices and return "
             "the\n"
             "              distance between those two points.\n"
             "              NOTE: the implementation caches distance values, "
             "so the\n"
             "              client code does not need to do so; indeed, it "
             "should not.\n"
             "  - poolSize: number of items in the pool\n"
             "  - pickSize: number of items to pick from the pool\n"
             "  - threshold: stop picking when the distance goes below this "
             "value\n"
             "  - firstPicks: (optional) the first items to be picked (seeds "
             "the list)\n"
             "  - seed: (optional) seed for the random number generator\n")
        .def("LazyBitVectorPickWithThreshold",
             RDPickers::LazyVectorMaxMinPicksWithThreshold,
             (python::arg("self"), python::arg("objects"),
              python::arg("poolSize"), python::arg("pickSize"),
              python::arg("threshold"),
              python::arg("firstPicks") = python::tuple(),
              python::arg("seed") = -1),
             "Pick a subset of items from a pool of bit vectors using the "
             "MaxMin Algorithm\n"
             "Ashton, M. et. al., Quant. Struct.-Act. Relat., 21 (2002), "
             "598-604 \n"
             "ARGUMENTS:\n\n"
             "  - vectors: a sequence of the bit vectors that should be picked "
             "from.\n"
             "  - poolSize: number of items in the pool\n"
             "  - pickSize: number of items to pick from the pool\n"
             "  - threshold: stop picking when the distance goes below this "
             "value\n"
             "  - firstPicks: (optional) the first items to be picked (seeds "
             "the list)\n"
             "  - seed: (optional) seed for the random number generator\n");
  };
};

void wrap_maxminpick() { MaxMin_wrap::wrap(); }