Skip to content

Commit b5fe81c

Browse files
feat: binary_dijkstra for binary images (#40)
* feat: binary_dijkstra for binary images * fix: get it working with a few tests * docs: add some examples * test: check that black is treated as background * test: use GHA
1 parent 5c8b051 commit b5fe81c

File tree

10 files changed

+476
-47377
lines changed

10 files changed

+476
-47377
lines changed

.github/workflows/test.yaml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- "*"
10+
11+
jobs:
12+
run_tests:
13+
name: Test ${{ matrix.os }} Python ${{ matrix.python-version }}
14+
runs-on: ${{ matrix.os }}
15+
strategy:
16+
matrix:
17+
os: [ubuntu-20.04, macos-latest, windows-2019]
18+
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
19+
20+
steps:
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v2
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
package-dir: ./python
26+
27+
- uses: actions/checkout@v2
28+
29+
- name: Install dependencies
30+
run: |
31+
python -m pip install --upgrade pip
32+
python -m pip install pytest -r requirements.txt -r requirements_dev.txt
33+
34+
- name: Compile
35+
run: python setup.py develop
36+
37+
- name: Test with pytest
38+
run: pytest -v -x automated_test.py

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,6 @@ ENV/
136136
.pytest_cache
137137

138138
test.py
139-
test
139+
test
140+
141+
dijkstra3d.cpp

.travis.yml

-17
This file was deleted.

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ field = np.ones((512, 512, 512), dtype=np.int32)
1111
source = (0,0,0)
1212
target = (511, 511, 511)
1313

14+
15+
# If you're working with a binary image with one color considered
16+
# foreground the other background, use this function.
17+
path = dijkstra3d.binary_dijkstra(field, source, target, background_color=0)
18+
path = dijkstra3d.binary_dijkstra(
19+
field, source, target,
20+
anisotropy=(2.0, 2.0, 1.0),
21+
)
22+
1423
# path is an [N,3] numpy array i.e. a list of x,y,z coordinates
1524
# terminates early, default is 26 connected
1625
path = dijkstra3d.dijkstra(field, source, target, connectivity=26)

appveyor.yml

-42
This file was deleted.

automated_test.py

+101
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,107 @@ def test_dijkstra2d_10x10_26(dtype, bidirectional, connectivity, compass):
112112
[5,5,0],
113113
]))
114114

115+
@pytest.mark.parametrize("connectivity", [ 18, 26 ])
116+
def test_binary_dijkstra2d_10x10_26(connectivity):
117+
values = np.ones((10,10,1), dtype=bool)
118+
119+
path = dijkstra3d.binary_dijkstra(
120+
values, (1,1,0), (1,1,0),
121+
connectivity=connectivity,
122+
)
123+
assert len(path) == 1
124+
assert np.all(path == np.array([ [1,1,0] ]))
125+
126+
path = dijkstra3d.binary_dijkstra(
127+
values, (0,0,0), (3,0,0),
128+
connectivity=connectivity,
129+
)
130+
131+
assert len(path) == 4
132+
assert np.all(path == np.array([
133+
[0,0,0],
134+
[1,1,0],
135+
[2,1,0],
136+
[3,0,0],
137+
])) or np.all(path == np.array([
138+
[0,0,0],
139+
[1,1,0],
140+
[2,0,0],
141+
[3,0,0],
142+
])) or np.all(path == np.array([
143+
[0,0,0],
144+
[1,0,0],
145+
[2,1,0],
146+
[3,0,0],
147+
])) or np.all(path == np.array([
148+
[0,0,0],
149+
[1,0,0],
150+
[2,0,0],
151+
[3,0,0],
152+
]))
153+
154+
path = dijkstra3d.binary_dijkstra(
155+
values, (0,0,0), (5,5,0),
156+
connectivity=connectivity
157+
)
158+
159+
assert len(path) == 6
160+
assert np.all(path == np.array([
161+
[0,0,0],
162+
[1,1,0],
163+
[2,2,0],
164+
[3,3,0],
165+
[4,4,0],
166+
[5,5,0],
167+
]))
168+
169+
path = dijkstra3d.binary_dijkstra(
170+
values, (0,0,0), (9,9,0),
171+
connectivity=connectivity,
172+
)
173+
174+
assert len(path) == 10
175+
assert np.all(path == np.array([
176+
[0,0,0],
177+
[1,1,0],
178+
[2,2,0],
179+
[3,3,0],
180+
[4,4,0],
181+
[5,5,0],
182+
[6,6,0],
183+
[7,7,0],
184+
[8,8,0],
185+
[9,9,0]
186+
]))
187+
188+
path = dijkstra3d.binary_dijkstra(values, (2,1,0), (3,0,0), connectivity=connectivity)
189+
190+
assert len(path) == 2
191+
assert np.all(path == np.array([
192+
[2,1,0],
193+
[3,0,0],
194+
]))
195+
196+
path = dijkstra3d.binary_dijkstra(values, (9,9,0), (5,5,0), connectivity=connectivity)
197+
198+
assert len(path) == 5
199+
assert np.all(path == np.array([
200+
[9,9,0],
201+
[8,8,0],
202+
[7,7,0],
203+
[6,6,0],
204+
[5,5,0],
205+
]))
206+
207+
values[5:,5:] = 0
208+
path = dijkstra3d.binary_dijkstra(values, (9,9,0), (5,5,0), connectivity=connectivity)
209+
assert path.size == 0
210+
path = dijkstra3d.binary_dijkstra(values, (9,9,0), (9,9,0), connectivity=connectivity)
211+
assert path.size == 0
212+
213+
path = dijkstra3d.binary_dijkstra(values, (9,9,0), (9,9,0), connectivity=connectivity, background_color=1)
214+
assert np.all(path == np.array([[9,9,0]]))
215+
115216
@pytest.mark.parametrize("dtype", TEST_TYPES)
116217
@pytest.mark.parametrize("connectivity", [ 18, 26 ])
117218
def test_value_target_dijkstra2d_10x10_26(dtype, connectivity):

0 commit comments

Comments
 (0)