Fixed database typo and removed unnecessary class identifier.

This commit is contained in:
Batuhan Berk Başoğlu 2020-10-14 10:10:37 -04:00
parent 00ad49a143
commit 45fb349a7d
5098 changed files with 952558 additions and 85 deletions

View file

@ -0,0 +1,9 @@
from ..._shared.testing import setup_test, teardown_test
def setup():
setup_test()
def teardown():
teardown_test()

View file

@ -0,0 +1,45 @@
import numpy as np
import skimage.graph.mcp as mcp
from skimage._shared.testing import assert_array_equal
a = np.ones((8, 8), dtype=np.float32)
horizontal_ramp = np.array([[ 0., 1., 2., 3., 4., 5., 6., 7.,],
[ 0., 1., 2., 3., 4., 5., 6., 7.,],
[ 0., 1., 2., 3., 4., 5., 6., 7.,],
[ 0., 1., 2., 3., 4., 5., 6., 7.,],
[ 0., 1., 2., 3., 4., 5., 6., 7.,],
[ 0., 1., 2., 3., 4., 5., 6., 7.,],
[ 0., 1., 2., 3., 4., 5., 6., 7.,],
[ 0., 1., 2., 3., 4., 5., 6., 7.,]])
vertical_ramp = np.array( [[ 0., 0., 0., 0., 0., 0., 0., 0.,],
[ 1., 1., 1., 1., 1., 1., 1., 1.,],
[ 2., 2., 2., 2., 2., 2., 2., 2.,],
[ 3., 3., 3., 3., 3., 3., 3., 3.,],
[ 4., 4., 4., 4., 4., 4., 4., 4.,],
[ 5., 5., 5., 5., 5., 5., 5., 5.,],
[ 6., 6., 6., 6., 6., 6., 6., 6.,],
[ 7., 7., 7., 7., 7., 7., 7., 7.,]])
def test_anisotropy():
# Create seeds; vertical seeds create a horizonral ramp
seeds_for_horizontal = [(i, 0) for i in range(8)]
seeds_for_vertcal = [(0, i) for i in range(8)]
for sy in range(1, 5):
for sx in range(1, 5):
sampling = sy, sx
# Trace horizontally
m1 = mcp.MCP_Geometric(a, sampling=sampling, fully_connected=True)
costs1, traceback = m1.find_costs(seeds_for_horizontal)
# Trace vertically
m2 = mcp.MCP_Geometric(a, sampling=sampling, fully_connected=True)
costs2, traceback = m2.find_costs(seeds_for_vertcal)
# Check
assert_array_equal(costs1, horizontal_ramp * sx)
assert_array_equal(costs2, vertical_ramp * sy)

View file

@ -0,0 +1,75 @@
import numpy as np
import skimage.graph.mcp as mcp
# import stentseg.graph._mcp as mcp
from skimage._shared.testing import assert_array_equal
a = np.ones((8, 8), dtype=np.float32)
count = 0
class MCP(mcp.MCP_Connect):
def _reset(self):
""" Reset the id map.
"""
mcp.MCP_Connect._reset(self)
self._conn = {}
self._bestconn = {}
def create_connection(self, id1, id2, pos1, pos2, cost1, cost2):
# Process data
hash = min(id1, id2), max(id1, id2)
val = min(pos1, pos2), max(pos1, pos2)
cost = min(cost1, cost2)
# Add to total list
self._conn.setdefault(hash, []).append(val)
# Keep track of connection with lowest cost
curcost = self._bestconn.get(hash, (np.inf,))[0]
if cost < curcost:
self._bestconn[hash] = (cost,) + val
def test_connections():
# Create MCP object with three seed points
mcp = MCP(a)
costs, traceback = mcp.find_costs([(1, 1), (7, 7), (1, 7)])
# Test that all three seed points are connected
connections = set(mcp._conn.keys())
assert (0, 1) in connections
assert (1, 2) in connections
assert (0, 2) in connections
# Test that any two neighbors have only been connected once
for position_tuples in mcp._conn.values():
n1 = len(position_tuples)
n2 = len(set(position_tuples))
assert n1 == n2
# For seed 0 and 1
cost, pos1, pos2 = mcp._bestconn[(0, 1)]
# Test meeting points
assert (pos1, pos2) == ((3, 3), (4, 4))
# Test the whole path
path = mcp.traceback(pos1) + list(reversed(mcp.traceback(pos2)))
assert_array_equal(
path, [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)])
# For seed 1 and 2
cost, pos1, pos2 = mcp._bestconn[(1, 2)]
# Test meeting points
assert (pos1, pos2) == ((3, 7), (4, 7))
# Test the whole path
path = mcp.traceback(pos1) + list(reversed(mcp.traceback(pos2)))
assert_array_equal(
path, [(1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7)])
# For seed 0 and 2
cost, pos1, pos2 = mcp._bestconn[(0, 2)]
# Test meeting points
assert (pos1, pos2) == ((1, 3), (1, 4))
# Test the whole path
path = mcp.traceback(pos1) + list(reversed(mcp.traceback(pos2)))
assert_array_equal(
path, [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7)])

View file

@ -0,0 +1,53 @@
import numpy as np
import skimage.graph.mcp as mcp
from skimage._shared.testing import assert_array_equal
a = np.ones((8, 8), dtype=np.float32)
a[1::2] *= 2.0
class FlexibleMCP(mcp.MCP_Flexible):
""" Simple MCP subclass that allows the front to travel
a certain distance from the seed point, and uses a constant
cost factor that is independent of the cost array.
"""
def _reset(self):
mcp.MCP_Flexible._reset(self)
self._distance = np.zeros((8, 8), dtype=np.float32).ravel()
def goal_reached(self, index, cumcost):
if self._distance[index] > 4:
return 2
else:
return 0
def travel_cost(self, index, new_index, offset_length):
return 1.0 # fixed cost
def examine_neighbor(self, index, new_index, offset_length):
pass # We do not test this
def update_node(self, index, new_index, offset_length):
self._distance[new_index] = self._distance[index] + 1
def test_flexible():
# Create MCP and do a traceback
mcp = FlexibleMCP(a)
costs, traceback = mcp.find_costs([(0, 0)])
# Check that inner part is correct. This basically
# tests whether travel_cost works.
assert_array_equal(costs[:4, :4], [[1, 2, 3, 4],
[2, 2, 3, 4],
[3, 3, 3, 4],
[4, 4, 4, 4]])
# Test that the algorithm stopped at the right distance.
# Note that some of the costs are filled in but not yet frozen,
# so we take a bit of margin
assert np.all(costs[-2:, :] == np.inf)
assert np.all(costs[:, -2:] == np.inf)

View file

@ -0,0 +1,50 @@
import time
import random
import skimage.graph.heap as heap
from skimage._shared.testing import test_parallel
@test_parallel()
def test_heap():
_test_heap(100000, True)
_test_heap(100000, False)
def _test_heap(n, fast_update):
# generate random numbers with duplicates
random.seed(0)
a = [random.uniform(1.0, 100.0) for i in range(n // 2)]
a = a + a
t0 = time.perf_counter()
# insert in heap with random removals
if fast_update:
h = heap.FastUpdateBinaryHeap(128, n)
else:
h = heap.BinaryHeap(128)
for i in range(len(a)):
h.push(a[i], i)
if a[i] < 25:
# double-push same ref sometimes to test fast update codepaths
h.push(2 * a[i], i)
if 25 < a[i] < 50:
# pop some to test random removal
h.pop()
# pop from heap
b = []
while True:
try:
b.append(h.pop()[0])
except IndexError:
break
t1 = time.perf_counter()
# verify
for i in range(1, len(b)):
assert(b[i] >= b[i - 1])
return t1 - t0

View file

@ -0,0 +1,161 @@
import numpy as np
import skimage.graph.mcp as mcp
from skimage._shared.testing import (assert_array_equal, assert_almost_equal,
parametrize)
from skimage._shared._warnings import expected_warnings
np.random.seed(0)
a = np.ones((8, 8), dtype=np.float32)
a[1:-1, 1] = 0
a[1, 1:-1] = 0
warning_optional = r'|\A\Z'
def test_basic():
with expected_warnings(['Upgrading NumPy' + warning_optional]):
m = mcp.MCP(a, fully_connected=True)
costs, traceback = m.find_costs([(1, 6)])
return_path = m.traceback((7, 2))
assert_array_equal(costs,
[[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 1., 1., 1., 1., 1., 1.],
[1., 0., 1., 2., 2., 2., 2., 2.],
[1., 0., 1., 2., 3., 3., 3., 3.],
[1., 0., 1., 2., 3., 4., 4., 4.],
[1., 0., 1., 2., 3., 4., 5., 5.],
[1., 1., 1., 2., 3., 4., 5., 6.]])
assert_array_equal(return_path,
[(1, 6),
(1, 5),
(1, 4),
(1, 3),
(1, 2),
(2, 1),
(3, 1),
(4, 1),
(5, 1),
(6, 1),
(7, 2)])
def test_neg_inf():
expected_costs = np.where(a == 1, np.inf, 0)
expected_path = [(1, 6),
(1, 5),
(1, 4),
(1, 3),
(1, 2),
(2, 1),
(3, 1),
(4, 1),
(5, 1),
(6, 1)]
test_neg = np.where(a == 1, -1, 0)
test_inf = np.where(a == 1, np.inf, 0)
with expected_warnings(['Upgrading NumPy' + warning_optional]):
m = mcp.MCP(test_neg, fully_connected=True)
costs, traceback = m.find_costs([(1, 6)])
return_path = m.traceback((6, 1))
assert_array_equal(costs, expected_costs)
assert_array_equal(return_path, expected_path)
with expected_warnings(['Upgrading NumPy' + warning_optional]):
m = mcp.MCP(test_inf, fully_connected=True)
costs, traceback = m.find_costs([(1, 6)])
return_path = m.traceback((6, 1))
assert_array_equal(costs, expected_costs)
assert_array_equal(return_path, expected_path)
def test_route():
with expected_warnings(['Upgrading NumPy' + warning_optional]):
return_path, cost = mcp.route_through_array(a, (1, 6), (7, 2),
geometric=True)
assert_almost_equal(cost, np.sqrt(2) / 2)
assert_array_equal(return_path,
[(1, 6),
(1, 5),
(1, 4),
(1, 3),
(1, 2),
(2, 1),
(3, 1),
(4, 1),
(5, 1),
(6, 1),
(7, 2)])
def test_no_diagonal():
with expected_warnings(['Upgrading NumPy' + warning_optional]):
m = mcp.MCP(a, fully_connected=False)
costs, traceback = m.find_costs([(1, 6)])
return_path = m.traceback((7, 2))
assert_array_equal(costs,
[[2., 1., 1., 1., 1., 1., 1., 2.],
[1., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 1., 1., 1., 1., 1., 2.],
[1., 0., 1., 2., 2., 2., 2., 3.],
[1., 0., 1., 2., 3., 3., 3., 4.],
[1., 0., 1., 2., 3., 4., 4., 5.],
[1., 0., 1., 2., 3., 4., 5., 6.],
[2., 1., 2., 3., 4., 5., 6., 7.]])
assert_array_equal(return_path,
[(1, 6),
(1, 5),
(1, 4),
(1, 3),
(1, 2),
(1, 1),
(2, 1),
(3, 1),
(4, 1),
(5, 1),
(6, 1),
(7, 1),
(7, 2)])
def test_offsets():
offsets = [(1, i) for i in range(10)] + [(1, -i) for i in range(1, 10)]
with expected_warnings(['Upgrading NumPy' + warning_optional]):
m = mcp.MCP(a, offsets=offsets)
costs, traceback = m.find_costs([(1, 6)])
assert_array_equal(traceback,
[[-2, -2, -2, -2, -2, -2, -2, -2],
[-2, -2, -2, -2, -2, -2, -1, -2],
[15, 14, 13, 12, 11, 10, 0, 1],
[10, 0, 1, 2, 3, 4, 5, 6],
[10, 0, 1, 2, 3, 4, 5, 6],
[10, 0, 1, 2, 3, 4, 5, 6],
[10, 0, 1, 2, 3, 4, 5, 6],
[10, 0, 1, 2, 3, 4, 5, 6]])
@parametrize("shape", [(100, 100), (5, 8, 13, 17)] * 5)
def test_crashing(shape):
_test_random(shape)
def _test_random(shape):
# Just tests for crashing -- not for correctness.
a = np.random.rand(*shape).astype(np.float32)
starts = [[0] * len(shape), [-1] * len(shape),
(np.random.rand(len(shape)) * shape).astype(int)]
ends = [(np.random.rand(len(shape)) * shape).astype(int)
for i in range(4)]
with expected_warnings(['Upgrading NumPy' + warning_optional]):
m = mcp.MCP(a, fully_connected=True)
costs, offsets = m.find_costs(starts)
for point in [(np.random.rand(len(shape)) * shape).astype(int)
for i in range(4)]:
m.traceback(point)
m._reset()
m.find_costs(starts, ends)
for end in ends:
m.traceback(end)
return a, costs, offsets

View file

@ -0,0 +1,32 @@
import numpy as np
import skimage.graph.spath as spath
from skimage._shared.testing import assert_equal, assert_array_equal
def test_basic():
x = np.array([[1, 1, 3],
[0, 2, 0],
[4, 3, 1]])
path, cost = spath.shortest_path(x)
assert_array_equal(path, [0, 0, 1])
assert_equal(cost, 1)
def test_reach():
x = np.array([[1, 1, 3],
[0, 2, 0],
[4, 3, 1]])
path, cost = spath.shortest_path(x, reach=2)
assert_array_equal(path, [0, 0, 2])
assert_equal(cost, 0)
def test_non_square():
x = np.array([[1, 1, 1, 1, 5, 5, 5],
[5, 0, 0, 5, 9, 1, 1],
[0, 5, 1, 0, 5, 5, 0],
[6, 1, 1, 5, 0, 0, 1]])
path, cost = spath.shortest_path(x, reach=2)
assert_array_equal(path, [2, 1, 1, 2, 3, 3, 2])
assert_equal(cost, 0)