<commit_before>#!/usr/bin/env python

"""
This module defines site transformations which transforms a structure into
another structure. Site transformations differ from standard transformations
in that they operate in a site-specific manner.
All transformations should inherit the AbstractTransformation ABC.
"""

from __future__ import division

__author__ = "<NAME>, <NAME>"
__copyright__ = "Copyright 2011, The Materials Project"
__version__ = "1.2"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "Sep 23, 2011"

import math
import itertools
import logging
import time

from pymatgen.core.structure import Structure
from pymatgen.transformations.transformation_abc import AbstractTransformation
from pymatgen.analysis.ewald import EwaldSummation, EwaldMinimizer


class InsertSitesTransformation(AbstractTransformation):
    """
    This transformation substitutes certain sites with certain species.

    Args:
        species: A list of species. e.g., ["Li", "Fe"]
        coords: A list of coords corresponding to those species. e.g.,
            [[0,0,0],[0.5,0.5,0.5]].
        coords_are_cartesian (bool): Set to True if coords are given in
            cartesian coords. Defaults to False.
        validate_proximity (bool): Set to False if you do not wish to ensure that added sites are
            not too close to other sites. Defaults to True.
    """
    def __init__(self, species, coords, coords_are_cartesian=False,
                 validate_proximity=True):
        if len(species) != len(coords):
            raise ValueError("Species and coords must be the same length!")
        self._species = species
        self._coords = coords
        self._cartesian = coords_are_cartesian
        self._validate_proximity = validate_proximity

    def apply_transformation(self, structure):
        s = Structure.from_sites(structure.sites)
        for i, sp in enumerate(self._species):
            s.insert(i, sp, self._coords[i],
                     coords_are_cartesian=self._cartesian,
                     validate_proximity=self._validate_proximity)
        return s.get_sorted_structure()

    def __str__(self):
        return "InsertSiteTransformation : " + \
            "species {}, coords {}".format(self._species, self._coords)

    def __repr__(self):
        return self.__str__()

    @property
    def inverse(self):
        return None

    @property
    def is_one_to_many(self):
        return False

    @property
    def to_dict(self):
        return {"name": self.__class__.__name__, "version": __version__,
                "init_args": {"species": self._species, "coords": self._coords,
                              "coords_are_cartesian": self._cartesian,
                              "validate_proximity": self._validate_proximity},
                "@module": self.__class__.__module__,
                "@class": self.__class__.__name__}


class ReplaceSiteSpeciesTransformation(AbstractTransformation):
    """
    This transformation substitutes certain sites with certain species.

    Args:
        indices_species_map: A dict containing the species mapping in
            int-string pairs. E.g., { 1:"Na"} or {2:"Mn2+"}. Multiple<commit_msg>make InsertSiteTransformation.to_dict JSONable even if self._coords is/includes numpy array<commit_after>#!/usr/bin/env python

"""
This module defines site transformations which transforms a structure into
another structure. Site transformations differ from standard transformations
in that they operate in a site-specific manner.
All transformations should inherit the AbstractTransformation ABC.
"""

from __future__ import division

__author__ = "<NAME>, <NAME>"
__copyright__ = "Copyright 2011, The Materials Project"
__version__ = "1.2"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "Sep 23, 2011"

import math
import itertools
import logging
import time

from pymatgen.core.structure import Structure
from pymatgen.transformations.transformation_abc import AbstractTransformation
from pymatgen.analysis.ewald import EwaldSummation, EwaldMinimizer


class InsertSitesTransformation(AbstractTransformation):
    """
    This transformation substitutes certain sites with certain species.

    Args:
        species: A list of species. e.g., ["Li", "Fe"]
        coords: A list of coords corresponding to those species. e.g.,
            [[0,0,0],[0.5,0.5,0.5]].
        coords_are_cartesian (bool): Set to True if coords are given in
            cartesian coords. Defaults to False.
        validate_proximity (bool): Set to False if you do not wish to ensure that added sites are
            not too close to other sites. Defaults to True.
    """
    def __init__(self, species, coords, coords_are_cartesian=False,
                 validate_proximity=True):
        if len(species) != len(coords):
            raise ValueError("Species and coords must be the same length!")
        self._species = species
        self._coords = coords
        self._cartesian = coords_are_cartesian
        self._validate_proximity = validate_proximity

    def apply_transformation(self, structure):
        s = Structure.from_sites(structure.sites)
        for i, sp in enumerate(self._species):
            s.insert(i, sp, self._coords[i],
                     coords_are_cartesian=self._cartesian,
                     validate_proximity=self._validate_proximity)
        return s.get_sorted_structure()

    def __str__(self):
        return "InsertSiteTransformation : " + \
            "species {}, coords {}".format(self._species, self._coords)

    def __repr__(self):
        return self.__str__()

    @property
    def inverse(self):
        return None

    @property
    def is_one_to_many(self):
        return False

    @property
    def to_dict(self):
        return {"name": self.__class__.__name__, "version": __version__,
                "init_args": {"species": self._species, "coords": [list(x) for x in self._coords],
                              "coords_are_cartesian": self._cartesian,
                              "validate_proximity": self._validate_proximity},
                "@module": self.__class__.__module__,
                "@class": self.__class__.__name__}


class ReplaceSiteSpeciesTransformation(AbstractTransformation):
    """
    This transformation substitutes certain sites with certain species.

    Args:
        indices_species_map: A dict containing the species mapping in
            int-string pairs. E.g., { 1:"Na"} or {2:"Mn2+"}. Multiple
{'fraction_non_alphanumeric': 0.08819455133138372, 'fraction_numerical': 0.007388025242419579, 'mean_word_length': 2.900360144057623, 'pattern_counts': {'":': 18, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3826342', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1755, 'n_words': 533}