Skip to content

Commit

Permalink
Merge branch 'improve-attribute-completeness-figure-text-#828' of htt…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigaszi committed Oct 31, 2024
2 parents b07cdee + fc9edd3 commit 376aa8b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
24 changes: 16 additions & 8 deletions ohsome_quality_api/indicators/attribute_completeness/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
self.attribute_key = attribute_key
self.absolute_value_1 = None
self.absolute_value_2 = None
self.description = None

async def preprocess(self) -> None:
attribute = build_attribute_filter(self.attribute_key, self.topic.key)
Expand All @@ -69,28 +70,35 @@ def calculate(self) -> None:
if self.result.value is None:
self.result.description += " No features in this region"
return
description = Template(self.templates.result_description).substitute(
result=round(self.result.value, 2),
all=round(self.absolute_value_1, 1),
matched=round(self.absolute_value_2, 1),
)
self.create_description()

if self.result.value >= self.threshold_yellow:
self.result.class_ = 5
self.result.description = (
description + self.templates.label_description["green"]
self.description + self.templates.label_description["green"]
)
elif self.threshold_yellow > self.result.value >= self.threshold_red:
self.result.class_ = 3
self.result.description = (
description + self.templates.label_description["yellow"]
self.description + self.templates.label_description["yellow"]
)
else:
self.result.class_ = 1
self.result.description = (
description + self.templates.label_description["red"]
self.description + self.templates.label_description["red"]
)

def create_description(self):
self.description = Template(self.templates.result_description).substitute(
result=round(self.result.value, 2),
all=round(self.absolute_value_1, 1),
matched=round(self.absolute_value_2, 1),
topic=self.topic.name,
tags="tags " + ", ".join(self.attribute_key)
if len(self.attribute_key) > 1
else "tag " + self.attribute_key[0],
)

def create_figure(self) -> None:
"""Create a gauge chart.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ label_description:
undefined: >-
The quality level could not be calculated for this indicator.
result_description: >-
The ratio of the features (all: $all) compared to features with
expected tags (matched: $matched) is $result.
The ratio of the topic $topic in the selected area (all: $all) compared to the topic $topic with the
expected $tags (matched: $matched) is $result.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ def indicator(
)
asyncio.run(indicator.preprocess())
indicator.calculate()
assert indicator.description == (
"The ratio of the topic Building Count in the "
"selected area (all: 29936.0) "
"compared to the topic Building Count with the "
"expected tag height (matched: 8702.0) is 0.29. "
)
return indicator

# comment out for manual test
Expand Down
46 changes: 46 additions & 0 deletions tests/unittests/indicators/test_attribute_completeness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from ohsome_quality_api.indicators.attribute_completeness.indicator import (
AttributeCompleteness,
)


def test_create_description():
def topic_building_count(): # for getting an object with the attribute "name"
pass

indicator = AttributeCompleteness(
topic_building_count,
"feature",
"attribute_key",
)
indicator.result.value = 0.2
indicator.absolute_value_1 = 10
indicator.absolute_value_2 = 2
indicator.topic.name = "test-topic"
indicator.attribute_key = ["test-attribute"]
indicator.create_description()
assert indicator.description == (
"The ratio of the topic test-topic in "
"the selected area (all: 10) compared to the "
"topic test-topic with the expected tag test-attribute (matched: 2) is 0.2. "
)


def test_create_description_multiple_attributes():
def topic_building_count():
pass

indicator = AttributeCompleteness(
topic_building_count,
"feature",
["tag_1", "tag_2", "tag_3"],
)
indicator.result.value = 0.2
indicator.absolute_value_1 = 10
indicator.absolute_value_2 = 2
indicator.topic.name = "test-topic"
indicator.create_description()
assert indicator.description == (
"The ratio of the topic test-topic in "
"the selected area (all: 10) compared to the topic "
"test-topic with the expected tags tag_1, tag_2, tag_3 (matched: 2) is 0.2. "
)

0 comments on commit 376aa8b

Please sign in to comment.