Skip to content

Neighborhoods

Neighborhood

Bases: AbstractNeighborhood

A specific Neighborhood. An instance of this classes corresponds to a particular SimulationState. It stores a map of each site_id in the SimulationState to the IDs of the sites which are it's neighbors.

Source code in pylattica/core/neighborhoods.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Neighborhood(AbstractNeighborhood):
    """A specific Neighborhood. An instance of this classes corresponds
    to a particular SimulationState. It stores a map of each site_id
    in the SimulationState to the IDs of the sites which are it's neighbors.
    """

    def __init__(self, graph: rx.PyGraph):
        """Instantiates a NeighborhoodGraph."""
        self._graph = graph

    def neighbors_of(self, site_id: int, include_weights: bool = False) -> List[int]:
        """Retrieves a list of the IDs of the sites which are neighbors of the
        provided site. Optionally includes the weights of the connections to those
        neighbors.

        Parameters
        ----------
        site_id : int
            The site for which neighbors should be retrieved
        include_weights : bool, optional
            Whether or not weights if the neighbor connections should
            be included, by default False

        Returns
        -------
        list[int]
            Either a list of site IDs, or a list of tuples of (site ID, connection weight)
        """
        nbs = self._graph.neighbors(site_id)
        if include_weights:
            weighted_nbs = [
                (nb_id, self._graph.get_edge_data(site_id, nb_id)) for nb_id in nbs
            ]
            return weighted_nbs

        return list(nbs)

__init__(graph)

Instantiates a NeighborhoodGraph.

Source code in pylattica/core/neighborhoods.py
21
22
23
def __init__(self, graph: rx.PyGraph):
    """Instantiates a NeighborhoodGraph."""
    self._graph = graph

neighbors_of(site_id, include_weights=False)

Retrieves a list of the IDs of the sites which are neighbors of the provided site. Optionally includes the weights of the connections to those neighbors.

Parameters
int

The site for which neighbors should be retrieved

bool, optional

Whether or not weights if the neighbor connections should be included, by default False

Returns

list[int] Either a list of site IDs, or a list of tuples of (site ID, connection weight)

Source code in pylattica/core/neighborhoods.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def neighbors_of(self, site_id: int, include_weights: bool = False) -> List[int]:
    """Retrieves a list of the IDs of the sites which are neighbors of the
    provided site. Optionally includes the weights of the connections to those
    neighbors.

    Parameters
    ----------
    site_id : int
        The site for which neighbors should be retrieved
    include_weights : bool, optional
        Whether or not weights if the neighbor connections should
        be included, by default False

    Returns
    -------
    list[int]
        Either a list of site IDs, or a list of tuples of (site ID, connection weight)
    """
    nbs = self._graph.neighbors(site_id)
    if include_weights:
        weighted_nbs = [
            (nb_id, self._graph.get_edge_data(site_id, nb_id)) for nb_id in nbs
        ]
        return weighted_nbs

    return list(nbs)

SiteClassNeighborhood

Bases: MultiNeighborhood

A Neighborhood that distinguished neighbors of sites based on their class

Source code in pylattica/core/neighborhoods.py
75
76
77
78
79
80
81
82
83
84
85
86
class SiteClassNeighborhood(MultiNeighborhood):
    """A Neighborhood that distinguished neighbors of sites based on their class"""

    def __init__(
        self, structure: PeriodicStructure, neighborhoods: Dict[str, Neighborhood]
    ):
        self._struct = structure
        self._nbhoods = neighborhoods

    def _get_nbhood(self, site_id: int) -> List[int]:
        site_class = self._struct.site_class(site_id)
        return self._nbhoods.get(site_class)

StochasticNeighborhood

Bases: MultiNeighborhood

A NeighborhoodGraph for stochastic neighborhoods.

Source code in pylattica/core/neighborhoods.py
65
66
67
68
69
70
71
72
class StochasticNeighborhood(MultiNeighborhood):
    """A NeighborhoodGraph for stochastic neighborhoods."""

    def __init__(self, neighborhoods: List[Neighborhood]):
        self._neighborhoods = neighborhoods

    def _get_nbhood(self, _) -> List[int]:
        return random.choice(self._neighborhoods)