Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix zero division error in conformers #209

Merged
merged 4 commits into from
Aug 31, 2023
Merged

fix zero division error in conformers #209

merged 4 commits into from
Aug 31, 2023

Conversation

shuyana
Copy link
Contributor

@shuyana shuyana commented Aug 31, 2023

Changelogs

  • Fixed a division-by-zero error in dm.conformers.generate() when minimize_energy is set to True and the molecule has no rotatable bonds. Below is a minimal example that reproduces the error:

    import datamol as dm
    
    mol = dm.to_mol("c1ccccc1")
    dm.conformers.generate(mol, minimize_energy=True)

    Error message:

    /workspaces/datamol/datamol/conformers/_conformers.py:214: RuntimeWarning: invalid value encountered in scalar divide
    if E - minE <= ewindow and (E - minE) / rotatable_bonds <= eratio
    /workspaces/datamol/datamol/conformers/_conformers.py:214: RuntimeWarning: divide by zero encountered in scalar divide
      if E - minE <= ewindow and (E - minE) / rotatable_bonds <= eratio
    

Checklist:

  • Was this PR discussed in an issue? It is recommended to first discuss a new feature into a GitHub issue before opening a PR.
  • Add tests to cover the fixed bug(s) or the new introduced feature(s) (if appropriate).
  • Update the API documentation is a new function is added, or an existing one is deleted.
  • Write concise and explanatory changelogs below.
  • If possible, assign one of the following labels to the PR: feature, fix or test (or ask a maintainer to do it for you).

discussion related to that PR

@shuyana shuyana requested a review from hadim as a code owner August 31, 2023 18:22
@hadim hadim added the fix label Aug 31, 2023
@hadim
Copy link
Contributor

hadim commented Aug 31, 2023

Thanks, @shuyana and nice catch for the bug!

Do you mind adding a test for it?

@codecov
Copy link

codecov bot commented Aug 31, 2023

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% <ø> (ø)

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

@shuyana
Copy link
Contributor Author

shuyana commented Aug 31, 2023

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
Copy link
Contributor

hadim commented Aug 31, 2023

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
Copy link
Contributor Author

shuyana commented Aug 31, 2023

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

@hadim hadim merged commit c0b6450 into datamol-io:main Aug 31, 2023
16 checks passed
@shuyana shuyana deleted the fix-zero-division branch August 31, 2023 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants