From c39ec6fb83441d6cf174228bd8a72790336b7453 Mon Sep 17 00:00:00 2001 From: Peter Wittek Date: Thu, 19 Jan 2017 13:30:38 +0100 Subject: [PATCH 1/4] Python 3 compatibility improved --- README.md | 14 ++++++++------ example.py | 12 ++++++------ pykernels/__init__.py | 8 ++++---- pykernels/basic.py | 2 +- pykernels/graph/__init__.py | 6 +++--- pykernels/graph/allgraphlets.py | 7 ++++--- pykernels/graph/basic.py | 4 ++-- pykernels/graph/randomwalk.py | 6 +++--- pykernels/graph/shortestpath.py | 14 +++++++------- pykernels/regular.py | 2 +- 10 files changed, 39 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index c125c65..f7a2595 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ for binary sequences and even kernels for labeled graphs. ## Sample usage + from __future__ import print_function from sklearn.svm import SVC from sklearn.metrics import accuracy_score import numpy as np @@ -22,15 +23,16 @@ for binary sequences and even kernels for labeled graphs. X = np.array([[1,1], [0,0], [1,0], [0,1]]) y = np.array([1, 1, 0, 0]) - print 'Testing XOR' + print('Testing XOR') for clf, name in [(SVC(kernel=RBF(), C=1000), 'pykernel'), (SVC(kernel='rbf', C=1000), 'sklearn')]: clf.fit(X, y) - print name - print clf - print 'Predictions:', clf.predict(X) - print 'Accuracy:', accuracy_score(clf.predict(X), y) - print + print(name) + print(clf) + print('Predictions:', clf.predict(X)) + print('Accuracy:', accuracy_score(clf.predict(X), y)) + print() + ## implemented Kernels diff --git a/example.py b/example.py index 6e97350..a6603df 100644 --- a/example.py +++ b/example.py @@ -1,3 +1,4 @@ +from __future__ import print_function from sklearn.svm import SVC from sklearn.metrics import accuracy_score import numpy as np @@ -7,12 +8,11 @@ X = np.array([[1,1], [0,0], [1,0], [0,1]]) y = np.array([1, 1, 0, 0]) -print 'Testing XOR' +print('Testing XOR') for clf, name in [(SVC(kernel=RBF(), C=1000), 'pykernel'), (SVC(kernel='rbf', C=1000), 'sklearn')]: clf.fit(X, y) - print name - print clf - print 'Predictions:', clf.predict(X) - print 'Accuracy:', accuracy_score(clf.predict(X), y) - print + print(name) + print(clf) + print('Predictions:', clf.predict(X)) + print('Accuracy:', accuracy_score(clf.predict(X), y)) diff --git a/pykernels/__init__.py b/pykernels/__init__.py index 53f0736..de26884 100644 --- a/pykernels/__init__.py +++ b/pykernels/__init__.py @@ -1,6 +1,6 @@ -from basic import Linear, Polynomial, RBF -from regular import Exponential, Laplacian, RationalQuadratic, InverseMultiquadratic, Cauchy, TStudent,\ +from .basic import Linear, Polynomial, RBF +from .regular import Exponential, Laplacian, RationalQuadratic, InverseMultiquadratic, Cauchy, TStudent,\ ANOVA, Spline, Min, Log, Power, Chi2, AdditiveChi2, GeneralizedHistogramIntersection,\ Tanimoto, Sorensen, MinMax, Wavelet, Fourier -from graph.randomwalk import RandomWalk -from graph.allgraphlets import All34Graphlets +from .graph.randomwalk import RandomWalk +from .graph.allgraphlets import All34Graphlets diff --git a/pykernels/basic.py b/pykernels/basic.py index 0c72a67..27afedf 100644 --- a/pykernels/basic.py +++ b/pykernels/basic.py @@ -7,7 +7,7 @@ from pykernels.base import Kernel import numpy as np -from utils import euclidean_dist_matrix +from .utils import euclidean_dist_matrix class Linear(Kernel): """ diff --git a/pykernels/graph/__init__.py b/pykernels/graph/__init__.py index 0d777da..44ee814 100644 --- a/pykernels/graph/__init__.py +++ b/pykernels/graph/__init__.py @@ -1,3 +1,3 @@ -from randomwalk import RandomWalk -from allgraphlets import All34Graphlets -from shortestpath import ShortestPath \ No newline at end of file +from .randomwalk import RandomWalk +from .allgraphlets import All34Graphlets +from .shortestpath import ShortestPath diff --git a/pykernels/graph/allgraphlets.py b/pykernels/graph/allgraphlets.py index 4681328..54627a7 100644 --- a/pykernels/graph/allgraphlets.py +++ b/pykernels/graph/allgraphlets.py @@ -7,7 +7,8 @@ import itertools import numpy as np from pykernels.base import Kernel, GraphKernel -import basic +from .basic import graphs_to_adjacency_lists + def dec2bin(k, bitlength=0): """Decimal to binary""" @@ -114,8 +115,8 @@ def __init__(self, k=3): self.graphlet_array = _generate_graphlets(k) def _compute(self, data_1, data_2): - data_1 = basic.graphs_to_adjacency_lists(data_1) - data_2 = basic.graphs_to_adjacency_lists(data_2) + data_1 = graphs_to_adjacency_lists(data_1) + data_2 = graphs_to_adjacency_lists(data_2) d1 = np.zeros((data_1.shape[0], _number_of_graphlets(self.k))) d2 = np.zeros((data_2.shape[0], _number_of_graphlets(self.k))) for i, g in enumerate(data_1): diff --git a/pykernels/graph/basic.py b/pykernels/graph/basic.py index effd26c..82f07a1 100644 --- a/pykernels/graph/basic.py +++ b/pykernels/graph/basic.py @@ -21,10 +21,10 @@ def graphs_to_adjacency_lists(data): try: if data.ndim == 3: return np.array(data) - except Exception, exc: + except Exception: try: return np.array([G.adjacency_matix for G in data]) - except Exception, exc: + except Exception: return np.array(data) def relabel(data, data_2): diff --git a/pykernels/graph/randomwalk.py b/pykernels/graph/randomwalk.py index 7a69088..cb7b90a 100644 --- a/pykernels/graph/randomwalk.py +++ b/pykernels/graph/randomwalk.py @@ -8,7 +8,7 @@ from pykernels.base import Kernel, GraphKernel from scipy.sparse import lil_matrix, kron,identity from scipy.sparse.linalg import lsqr -import basic +from .basic import graphs_to_adjacency_lists def _norm(adj_mat): """Normalize adjacency matrix""" @@ -29,8 +29,8 @@ def __init__(self, lmb=0.5, tolerance=1e-8, maxiter=20): # either tensor of dimention 3 (list of adjacency matrices) def _compute(self, data_1, data_2): - data_1 = basic.graphs_to_adjacency_lists(data_1) - data_2 = basic.graphs_to_adjacency_lists(data_2) + data_1 = graphs_to_adjacency_lists(data_1) + data_2 = graphs_to_adjacency_lists(data_2) res = np.zeros((len(data_1), len(data_2))) N = len(data_1) * len(data_2) for i, graph1 in enumerate(data_1): diff --git a/pykernels/graph/shortestpath.py b/pykernels/graph/shortestpath.py index d62f627..51dae33 100644 --- a/pykernels/graph/shortestpath.py +++ b/pykernels/graph/shortestpath.py @@ -5,7 +5,7 @@ import numpy as np import numpy.matlib as matlib -import basic +from .basic import graphs_to_adjacency_lists, relabel from pykernels.base import Kernel, GraphKernel from scipy.sparse import lil_matrix @@ -18,9 +18,9 @@ def floyd_warshall(adj_mat, weights): res = res + ((adj_mat != 0) * weights) res[res == 0] = np.inf np.fill_diagonal(res, 0) - for i in xrange(N): - for j in xrange(N): - for k in xrange(N): + for i in range(N): + for j in range(N): + for k in range(N): if res[i, j] + res[j, k] < res[i, k]: res[i, k] = res[i, j] + res[j, k] return res @@ -85,8 +85,8 @@ def _create_accum_list(self, shortest_paths, maxpath): return res def _compute(self, data_1, data_2): - ams_1 = basic.graphs_to_adjacency_lists(data_1) - ams_2 = basic.graphs_to_adjacency_lists(data_2) + ams_1 = graphs_to_adjacency_lists(data_1) + ams_2 = graphs_to_adjacency_lists(data_2) sp_1, max1 = _apply_floyd_warshall(np.array(ams_1)) sp_2, max2 = _apply_floyd_warshall(np.array(ams_2)) maxpath = max(max1, max2) @@ -94,7 +94,7 @@ def _compute(self, data_1, data_2): accum_list_1 = self._create_accum_list(sp_1, maxpath) accum_list_2 = self._create_accum_list(sp_2, maxpath) else: - labels_1, labels_2, numlabels = basic.relabel( + labels_1, labels_2, numlabels = relabel( [G.node_labels for G in data_1], [G.node_labels for G in data_2]) accum_list_1 = self._create_accum_list_labeled(sp_1, maxpath, labels_1, numlabels) diff --git a/pykernels/regular.py b/pykernels/regular.py index c65f01e..963ffc6 100644 --- a/pykernels/regular.py +++ b/pykernels/regular.py @@ -7,7 +7,7 @@ from pykernels.base import Kernel import numpy as np -from utils import euclidean_dist_matrix +from .utils import euclidean_dist_matrix import warnings class Cossim(Kernel): From 41e57d58dde4d176612199e5eaa799e868309728 Mon Sep 17 00:00:00 2001 From: Peter Wittek Date: Thu, 19 Jan 2017 13:31:18 +0100 Subject: [PATCH 2/4] Syntax highlighting in example --- README.md | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f7a2595..b6f126e 100644 --- a/README.md +++ b/README.md @@ -13,26 +13,27 @@ for binary sequences and even kernels for labeled graphs. ## Sample usage - from __future__ import print_function - from sklearn.svm import SVC - from sklearn.metrics import accuracy_score - import numpy as np +```python +from __future__ import print_function +from sklearn.svm import SVC +from sklearn.metrics import accuracy_score +import numpy as np - from pykernels.basic import RBF +from pykernels.basic import RBF - X = np.array([[1,1], [0,0], [1,0], [0,1]]) - y = np.array([1, 1, 0, 0]) +X = np.array([[1,1], [0,0], [1,0], [0,1]]) +y = np.array([1, 1, 0, 0]) - print('Testing XOR') - - for clf, name in [(SVC(kernel=RBF(), C=1000), 'pykernel'), (SVC(kernel='rbf', C=1000), 'sklearn')]: - clf.fit(X, y) - print(name) - print(clf) - print('Predictions:', clf.predict(X)) - print('Accuracy:', accuracy_score(clf.predict(X), y)) - print() +print('Testing XOR') +for clf, name in [(SVC(kernel=RBF(), C=1000), 'pykernel'), (SVC(kernel='rbf', C=1000), 'sklearn')]: + clf.fit(X, y) + print(name) + print(clf) + print('Predictions:', clf.predict(X)) + print('Accuracy:', accuracy_score(clf.predict(X), y)) + print() +``` ## implemented Kernels From 82a692bc2c03b1ae6741824cc2ce52b3aec6bef8 Mon Sep 17 00:00:00 2001 From: Peter Wittek Date: Fri, 20 Jan 2017 20:46:53 +0100 Subject: [PATCH 3/4] All imports are relative --- pykernels/basic.py | 2 +- pykernels/graph/allgraphlets.py | 2 +- pykernels/graph/randomwalk.py | 2 +- pykernels/graph/shortestpath.py | 5 +++-- pykernels/regular.py | 5 +++-- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pykernels/basic.py b/pykernels/basic.py index 27afedf..b5a86f4 100644 --- a/pykernels/basic.py +++ b/pykernels/basic.py @@ -5,8 +5,8 @@ __author__ = 'lejlot' -from pykernels.base import Kernel import numpy as np +from .base import Kernel from .utils import euclidean_dist_matrix class Linear(Kernel): diff --git a/pykernels/graph/allgraphlets.py b/pykernels/graph/allgraphlets.py index 54627a7..497214d 100644 --- a/pykernels/graph/allgraphlets.py +++ b/pykernels/graph/allgraphlets.py @@ -6,7 +6,7 @@ import itertools import numpy as np -from pykernels.base import Kernel, GraphKernel +from ..base import Kernel, GraphKernel from .basic import graphs_to_adjacency_lists diff --git a/pykernels/graph/randomwalk.py b/pykernels/graph/randomwalk.py index cb7b90a..c85f229 100644 --- a/pykernels/graph/randomwalk.py +++ b/pykernels/graph/randomwalk.py @@ -5,9 +5,9 @@ __author__ = 'kasiajanocha' import numpy as np -from pykernels.base import Kernel, GraphKernel from scipy.sparse import lil_matrix, kron,identity from scipy.sparse.linalg import lsqr +from ..base import Kernel, GraphKernel from .basic import graphs_to_adjacency_lists def _norm(adj_mat): diff --git a/pykernels/graph/shortestpath.py b/pykernels/graph/shortestpath.py index 51dae33..9810083 100644 --- a/pykernels/graph/shortestpath.py +++ b/pykernels/graph/shortestpath.py @@ -5,9 +5,10 @@ import numpy as np import numpy.matlib as matlib -from .basic import graphs_to_adjacency_lists, relabel -from pykernels.base import Kernel, GraphKernel from scipy.sparse import lil_matrix +from ..base import Kernel, GraphKernel +from .basic import graphs_to_adjacency_lists, relabel + def floyd_warshall(adj_mat, weights): """ diff --git a/pykernels/regular.py b/pykernels/regular.py index 963ffc6..bafa655 100644 --- a/pykernels/regular.py +++ b/pykernels/regular.py @@ -5,10 +5,11 @@ __author__ = 'lejlot' -from pykernels.base import Kernel import numpy as np -from .utils import euclidean_dist_matrix import warnings +from .base import Kernel +from .utils import euclidean_dist_matrix + class Cossim(Kernel): """ From 2ad1f34e0448eb75f9e5d1601c3c00fe5be15f92 Mon Sep 17 00:00:00 2001 From: Peter Wittek Date: Fri, 20 Jan 2017 20:51:31 +0100 Subject: [PATCH 4/4] Cheap trick to default to xrange in Python2 --- pykernels/graph/shortestpath.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pykernels/graph/shortestpath.py b/pykernels/graph/shortestpath.py index 9810083..1afe552 100644 --- a/pykernels/graph/shortestpath.py +++ b/pykernels/graph/shortestpath.py @@ -2,12 +2,15 @@ A module containing Shortest Path Kernel. """ __author__ = 'kasiajanocha' - import numpy as np import numpy.matlib as matlib from scipy.sparse import lil_matrix from ..base import Kernel, GraphKernel from .basic import graphs_to_adjacency_lists, relabel +try: + range = xrange +except: + pass def floyd_warshall(adj_mat, weights):