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

Allow OxidationStateDoc to skip assignment if oxidation states are already assigned #1126

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions emmet-core/emmet/core/oxidation_states.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from collections import defaultdict
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Literal

import numpy as np
from pydantic import Field
Expand Down Expand Up @@ -30,13 +30,48 @@ class OxidationStateDoc(PropertyDoc):
average_oxidation_states: Dict[str, float] = Field(
description="Average oxidation states for each unique species."
)
method: Optional[str] = Field(
method: Optional[Literal["Already Assigned", "Bond Valence Analysis", "Oxidation State Guess"]] = Field(
None, description="Method used to compute oxidation states."
)

@classmethod
def from_structure(cls, structure: Structure, material_id: MPID, **kwargs): # type: ignore[override]
# TODO: add check for if it already has oxidation states, if so pass this along unchanged ("method": "manual")

# Check if structure already has oxidation states,
# if so pass this along unchanged with "method" == "Already Assigned"
valences = []
species = []

site_oxidation_list = defaultdict(list)
for site in structure:
oxi_state = getattr(site.species, "oxi_state")
if oxi_state:
site_oxidation_list[site.species.element].append(oxi_state)
species.append(site.species)
valences.append(oxi_state)

average_oxidation_states = {
str(el): np.mean(oxi_states) for el, oxi_states in site_oxidation_list.items() # type: ignore
}

if any(valences):
d = {
"possible_species": species,
"possible_valences": valences,
"average_oxidation_states": average_oxidation_states,
"method": "Already Assigned"
"state": "successful"
}
return super().from_structure(
meta_structure=structure,
material_id=material_id,
structure=structure,
**d,
**kwargs
)

# otherwise, continue with assignment

structure.remove_oxidation_states()

# Null document
Expand Down Expand Up @@ -77,7 +112,7 @@ def from_structure(cls, structure: Structure, material_id: MPID, **kwargs): # t
}

except Exception as e:
logging.error("BVAnalyzer failed with: {}".format(e))
logging.debug("BVAnalyzer failed for {structure.composition.reduced_composition} with: {}. Trying oxi_state_guesses.".format(e))

try:
first_oxi_state_guess = structure.composition.oxi_state_guesses(
Expand Down
Loading