Coverage for src/abcd_graph/callbacks/property_collector.py: 100%

1# Copyright (c) 2024 Jordan Barrett & Aleksander Wojnarowicz

2#

3# Permission is hereby granted, free of charge, to any person obtaining a copy

4# of this software and associated documentation files (the "Software"), to deal

5# in the Software without restriction, including without limitation the rights

6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

7# copies of the Software, and to permit persons to whom the Software is

8# furnished to do so, subject to the following conditions:

9#

10# The above copyright notice and this permission notice shall be included in all

11# copies or substantial portions of the Software.

12#

13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

19# SOFTWARE.

20

21 from typing import Optional

22

23 import numpy as np

24 from numpy.typing import NDArray

25

26 from abcd_graph.callbacks.abstract import (

27 ABCDCallback,

28 BuildContext,

29)

30 from abcd_graph.exporter import GraphExporter

31 from abcd_graph.graph.core.abcd_objects import (

32 Community,

33 GraphImpl,

34)

35

36

37 class PropertyCollector(ABCDCallback):

38 def __init__(self) -> None:

39 self._graph: Optional[GraphImpl] = None

40

41 self._communities: list[Community] = []

42

43 self._degree_sequence: dict[int, int] = {}

44

45 self._xi_matrix: Optional[NDArray[np.float64]] = None

46

47 self._expected_degree_cdf: dict[int, float] = {}

48

49 self._actual_degree_cdf: dict[int, float] = {}

50

51 self._expected_community_cdf: dict[int, float] = {}

52

53 self._actual_community_cdf: dict[int, float] = {}

54

55 def after_build(self, graph: GraphImpl, context: BuildContext, exporter: GraphExporter) -> None:

56 self._graph = graph

57

58 @property

59 def degree_sequence(self) -> dict[int, int]:

60 if not self._degree_sequence:

61 self._degree_sequence = self._graph.degree_sequence # type: ignore[union-attr]

62

63 return self._degree_sequence

64

65 @property

66 def xi_matrix(self) -> NDArray[np.float64]:

67 if self._xi_matrix is None:

68 self._xi_matrix = self._graph.xi_matrix # type: ignore[union-attr]

69 return self._xi_matrix

70

71 @property

72 def expected_degree_cdf(self) -> dict[int, float]:

73 if not self._expected_degree_cdf:

74 self._expected_degree_cdf = self._graph.expected_degree_cdf # type: ignore[union-attr]

75

76 return self._expected_degree_cdf

77

78 @property

79 def actual_degree_cdf(self) -> dict[int, float]:

80 if not self._actual_degree_cdf:

81 self._actual_degree_cdf = self._graph.actual_degree_cdf # type: ignore[union-attr]

82

83 return self._actual_degree_cdf

84

85 @property

86 def expected_community_cdf(self) -> dict[int, float]:

87 if not self._expected_community_cdf:

88 self._expected_community_cdf = self._graph.expected_community_cdf # type: ignore[union-attr]

89

90 return self._expected_community_cdf

91

92 @property

93 def actual_community_cdf(self) -> dict[int, float]:

94 if not self._actual_community_cdf:

95 self._actual_community_cdf = self._graph.actual_community_cdf # type: ignore[union-attr]

96

97 return self._actual_community_cdf