Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 compatibility improved #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@ for binary sequences and even kernels for labeled graphs.

## Sample usage

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'
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
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

Expand Down
12 changes: 6 additions & 6 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import numpy as np
Expand All @@ -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))
8 changes: 4 additions & 4 deletions pykernels/__init__.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pykernels/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
6 changes: 3 additions & 3 deletions pykernels/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from randomwalk import RandomWalk
from allgraphlets import All34Graphlets
from shortestpath import ShortestPath
from .randomwalk import RandomWalk
from .allgraphlets import All34Graphlets
from .shortestpath import ShortestPath
7 changes: 4 additions & 3 deletions pykernels/graph/allgraphlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pykernels/graph/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions pykernels/graph/randomwalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pykernels.base import Kernel, GraphKernel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide to use relative imports, I would keep it consistent, i.e. also change pykernels.base import to .base

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I don't have strong feelings either way, it is just the previous import scheme was broken in Python 3.

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"""
Expand All @@ -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):
Expand Down
14 changes: 7 additions & 7 deletions pykernels/graph/shortestpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Copy link
Member

@kudkudak kudkudak Jan 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will slow down code for Python2. I would use range from six module (http://pythonhosted.org/six/) which defaults to xrange for Python2

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are talking about one single file where the xrange functions were replaced, we could simply do this:

try:
  range = xrange
except:
  pass

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
Expand Down Expand Up @@ -85,16 +85,16 @@ 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)
if not self.labeled:
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)
Expand Down
2 changes: 1 addition & 1 deletion pykernels/regular.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down