Files
rdkit/Code/DataStructs/Wrap/DataStructs.cpp
Greg Landrum c90cee9b77 Add Molecular Interaction Fields (#7993)
* Add RealValueVect.

* Add UniformRealValueGrid3D

* Add Molecular Interaction Fields (MIFs)

* line endings

* cherry-pick f1bc94a4c8

* format

* Adapt tests for python3.

* Adapt RealValueVector pickling for python3.

* Speed-up of MIF calculations.

* Bugfix in MIFDescriptors.cpp.

* all tests pass

* clean up some memory leaks

* update copyrights

* rename

* rename the library

* complete the rename

* lost file

* another forgotten file

* cleanup

* clang-tidy

* clang-tidy

* windows DLL builds work

* python wrapper and tests cleanup

* convert to catch2 testing

* switch RealValueVect to use std::vector

* remove obsolete friend

* - Replace explicit loops with stdlib implicit equivalents
- Replace explicit types with auto where possible
- Avoid unnecessary copy operations where possible
- Replace raw pointers with exception-safe unique_ptr
- Replace C-style #define with constexpr
- Replace C-style casts with C++ casts
- Replace C-style arrays with std::vector
- Avoid code duplication with templated operators
- Replace VdWaals class taking multiple atom type definitions and force-field name as string parameter with force-field-specific classes deriving from an abstract VdWaals class
- Replace x,y,z doubles with Point3D class where possible
- Removed unused (and untested) DistanceToClosestAtom class
- Renamed some variables and functions for better clarity
- Converted tabs to spaces
- Made the mol parameter in cube read/write functions optional for convenience
- Made the Python wrappers more pythonic (e.g., avoid C++-style passing objects as parameters which are modified in place)
- Implemented alternative Python class constructors using boost::python::make_constructor rather than with external non-class functions
- The Python wrappers taking a sequence of Point3D now take a sequence of sequences, such that the output of Conformer.GetPositions() can be passed
- Made the Python wrapper sequence parsing more robust
- Removed duplicated code from Python wrappers

* - avoid an unnecessary copy

* progress

* works

* more cleanup

* all tests pass

* changes in response to review

---------

Co-authored-by: dfhahn <dfhahn@users.noreply.github.com>
Co-authored-by: ptosco <paolo.tosco@novartis.com>
2024-12-14 17:08:43 +01:00

120 lines
4.2 KiB
C++

// $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 <RDBoost/python.h>
#include <RDBoost/Wrap.h>
#include <DataStructs/BitVects.h>
#include <DataStructs/DiscreteValueVect.h>
#include <DataStructs/SparseIntVect.h>
#include <DataStructs/RealValueVect.h>
#include <RDBoost/boost_numpy.h>
#include <numpy/npy_common.h>
#include <RDBoost/import_array.h>
#include <RDBoost/pyint_api.h>
namespace python = boost::python;
void wrap_SBV();
void wrap_EBV();
void wrap_BitOps();
void wrap_Utils();
void wrap_discreteValVect();
void wrap_realValVect();
void wrap_sparseIntVect();
void wrap_FPB();
namespace {
template <typename T, typename U>
void converter(const T &v, python::object destArray, U func) {
if (!PyArray_Check(destArray.ptr())) {
throw_value_error("Expecting a Numeric array object");
}
auto *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 = func(v[i]);
PyArray_SETITEM(destP, static_cast<char *>(PyArray_GETPTR1(destP, i)),
iItem);
Py_DECREF(iItem);
}
}
} // namespace
template <typename T>
void convertToIntNumpyArray(const T &v, python::object destArray) {
converter(v, destArray, PyLong_FromLong);
}
template <typename T>
void convertToDoubleNumpyArray(const T &v, python::object destArray) {
converter(v, destArray, PyFloat_FromDouble);
}
BOOST_PYTHON_MODULE(cDataStructs) {
rdkit_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";
wrap_Utils();
wrap_SBV();
wrap_EBV();
wrap_BitOps();
wrap_discreteValVect();
wrap_realValVect();
wrap_sparseIntVect();
wrap_FPB();
python::def(
"ConvertToNumpyArray",
(void (*)(const ExplicitBitVect &, python::object))convertToIntNumpyArray,
(python::arg("bv"), python::arg("destArray")));
python::def("ConvertToNumpyArray",
(void (*)(const RDKit::DiscreteValueVect &,
python::object))convertToIntNumpyArray,
(python::arg("bv"), python::arg("destArray")));
python::def("ConvertToNumpyArray",
(void (*)(const RDKit::SparseIntVect<std::int32_t> &,
python::object))convertToIntNumpyArray,
(python::arg("bv"), python::arg("destArray")));
python::def("ConvertToNumpyArray",
(void (*)(const RDKit::SparseIntVect<boost::int64_t> &,
python::object))convertToIntNumpyArray,
(python::arg("bv"), python::arg("destArray")));
python::def("ConvertToNumpyArray",
(void (*)(const RDKit::SparseIntVect<std::uint32_t> &,
python::object))convertToIntNumpyArray,
(python::arg("bv"), python::arg("destArray")));
python::def("ConvertToNumpyArray",
(void (*)(const RDKit::SparseIntVect<boost::uint64_t> &,
python::object))convertToIntNumpyArray,
(python::arg("bv"), python::arg("destArray")));
python::def("ConvertToNumpyArray",
(void (*)(const RDKit::RealValueVect &,
python::object))convertToDoubleNumpyArray,
(python::arg("rvv"), python::arg("destArray")));
}