materialsproject / pymatgen

Python Materials Genomics (pymatgen) is a robust materials analysis code that defines classes for structures and molecules with support for many electronic structure codes. It powers the Materials Project.
https://pymatgen.org
Other
1.52k stars 867 forks source link

[BUG] Incorrect usage of `np.all() > tol` for element-wise value comparison #4166

Open DanielYang59 opened 2 days ago

DanielYang59 commented 2 days ago

The following method: https://github.com/materialsproject/pymatgen/blob/bd9fba9ec62437b5b62fbd0b2c2c723216cc5a2c/src/pymatgen/io/lobster/outputs.py#L1694-L1737

Used a pattern like np.array().all() > tolerance with the intention to perform element-wise value comparison, however this seems to be misused as all():

"Test whether all array elements along a given axis evaluate to True."

import numpy as np

arr = np.array([1, 2, 3])

print(arr.all())  # True

print(np.array([False, 1, 2]).all())  # False

Should be something like:

- if abs(band2 - 1.0).all() > limit_deviation:
# However this logic might need double check as it only evaluates to True 
# when ALL deviations are greater than threshold
+ if np.all(np.abs(band2 - 1) > limit_deviation):
# Perhaps it should be the following?
+ if np.any(np.abs(band2 - 1) > limit_deviation):

# The same to the other 
- elif band2.all() > limit_deviation:
+ elif np.all(band2 > limit_deviation)
# The following seems more sensible?
+ elif np.any(band2 > limit_deviation)

I might go ahead and change this if I didn't misunderstand anything? @JaGeo

JaGeo commented 2 days ago

@naik-aakash Could you kindly check this (after Thursday ;))? Thanks a lot.