Skip to content

Commit

Permalink
Remove numpy dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Sandve Alnæs committed Jan 27, 2016
1 parent d8c258c commit 717b890
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 44 deletions.
30 changes: 13 additions & 17 deletions nbdime/diffing/seq_bruteforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,28 @@

from six.moves import xrange as range
import operator
import numpy as np
from .lcs import diff_from_lcs

__all__ = ["diff_sequence_bruteforce"]


def bruteforce_compare_grid(A, B, compare=operator.__eq__):
"Brute force compute grid G[i, j] == compare(A[i], B[j])."
N, M = len(A), len(B)
G = np.empty((N, M), dtype=int)
for i in range(N):
for j in range(M):
G[i, j] = compare(A[i], B[j])
return G
return [[compare(a, b) for b in B] for a in A]


def bruteforce_llcs_grid(G):
"Brute force compute grid R[x, y] == llcs(A[:x], B[:y]), given G[i,j] = compare(A[i], B[j])."
N, M = G.shape
R = np.zeros((N+1, M+1), dtype=int)
"Brute force compute grid R[x][y] == llcs(A[:x], B[:y]), given G[i][j] = compare(A[i], B[j])."
N = len(G)
M = len(G[0]) if N else 0

R = [[0]*(M+1) for i in range(N+1)]
for x in range(1, N+1):
for y in range(1, M+1):
if G[x-1, y-1]:
R[x, y] = R[x-1, y-1] + 1
if G[x-1][y-1]:
R[x][y] = R[x-1][y-1] + 1
else:
R[x, y] = max(R[x-1, y], R[x, y-1])
R[x][y] = max(R[x-1][y], R[x][y-1])
return R


Expand All @@ -48,16 +44,16 @@ def bruteforce_lcs_indices(A, B, G, R, compare=operator.__eq__):
x = N
y = M
while x > 0 and y > 0:
if G[x-1, y-1]:
assert R[x, y] == R[x-1, y-1] + 1
if G[x-1][y-1]:
assert R[x][y] == R[x-1][y-1] + 1
x -= 1
y -= 1
A_indices.append(x)
B_indices.append(y)
elif R[x, y] == R[x-1, y]:
elif R[x][y] == R[x-1][y]:
x -= 1
else:
assert R[x, y] == R[x, y-1]
assert R[x][y] == R[x][y-1]
y -= 1
A_indices.reverse()
B_indices.reverse()
Expand Down
16 changes: 8 additions & 8 deletions nbdime/tests/test_diff_sequence_bruteforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ def test_diff_sequence_bruteforce():
]
for a, b in examples:
G = bruteforce_compare_grid(a, b)
assert all(bool(G[i, j]) == (a[i] == b[j]) for i in range(len(a)) for j in range(len(b)))
assert all(bool(G[i][j]) == (a[i] == b[j]) for i in range(len(a)) for j in range(len(b)))

R = bruteforce_llcs_grid(G)
for i in range(len(a)):
for j in range(len(b)):
assert R[i+1, j+1] >= R[i, j]
assert R[i+1, j] >= R[i, j]
assert R[i, j+1] >= R[i, j]
assert R[i+1, j+1] - R[i, j] <= 1
assert R[i+1, j] - R[i, j] <= 1
assert R[i, j+1] - R[i, j] <= 1
llcs = R[len(a), len(b)]
assert R[i+1][j+1] >= R[i][j]
assert R[i+1][j] >= R[i][j]
assert R[i][j+1] >= R[i][j]
assert R[i+1][j+1] - R[i][j] <= 1
assert R[i+1][j] - R[i][j] <= 1
assert R[i][j+1] - R[i][j] <= 1
llcs = R[len(a)][len(b)]

A_indices, B_indices = bruteforce_lcs_indices(a, b, G, R)
assert len(A_indices) == len(B_indices)
Expand Down
24 changes: 7 additions & 17 deletions nbdime/tests/test_myers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from __future__ import print_function

import operator
import numpy as np
from six.moves import xrange as range


Expand All @@ -18,12 +17,10 @@

class DebuggingArray(object):
"Debugging tool to capture array accesses."
def __init__(self, n, dt, name):
def __init__(self, n, name):
print("Alloc %s[%d]" % (name, n))
self.a = np.empty(n, dtype=dt)
self.untouchedvalue = -123456789
self.a[:] = self.untouchedvalue
self.w = np.zeros(n, dtype=dt)
self.a = [None]*n
self.w = [0]*n
self.name = name

def __getitem__(self, i):
Expand All @@ -43,20 +40,16 @@ def __setitem__(self, i, v):


def alloc_V_array(N, M, name):
# 32 bit should be sufficient for all practical use cases
assert max(N, M) < 2**31
int_t = np.int32

# Size of array should be big enough for N+M edits,
# and thus indexing from V[V0-D] to V[V0+D], V0=N+M
n = 2*(N + M)+1

# Not initializing V with zeros, if the algorithms access uninitialized values that's a bug
V = np.empty(n, dtype=int_t)
V = [None]*n

# Enabling this allows debugging accesses to uninitialized values
if DEBUGGING:
V = DebuggingArray(n, int_t, name)
V = DebuggingArray(n, name)

return V

Expand All @@ -71,11 +64,8 @@ def measure_snake_at(i, j, A, B, compare=operator.__eq__):

def brute_force_snake_grid(A, B, compare=operator.__eq__):
N, M = len(A), len(B)
G = np.empty((N, M), dtype=int)
for i in range(N):
for j in range(M):
G[i, j] = measure_snake_at(i, j, A, B, compare)
return G
return [[measure_snake_at(i, j, A, B, compare)
for j in range(M)] for i in range(N)]


def brute_force_snakes(A, B, compare=operator.__eq__):
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
six
nbformat
numpy
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

setuptools_args = {}
install_requires = setuptools_args['install_requires'] = [
'nbformat', 'six', 'numpy',
'nbformat', 'six',
]

extras_require = setuptools_args['extras_require'] = {
Expand Down

0 comments on commit 717b890

Please sign in to comment.