datamol-io / datamol

Molecular Processing Made Easy.
https://docs.datamol.io
Apache License 2.0
469 stars 51 forks source link

fix zero division error in conformers #209

Closed shuyana closed 1 year ago

shuyana commented 1 year ago

Changelogs


Checklist:


discussion related to that PR

hadim commented 1 year ago

Thanks, @shuyana and nice catch for the bug!

Do you mind adding a test for it?

codecov[bot] commented 1 year ago

Codecov Report

Merging #209 (100fd4c) into main (2619c61) will not change coverage. The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #209   +/-   ##
=======================================
  Coverage   91.94%   91.94%           
=======================================
  Files          46       46           
  Lines        3826     3826           
=======================================
  Hits         3518     3518           
  Misses        308      308           
Flag Coverage Δ
unittests 91.94% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
datamol/conformers/_conformers.py 93.16% <ø> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

shuyana commented 1 year ago

Thank you for the quick reply! I think the existing test, test_conformer_energy(), covers the case where rotatable_bonds is not zero. So, I've added a test for the case where rotatable_bonds is zero:

def test_conformer_no_rotatable_bonds():
    mol = dm.to_mol("c1ccccc1")

    random.seed(42)
    np.random.seed(42)
    mol1 = dm.conformers.generate(mol, minimize_energy=True)

    random.seed(42)
    np.random.seed(42)
    mol2 = dm.conformers.generate(mol, minimize_energy=True, eratio=3)

    # `eratio` should be ignored for this molecule as it has no rotatable bonds
    assert mol1.GetNumConformers() == mol2.GetNumConformers()

This test fails without the fix because (E - minE) / rotatable_bonds is inf and always greater than the finite eratio for mol2. On the other hand, the test passes with the fix because (E - minE) / rotatable_bonds is not used when rotatable_bonds is zero.

If you have any comments or suggestions, please let me know!

hadim commented 1 year ago

Thanks. That sounds good to me.

I am good to merge here once the ruff and black CI errors are gone (likely as simple as running those locally).

shuyana commented 1 year ago

Thanks for the review! I've fixed the CI errors and pushed the changes.