-
Notifications
You must be signed in to change notification settings - Fork 873
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 ValueError when passing selective_dynamics
to Poscar
#2951
Conversation
Pls provide a unit test to confirm the new functionality. Thanks. |
@chiang-yuan Thanks! The test should go into |
Thanks @janosh ! I followed the original style to test by comparing two primitive python lists and converted numpy array back to list to assign variables. Since the system can be large and comparing two nested python lists is somewhat tricky, (For example, |
selective_dynamics
to Poscar
if selective_dynamics is not None: | ||
selective_dynamics = np.array(selective_dynamics) | ||
if not selective_dynamics.all(): | ||
site_properties["selective_dynamics"] = selective_dynamics |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chiang-yuan I simplified the code some. Was there a reason to convert np.array
to list before assigning to site_props
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@janosh I converted np.array back to list because the other unit test in io/vasp/tests/test_inputs.py
compare selective_dyanmics
with list, which will yield the error in pytest like the one after your revision.
> assert poscar.selective_dynamics == [[True, True, True], [False, False, False]]
E ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
/home/runner/work/pymatgen/pymatgen/pymatgen/io/vasp/tests/test_inputs.py:87: ValueError
I bypass them by converting it back to list (which is what the original code intended to do) and adding another unit test comparing by list as well. Do you think it is better to revise the previous unit test and compare all of them in the format of nd.array
like I suggested earlier?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make the test datatype agnostic so that it handles both arrays and lists. It's better for tests to care only about semantics and not equivalent ways of encoding the same meaning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Thanks @janosh !
Before this fix, users could not directly add a
selective_dynamics
array to create aPoscar
object:and the code would produce an error:
A similar issue was reported previously about manually using
add_site_property
.The minimal example reproducing the error and the bug fix is demonstrated in the attached notebook: poscar_debug.md
Summary
Include a summary of major changes in bullet points:
selective_dynamics
boolean array is given for creating aPoscar
objectvelocities
andpredictor_corrector