Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kavanase committed May 9, 2024
1 parent 4c50e0a commit b2676cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
36 changes: 27 additions & 9 deletions doped/chemical_potentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,12 +1219,23 @@ def from_vaspruns(self, path="competing_phases", folder="vasp_std", csv_path=Non
else:
raise ValueError("Path should either be a list of paths, a string or a pathlib Path object")

if skipped_folders and verbose:
# only warn about skipped folders that are recognised calculation folders (containing a material
# composition in the name, or 'EaH' in the name)
skipped_folders_for_warning = []
for folder_name in skipped_folders:
comps = []
for i in folder_name.split(" or ")[0].split("_"):
with contextlib.suppress(ValueError):
comps.append(Composition(i))
if "EaH" in folder_name or comps:
skipped_folders_for_warning.append(folder_name)

if skipped_folders_for_warning and verbose:
parent_folder_string = f" (in {path})" if isinstance(path, (PurePath, str)) else ""
print(
warnings.warn(
f"vasprun.xml files could not be found in the following "
f"directories{parent_folder_string}, and so they will be skipped for parsing:\n"
+ "\n".join(skipped_folders)
+ "\n".join(skipped_folders_for_warning)
)

# Ignore POTCAR warnings when loading vasprun.xml
Expand Down Expand Up @@ -1690,13 +1701,20 @@ def intrinsic_phase_diagram(self) -> dict:
self.calculate_chempots()
return self._intrinsic_phase_diagram

def cplap_input(self, dependent_variable=None, filename="input.dat"):
"""For completeness' sake, automatically saves to input.dat for cplap
def _cplap_input(self, dependent_variable=None, filename="input.dat"):
"""
Generates an ``input.dat`` file for the ``CPLAP`` ``FORTRAN`` code
(legacy code for computing and analysing chemical potential limits, no
longer recommended).
Args:
dependent_variable (str) Pick one of the variables as dependent, the first element is
chosen from the composition if this isn't set
filename (str): filename, should end in .dat
Returns
dependent_variable (str):
Pick one of the variables as dependent, the first element in
the composition is chosen if this isn't set.
filename (str):
Filename, should end in ``.dat``.
Returns:
None, writes input.dat file.
"""
if not hasattr(self, "chempots"):
Expand Down
21 changes: 13 additions & 8 deletions tests/test_chemical_potentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ def test_vaspruns_none_parsed(self):
with warnings.catch_warnings(record=True) as w, pytest.raises(FileNotFoundError) as e:
cpa.from_vaspruns(path=path, folder="relax", csv_path=self.csv_path)
print([str(warning.message) for warning in w]) # for debugging
assert len(w) == 1
assert "Failed to parse the following `vasprun.xml` files:\n(files: error)\n" in str(w[0].message)
assert "Is a directory" in str(w[0].message)
assert "vasprun.xml files could not be found in the following directories (in" in str(w[0].message)
assert "ZrO2 or ZrO2/relax" in str(w[0].message)
print(e.value)
assert "No vasprun files have been parsed," in str(e.value)

def test_cplap_input(self):
cpa = chemical_potentials.CompetingPhasesAnalyzer(self.stable_system)
cpa.from_csv(self.csv_path)
cpa.cplap_input(dependent_variable="O")
cpa._cplap_input(dependent_variable="O")

assert Path("input.dat").is_file()

Expand Down Expand Up @@ -688,8 +688,10 @@ def test_init_ytos(self):

def test_api_keys_errors(self):
nonvalid_api_key_error = ValueError(
"API key test is not a valid legacy Materials Project API key. These are "
"available at https://legacy.materialsproject.org/open"
"The supplied API key (``api_key`` or 'PMG_MAPI_KEY' in your ``.pmgrc.yaml`` "
"file) test is not a valid legacy Materials Project API key, which is "
"required by doped. See the doped installation instructions for details:\n"
"https://doped.readthedocs.io/en/latest/Installation.html#setup-potcars-and-materials-project-api"
)
with pytest.raises(ValueError) as e:
chemical_potentials.CompetingPhases(
Expand All @@ -699,8 +701,11 @@ def test_api_keys_errors(self):
assert str(nonvalid_api_key_error) in str(e.value)

new_api_key_error = ValueError(
"You are trying to use the new Materials Project (MP) API which is not supported "
"by doped. Please use the legacy MP API (https://legacy.materialsproject.org/open)."
"The supplied API key (``api_key`` or 'PMG_MAPI_KEY' in your ``.pmgrc.yaml`` "
"file) corresponds to the new Materials Project (MP) API, which is not "
"supported by doped. Please use the legacy MP API as detailed on the doped "
"installation instructions:\n"
"https://doped.readthedocs.io/en/latest/Installation.html#setup-potcars-and-materials-project-api"
)
with pytest.raises(ValueError) as e:
chemical_potentials.CompetingPhases(
Expand Down

0 comments on commit b2676cc

Please sign in to comment.