File: BitCorrMatGenerator.cpp

package info (click to toggle)
rdkit 201203-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 37,840 kB
  • sloc: cpp: 93,902; python: 51,897; java: 5,192; ansic: 3,497; xml: 2,499; sql: 1,641; yacc: 1,518; lex: 1,076; makefile: 325; fortran: 183; sh: 153; cs: 51
file content (97 lines) | stat: -rw-r--r-- 3,492 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
// $Id: BitCorrMatGenerator.cpp 1528 2010-09-26 17:04:37Z glandrum $
//
//  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 <boost/python.hpp>
#define PY_ARRAY_UNIQUE_SYMBOL rdinfotheory_array_API
#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 *>(res->data),
           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();
}