Hi All,

I’m attempting to write my own small RDKit extension library to store C++ 
functions I’ll write which will be called from Python.  To start out, I have 
one function, which is a slightly modified RunReactants from the 2018.03 
release.  Previously, I directly modified this function in the RDKit source 
code, and then compiled the whole RDKit myself to use it (from python).  This 
approach worked fine, but made it hard to update the RDKit to new versions and 
made other code organization difficult.  I imagine as I make more 
changes/additions on the C++ side, this will get more awkward.

I used the following steps to set up my extension library as it currently is:

  1.  Installed RDKit using Conda (2018.03 release with python 3.6)
  2.  Copied ReactionRunner.h and ReactionRunner.cpp and made my changes
  3.  Added a top level library.h file that includes my ReactionRunner changes 
and wraps them for python using the syntax used for RunReactants in RDKit
  4.  Added a cmake file
  5.  Compiled my library (on osx 10.13.4 using the XCode compiler)
  6.  Copied my library from where I built it into my conda python 
site-packages directory so that it can be imported by my conda python

Currently, I can compile and import my library into python just fine, but I get 
a segmentation fault when I try to call my RunReactants function.  I’m new at 
using boost python to wrap C++ functions, so I’m hoping I’m missing something 
obvious!  I’ve tried using a debugger (lldb) to track down my error, but am 
having trouble parsing the output.

If anyone has an idea for what is going wrong with my setup, or can point me to 
general tutorials for how to get something like this working, I’m a little 
stuck at the moment.

Thanks!


  *   Kovas

Contents of my library.h file (ignore the hello function definition):

#ifndef RDKIT_EXTENSION_LIBRARY_H
#define RDKIT_EXTENSION_LIBRARY_H

// #include <python.hpp>
#include <RDBoost/python.h>
#include <RDBoost/Wrap.h>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

#include <GraphMol/ROMol.h>
#include <GraphMol/ChemReactions/Reaction.h>
#include <RDGeneral/Exceptions.h>
#include <GraphMol/SanitException.h>
#include <GraphMol/ChemReactions/ReactionUtils.h>

// custom reaction runner header file
#include "ReactionRunner.h"

void hello();

namespace python = boost::python;

template <typename T>
PyObject *RunReactants(RDKit::ChemicalReaction *rxn, T reactants) {
    if (!rxn->isInitialized()) {
        NOGIL gil;
        rxn->initReactantMatchers();
    }
    RDKit::MOL_SPTR_VECT reacts;
    unsigned int len1 =
            python::extract<unsigned int>(reactants.attr("__len__")());
    reacts.resize(len1);
    for (unsigned int i = 0; i < len1; ++i) {
        reacts[i] = python::extract<RDKit::ROMOL_SPTR>(reactants[i]);
        if (!reacts[i]) throw_value_error("reaction called with None 
reactants");
    }
    std::vector<RDKit::MOL_SPTR_VECT> mols;
    {
        NOGIL gil;
        mols = rxn->runReactants(reacts);
    }
    PyObject *res = PyTuple_New(mols.size());

    for (unsigned int i = 0; i < mols.size(); ++i) {
        PyObject *lTpl = PyTuple_New(mols[i].size());
        for (unsigned int j = 0; j < mols[i].size(); ++j) {
            PyTuple_SetItem(lTpl, j,
                            
python::converter::shared_ptr_to_python(mols[i][j]));
        }
        PyTuple_SetItem(res, i, lTpl);
    }
    return res;
}

BOOST_PYTHON_MODULE(librdkit_extension) {
    // def("hello", hello);
    python::def("RunReactants", (PyObject * (*)(RDKit::ChemicalReaction *, 
python::tuple))RunReactants);
}

#endif
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to