Skip to content

Commit

Permalink
fea: add get_anom_formula_from_prototype_formula
Browse files Browse the repository at this point in the history
  • Loading branch information
CompRhys committed Jun 28, 2024
1 parent 8b3556f commit e87b3aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions aviary/wren/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,22 @@ def prototype_formula(composition: Composition) -> str:
return anon


def get_anom_formula_from_prototype_formula(prototype_formula: str) -> str:
"""Get an anonymous formula from a prototype formula."""
subst = r"\g<1>1"
prototype_formula = re.sub(r"([A-z](?![0-9]))", subst, prototype_formula)
anom_list = ["".join(g) for _, g in groupby(prototype_formula, str.isalpha)]
counts = anom_list[1::2]
dummy = anom_list[0::2]

return "".join(
[
f"{d}{c}" if c != "1" else d
for d, c in zip(dummy, sorted(counts), strict=False)
]
)


def count_wyckoff_positions(aflow_label: str) -> int:
"""Count number of Wyckoff positions in Wyckoff representation.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_wyckoff_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
get_aflow_label_from_aflow,
get_aflow_label_from_spglib,
get_aflow_strs_from_iso_and_composition,
get_anom_formula_from_prototype_formula,
get_isopointal_proto_from_aflow,
prototype_formula,
)

from .conftest import TEST_DIR
Expand Down Expand Up @@ -81,6 +83,14 @@ def test_get_aflow_strs_from_iso_and_composition(
assert aflows == expected.split(" ")


def test_prototype_formula():
assert prototype_formula(Composition("Ce2Al3GaPd4")) == "A3B2CD4"


def test_get_anom_formula_from_prototype_formula():
assert get_anom_formula_from_prototype_formula("A3B2CD4") == "AB2C3D4"


@pytest.mark.parametrize(
"aflow_label, expected",
[
Expand Down

0 comments on commit e87b3aa

Please sign in to comment.