Skip to content

Commit

Permalink
add test for TransformedStructure.set_parameter()
Browse files Browse the repository at this point in the history
fix get_decomp_and_e_above_hull doc str ValueError condition
  • Loading branch information
janosh committed Mar 10, 2024
1 parent daa0bb0 commit 910e571
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.1
rev: v0.3.2
hooks:
- id: ruff
args: [--fix, --unsafe-fixes]
Expand Down
7 changes: 6 additions & 1 deletion pymatgen/alchemy/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,20 @@ def __str__(self) -> str:
output.extend(("\nOther parameters", "------------", str(self.other_parameters)))
return "\n".join(output)

def set_parameter(self, key: str, value: Any) -> None:
def set_parameter(self, key: str, value: Any) -> TransformedStructure:
"""Sets a parameter.
Args:
key (str): The string key.
value (Any): The value.
Returns:
TransformedStructure
"""
self.other_parameters[key] = value

return self

@property
def was_modified(self) -> bool:
"""Boolean describing whether the last transformation on the structure
Expand Down
10 changes: 7 additions & 3 deletions pymatgen/analysis/phase_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,8 @@ def get_decomp_and_e_above_hull(
'ignore' just returns (None, None). Defaults to 'raise'.
Raises:
ValueError: If no valid decomposition exists in this phase diagram for given entry.
ValueError: If on_error is 'raise' and no valid decomposition exists in this
phase diagram for given entry.
Returns:
tuple[decomp, energy_above_hull]: The decomposition is provided
Expand Down Expand Up @@ -825,9 +826,9 @@ def get_equilibrium_reaction_energy(self, entry: PDEntry) -> float | None:
return 0

entries = [e for e in self._get_stable_entries_in_space(frozenset(elem_space)) if e != entry]
modpd = PhaseDiagram(entries, elements=elem_space)
mod_pd = PhaseDiagram(entries, elements=elem_space)

return modpd.get_decomp_and_e_above_hull(entry, allow_negative=True)[1]
return mod_pd.get_decomp_and_e_above_hull(entry, allow_negative=True)[1]

def get_decomp_and_phase_separation_energy(
self,
Expand Down Expand Up @@ -1752,6 +1753,9 @@ def get_pd_for_entry(self, entry: Entry | Composition) -> PhaseDiagram:
Returns:
PhaseDiagram: phase diagram that the entry is part of
Raises:
ValueError: If no suitable PhaseDiagram is found for the entry.
"""
entry_space = frozenset(entry.elements) if isinstance(entry, Composition) else frozenset(entry.elements)

Expand Down
4 changes: 2 additions & 2 deletions pymatgen/analysis/structure_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ def get_connections(self):
def get_sitej(self, site_index, image_index):
"""
Assuming there is some value in the connectivity array at indices
(1, 3, 12). sitei can be obtained directly from the input structure
(structure[1]). sitej can be obtained by passing 3, 12 to this function.
(1, 3, 12). site_i can be obtained directly from the input structure
(structure[1]). site_j can be obtained by passing 3, 12 to this function.
Args:
site_index (int): index of the site (3 in the example)
Expand Down
5 changes: 5 additions & 0 deletions tests/alchemy/test_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def test_undo_and_redo_last_change(self):
ts.undo_last_change()
ts.redo_next_change()

def test_set_parameter(self):
trans = self.trans.set_parameter("author", "will")
assert trans.other_parameters["author"] == "will"
assert trans is self.trans

def test_as_dict(self):
self.trans.set_parameter("author", "will")
dct = self.trans.as_dict()
Expand Down

0 comments on commit 910e571

Please sign in to comment.