Fixed database typo and removed unnecessary class identifier.
This commit is contained in:
parent
00ad49a143
commit
45fb349a7d
5098 changed files with 952558 additions and 85 deletions
17
venv/Lib/site-packages/skimage/graph/__init__.py
Normal file
17
venv/Lib/site-packages/skimage/graph/__init__.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
from .spath import shortest_path
|
||||
from .mcp import MCP, MCP_Geometric, MCP_Connect, MCP_Flexible, route_through_array
|
||||
|
||||
|
||||
__all__ = ['shortest_path',
|
||||
'MCP',
|
||||
'MCP_Geometric',
|
||||
'MCP_Connect',
|
||||
'MCP_Flexible',
|
||||
'route_through_array',
|
||||
'rag_mean_color',
|
||||
'cut_threshold',
|
||||
'cut_normalized',
|
||||
'ncut',
|
||||
'draw_rag',
|
||||
'merge_hierarchical',
|
||||
'RAG']
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
venv/Lib/site-packages/skimage/graph/_mcp.cp36-win32.pyd
Normal file
BIN
venv/Lib/site-packages/skimage/graph/_mcp.cp36-win32.pyd
Normal file
Binary file not shown.
BIN
venv/Lib/site-packages/skimage/graph/_spath.cp36-win32.pyd
Normal file
BIN
venv/Lib/site-packages/skimage/graph/_spath.cp36-win32.pyd
Normal file
Binary file not shown.
BIN
venv/Lib/site-packages/skimage/graph/heap.cp36-win32.pyd
Normal file
BIN
venv/Lib/site-packages/skimage/graph/heap.cp36-win32.pyd
Normal file
Binary file not shown.
89
venv/Lib/site-packages/skimage/graph/mcp.py
Normal file
89
venv/Lib/site-packages/skimage/graph/mcp.py
Normal file
|
@ -0,0 +1,89 @@
|
|||
from ._mcp import MCP, MCP_Geometric, MCP_Connect, MCP_Flexible
|
||||
|
||||
|
||||
def route_through_array(array, start, end, fully_connected=True,
|
||||
geometric=True):
|
||||
"""Simple example of how to use the MCP and MCP_Geometric classes.
|
||||
|
||||
See the MCP and MCP_Geometric class documentation for explanation of the
|
||||
path-finding algorithm.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
array : ndarray
|
||||
Array of costs.
|
||||
start : iterable
|
||||
n-d index into `array` defining the starting point
|
||||
end : iterable
|
||||
n-d index into `array` defining the end point
|
||||
fully_connected : bool (optional)
|
||||
If True, diagonal moves are permitted, if False, only axial moves.
|
||||
geometric : bool (optional)
|
||||
If True, the MCP_Geometric class is used to calculate costs, if False,
|
||||
the MCP base class is used. See the class documentation for
|
||||
an explanation of the differences between MCP and MCP_Geometric.
|
||||
|
||||
Returns
|
||||
-------
|
||||
path : list
|
||||
List of n-d index tuples defining the path from `start` to `end`.
|
||||
cost : float
|
||||
Cost of the path. If `geometric` is False, the cost of the path is
|
||||
the sum of the values of `array` along the path. If `geometric` is
|
||||
True, a finer computation is made (see the documentation of the
|
||||
MCP_Geometric class).
|
||||
|
||||
See Also
|
||||
--------
|
||||
MCP, MCP_Geometric
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> import numpy as np
|
||||
>>> from skimage.graph import route_through_array
|
||||
>>>
|
||||
>>> image = np.array([[1, 3], [10, 12]])
|
||||
>>> image
|
||||
array([[ 1, 3],
|
||||
[10, 12]])
|
||||
>>> # Forbid diagonal steps
|
||||
>>> route_through_array(image, [0, 0], [1, 1], fully_connected=False)
|
||||
([(0, 0), (0, 1), (1, 1)], 9.5)
|
||||
>>> # Now allow diagonal steps: the path goes directly from start to end
|
||||
>>> route_through_array(image, [0, 0], [1, 1])
|
||||
([(0, 0), (1, 1)], 9.19238815542512)
|
||||
>>> # Cost is the sum of array values along the path (16 = 1 + 3 + 12)
|
||||
>>> route_through_array(image, [0, 0], [1, 1], fully_connected=False,
|
||||
... geometric=False)
|
||||
([(0, 0), (0, 1), (1, 1)], 16.0)
|
||||
>>> # Larger array where we display the path that is selected
|
||||
>>> image = np.arange((36)).reshape((6, 6))
|
||||
>>> image
|
||||
array([[ 0, 1, 2, 3, 4, 5],
|
||||
[ 6, 7, 8, 9, 10, 11],
|
||||
[12, 13, 14, 15, 16, 17],
|
||||
[18, 19, 20, 21, 22, 23],
|
||||
[24, 25, 26, 27, 28, 29],
|
||||
[30, 31, 32, 33, 34, 35]])
|
||||
>>> # Find the path with lowest cost
|
||||
>>> indices, weight = route_through_array(image, (0, 0), (5, 5))
|
||||
>>> indices = np.array(indices).T
|
||||
>>> path = np.zeros_like(image)
|
||||
>>> path[indices[0], indices[1]] = 1
|
||||
>>> path
|
||||
array([[1, 1, 1, 1, 1, 0],
|
||||
[0, 0, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 1]])
|
||||
|
||||
"""
|
||||
start, end = tuple(start), tuple(end)
|
||||
if geometric:
|
||||
mcp_class = MCP_Geometric
|
||||
else:
|
||||
mcp_class = MCP
|
||||
m = mcp_class(array, fully_connected=fully_connected)
|
||||
costs, traceback_array = m.find_costs([start], [end])
|
||||
return m.traceback(end), costs[end]
|
36
venv/Lib/site-packages/skimage/graph/setup.py
Normal file
36
venv/Lib/site-packages/skimage/graph/setup.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from skimage._build import cython
|
||||
import os.path
|
||||
|
||||
base_path = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
def configuration(parent_package='', top_path=None):
|
||||
from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs
|
||||
|
||||
config = Configuration('graph', parent_package, top_path)
|
||||
|
||||
# This function tries to create C files from the given .pyx files. If
|
||||
# it fails, try to build with pre-generated .c files.
|
||||
cython(['_spath.pyx',
|
||||
'_mcp.pyx',
|
||||
'heap.pyx'], working_path=base_path)
|
||||
|
||||
config.add_extension('_spath', sources=['_spath.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
config.add_extension('_mcp', sources=['_mcp.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
config.add_extension('heap', sources=['heap.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
return config
|
||||
|
||||
if __name__ == '__main__':
|
||||
from numpy.distutils.core import setup
|
||||
setup(maintainer='scikit-image Developers',
|
||||
maintainer_email='scikit-image@python.org',
|
||||
description='Graph-based Image-processing Algorithms',
|
||||
url='https://github.com/scikit-image/scikit-image',
|
||||
license='Modified BSD',
|
||||
**(configuration(top_path='').todict())
|
||||
)
|
81
venv/Lib/site-packages/skimage/graph/spath.py
Normal file
81
venv/Lib/site-packages/skimage/graph/spath.py
Normal file
|
@ -0,0 +1,81 @@
|
|||
import numpy as np
|
||||
from . import _spath
|
||||
|
||||
|
||||
def shortest_path(arr, reach=1, axis=-1, output_indexlist=False):
|
||||
"""Find the shortest path through an n-d array from one side to another.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
arr : ndarray of float64
|
||||
reach : int, optional
|
||||
By default (``reach = 1``), the shortest path can only move
|
||||
one row up or down for every step it moves forward (i.e.,
|
||||
the path gradient is limited to 1). `reach` defines the
|
||||
number of elements that can be skipped along each non-axis
|
||||
dimension at each step.
|
||||
axis : int, optional
|
||||
The axis along which the path must always move forward (default -1)
|
||||
output_indexlist : bool, optional
|
||||
See return value `p` for explanation.
|
||||
|
||||
Returns
|
||||
-------
|
||||
p : iterable of int
|
||||
For each step along `axis`, the coordinate of the shortest path.
|
||||
If `output_indexlist` is True, then the path is returned as a list of
|
||||
n-d tuples that index into `arr`. If False, then the path is returned
|
||||
as an array listing the coordinates of the path along the non-axis
|
||||
dimensions for each step along the axis dimension. That is,
|
||||
`p.shape == (arr.shape[axis], arr.ndim-1)` except that p is squeezed
|
||||
before returning so if `arr.ndim == 2`, then
|
||||
`p.shape == (arr.shape[axis],)`
|
||||
cost : float
|
||||
Cost of path. This is the absolute sum of all the
|
||||
differences along the path.
|
||||
|
||||
"""
|
||||
# First: calculate the valid moves from any given position. Basically,
|
||||
# always move +1 along the given axis, and then can move anywhere within
|
||||
# a grid defined by the reach.
|
||||
if axis < 0:
|
||||
axis += arr.ndim
|
||||
offset_ind_shape = (2 * reach + 1,) * (arr.ndim - 1)
|
||||
offset_indices = np.indices(offset_ind_shape) - reach
|
||||
offset_indices = np.insert(offset_indices, axis,
|
||||
np.ones(offset_ind_shape), axis=0)
|
||||
offset_size = np.multiply.reduce(offset_ind_shape)
|
||||
offsets = np.reshape(offset_indices, (arr.ndim, offset_size), order='F').T
|
||||
|
||||
# Valid starting positions are anywhere on the hyperplane defined by
|
||||
# position 0 on the given axis. Ending positions are anywhere on the
|
||||
# hyperplane at position -1 along the same.
|
||||
non_axis_shape = arr.shape[:axis] + arr.shape[axis + 1:]
|
||||
non_axis_indices = np.indices(non_axis_shape)
|
||||
non_axis_size = np.multiply.reduce(non_axis_shape)
|
||||
start_indices = np.insert(non_axis_indices, axis,
|
||||
np.zeros(non_axis_shape), axis=0)
|
||||
starts = np.reshape(start_indices, (arr.ndim, non_axis_size), order='F').T
|
||||
end_indices = np.insert(non_axis_indices, axis,
|
||||
np.full(non_axis_shape, -1,
|
||||
dtype=non_axis_indices.dtype), axis=0)
|
||||
ends = np.reshape(end_indices, (arr.ndim, non_axis_size), order='F').T
|
||||
|
||||
# Find the minimum-cost path to one of the end-points
|
||||
m = _spath.MCP_Diff(arr, offsets=offsets)
|
||||
costs, traceback = m.find_costs(starts, ends, find_all_ends=False)
|
||||
|
||||
# Figure out which end-point was found
|
||||
for end in ends:
|
||||
cost = costs[tuple(end)]
|
||||
if cost != np.inf:
|
||||
break
|
||||
traceback = m.traceback(end)
|
||||
|
||||
if not output_indexlist:
|
||||
traceback = np.array(traceback)
|
||||
traceback = np.concatenate([traceback[:, :axis],
|
||||
traceback[:, axis + 1:]], axis=1)
|
||||
traceback = np.squeeze(traceback)
|
||||
|
||||
return traceback, cost
|
9
venv/Lib/site-packages/skimage/graph/tests/__init__.py
Normal file
9
venv/Lib/site-packages/skimage/graph/tests/__init__.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
from ..._shared.testing import setup_test, teardown_test
|
||||
|
||||
|
||||
def setup():
|
||||
setup_test()
|
||||
|
||||
|
||||
def teardown():
|
||||
teardown_test()
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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)
|
75
venv/Lib/site-packages/skimage/graph/tests/test_connect.py
Normal file
75
venv/Lib/site-packages/skimage/graph/tests/test_connect.py
Normal 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)])
|
53
venv/Lib/site-packages/skimage/graph/tests/test_flexible.py
Normal file
53
venv/Lib/site-packages/skimage/graph/tests/test_flexible.py
Normal 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)
|
50
venv/Lib/site-packages/skimage/graph/tests/test_heap.py
Normal file
50
venv/Lib/site-packages/skimage/graph/tests/test_heap.py
Normal 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
|
161
venv/Lib/site-packages/skimage/graph/tests/test_mcp.py
Normal file
161
venv/Lib/site-packages/skimage/graph/tests/test_mcp.py
Normal 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
|
32
venv/Lib/site-packages/skimage/graph/tests/test_spath.py
Normal file
32
venv/Lib/site-packages/skimage/graph/tests/test_spath.py
Normal 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)
|
Loading…
Add table
Add a link
Reference in a new issue