amachine.am_cohesion API documentation

# amachine.am_cohesion

View Source

     1from abc import ABC, abstractmethod
     2import warnings
     3from dataclasses import dataclass
     4
     5class CohesionKernel(ABC):
     6	"""
     7	Defines the substitution weight distributions over target symbols
     8	given a source symbol.
     9	"""
    10
    11	def _normalize(self, source : str, dist: dict[str, float]) -> dict[str, float]:
    12		wsum = sum(w for w in dist.values())
    13		if wsum <= 1e-15:
    14			warnings.warn( "Weight function sums to 0, implicitly mapping source with probability 1" )
    15			res = dist.copy()
    16			res[ source ] = 1.0
    17			return res
    18		return {s: w / wsum for s, w in dist.items()}
    19
    20	@abstractmethod
    21	def cohesion_scores( self, source: str, symbols: set[str] ) -> dict[str, float]:
    22		"""
    23		Maps a source symbol to a distribution of cohesion scores over other symbols.
    24		"""
    25
    26@dataclass
    27class Uniform(CohesionKernel):
    28	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    29		return { s: 1.0 for s in symbols }
    30
    31@dataclass
    32class Marginal(CohesionKernel):
    33	"""Per-symbol weight distribution."""
    34	scores : dict[str, float]
    35
    36	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    37		return {s: self.scores[s] for s in symbols }
    38
    39@dataclass
    40class Symmetric(CohesionKernel):
    41	"""w(a->b) == w(b->a)"""
    42	scores : dict[frozenset, float]
    43	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    44		return {s: self.scores[frozenset({source, s})] for s in symbols }
    45
    46@dataclass
    47class Asymmetric(CohesionKernel):
    48	"""Directed pairwise scores."""
    49	scores : dict[tuple[str,str], float]
    50
    51	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    52		return {s: self.scores[(source, s)] for s in symbols }

class CohesionKernel(abc.ABC): View Source

     6class CohesionKernel(ABC):
     7	"""
     8	Defines the substitution weight distributions over target symbols
     9	given a source symbol.
    10	"""
    11
    12	def _normalize(self, source : str, dist: dict[str, float]) -> dict[str, float]:
    13		wsum = sum(w for w in dist.values())
    14		if wsum <= 1e-15:
    15			warnings.warn( "Weight function sums to 0, implicitly mapping source with probability 1" )
    16			res = dist.copy()
    17			res[ source ] = 1.0
    18			return res
    19		return {s: w / wsum for s, w in dist.items()}
    20
    21	@abstractmethod
    22	def cohesion_scores( self, source: str, symbols: set[str] ) -> dict[str, float]:
    23		"""
    24		Maps a source symbol to a distribution of cohesion scores over other symbols.
    25		"""

Defines the substitution weight distributions over target symbols given a source symbol.

@abstractmethod
def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]: View Source

    21	@abstractmethod
    22	def cohesion_scores( self, source: str, symbols: set[str] ) -> dict[str, float]:
    23		"""
    24		Maps a source symbol to a distribution of cohesion scores over other symbols.
    25		"""

Maps a source symbol to a distribution of cohesion scores over other symbols.

@dataclass
class Uniform(CohesionKernel): View Source

    27@dataclass
    28class Uniform(CohesionKernel):
    29	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    30		return { s: 1.0 for s in symbols }

def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]: View Source

    29	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    30		return { s: 1.0 for s in symbols }

Maps a source symbol to a distribution of cohesion scores over other symbols.

@dataclass
class Marginal(CohesionKernel): View Source

    32@dataclass
    33class Marginal(CohesionKernel):
    34	"""Per-symbol weight distribution."""
    35	scores : dict[str, float]
    36
    37	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    38		return {s: self.scores[s] for s in symbols }

Per-symbol weight distribution.

Marginal(scores: dict[str, float])
scores: dict[str, float]
def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]: View Source

    37	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    38		return {s: self.scores[s] for s in symbols }

Maps a source symbol to a distribution of cohesion scores over other symbols.

@dataclass
class Symmetric(CohesionKernel): View Source

    40@dataclass
    41class Symmetric(CohesionKernel):
    42	"""w(a->b) == w(b->a)"""
    43	scores : dict[frozenset, float]
    44	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    45		return {s: self.scores[frozenset({source, s})] for s in symbols }

w(a->b) == w(b->a)

Symmetric(scores: dict[frozenset, float])
scores: dict[frozenset, float]
def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]: View Source

    44	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    45		return {s: self.scores[frozenset({source, s})] for s in symbols }

Maps a source symbol to a distribution of cohesion scores over other symbols.

@dataclass
class Asymmetric(CohesionKernel): View Source

    47@dataclass
    48class Asymmetric(CohesionKernel):
    49	"""Directed pairwise scores."""
    50	scores : dict[tuple[str,str], float]
    51
    52	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    53		return {s: self.scores[(source, s)] for s in symbols }

Directed pairwise scores.

Asymmetric(scores: dict[tuple[str, str], float])
scores: dict[tuple[str, str], float]
def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]: View Source

    52	def cohesion_scores(self, source: str, symbols: set[str]) -> dict[str, float]:
    53		return {s: self.scores[(source, s)] for s in symbols }

Maps a source symbol to a distribution of cohesion scores over other symbols.