Skip to content

Commit

Permalink
Merge pull request spatialdev#22 from MonicaBrandeis/mb_grid_stop_con…
Browse files Browse the repository at this point in the history
…dition

Update grid stop condition to be accurate
  • Loading branch information
Daniel B authored Jan 10, 2020
2 parents 56a27e8 + e801807 commit b1dac0c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 22 deletions.
34 changes: 12 additions & 22 deletions src/mqm/mqm_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ def stop_condition(count_zero_list, count_list, grid_percent, count_num, cell_nu
"""
# variables
smallest_max_count = 0
smallest_max_count = 0.0
smallest_max_count_ind = -1
stop_flag = False

# add zero-count back to the count list and find a maximum count that is smaller than a threshold
if count_zero_list:
count_list.insert(0, count_zero_list[0])
count_list.insert(0, float(count_zero_list[0]))
for ind, ele in enumerate(count_list):
if ele > count_num:
break
Expand All @@ -163,29 +163,19 @@ def stop_condition(count_zero_list, count_list, grid_percent, count_num, cell_nu
smallest_max_count = ele
smallest_max_count_ind = ind

# check the stop condition
if smallest_max_count_ind != -1:
total_count_within_count_num = 0
total_grids = 0
list_length = 0
# check the stop condition
if smallest_max_count_ind != -1:
total_area = sum(val for _, val in out_distribution.items()) + count_zero_list[1]
area_less_threshold = 0

if not count_zero_list: # the list is empty
list_length = smallest_max_count_ind + 1
total_grids = cell_num

else:
list_length = smallest_max_count_ind + 2
total_grids = cell_num + count_zero_list[1]

for i in range(list_length):
if count_list[i] == 0:
total_count_within_count_num += count_zero_list[1]
if count_list[0] == 0:
area_less_threshold = sum(out_distribution[key] for key in count_list[1: smallest_max_count_ind + 1]) + count_zero_list[1]

else:
total_count_within_count_num += out_distribution[count_list[i]]
if (float(total_count_within_count_num / total_grids)) > grid_percent:
stop_flag = True
area_less_threshold = sum(out_distribution[key] for key in count_list[: smallest_max_count_ind + 1])

if (float(area_less_threshold / total_area)) >= grid_percent:
stop_flag = True

return stop_flag

Expand Down
62 changes: 62 additions & 0 deletions tests/mqm/test_mqm_tool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
import mqm
import pytest
import os


def test_import():
assert mqm.road_count is not None

""" Stop Condition Testing
Args:
count_zero_list: a pair to the number of grids with zero flagged feature.
count_list: a list to the number of flagged features.
grid_percent: a percentage value for grids.
count_num: a count number for a stop condition in the first k-d tree.
cell_num: the number of keys in the dictionary.
out_distribution: a dictionary in which key and value represent the number of flagged features and counts, respectively.
Returns: stop_flag =True or False
"""


def test_default_stop_condition():
out_distribution_1 = {3.0: 2, 10.0: 1, 65.0: 1, 77.0: 1, 102.0: 1}
count_zero_list_1 = [0, 2]
count_list_1 = [13.0, 65.0, 179.0]
test_case_1 = mqm.stop_condition(count_zero_list_1, count_list_1, 0.9, 10, 5, out_distribution_1)
assert test_case_1 == False

out_distribution_2 = {2.0: 4, 3.0: 3, 4.0: 7, 5.0: 1, 6.0: 1, 8.0: 1, 9.0: 2, 10.0: 2, 11.0: 1, 13.0: 1, 18.0: 1,
19.0: 1, 20.0: 1, 23.0: 1, 31.0: 2}
count_zero_list_2 = [0, 99]
count_list_2 = [2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0, 13.0, 18.0, 19.0, 20.0, 23.0, 31.0]
test_case_2 = mqm.stop_condition(count_zero_list_2, count_list_2, 0.9, 10, 15, out_distribution_2)
assert test_case_2 == True


def test_percentage_stop_condition():
out_distribution_3 = {2.0: 4, 3.0: 3, 4.0: 7, 5.0: 1, 6.0: 1, 8.0: 1, 9.0: 2, 10.0: 2, 11.0: 1, 13.0: 1, 18.0: 1,
19.0: 1, 20.0: 1, 23.0: 1, 31.0: 2}
count_zero_list_3 = [0, 99]
count_list_3 = [2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0, 13.0, 18.0, 19.0, 20.0, 23.0, 31.0]
test_case_3 = mqm.stop_condition(count_zero_list_3, count_list_3, 0.95, 10, 15, out_distribution_3)
assert test_case_3 == False

out_distribution_4 = {2.0: 7, 3.0: 7, 4.0: 8, 5.0: 2, 7.0: 2, 8.0: 2, 9.0: 1, 10.0: 1, 11.0: 1, 16.0: 1, 18.0: 1,
20.0: 1, 21.0: 1, 31.0: 2}
count_zero_list_4 = [0, 219]
count_list_4 = [2.0, 3.0, 4.0, 5.0, 7.0, 8.0, 9.0, 10.0, 11.0, 16.0, 18.0, 20.0, 21.0, 31.0]
test_case_4 = mqm.stop_condition(count_zero_list_4, count_list_4, 0.95, 10, 15, out_distribution_4)
assert test_case_4 == True


def test_min_counts_stop_condition():
out_distribution_5 = {2.0: 4, 3.0: 3, 4.0: 7, 5.0: 1, 6.0: 1, 8.0: 1, 9.0: 2, 10.0: 2, 11.0: 1, 13.0: 1, 18.0: 1,
19.0: 1, 20.0: 1, 23.0: 1, 31.0: 2}
count_zero_list_5 = [0, 99]
count_list_5 = [2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0, 13.0, 18.0, 19.0, 20.0, 23.0, 31.0]
test_case_5 = mqm.stop_condition(count_zero_list_5, count_list_5, 0.9, 5, 15, out_distribution_5)
assert test_case_5 == False

out_distribution_6 = {2.0: 7, 3.0: 7, 4.0: 8, 5.0: 2, 7.0: 2, 8.0: 2, 9.0: 1, 10.0: 1, 11.0: 1, 16.0: 1, 18.0: 1,
20.0: 1, 21.0: 1, 31.0: 2}
count_zero_list_6 = [0, 219]
count_list_6 = [2.0, 3.0, 4.0, 5.0, 7.0, 8.0, 9.0, 10.0, 11.0, 16.0, 18.0, 20.0, 21.0, 31.0]
test_case_6 = mqm.stop_condition(count_zero_list_6, count_list_6, 0.9, 5, 15, out_distribution_6)
assert test_case_6 == True

0 comments on commit b1dac0c

Please sign in to comment.