Skip to content

Commit

Permalink
Add fix for mixed dict keys when calculating shortest paths
Browse files Browse the repository at this point in the history
  • Loading branch information
geographika committed May 24, 2023
1 parent 8a458d7 commit 53220ea
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install .
- name: Install demo dependencies
run: |
pip install -r requirements.demo.txt
- name: Lint with flake8
run: |
flake8 .
Expand Down
8 changes: 6 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ C:\VirtualEnvs\wayfarer\Scripts\activate.ps1

git clone https://github.com/compassinformatics/wayfarer

cd H:\Temp\wayfarer
pip install wayfarer
requirements.txt
RUN pip3 install -r requirements.txt
pip install -r requirements.demo.txt
Copy-Item -Path demo -Destination C:\VirtualEnvs\wayfarer -Recurse
Copy-Item -Path data -Destination C:\VirtualEnvs\wayfarer -Recurse
cd C:\VirtualEnvs\wayfarer\demo
uvicorn main:app --workers 8 --port 8001
20 changes: 15 additions & 5 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,25 @@ def test_get_shortest_edge():
def test_get_shortest_edge_identical():
"""
If two edges have identical lengths always
return the higher value of the two keys
return whichever key is first in the dict
"""
edges = {
-1: {"EDGE_ID": 2, "LEN_": 100},
1: {"EDGE_ID": 1, "LEN_": 100},
-1: {"EDGE_ID": 2, "LEN_": 100},
}
res = functions.get_shortest_edge(edges)
assert res[0] == 1


def test_get_shortest_edge_mixed_keys():
edges = {
1: {"EDGE_ID": "A", "LEN_": 100, "NODEID_FROM": 1, "NODEID_TO": 2},
"A": {"EDGE_ID": "B", "LEN_": 20, "NODEID_FROM": 2, "NODEID_TO": 3},
}
res = functions.get_shortest_edge(edges)
assert res[0] == "A"


def test_get_unique_ordered_list():
lst = [2, 2, 1, 1, 4, 4, 1, 2, 3]
res = functions.get_unique_ordered_list(lst)
Expand Down Expand Up @@ -447,8 +456,9 @@ def test_doctest():
# test_edges_to_graph()
# test_get_edges_from_nodes()
# test_edges_to_graph()
# test_get_shortest_edge()
# test_get_shortest_edge_identical()
test_get_shortest_edge()
test_get_shortest_edge_identical()
test_get_shortest_edge_mixed_keys()
# test_get_unique_ordered_list()
# test_get_edges_from_node_pair()
# test_to_edge()
Expand All @@ -460,5 +470,5 @@ def test_doctest():
# test_doctest()
# test_get_edges_from_nodes_non_unique()
# test_has_no_overlaps()
test_has_no_overlaps_loop()
# test_has_no_overlaps_loop()
print("Done!")
2 changes: 1 addition & 1 deletion wayfarer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import NamedTuple


__version__ = "0.9.2"
__version__ = "0.9.3"

LENGTH_FIELD = "LEN_"
EDGE_ID_FIELD = "EDGE_ID"
Expand Down
6 changes: 4 additions & 2 deletions wayfarer/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,11 @@ def get_shortest_edge(edges: dict, length_field=LENGTH_FIELD):

if not edges:
raise ValueError("The edge list is empty")
return min(
sorted(edges.items(), reverse=True), key=lambda x: x[1][length_field]

min_key = min(
edges, key=lambda k: edges[k][length_field]
) # py3 can add default=None
return min_key, edges[min_key]


def add_direction_flag(start_node, end_node, attributes, **kwargs):
Expand Down

0 comments on commit 53220ea

Please sign in to comment.