File: DataStructs.cpp

package info (click to toggle)
rdkit 201403-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 62,288 kB
  • ctags: 15,156
  • sloc: cpp: 125,376; python: 55,674; java: 4,831; ansic: 4,178; xml: 2,499; sql: 1,775; yacc: 1,551; lex: 1,051; makefile: 353; fortran: 183; sh: 148; cs: 93
file content (82 lines) | stat: -rw-r--r-- 2,634 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
// $Id$
//
// Copyright (c) 2001-2006 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 PY_ARRAY_UNIQUE_SYMBOL rddatastructs_array_API

#include <boost/python.hpp>
#include <RDBoost/Wrap.h>
#include <DataStructs/BitVects.h>
#include <DataStructs/DiscreteValueVect.h>
#include "DataStructs.h"
#include <boost/python/numeric.hpp>
#include <numpy/npy_common.h>
#include <numpy/arrayobject.h>


namespace python = boost::python;

void wrap_SBV();
void wrap_EBV();
void wrap_BitOps();
void wrap_Utils();
void wrap_discreteValVect();
void wrap_sparseIntVect();

template <typename T>
void convertToNumpyArray(const T &v,python::object destArray){
  if (!PyArray_Check(destArray.ptr())) {
    throw_value_error("Expecting a Numeric array object");
  }
  PyArrayObject *destP=(PyArrayObject *)destArray.ptr();
  npy_intp ndims[1];
  ndims[0]=v.size();
  PyArray_Dims dims;
  dims.ptr=ndims;
  dims.len=1;
  PyArray_Resize(destP,&dims,0,NPY_ANYORDER);
  for(unsigned int i=0;i<v.size();++i){
    PyObject *iItem = PyInt_FromLong(v[i]);
    PyArray_SETITEM(destP,PyArray_GETPTR1(destP,i),iItem);
  }
}



BOOST_PYTHON_MODULE(cDataStructs)
{
  import_array();
  python::scope().attr("__doc__") =
    "Module containing an assortment of functionality for basic data structures.\n"
    "\n"
    "At the moment the data structures defined are:\n"
    "  Bit Vector classes (for storing signatures, fingerprints and the like:\n"
    "    - ExplicitBitVect: class for relatively small (10s of thousands of bits) or\n"
    "                       dense bit vectors.\n"
    "    - SparseBitVect:   class for large, sparse bit vectors\n"
    "  DiscreteValueVect:   class for storing vectors of integers\n"
    "  SparseIntVect:       class for storing sparse vectors of integers\n"
    ;
  
  python::register_exception_translator<IndexErrorException>(&translate_index_error);
  python::register_exception_translator<ValueErrorException>(&translate_value_error);

  wrap_Utils();
  wrap_SBV();
  wrap_EBV();
  wrap_BitOps();
  wrap_discreteValVect();
  wrap_sparseIntVect();

  python::def("ConvertToNumpyArray", (void (*)(const ExplicitBitVect &,python::object))convertToNumpyArray,
              (python::arg("bv"),python::arg("destArray")));
  python::def("ConvertToNumpyArray", (void (*)(const RDKit::DiscreteValueVect &,python::object))convertToNumpyArray,
              (python::arg("bv"),python::arg("destArray")));

}