Skip to content

UFloat Warnings with ComputedEntry.energy #4386

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

Open
kavanase opened this issue May 2, 2025 · 0 comments
Open

UFloat Warnings with ComputedEntry.energy #4386

kavanase opened this issue May 2, 2025 · 0 comments
Labels

Comments

@kavanase
Copy link
Contributor

kavanase commented May 2, 2025

Python version

Python 3.10-3.12

Pymatgen version

2025.5.2

Operating system version

No response

Current behavior

From running our doped tests with the latest pymatgen release, we get many of these warnings (which breaks tests):

Using UFloat objects with std_dev==0 may give unexpected results.

From setting the warning level to error, the traceback shows:

File ~/Packages/doped/doped/core.py:1428, in DefectEntry.sc_entry_energy(self)
   1425     return self._sc_entry_energy
   1427 self._sc_entry_hash = hash(self.sc_entry)
-> 1428 self._sc_entry_energy = self.sc_entry.energy
   1430 return self._sc_entry_energy

File ~/Packages/pymatgen/src/pymatgen/entries/computed_entries.py:350, in ComputedEntry.energy(self)
    347 @property
    348 def energy(self) -> float:
    349     """The *corrected* energy of the entry."""
--> 350     return self.uncorrected_energy + self.correction

File ~/Packages/pymatgen/src/pymatgen/entries/computed_entries.py:367, in ComputedEntry.correction(self)
    362 """
    363 Returns:
    364     float: the total energy correction / adjustment applied to the entry in eV.
    365 """
    366 # adds to ufloat(0.0, 0.0) to ensure that no corrections still result in ufloat object
--> 367 corr = ufloat(0.0, 0.0) + sum(ufloat(ea.value, ea.uncertainty) for ea in self.energy_adjustments)
    368 return corr.nominal_value

File ~/miniconda3/lib/python3.10/site-packages/uncertainties/core.py:1024, in ufloat(nominal_value, std_dev, tag)
    992 """
    993 Create an uncertainties Variable
    994 
   (...)
   1021    and `tag` which match the input values.
   1022 """
   1023 if std_dev == 0:
-> 1024     warn("Using UFloat objects with std_dev==0 may give unexpected results.")
   1025 return Variable(nominal_value, std_dev, tag=tag)

UserWarning: Using UFloat objects with std_dev==0 may give unexpected results.

I think this is due to a recent update in uncertainties, which now includes this warning, which occurs when there are no energy adjustments with uncertainties in ComputedEntry.energy_adjustments. Some discussion here: lmfit/lmfit-py#999

Expected Behavior

Not totally sure, but it seems like that warning is there to stay, so might make sense to catch/handle it here somehow?
Only use ufloat when there is an energy adjustment with non-zero uncertainty, otherwise just normal floats for correction values?

Minimal example

from pymatgen.entries.computed_entries import ComputedEntry

ce = ComputedEntry(composition="H", energy=1)
ce.energy  # shows warning

Relevant files to reproduce this bug

No response

@kavanase kavanase added the bug label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant