27 Commits

Author SHA1 Message Date
Emily Rhodes
3836049ab2 Add optional default_val parameter to GetProp() (#9242)
* SHARED-12256: Add test and change function.

* SHARED-12256: Update to only wrapping changes.

* SHARED-12256: Parameterize tests.

* SHARED-12256: GetPropIfPresent changes.

* Revert "SHARED-12256: GetPropIfPresent changes."

This reverts commit f598f8c161.

* SHARED-12256: Make default the keyword in the boost wrappings.

* SHARED-12256: Overload function instead of using a sentinel.

* SHARED-12256: Extend GetProp changes.

* SHARED-12256: Add entry point for tests and fix tests.
2026-05-06 06:09:11 +02:00
Yakov Pechersky
872b054d5c Defer numpy initialization to first use (#9127)
* Defer numpy initialization to first use in rdchem, rdmolops, cDataStructs

`from rdkit import Chem` unconditionally bootstrapped numpy (~120ms) via
import_array()/boost::python::numpy::initialize() in module init functions,
even when no numpy-dependent APIs were called. This is costly in cold-start
environments like AWS Lambda.

Move numpy initialization behind lazy guards (static bool + first-call init)
in rdchem.so, rdmolops.so, and cDataStructs.so. Numpy now loads only when
an API that actually needs it is invoked (GetDistanceMatrix, GetPositions,
SetPositions, GetAdjacencyMatrix, ConvertToNumpyArray, etc.).

Also change Conformer::SetPos to accept python::object instead of
np::ndarray to prevent Boost.Python from requiring numpy type conversion
before the lazy guard runs.

Adds test_lazy_numpy.py with subprocess-based tests verifying:
- `from rdkit import Chem` does not load numpy
- SmilesToMol/MolToSmiles work without numpy
- numpy loads on demand when array APIs are called

* skip inchi tests if not available

* switch to threadsafe once_flag, like elsewhere

* finish ifdef style

* switch to magic static style

* Revert "switch to magic static style"

This reverts commit 7300188db7.
2026-02-23 18:42:42 +01:00
Ricardo Rodriguez
a4b63d7df5 Minor refactor of the python wrappers (#8847)
* refactor python wrappers

* fix FilterHierarchyMatcher converted already registered warning
2025-10-05 09:42:31 +02:00
Brian Kelley
d304f9f416 Trim spaces from RDProp strings to simulate reading from SDFiles (#8760)
* Trim spaces from RDProp strings to simulate reading from SDFiles

* Update documentation

* Use the correct doc strings

---------

Co-authored-by: Brian Kelley <bkelley@glysade.com>
2025-09-26 06:14:23 +02:00
Dan Nealschneider
3facd882d6 Speed up GetProp Python keyerrors (#8372)
* Speed up GetProp Python keyerrors

A common pattern _in Python_ for checking for the presence or
absence of a key is:

    try:
       return mol.GetProp('mykey')
    except KeyError:
       return None

Shockingly, this is really slow with boost python objects! I was
recently profiling a workflow and 90% of the time or more was
spent in failed GetProp calls (mostly on bonds, some on atoms
or mols).

I sped up the workflow by protecting the calls using HasProp. But
I think this is a silly trap we've set for our users.

The problem comes because boost::python uses a C++ exception to
indicate that there is already a Python exception set. In C++,
exceptions are slow - they require unrolling a stack. In Python,
exceptions are about the same speed as any other control flow!

This commit speeds up GetProp failures by circumventing the
boost throw_exception_already_set() mechanism.

In my testing, this speeds up failed GetProp substantially:

* Factor of 1000x on Mac
* Factor of 40x on Linux

* Update typed GetXXXProp to bypass boost exceptions

Based on PR #8372

Updates the typed GetIntProp, GetDoubleProp, etc to bypass C++
exceptions in access. This speeds up missing key errors
significantly - for instance, calling mol. GetIntProp with a
missing prop 100,000 times:

Before: 28s
After:  0.05s
2025-04-07 13:55:22 +02:00
Richard Gowers
8dbe2728f5 fix SetPositions when using strided numpy array (#8150)
* fix SetPositions when using strided numpy array

previously SetPositions assumed that the provided numpy array used contiguous-C stride patterns

* cast to a const pointer to avoid compiler warning
2025-01-09 07:17:08 +01:00
Greg Landrum
da6cd73168 Run clang-format across everything (#7849)
* run clang-format-18 across Code/*.cpp and Code/*.h

* run clang-format-18 across External
2024-09-26 13:39:02 +02:00
Brian Kelley
806a5e931a Add python Conformer.SetPositions wrapper (#7449)
* Add python Conformer.SetPositions wrapper

* Don't register scalar converters, this changes ALL docstrings

* Change TypeError to ValueError

* Remove comment

* Use CXSMiles for coords

* numpy2 compatibility

---------

Co-authored-by: Brian Kelley <bkelley@relaytx.com>
Co-authored-by: Greg Landrum <greg.landrum@gmail.com>
2024-06-25 04:38:31 +02:00
Paolo Tosco
c43f6d363a - enable building stubs in both in-tree and out-of-tree builds (#6980)
with cmake --build . --target stubs (also make stubs on *NIX)
- improved the patching script to do a better assignment of
  overloaded constructor parameters, whcih results in a number
  of docstring fixes

Co-authored-by: ptosco <paolo.tosco@novartis.com>
2023-12-13 04:49:34 +01:00
Paolo Tosco
2b4202867e Add Python modules to generate stubs and automatically patch docstrings (#6919)
* - added gen_rdkit_stubs Python module to generate rdkit-stubs
- added patch_rdkit_docstrings Python module to patch existing C++ sources to fix docstrings missing self parameter and add named parameters taken from C++ signatures where possible
- added rdkit-stubs/CMakeLists.txt to build rdkit-stubs as part of the RDKit build
- added an option to CMakeLists.txt to enable building rdkit-stubs as part of the RDKit build (defaults to OFF)

* fixed CMakeLists.txt, rdkit-stubs/CMakeLists.txt and a doctest

* - added missing cmp_func parameter
- fixed case with overloads with optional parameters
- do not trim params if expected_param_count == -1
- add dummy parameter names if we could not find any
- keep into account member functions when making up parameter names
- address __init__ and make_constructor __init__ functions
- fix incorrectly assigned staticmethods

* patched sources

* address residual few remarks

---------

Co-authored-by: ptosco <paolo.tosco@novartis.com>
2023-11-30 04:54:18 +01:00
Brian Kelley
ef234130f8 Optimize GetPropsFromDict: use tags for conversion (#6355)
* Optimize GetPropsFromDict, use tags for conversion, not the try and fail technique

* Autoconvert strings to ints and bools if possible

* Add autoConvert option to GetPropsAsDict default=true

* Update Code/GraphMol/Wrap/props.hpp

Co-authored-by: Greg Landrum <greg.landrum@gmail.com>

* change autoConvert to autoConvertStrings, add failed datatype conversion notices

* Fix Invariant usage

* Fix namespace for string

* Add GetProp(key, autoConvert) to allow for converting only what you want

* Make TestSetProps private

* Get _TestSetProps from rdmolops

---------

Co-authored-by: Greg Landrum <greg.landrum@gmail.com>
Co-authored-by: Brian Kelley <bkelley@relaytx.com>
2023-05-25 09:50:08 -04:00
Greg Landrum
1f4584b2ca run clang_format (#5676) 2022-11-01 04:14:26 +01:00
Eisuke Kawashima
11532089de Run clang-format against cpp (#4358) 2021-10-20 04:25:27 +02:00
Paolo Tosco
145828967f Fixes #3492 (#3527)
* fixes #3492

* added missing const
2020-11-03 06:35:07 +01:00
Greg Landrum
d41752d558 run clang-tidy with readability-braces-around-statements (#2899)
* run clang-tidy with readability-braces-around-statements
clang-format the results
clean up all the parts that clang-tidy-8 broke

* fix problem on windows
2020-01-25 14:19:32 +01:00
Eisuke Kawashima
5cd27a242f Fix typo (#2862)
* Fix typo

* Reflect the comments

* Fix more typos
2019-12-31 06:43:27 +01:00
Greg Landrum
bc0b177ceb Fixes #2450 (#2457)
* Fixes #2450
adds `hasOwningMol()` to the public API for Bond and Atom

* add to other classes;
add to python wrappers

* get the smiles generation partially working

* changes in response to review
2019-05-22 09:23:44 -04:00
Greg Landrum
0ea29df940 Fixes #2441 (#2443)
* add RDProps mixin class to Conformer

* test pickling

* add the props interface to the python wrapper for Conformers
2019-05-09 11:25:40 -04:00
Jan Domański
56234066b8 Conformer GetPos returns a numpy array rather than a tuple of tuples (#1087)
* Conformer GetPos returns a numpy array rather than a tuple of tuples

* Add missing method binding to GetPositions for a conformer

* Minor fixes
- avoid copying the coordinates,
- add a test
2016-10-14 05:02:59 +02:00
Brian Kelley
c4610271df Disambiguates call to SetAtomPosition 2015-11-29 20:34:11 -05:00
Greg Landrum
e08e0d16d8 first pass, using google style 2015-11-14 14:58:11 +01:00
Brian Kelley
403a3d6b7c Suppresses Boost warnings 2015-10-18 13:41:03 -04:00
Greg Landrum
ad2365a35a Fixes #545 2015-07-28 15:14:48 +02:00
Greg Landrum
f3fbef45c5 update copyright statements 2010-09-26 17:04:37 +00:00
Greg Landrum
c86ee3fcf6 implement and test feature request 1906758 :
support dimensionality flag on conformations
https://sourceforge.net/tracker/index.php?func=detail&aid=1906758&group_id=160139&atid=814653
2008-03-04 07:06:26 +00:00
Greg Landrum
5d03333c22 setup svn keywords (should have done this before import... grn) 2006-05-06 22:54:39 +00:00
Greg Landrum
75a79b6327 initial import 2006-05-06 22:20:08 +00:00