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

Glob logic with bader_analysis_from_path is error-prone #4089

Open
Andrew-S-Rosen opened this issue Oct 1, 2024 · 0 comments
Open

Glob logic with bader_analysis_from_path is error-prone #4089

Andrew-S-Rosen opened this issue Oct 1, 2024 · 0 comments
Labels

Comments

@Andrew-S-Rosen
Copy link
Member

Andrew-S-Rosen commented Oct 1, 2024

Python version

3.11.9

Pymatgen version

2024.9.17.1

Operating system version

Linux

Current behavior

When calling bader_analysis_from_path, I get an indexing error due to poor glob logic.

Expected Behavior

No indexing error

Minimal example

The following files are taken directly from MP.

from monty.serialization import loadfn
from pymatgen.command_line.bader_caller import bader_analysis_from_path

chgcar = loadfn('CHGCAR.json.gz')['data']
aeccar0 = loadfn('AECCAR0.json.gz')['data']
aeccar2 = loadfn('AECCAR2.json.gz')['data']

chgcar.write_file('CHGCAR')
aeccar0.write_file('AECCAR0')
aeccar2.write_file('AECCAR2')
bader_analysis_from_path('.')

Traceback:

File /scratch/gpfs/asrosen/software/miniconda/envs/quacc/lib/python3.11/site-packages/pymatgen/command_line/bader_caller.py:537, in bader_analysis_from_path(path, suffix)
    535 if not aeccar0_path:
    536     warnings.warn("Could not find AECCAR0, interpret Bader results with severe caution!")
--> 537 aeccar0 = Chgcar.from_file(aeccar0_path) if aeccar0_path else None
    539 aeccar2_path = _get_filepath("AECCAR2")
    540 if not aeccar2_path:

File /scratch/gpfs/asrosen/software/miniconda/envs/quacc/lib/python3.11/site-packages/pymatgen/io/vasp/outputs.py:3711, in Chgcar.from_file(cls, filename)
   3701 @classmethod
   3702 def from_file(cls, filename: str) -> Self:
   3703     """Read a CHGCAR file.
   3704
   3705     Args:
   (...)
   3709         Chgcar
   3710     """
-> 3711     poscar, data, data_aug = VolumetricData.parse_file(filename)
   3712     return cls(poscar, data, data_aug=data_aug)

File /scratch/gpfs/asrosen/software/miniconda/envs/quacc/lib/python3.11/site-packages/pymatgen/io/vasp/outputs.py:3562, in VolumetricData.parse_file(filename)
   3557     data_aug = {
   3558         "total": all_dataset_aug.get(0),
   3559         "diff": all_dataset_aug.get(1),
   3560     }
   3561 else:
-> 3562     data = {"total": all_dataset[0]}
   3563     data_aug = {"total": all_dataset_aug.get(0)}
   3564 return poscar, data, data_aug

IndexError: list index out of range

This is because of the following code, which is looking for the .json.gz files instead of the newly written out files:

def _get_filepath(filename: str, msg: str = "") -> str | None:
paths = glob(glob_pattern := f"{path}/{filename}{suffix}*")
if len(paths) == 0:
warnings.warn(msg or f"no matches for {glob_pattern=}")
return None
if len(paths) > 1:
# using reverse=True because, if multiple files are present,
# they likely have suffixes 'static', 'relax', 'relax2', etc.
# and this would give 'static' over 'relax2' over 'relax'
# however, better to use 'suffix' kwarg to avoid this!
paths.sort(reverse=True)
warnings.warn(f"Multiple files detected, using {os.path.basename(path)}")
return paths[0]

Relevant files to reproduce this bug

CHGCAR.json.gz
AECCAR0.json.gz
AECCAR2.json.gz

CC @aryannsaha

@Andrew-S-Rosen Andrew-S-Rosen changed the title List index out of range when using bader_analysis_from_path File grepping with bader_analysis_from_path is questionable Oct 1, 2024
@Andrew-S-Rosen Andrew-S-Rosen changed the title File grepping with bader_analysis_from_path is questionable File grepping with bader_analysis_from_path is error-prone Oct 1, 2024
@Andrew-S-Rosen Andrew-S-Rosen changed the title File grepping with bader_analysis_from_path is error-prone Glob logic with bader_analysis_from_path is error-prone Oct 1, 2024
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