Skip to content

Commit a264919

Browse files
committed
Update examples.
1 parent 01f0b4a commit a264919

11 files changed

+629
-0
lines changed

gklearn/examples/ged/__init__.py

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# -*- coding: utf-8 -*-
2+
"""compute_graph_edit_distance.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1Wfgn7WVuyOQQgwOvdUQBz0BzEVdp0YM3
8+
9+
**This script demonstrates how to compute a graph edit distance.**
10+
---
11+
12+
**0. Install `graphkit-learn`.**
13+
"""
14+
15+
"""**1. Get dataset.**"""
16+
17+
from gklearn.utils import Dataset
18+
19+
# Predefined dataset name, use dataset "MUTAG".
20+
ds_name = 'MUTAG'
21+
22+
# Initialize a Dataset.
23+
dataset = Dataset()
24+
# Load predefined dataset "MUTAG".
25+
dataset.load_predefined_dataset(ds_name)
26+
graph1 = dataset.graphs[0]
27+
graph2 = dataset.graphs[1]
28+
print(graph1, graph2)
29+
30+
"""**2. Compute graph edit distance.**"""
31+
32+
from gklearn.ged.env import GEDEnv
33+
34+
35+
ged_env = GEDEnv() # initailize GED environment.
36+
ged_env.set_edit_cost('CONSTANT', # GED cost type.
37+
edit_cost_constants=[3, 3, 1, 3, 3, 1] # edit costs.
38+
)
39+
ged_env.add_nx_graph(graph1, '') # add graph1
40+
ged_env.add_nx_graph(graph2, '') # add graph2
41+
listID = ged_env.get_all_graph_ids() # get list IDs of graphs
42+
ged_env.init(init_type='LAZY_WITHOUT_SHUFFLED_COPIES') # initialize GED environment.
43+
options = {'initialization_method': 'RANDOM', # or 'NODE', etc.
44+
'threads': 1 # parallel threads.
45+
}
46+
ged_env.set_method('BIPARTITE', # GED method.
47+
options # options for GED method.
48+
)
49+
ged_env.init_method() # initialize GED method.
50+
51+
ged_env.run_method(listID[0], listID[1]) # run.
52+
53+
pi_forward = ged_env.get_forward_map(listID[0], listID[1]) # forward map.
54+
pi_backward = ged_env.get_backward_map(listID[0], listID[1]) # backward map.
55+
dis = ged_env.get_upper_bound(listID[0], listID[1]) # GED bewteen two graphs.
56+
print(pi_forward)
57+
print(pi_backward)
58+
print(dis)

gklearn/examples/kernels/__init__.py

Whitespace-only changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# -*- coding: utf-8 -*-
2+
"""compute_distance_in_kernel_space.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/17tZP6IrineQmzo9sRtfZOnHpHx6HnlMA
8+
9+
**This script demonstrates how to compute distance in kernel space between the image of a graph and the mean of images of a group of graphs.**
10+
---
11+
12+
**0. Install `graphkit-learn`.**
13+
"""
14+
15+
"""**1. Get dataset.**"""
16+
17+
from gklearn.utils import Dataset
18+
19+
# Predefined dataset name, use dataset "MUTAG".
20+
ds_name = 'MUTAG'
21+
22+
# Initialize a Dataset.
23+
dataset = Dataset()
24+
# Load predefined dataset "MUTAG".
25+
dataset.load_predefined_dataset(ds_name)
26+
len(dataset.graphs)
27+
28+
"""**2. Compute graph kernel.**"""
29+
30+
from gklearn.kernels import PathUpToH
31+
import multiprocessing
32+
33+
# Initailize parameters for graph kernel computation.
34+
kernel_options = {'depth': 3,
35+
'k_func': 'MinMax',
36+
'compute_method': 'trie'
37+
}
38+
39+
# Initialize graph kernel.
40+
graph_kernel = PathUpToH(node_labels=dataset.node_labels, # list of node label names.
41+
edge_labels=dataset.edge_labels, # list of edge label names.
42+
ds_infos=dataset.get_dataset_infos(keys=['directed']), # dataset information required for computation.
43+
**kernel_options, # options for computation.
44+
)
45+
46+
# Compute Gram matrix.
47+
gram_matrix, run_time = graph_kernel.compute(dataset.graphs,
48+
parallel='imap_unordered', # or None.
49+
n_jobs=multiprocessing.cpu_count(), # number of parallel jobs.
50+
normalize=True, # whether to return normalized Gram matrix.
51+
verbose=2 # whether to print out results.
52+
)
53+
54+
"""**3. Compute distance in kernel space.**
55+
56+
Given a dataset $\mathcal{G}_N$, compute the distance in kernel space between the image of $G_1 \in \mathcal{G}_N$ and the mean of images of $\mathcal{G}_k \subset \mathcal{G}_N$.
57+
"""
58+
59+
from gklearn.preimage.utils import compute_k_dis
60+
61+
# Index of $G_1$.
62+
idx_1 = 10
63+
# Indices of graphs in $\mathcal{G}_k$.
64+
idx_graphs = range(0, 10)
65+
66+
# Compute the distance in kernel space.
67+
dis_k = compute_k_dis(idx_1,
68+
idx_graphs,
69+
[1 / len(idx_graphs)] * len(idx_graphs), # weights for images of graphs in $\mathcal{G}_k$; all equal when computing the mean.
70+
gram_matrix, # gram matrix of al graphs.
71+
withterm3=False
72+
)
73+
print(dis_k)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# -*- coding: utf-8 -*-
2+
"""compute_graph_kernel.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/17Q2QCl9CAtDweGF8LiWnWoN2laeJqT0u
8+
9+
**This script demonstrates how to compute a graph kernel.**
10+
---
11+
12+
**0. Install `graphkit-learn`.**
13+
"""
14+
15+
"""**1. Get dataset.**"""
16+
17+
from gklearn.utils import Dataset
18+
19+
# Predefined dataset name, use dataset "MUTAG".
20+
ds_name = 'MUTAG'
21+
22+
# Initialize a Dataset.
23+
dataset = Dataset()
24+
# Load predefined dataset "MUTAG".
25+
dataset.load_predefined_dataset(ds_name)
26+
len(dataset.graphs)
27+
28+
"""**2. Compute graph kernel.**"""
29+
30+
from gklearn.kernels import PathUpToH
31+
32+
# Initailize parameters for graph kernel computation.
33+
kernel_options = {'depth': 3,
34+
'k_func': 'MinMax',
35+
'compute_method': 'trie'
36+
}
37+
38+
# Initialize graph kernel.
39+
graph_kernel = PathUpToH(node_labels=dataset.node_labels, # list of node label names.
40+
edge_labels=dataset.edge_labels, # list of edge label names.
41+
ds_infos=dataset.get_dataset_infos(keys=['directed']), # dataset information required for computation.
42+
**kernel_options, # options for computation.
43+
)
44+
45+
print('done.')
46+
47+
import multiprocessing
48+
import matplotlib.pyplot as plt
49+
50+
# Compute Gram matrix.
51+
gram_matrix, run_time = graph_kernel.compute(dataset.graphs,
52+
parallel='imap_unordered', # or None.
53+
n_jobs=multiprocessing.cpu_count(), # number of parallel jobs.
54+
normalize=True, # whether to return normalized Gram matrix.
55+
verbose=2 # whether to print out results.
56+
)
57+
# Print results.
58+
print()
59+
print(gram_matrix)
60+
print(run_time)
61+
plt.imshow(gram_matrix)
62+
63+
import multiprocessing
64+
65+
# Compute grah kernels between a graph and a list of graphs.
66+
kernel_list, run_time = graph_kernel.compute(dataset.graphs, # a list of graphs.
67+
dataset.graphs[0], # a single graph.
68+
parallel='imap_unordered', # or None.
69+
n_jobs=multiprocessing.cpu_count(), # number of parallel jobs.
70+
verbose=2 # whether to print out results.
71+
)
72+
# Print results.
73+
print()
74+
print(kernel_list)
75+
print(run_time)
76+
77+
import multiprocessing
78+
79+
# Compute a grah kernel between two graphs.
80+
kernel, run_time = graph_kernel.compute(dataset.graphs[0], # a single graph.
81+
dataset.graphs[1], # another single graph.
82+
verbose=2 # whether to print out results.
83+
)
84+
# Print results.
85+
print()
86+
print(kernel)
87+
print(run_time)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
"""compute_graph_kernel_v0.1.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/10jUz7-ahPiE_T1qvFrh2NvCVs1e47noj
8+
9+
**This script demonstrates how to compute a graph kernel.**
10+
---
11+
12+
**0. Install `graphkit-learn`.**
13+
"""
14+
15+
"""**1. Get dataset.**"""
16+
17+
from gklearn.utils.graphfiles import loadDataset
18+
19+
graphs, targets = loadDataset('../../../datasets/MUTAG/MUTAG_A.txt')
20+
21+
"""**2. Compute graph kernel.**"""
22+
23+
from gklearn.kernels import untilhpathkernel
24+
25+
gram_matrix, run_time = untilhpathkernel(
26+
graphs, # The list of input graphs.
27+
depth=5, # The longest length of paths.
28+
k_func='MinMax', # Or 'tanimoto'.
29+
compute_method='trie', # Or 'naive'.
30+
n_jobs=1, # The number of jobs to run in parallel.
31+
verbose=True)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
"""model_selection_old.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1uVkl7scNgEPrimX8ks6iEC5ijuhB8L_D
8+
9+
**This script demonstrates how to compute a graph kernel.**
10+
---
11+
12+
**0. Install `graphkit-learn`.**
13+
"""
14+
15+
"""**1. Perform model seletion and classification.**"""
16+
17+
from gklearn.utils import model_selection_for_precomputed_kernel
18+
from gklearn.kernels import untilhpathkernel
19+
import numpy as np
20+
21+
# Set parameters.
22+
datafile = '../../../datasets/MUTAG/MUTAG_A.txt'
23+
param_grid_precomputed = {'depth': np.linspace(1, 10, 10),
24+
'k_func': ['MinMax', 'tanimoto'],
25+
'compute_method': ['trie']}
26+
param_grid = {'C': np.logspace(-10, 10, num=41, base=10)}
27+
28+
# Perform model selection and classification.
29+
model_selection_for_precomputed_kernel(
30+
datafile, # The path of dataset file.
31+
untilhpathkernel, # The graph kernel used for estimation.
32+
param_grid_precomputed, # The parameters used to compute gram matrices.
33+
param_grid, # The penelty Parameters used for penelty items.
34+
'classification', # Or 'regression'.
35+
NUM_TRIALS=30, # The number of the random trials of the outer CV loop.
36+
ds_name='MUTAG', # The name of the dataset.
37+
n_jobs=1,
38+
verbose=True)

gklearn/examples/preimage/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)