Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
species.sort(key=itemgetter(0))
# Generate a list of [element1, charge1, sign1, element2, ...]
vals = list(
itertools.chain.from_iterable([x[0], abs(x[1]), get_sign(x[1])] for x in species)
)
glob_form = glob.format(*vals)
with self as c:
c.execute(
f"SELECT structure FROM {table} WHERE composition GLOB ?",
(glob_form, ), )
structs = c.fetchall()
return [SmactStructure.from_poscar(pos[0]) for pos in structs]
species: List[Union[Tuple[str, int, int], Tuple[smact.Species, int]]],
api_key: str, ):
"""Create a SmactStructure using the first Materials Project entry for a composition.
Args:
species: See :meth:`~.__init__`.
api_key: A www.materialsproject.org API key.
Returns:
:class:`~.SmactStructure`
"""
sanit_species = SmactStructure._sanitise_species(species)
with MPRester(api_key) as m:
eles = SmactStructure._get_ele_stoics(sanit_species)
formula = "".join(f"{ele}{stoic}" for ele, stoic in eles.items())
structs = m.query(
criteria={"reduced_cell_formula": formula},
properties=["structure"], )
if len(structs) == 0:
raise ValueError(
"Could not find composition in Materials Project Database, "
"please supply a structure."
)
struct = structs[0]['structure'] # Default to first found structure
if 0 not in (spec[1] for spec in sanit_species): # If everything's charged
bva = BVAnalyzer()
struct = bva.get_oxi_state_decorated_structure(struct)
def from_mp(
species: List[Union[Tuple[str, int, int], Tuple[smact.Species, int]]],
api_key: str, ):
"""Create a SmactStructure using the first Materials Project entry for a composition.
Args:
species: See :meth:`~.__init__`.
api_key: A www.materialsproject.org API key.
Returns:
:class:`~.SmactStructure`
"""
sanit_species = SmactStructure._sanitise_species(species)
with MPRester(api_key) as m:
eles = SmactStructure._get_ele_stoics(sanit_species)
formula = "".join(f"{ele}{stoic}" for ele, stoic in eles.items())
structs = m.query(
criteria={"reduced_cell_formula": formula},
properties=["structure"], )
if len(structs) == 0:
raise ValueError(
"Could not find composition in Materials Project Database, "
"please supply a structure."
)
struct = structs[0]['structure'] # Default to first found structure
raise ValueError(
"Could not find composition in Materials Project Database, "
"please supply a structure."
)
struct = structs[0]['structure'] # Default to first found structure
if 0 not in (spec[1] for spec in sanit_species): # If everything's charged
bva = BVAnalyzer()
struct = bva.get_oxi_state_decorated_structure(struct)
lattice_mat = struct.lattice.matrix
lattice_param = 1.0 # TODO Use actual lattice parameter
sites, _ = SmactStructure.__parse_py_sites(struct)
return SmactStructure(
sanit_species,
lattice_mat,
sites,
lattice_param,
sanitise_species=False, )
:class:`~.SmactStructure`
"""
if not isinstance(structure, pymatgen.Structure):
raise TypeError("Structure must be a pymatgen.Structure instance.")
bva = BVAnalyzer()
struct = bva.get_oxi_state_decorated_structure(structure)
sites, species = SmactStructure.__parse_py_sites(struct)
lattice_mat = struct.lattice.matrix
lattice_param = 1.0
return SmactStructure(
species,
lattice_mat,
sites,
lattice_param,
sanitise_species=True, )
def from_file(fname: str):
"""Create SmactStructure from a POSCAR file.
Args:
fname: The name of the POSCAR file.
See :meth:`~.as_poscar` for format specification.
Returns:
:class:`~.SmactStructure`
"""
with open(fname, 'r') as f:
return SmactStructure.from_poscar(f.read())
Yields:
Potential structures, as tuples of (structure, probability, parent).
"""
# For now, consider just structures with the same species, and unary substitutions.
# This means we need only consider structures with a difference of 0 or 1 species.
if include_same:
for identical in self.db.get_with_species(species, self.table):
yield (identical, 1.0, identical)
sub_spec = itertools.combinations(species, len(species) - 1)
sub_spec = list(map(list, sub_spec))
potential_unary_parents: List[List[SmactStructure]] = list(
self.db.get_with_species(specs, self.table) for specs in sub_spec
)
for spec_idx, parents in enumerate(potential_unary_parents):
# Get missing ion
(diff_spec, ) = set(species) - set(sub_spec[spec_idx])
diff_spec_str = unparse_spec(diff_spec)
# Determine conditional substitution likelihoods
diff_sub_probs = self.cm.cond_sub_probs(diff_spec_str)
for parent in parents:
# print("Testing parent")
# Filter out any structures with identical species
if parent.has_species(diff_spec):
continue
def __eq__(self, other):
"""Determine equality of SmactStructures based on their attributes.
:attr:`~.species`, :attr:`~.lattice_mat`, :attr:`~.lattice_param` and
:attr:`~.sites` must all be equal for the comparison to be True.
Note:
For the SmactStructures to be equal their attributes must be
*identical*. For example, it is insufficient that the two
structures have the same space group or the same species;
the site coordinates must be equal also.
"""
if not isinstance(other, SmactStructure):
return False
return all([
self.species == other.species,
np.array_equal(self.lattice_mat, other.lattice_mat),
self.lattice_param == other.lattice_param,
self.sites == other.sites,
list(self.sites.keys()) == list(other.sites.keys()),
])
"please supply a structure."
)
struct = structs[0]['structure'] # Default to first found structure
if 0 not in (spec[1] for spec in sanit_species): # If everything's charged
bva = BVAnalyzer()
struct = bva.get_oxi_state_decorated_structure(struct)
lattice_mat = struct.lattice.matrix
lattice_param = 1.0 # TODO Use actual lattice parameter
sites, _ = SmactStructure.__parse_py_sites(struct)
return SmactStructure(
sanit_species,
lattice_mat,
sites,
lattice_param,
sanitise_species=False, )
Args:
composition: The composition to search for.
See :meth:`SmactStructure.composition`.
table: The name of the table in which to search.
Returns:
A list of :class:`~.SmactStructure` s.
"""
with self as c:
c.execute(
f"SELECT structure FROM {table} WHERE composition = ?",
(composition, ), )
structs = c.fetchall()
return [SmactStructure.from_poscar(pos[0]) for pos in structs]