graphslim 1.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. graphslim/__init__.py +0 -0
  2. graphslim/coarsening/__init__.py +12 -0
  3. graphslim/coarsening/affinity_gs.py +110 -0
  4. graphslim/coarsening/algebraic_jc.py +110 -0
  5. graphslim/coarsening/averaging.py +58 -0
  6. graphslim/coarsening/clustering.py +64 -0
  7. graphslim/coarsening/clusteringagg.py +77 -0
  8. graphslim/coarsening/coarsening_base.py +246 -0
  9. graphslim/coarsening/heavy_edge.py +110 -0
  10. graphslim/coarsening/kron.py +109 -0
  11. graphslim/coarsening/utils.py +2002 -0
  12. graphslim/coarsening/variation_cliques.py +131 -0
  13. graphslim/coarsening/variation_edges.py +131 -0
  14. graphslim/coarsening/variation_neighborhoods.py +131 -0
  15. graphslim/coarsening/vng.py +118 -0
  16. graphslim/condensation/__init__.py +9 -0
  17. graphslim/condensation/doscond.py +64 -0
  18. graphslim/condensation/doscondx.py +59 -0
  19. graphslim/condensation/gcond.py +80 -0
  20. graphslim/condensation/gcond_base.py +186 -0
  21. graphslim/condensation/gcondx.py +76 -0
  22. graphslim/condensation/gcsntk.py +160 -0
  23. graphslim/condensation/geom.py +449 -0
  24. graphslim/condensation/msgc.py +244 -0
  25. graphslim/condensation/sfgc.py +215 -0
  26. graphslim/condensation/sgdd.py +102 -0
  27. graphslim/condensation/utils.py +236 -0
  28. graphslim/configs/doscond/citeseer.json +8 -0
  29. graphslim/configs/doscond/cora.json +8 -0
  30. graphslim/configs/doscond/flickr.json +8 -0
  31. graphslim/configs/doscond/ogbn-arxiv.json +9 -0
  32. graphslim/configs/doscond/reddit.json +9 -0
  33. graphslim/configs/doscondx/citeseer.json +8 -0
  34. graphslim/configs/doscondx/cora.json +8 -0
  35. graphslim/configs/doscondx/flickr.json +7 -0
  36. graphslim/configs/doscondx/ogbn-arxiv.json +8 -0
  37. graphslim/configs/doscondx/reddit.json +7 -0
  38. graphslim/configs/gcond/citeseer.json +11 -0
  39. graphslim/configs/gcond/cora.json +11 -0
  40. graphslim/configs/gcond/flickr.json +10 -0
  41. graphslim/configs/gcond/ogbn-arxiv.json +11 -0
  42. graphslim/configs/gcond/reddit.json +11 -0
  43. graphslim/configs/gcondx/citeseer.json +8 -0
  44. graphslim/configs/gcondx/cora.json +8 -0
  45. graphslim/configs/gcondx/flickr.json +7 -0
  46. graphslim/configs/gcondx/ogbn-arxiv.json +8 -0
  47. graphslim/configs/gcondx/reddit.json +11 -0
  48. graphslim/configs/gcsntk/citeseer.json +11 -0
  49. graphslim/configs/gcsntk/cora.json +11 -0
  50. graphslim/configs/gcsntk/flickr.json +13 -0
  51. graphslim/configs/gcsntk/ogbn-arxiv.json +13 -0
  52. graphslim/configs/gcsntk/reddit.json +13 -0
  53. graphslim/configs/geom/citeseer.json +26 -0
  54. graphslim/configs/geom/cora.json +25 -0
  55. graphslim/configs/geom/flickr.json +30 -0
  56. graphslim/configs/geom/ogbn-arxiv.json +27 -0
  57. graphslim/configs/geom/reddit.json +29 -0
  58. graphslim/configs/msgc/citeseer.json +12 -0
  59. graphslim/configs/msgc/cora.json +12 -0
  60. graphslim/configs/msgc/flickr.json +12 -0
  61. graphslim/configs/msgc/ogbn-arxiv.json +12 -0
  62. graphslim/configs/msgc/reddit.json +12 -0
  63. graphslim/configs/sfgc/citeseer.json +12 -0
  64. graphslim/configs/sfgc/cora.json +12 -0
  65. graphslim/configs/sfgc/flickr.json +12 -0
  66. graphslim/configs/sfgc/ogbn-arxiv.json +12 -0
  67. graphslim/configs/sfgc/reddit.json +12 -0
  68. graphslim/configs/sgdd/citeseer.json +16 -0
  69. graphslim/configs/sgdd/cora.json +16 -0
  70. graphslim/configs/sgdd/flickr.json +15 -0
  71. graphslim/configs/sgdd/ogbn-arxiv.json +17 -0
  72. graphslim/configs/sgdd/reddit.json +17 -0
  73. graphslim/configs.py +193 -0
  74. graphslim/dataset/__init__.py +3 -0
  75. graphslim/dataset/attack.py +95 -0
  76. graphslim/dataset/convertor.py +257 -0
  77. graphslim/dataset/loader.py +404 -0
  78. graphslim/dataset/utils.py +374 -0
  79. graphslim/evaluation/__init__.py +3 -0
  80. graphslim/evaluation/eval_agent.py +210 -0
  81. graphslim/evaluation/nas_eval.py +161 -0
  82. graphslim/evaluation/utils.py +145 -0
  83. graphslim/evaluation/vis.py +73 -0
  84. graphslim/models/__init__.py +13 -0
  85. graphslim/models/appnp.py +196 -0
  86. graphslim/models/base.py +212 -0
  87. graphslim/models/cheby.py +29 -0
  88. graphslim/models/gat.py +80 -0
  89. graphslim/models/gcn.py +23 -0
  90. graphslim/models/gntk.py +166 -0
  91. graphslim/models/graphsage.py +149 -0
  92. graphslim/models/ignr.py +213 -0
  93. graphslim/models/krr.py +21 -0
  94. graphslim/models/layers.py +379 -0
  95. graphslim/models/parametrized_adj.py +86 -0
  96. graphslim/models/prbcd.py +445 -0
  97. graphslim/models/random_attack.py +162 -0
  98. graphslim/models/reparam_module.py +150 -0
  99. graphslim/models/sgc.py +86 -0
  100. graphslim/models/sgformer.py +476 -0
  101. graphslim/models/sntk.py +96 -0
  102. graphslim/sparsification/__init__.py +15 -0
  103. graphslim/sparsification/cent_degree.py +28 -0
  104. graphslim/sparsification/cent_pagerank.py +55 -0
  105. graphslim/sparsification/coreset_base.py +46 -0
  106. graphslim/sparsification/edge_sparsification_base.py +34 -0
  107. graphslim/sparsification/fixed_idx/idx_citeseer_0.1_kcenter_15.npy +0 -0
  108. graphslim/sparsification/fixed_idx/idx_citeseer_0.25_kcenter_15.npy +0 -0
  109. graphslim/sparsification/fixed_idx/idx_citeseer_0.5_kcenter_15.npy +0 -0
  110. graphslim/sparsification/fixed_idx/idx_citeseer_1.0_kcenter_15.npy +0 -0
  111. graphslim/sparsification/fixed_idx/idx_cora_0.1_kcenter_15.npy +0 -0
  112. graphslim/sparsification/fixed_idx/idx_cora_0.25_kcenter_15.npy +0 -0
  113. graphslim/sparsification/fixed_idx/idx_cora_0.5_kcenter_15.npy +0 -0
  114. graphslim/sparsification/fixed_idx/idx_cora_1.0_kcenter_15.npy +0 -0
  115. graphslim/sparsification/fixed_idx/idx_flickr_0.001_kcenter_15.npy +0 -0
  116. graphslim/sparsification/fixed_idx/idx_flickr_0.005_kcenter_15.npy +0 -0
  117. graphslim/sparsification/fixed_idx/idx_flickr_0.01_kcenter_15.npy +0 -0
  118. graphslim/sparsification/fixed_idx/idx_ogbn-arxiv_0.001_kcenter_15.npy +0 -0
  119. graphslim/sparsification/fixed_idx/idx_ogbn-arxiv_0.005_kcenter_15.npy +0 -0
  120. graphslim/sparsification/fixed_idx/idx_ogbn-arxiv_0.01_kcenter_15.npy +0 -0
  121. graphslim/sparsification/fixed_idx/idx_reddit_0.0005_kcenter_15.npy +0 -0
  122. graphslim/sparsification/fixed_idx/idx_reddit_0.001_kcenter_15.npy +0 -0
  123. graphslim/sparsification/fixed_idx/idx_reddit_0.002_kcenter_15.npy +0 -0
  124. graphslim/sparsification/fixed_idx/idx_reddit_0.005_kcenter_15.npy +0 -0
  125. graphslim/sparsification/g_spar.py +21 -0
  126. graphslim/sparsification/herding.py +25 -0
  127. graphslim/sparsification/herding_agg.py +25 -0
  128. graphslim/sparsification/kcenter.py +24 -0
  129. graphslim/sparsification/kcenter_agg.py +27 -0
  130. graphslim/sparsification/kcenter_sample.py +79 -0
  131. graphslim/sparsification/local_degree.py +21 -0
  132. graphslim/sparsification/model_based_coreset_base.py +63 -0
  133. graphslim/sparsification/model_free_coreset_base.py +61 -0
  134. graphslim/sparsification/random.py +17 -0
  135. graphslim/sparsification/random_edge.py +21 -0
  136. graphslim/sparsification/rank_degree.py +68 -0
  137. graphslim/sparsification/scan.py +21 -0
  138. graphslim/sparsification/spanning_forest.py +19 -0
  139. graphslim/sparsification/t_spanner.py +28 -0
  140. graphslim/sparsification/utils.py +0 -0
  141. graphslim/train_all.py +69 -0
  142. graphslim/utils.py +946 -0
  143. graphslim-1.0.1.dist-info/METADATA +34 -0
  144. graphslim-1.0.1.dist-info/RECORD +146 -0
  145. graphslim-1.0.1.dist-info/WHEEL +5 -0
  146. graphslim-1.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,110 @@
1
+ import copy
2
+
3
+ import numpy as np
4
+ import scipy as sp
5
+ import torch
6
+ from pygsp import graphs
7
+ from torch_geometric.utils import to_dense_adj
8
+
9
+ from graphslim.coarsening.utils import contract_variation_edges, contract_variation_linear, get_proximity_measure, \
10
+ matching_optimal, matching_greedy, get_coarsening_matrix, coarsen_matrix, coarsen_vector, zero_diag
11
+ from graphslim.dataset.convertor import pyg2gsp, csr2ei, ei2csr
12
+ from graphslim.dataset.utils import save_reduced
13
+ from graphslim.evaluation import *
14
+ from graphslim.utils import one_hot, to_tensor
15
+ from graphslim.coarsening.coarsening_base import Coarsen
16
+
17
+
18
+ class HeavyEdge(Coarsen):
19
+
20
+ def coarsen(self, G):
21
+ """
22
+ This function provides a common interface for coarsening algorithms that contract subgraphs
23
+
24
+ Parameters
25
+ ----------
26
+ G : pygsp Graph
27
+ K : int
28
+ The size of the subspace we are interested in preserving.
29
+ r : float between (0,1)
30
+ The desired reduction defined as 1 - n/N.
31
+ method : String
32
+ ['variation_neighborhoods', 'variation_edges', 'variation_cliques', 'heavy_edge', 'algebraic_JC', 'affinity_GS', 'kron']
33
+
34
+ Returns
35
+ -------
36
+ C : np.array of size n x N
37
+ The coarsening matrix.
38
+ Gc : pygsp Graph
39
+ The smaller graph.
40
+ Call : list of np.arrays
41
+ Coarsening matrices for each level
42
+ Gall : list of (n_levels+1) pygsp Graphs
43
+ All graphs involved in the multilevel coarsening
44
+
45
+ Example
46
+ -------
47
+ C, Gc, Call, Gall = coarsen(G, K=10, r=0.8)
48
+ """
49
+ K = 10
50
+ r = 0.5
51
+ max_levels = 10
52
+ Uk = None
53
+ lk = None
54
+ max_level_r = 0.99,
55
+ r = np.clip(r, 0, 0.999)
56
+ G0 = G
57
+ N = G.N
58
+
59
+ # current and target graph sizes
60
+ n, n_target = N, np.ceil((1 - r) * N)
61
+
62
+ C = sp.sparse.eye(N, format="csc")
63
+ Gc = G
64
+
65
+ Call, Gall = [], []
66
+ Gall.append(G)
67
+ method = "heavy_edge"
68
+ algorithm=self.args.coarsen_strategy
69
+ # algorithm = "greedy"
70
+ for level in range(1, max_levels + 1):
71
+
72
+ G = Gc
73
+
74
+ # how much more we need to reduce the current graph
75
+ r_cur = np.clip(1 - n_target / n, 0.0, max_level_r)
76
+
77
+ weights = get_proximity_measure(G, method, K=K)
78
+
79
+ if algorithm == "optimal":
80
+ # the edge-weight should be light at proximal edges
81
+ weights = -weights
82
+ if "rss" not in method:
83
+ weights -= min(weights)
84
+ coarsening_list = matching_optimal(G, weights=weights, r=r_cur)
85
+
86
+ elif algorithm == "greedy":
87
+ coarsening_list = matching_greedy(G, weights=weights, r=r_cur)
88
+ iC = get_coarsening_matrix(G, coarsening_list)
89
+
90
+ if iC.shape[1] - iC.shape[0] <= 2:
91
+ break # avoid too many levels for so few nodes
92
+
93
+ C = iC.dot(C)
94
+ Call.append(iC)
95
+
96
+ Wc = zero_diag(coarsen_matrix(G.W, iC)) # coarsen and remove self-loops
97
+ Wc = (Wc + Wc.T) / 2 # this is only needed to avoid pygsp complaining for tiny errors
98
+
99
+ if not hasattr(G, "coords"):
100
+ Gc = graphs.Graph(Wc)
101
+ else:
102
+ Gc = graphs.Graph(Wc, coords=coarsen_vector(G.coords, iC))
103
+ Gall.append(Gc)
104
+
105
+ n = Gc.N
106
+
107
+ if n <= n_target:
108
+ break
109
+
110
+ return C, Gc, Call, Gall
@@ -0,0 +1,109 @@
1
+ import copy
2
+
3
+ import numpy as np
4
+ import scipy as sp
5
+ import torch
6
+ from pygsp import graphs
7
+ from torch_geometric.utils import to_dense_adj
8
+
9
+ from graphslim.coarsening.utils import contract_variation_edges, contract_variation_linear, get_proximity_measure, \
10
+ matching_optimal, matching_greedy, get_coarsening_matrix, coarsen_matrix, coarsen_vector, zero_diag
11
+ from graphslim.dataset.convertor import pyg2gsp, csr2ei, ei2csr
12
+ from graphslim.dataset.utils import save_reduced
13
+ from graphslim.evaluation import *
14
+ from graphslim.utils import one_hot, to_tensor
15
+ from graphslim.coarsening.coarsening_base import Coarsen
16
+
17
+
18
+ class Kron(Coarsen):
19
+
20
+ def coarsen(self, G):
21
+ """
22
+ This function provides a common interface for coarsening algorithms that contract subgraphs
23
+
24
+ Parameters
25
+ ----------
26
+ G : pygsp Graph
27
+ K : int
28
+ The size of the subspace we are interested in preserving.
29
+ r : float between (0,1)
30
+ The desired reduction defined as 1 - n/N.
31
+ method : String
32
+ ['variation_neighborhoods', 'variation_edges', 'variation_cliques', 'heavy_edge', 'algebraic_JC', 'affinity_GS', 'kron']
33
+
34
+ Returns
35
+ -------
36
+ C : np.array of size n x N
37
+ The coarsening matrix.
38
+ Gc : pygsp Graph
39
+ The smaller graph.
40
+ Call : list of np.arrays
41
+ Coarsening matrices for each level
42
+ Gall : list of (n_levels+1) pygsp Graphs
43
+ All graphs involved in the multilevel coarsening
44
+
45
+ Example
46
+ -------
47
+ C, Gc, Call, Gall = coarsen(G, K=10, r=0.8)
48
+ """
49
+ K = 10
50
+ r = 0.5
51
+ max_levels = 10
52
+ Uk = None
53
+ lk = None
54
+ max_level_r = 0.99,
55
+ r = np.clip(r, 0, 0.999)
56
+ G0 = G
57
+ N = G.N
58
+
59
+ # current and target graph sizes
60
+ n, n_target = N, np.ceil((1 - r) * N)
61
+
62
+ C = sp.sparse.eye(N, format="csc")
63
+ Gc = G
64
+
65
+ Call, Gall = [], []
66
+ Gall.append(G)
67
+ method = "kron"
68
+ algorithm=self.args.coarsen_strategy
69
+ for level in range(1, max_levels + 1):
70
+
71
+ G = Gc
72
+
73
+ # how much more we need to reduce the current graph
74
+ r_cur = np.clip(1 - n_target / n, 0.0, max_level_r)
75
+
76
+ weights = get_proximity_measure(G, method, K=K)
77
+
78
+ if algorithm == "optimal":
79
+ # the edge-weight should be light at proximal edges
80
+ weights = -weights
81
+ if "rss" not in method:
82
+ weights -= min(weights)
83
+ coarsening_list = matching_optimal(G, weights=weights, r=r_cur)
84
+
85
+ elif algorithm == "greedy":
86
+ coarsening_list = matching_greedy(G, weights=weights, r=r_cur)
87
+ iC = get_coarsening_matrix(G, coarsening_list)
88
+
89
+ if iC.shape[1] - iC.shape[0] <= 2:
90
+ break # avoid too many levels for so few nodes
91
+
92
+ C = iC.dot(C)
93
+ Call.append(iC)
94
+
95
+ Wc = zero_diag(coarsen_matrix(G.W, iC)) # coarsen and remove self-loops
96
+ Wc = (Wc + Wc.T) / 2 # this is only needed to avoid pygsp complaining for tiny errors
97
+
98
+ if not hasattr(G, "coords"):
99
+ Gc = graphs.Graph(Wc)
100
+ else:
101
+ Gc = graphs.Graph(Wc, coords=coarsen_vector(G.coords, iC))
102
+ Gall.append(Gc)
103
+
104
+ n = Gc.N
105
+
106
+ if n <= n_target:
107
+ break
108
+
109
+ return C, Gc, Call, Gall