File: BitCorrMatGenerator.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 (95 lines) | stat: -rw-r--r-- 3,467 bytes parent folder | download | duplicates (2)
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
// $Id$
//
//  Copyright (C) 2003-2008 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
#include <RDBoost/python.h>
#define PY_ARRAY_UNIQUE_SYMBOL rdinfotheory_array_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>

#include <RDBoost/Wrap.h>
#include <RDBoost/PySequenceHolder.h>
#include <ML/InfoTheory/CorrMatGenerator.h>
#include <RDGeneral/types.h>

namespace python = boost::python;

namespace RDInfoTheory {

PyObject *getCorrMatrix(BitCorrMatGenerator *cmGen) {
  double *dres = cmGen->getCorrMat();
  unsigned int nb = cmGen->getCorrBitList().size();
  npy_intp dim = nb * (nb - 1) / 2;
  PyArrayObject *res = (PyArrayObject *)PyArray_SimpleNew(1, &dim, NPY_DOUBLE);
  memcpy(static_cast<void *>(PyArray_DATA(res)), static_cast<void *>(dres),
         dim * sizeof(double));
  return PyArray_Return(res);
}

void setBitList(BitCorrMatGenerator *cmGen, python::object bitList) {
  PySequenceHolder<int> blist(bitList);
  unsigned int nb = blist.size();
  RDKit::INT_VECT res;
  res.reserve(nb);
  for (unsigned int i = 0; i < nb; i++) {
    res.push_back(blist[i]);
  }
  cmGen->setBitIdList(res);
}

void CollectVotes(BitCorrMatGenerator *cmGen, python::object bitVect) {
  python::extract<ExplicitBitVect> ebvWorks(bitVect);
  python::extract<SparseBitVect> sbvWorks(bitVect);
  if (ebvWorks.check()) {
    ExplicitBitVect ev = python::extract<ExplicitBitVect>(bitVect);
    cmGen->collectVotes(ev);
  } else if (sbvWorks.check()) {
    SparseBitVect sv = python::extract<SparseBitVect>(bitVect);
    cmGen->collectVotes(sv);
  } else {
    throw_value_error(
        "CollectVote can only take ExplicitBitVects or SparseBitVects");
  }
}

struct corrmat_wrap {
  static void wrap() {
    std::string docString =
        "A class to generate a pariwise correlation matrix between a list of "
        "bits\n"
        "The mode of operation for this class is something like this\n"
        "   >>> cmg = BitCorrMatGenerator() \n"
        "   >>> cmg.SetBitList(blist) \n"
        "   >>> for fp in fpList:  \n"
        "   >>>    cmg.CollectVotes(fp)  \n"
        "   >>> corrMat = cmg.GetCorrMatrix() \n"
        "    \n"
        "   The resulting correlation matrix is a one dimensional nummeric "
        "array containing the \n"
        "   lower triangle elements\n";
    python::class_<BitCorrMatGenerator>("BitCorrMatGenerator",
                                        docString.c_str())
        .def("SetBitList", setBitList,
             "Set the list of bits that need to be correllated\n\n"
             " This may for example be ther top ranking ensemble bits\n\n"
             "ARGUMENTS:\n\n"
             "  - bitList : an integer list of bit IDs\n")
        .def("CollectVotes", CollectVotes,
             "For each pair of on bits (bi, bj) in fp increase the correlation "
             "count for the pair by 1\n\n"
             "ARGUMENTS:\n\n"
             "  - fp : a bit vector to collect the fingerprints from\n")
        .def("GetCorrMatrix", getCorrMatrix,
             "Get the correlation matrix following the collection of votes "
             "from a bunch of fingerprints\n");
  };
};
}

void wrap_corrmatgen() { RDInfoTheory::corrmat_wrap::wrap(); }