-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a7ed22
commit 5e0ff24
Showing
13 changed files
with
140 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
pywlk/dev/ | ||
pywlgk/dev/ | ||
docs/build/ | ||
*.pclprof | ||
.idea/ | ||
pywlk/__pycache__/ | ||
pywlgk/__pycache__/ | ||
tests/__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Code Documentation | ||
================== | ||
|
||
.. autofunction:: pywlk.wlk | ||
.. autofunction:: pywlgk.wlk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .wlk import wlk | ||
from .wlk import wlk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
from typing import List, Optional, Tuple | ||
|
||
import numpy as np | ||
|
||
|
||
class L: | ||
def __init__(self, value: Optional[List[int]] = None): | ||
self.value: List[int] = value or [] | ||
|
||
def __mul__(self, other): | ||
if other == 0: | ||
return L() | ||
return L(self.value) | ||
|
||
def __add__(self, other): | ||
return L(self.value + other.value) | ||
|
||
def __radd__(self, other): | ||
if isinstance(other, int): | ||
return L(self.value + [other]) | ||
return L(other.value + self.value) | ||
|
||
def __lt__(self, other): | ||
for a, b in zip(self.value, other.value): | ||
if type(a) != type(b): | ||
return str(type(a)) < str(type(b)) | ||
if a < b: | ||
return True | ||
elif a > b: | ||
return False | ||
return len(self.value) < len(other.value) | ||
|
||
def __lshift__(self, other): | ||
return L(other.value + sorted(self.value)) | ||
|
||
def __eq__(self, other): | ||
return self.value == other.value | ||
|
||
def __repr__(self): | ||
return f"L({self.value})" | ||
|
||
def __hash__(self): | ||
return hash(str(self.value)) | ||
|
||
|
||
def prep_labels(labels: List) -> Tuple[List[List[L]], int]: | ||
output = [] | ||
mapping = {} | ||
current_id = -1 | ||
for i, graph_labels in enumerate(labels): | ||
output.append([]) | ||
for l in graph_labels: | ||
if l not in mapping: | ||
current_id += 1 | ||
mapping[l] = current_id | ||
output[-1].append(L([mapping[l]])) | ||
return output, current_id | ||
|
||
|
||
def adj_mat2list(adj): | ||
return [set(np.where(neighbors == 1)[0]) for neighbors in adj] | ||
from typing import List, Optional, Tuple | ||
|
||
import numpy as np | ||
|
||
|
||
class L: | ||
def __init__(self, value: Optional[List[int]] = None): | ||
self.value: List[int] = value or [] | ||
|
||
def __mul__(self, other): | ||
if other == 0: | ||
return L() | ||
return L(self.value) | ||
|
||
def __add__(self, other): | ||
return L(self.value + other.value) | ||
|
||
def __radd__(self, other): | ||
if isinstance(other, int): | ||
return L(self.value + [other]) | ||
return L(other.value + self.value) | ||
|
||
def __lt__(self, other): | ||
for a, b in zip(self.value, other.value): | ||
if type(a) != type(b): | ||
return str(type(a)) < str(type(b)) | ||
if a < b: | ||
return True | ||
elif a > b: | ||
return False | ||
return len(self.value) < len(other.value) | ||
|
||
def __lshift__(self, other): | ||
return L(other.value + sorted(self.value)) | ||
|
||
def __eq__(self, other): | ||
return self.value == other.value | ||
|
||
def __repr__(self): | ||
return f"L({self.value})" | ||
|
||
def __hash__(self): | ||
return hash(str(self.value)) | ||
|
||
|
||
def prep_labels(labels: List) -> Tuple[List[List[L]], int]: | ||
output = [] | ||
mapping = {} | ||
current_id = -1 | ||
for i, graph_labels in enumerate(labels): | ||
output.append([]) | ||
for l in graph_labels: | ||
if l not in mapping: | ||
current_id += 1 | ||
mapping[l] = current_id | ||
output[-1].append(L([mapping[l]])) | ||
return output, current_id | ||
|
||
|
||
def adj_mat2list(adj): | ||
return [set(np.where(neighbors == 1)[0]) for neighbors in adj] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
from typing import Callable, Optional, List | ||
|
||
import numpy as np | ||
|
||
from pywlk.utils import L, prep_labels, adj_mat2list | ||
|
||
|
||
def wlk( | ||
adjs: List[np.ndarray], | ||
labels: List[np.ndarray], | ||
k: int = 4, | ||
normalize: bool = False, | ||
kernel_fn: Optional[Callable] = None, | ||
): | ||
""" | ||
Main function implementing the logic for the WLK algorithm. | ||
:param adjs: List of adjacency matrices | ||
:param labels: List of labels which can have any type | ||
:param k: number of iterations of the WLK algorithm, must be non-negative | ||
:param normalize: whether to normalize the kernel matrix or not | ||
:param kernel_fn: Kernel function to use. Default is dot product (if kernel_fn=None) as used in the original paper. Alternative functions can be used to compute the kernel matrix. These have to be provided as callables. | ||
:return: A symmetric matrix storing the pairwise metric values between graphs. Depending on the kernel function it can be distances or similarities. | ||
""" | ||
sizes = [len(graph_labels) for graph_labels in labels] | ||
edges = [adj_mat2list(adj) for adj in adjs] | ||
labels, current_id = prep_labels(labels) | ||
classes = [] | ||
|
||
for _ in range(k): | ||
tmp_labels = [[ | ||
L([graph_labels[i].value[0]] + list(sorted(graph_labels[-sizes[j] + n].value[0] for n in neighbors))) | ||
for i, neighbors in enumerate(edges[j]) | ||
] for j, graph_labels in enumerate(labels)] | ||
mapping = {} | ||
for graph_labels in tmp_labels: | ||
for l in graph_labels: | ||
if l not in mapping: | ||
current_id += 1 | ||
x = L([current_id]) | ||
mapping[l] = x | ||
for i, tmp_graph_labels in enumerate(tmp_labels): | ||
labels[i] += [mapping[label] for label in tmp_graph_labels] | ||
|
||
classes += [L([c]) for c in range(current_id + 1)] | ||
tmp = np.eye(len(classes), dtype=int) | ||
mapping = {val: tmp[i] for i, val in enumerate(classes)} | ||
subtree_k = np.array([np.array([mapping[label] for label in graph_labels]).sum(axis=0) for graph_labels in labels]) | ||
matrix = np.dot(subtree_k, subtree_k.T) if not kernel_fn else kernel_fn(subtree_k) | ||
if not normalize: | ||
return matrix | ||
return np.nan_to_num(np.divide(matrix, np.sqrt(np.outer(np.diagonal(matrix), np.diagonal(matrix))))) | ||
from typing import Callable, Optional, List | ||
|
||
import numpy as np | ||
|
||
from pywlgk.utils import L, prep_labels, adj_mat2list | ||
|
||
|
||
def wlk( | ||
adjs: List[np.ndarray], | ||
labels: List[np.ndarray], | ||
k: int = 4, | ||
normalize: bool = False, | ||
kernel_fn: Optional[Callable] = None, | ||
): | ||
""" | ||
Main function implementing the logic for the WLK algorithm. | ||
:param adjs: List of adjacency matrices | ||
:param labels: List of labels which can have any type | ||
:param k: number of iterations of the WLK algorithm, must be non-negative | ||
:param normalize: whether to normalize the kernel matrix or not | ||
:param kernel_fn: Kernel function to use. Default is dot product (if kernel_fn=None) as used in the original paper. Alternative functions can be used to compute the kernel matrix. These have to be provided as callables. | ||
:return: A symmetric matrix storing the pairwise metric values between graphs. Depending on the kernel function it can be distances or similarities. | ||
""" | ||
sizes = [len(graph_labels) for graph_labels in labels] | ||
edges = [adj_mat2list(adj) for adj in adjs] | ||
labels, current_id = prep_labels(labels) | ||
classes = [] | ||
|
||
for _ in range(k): | ||
tmp_labels = [[ | ||
L([graph_labels[i].value[0]] + list(sorted(graph_labels[-sizes[j] + n].value[0] for n in neighbors))) | ||
for i, neighbors in enumerate(edges[j]) | ||
] for j, graph_labels in enumerate(labels)] | ||
mapping = {} | ||
for graph_labels in tmp_labels: | ||
for l in graph_labels: | ||
if l not in mapping: | ||
current_id += 1 | ||
x = L([current_id]) | ||
mapping[l] = x | ||
for i, tmp_graph_labels in enumerate(tmp_labels): | ||
labels[i] += [mapping[label] for label in tmp_graph_labels] | ||
|
||
classes += [L([c]) for c in range(current_id + 1)] | ||
tmp = np.eye(len(classes), dtype=int) | ||
mapping = {val: tmp[i] for i, val in enumerate(classes)} | ||
subtree_k = np.array([np.array([mapping[label] for label in graph_labels]).sum(axis=0) for graph_labels in labels]) | ||
matrix = np.dot(subtree_k, subtree_k.T) if not kernel_fn else kernel_fn(subtree_k) | ||
if not normalize: | ||
return matrix | ||
return np.nan_to_num(np.divide(matrix, np.sqrt(np.outer(np.diagonal(matrix), np.diagonal(matrix))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package: | ||
name: "pywlk" | ||
name: "pywlgk" | ||
version: "1.0.0" | ||
|
||
source: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.