Skip to content

Commit 11d6efb

Browse files
refactor(indicators): separate templates from metadata
separate indicators text templates for label and result description from metadata.
1 parent 5b6135a commit 11d6efb

File tree

24 files changed

+158
-116
lines changed

24 files changed

+158
-116
lines changed

ohsome_quality_api/indicators/attribute_completeness/indicator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def calculate(self) -> None:
6767
self.result.value = None
6868
if self.result.value is None:
6969
return
70-
description = Template(self.metadata.result_description).substitute(
70+
description = Template(self.templates.result_description).substitute(
7171
result=round(self.result.value, 2),
7272
all=round(self.absolute_value_1, 1),
7373
matched=round(self.absolute_value_2, 1),
@@ -79,17 +79,17 @@ def calculate(self) -> None:
7979
if self.result.value >= self.threshold_yellow:
8080
self.result.class_ = 5
8181
self.result.description = (
82-
description + self.metadata.label_description["green"]
82+
description + self.templates.label_description["green"]
8383
)
8484
elif self.threshold_yellow > self.result.value >= self.threshold_red:
8585
self.result.class_ = 3
8686
self.result.description = (
87-
description + self.metadata.label_description["yellow"]
87+
description + self.templates.label_description["yellow"]
8888
)
8989
else:
9090
self.result.class_ = 1
9191
self.result.description = (
92-
description + self.metadata.label_description["red"]
92+
description + self.templates.label_description["red"]
9393
)
9494

9595
def create_figure(self) -> None:

ohsome_quality_api/indicators/attribute_completeness/metadata.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,3 @@ attribute-completeness:
88
Derive the ratio of OSM features compared to features which
99
match additional expected tags (e.g. amenity=hospital vs
1010
amenity=hospital and wheelchair=yes).
11-
label_description:
12-
red: >-
13-
Less than 25% of the features match the expected tags.
14-
yellow: >-
15-
Around 25-75% of the features match the expected tags.
16-
green: >-
17-
More than 75% of the features match the expected tags.
18-
undefined: >-
19-
The quality level could not be calculated for this indicator.
20-
result_description: >-
21-
The ratio of the features (all: $all) compared to features with
22-
expected tags (matched: $matched) is $result.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
label_description:
3+
red: >-
4+
Less than 25% of the features match the expected tags.
5+
yellow: >-
6+
Around 25-75% of the features match the expected tags.
7+
green: >-
8+
More than 75% of the features match the expected tags.
9+
undefined: >-
10+
The quality level could not be calculated for this indicator.
11+
result_description: >-
12+
The ratio of the features (all: $all) compared to features with
13+
expected tags (matched: $matched) is $result.

ohsome_quality_api/indicators/base.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import json
2+
import os
23
from abc import ABCMeta, abstractmethod
34

45
import plotly.graph_objects as go
6+
import yaml
57
from geojson import Feature, Polygon
68

79
from ohsome_quality_api.definitions import get_attribution, get_metadata
8-
from ohsome_quality_api.indicators.models import IndicatorMetadata, Result
10+
from ohsome_quality_api.indicators.models import (
11+
IndicatorMetadata,
12+
IndicatorTemplates,
13+
Result,
14+
)
915
from ohsome_quality_api.topics.models import BaseTopic as Topic
10-
from ohsome_quality_api.utils.helper import json_serialize
16+
from ohsome_quality_api.utils.helper import (
17+
camel_to_snake,
18+
get_module_dir,
19+
json_serialize,
20+
)
1121

1222

1323
class BaseIndicator(metaclass=ABCMeta):
@@ -21,10 +31,11 @@ def __init__(
2131
self.metadata: IndicatorMetadata = get_metadata(
2232
"indicators", type(self).__name__
2333
)
34+
self.templates: IndicatorTemplates = self.get_template()
2435
self.topic: Topic = topic
2536
self.feature: Feature = feature
2637
self.result: Result = Result(
27-
description=self.metadata.label_description["undefined"],
38+
description=self.templates.label_description["undefined"],
2839
)
2940
self._get_default_figure()
3041

@@ -34,10 +45,7 @@ def as_dict(self, include_data: bool = False, exclude_label: bool = False) -> di
3445
else:
3546
result = self.result.model_dump(by_alias=True)
3647
raw_dict = {
37-
"metadata": self.metadata.model_dump(
38-
by_alias=True,
39-
exclude={"result_description", "label_description"},
40-
),
48+
"metadata": self.metadata.model_dump(by_alias=True),
4149
"topic": self.topic.model_dump(
4250
by_alias=True,
4351
exclude={"ratio_filter"},
@@ -86,6 +94,7 @@ def data(self) -> dict:
8694
data = vars(self).copy()
8795
data.pop("result")
8896
data.pop("metadata")
97+
data.pop("templates")
8998
data.pop("topic")
9099
data.pop("feature")
91100
return json.loads(json.dumps(data, default=json_serialize).encode())
@@ -167,3 +176,13 @@ def _get_default_figure(self) -> None:
167176
raw = fig.to_dict()
168177
raw["layout"].pop("template") # remove boilerplate
169178
self.result.figure = raw
179+
180+
def get_template(self) -> IndicatorTemplates:
181+
"""Get template for indicator."""
182+
indicator_key = camel_to_snake(type(self).__name__)
183+
dir = get_module_dir(f"ohsome_quality_api.indicators.{indicator_key}")
184+
file = os.path.join(dir, "templates.yaml")
185+
with open(file, "r") as file:
186+
raw = yaml.safe_load(file)
187+
templates = IndicatorTemplates(**raw)
188+
return templates

ohsome_quality_api/indicators/building_comparison/indicator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def calculate(self) -> None:
111111
edge_case = self.check_minor_edge_cases(key)
112112
# ZeroDivisionError can not occur because of `check_major_edge_cases()`
113113
self.ratio[key] = self.area_osm[key] / self.area_ref[key]
114-
template = Template(self.metadata.result_description)
114+
template = Template(self.templates.result_description)
115115
description = template.substitute(
116116
ratio=round(self.ratio[key] * 100, 2),
117117
coverage=round(self.area_cov[key] * 100, 2),
@@ -130,13 +130,13 @@ def calculate(self) -> None:
130130
self.result.class_ = 3
131131
elif self.th_low > self.result.value >= 0:
132132
self.result.class_ = 1
133-
label_description = self.metadata.label_description[self.result.label]
133+
label_description = self.templates.label_description[self.result.label]
134134
self.result.description = " ".join((label_description, result_description))
135135
elif major_edge_case:
136-
label_description = self.metadata.label_description[self.result.label]
136+
label_description = self.templates.label_description[self.result.label]
137137
self.result.description = " ".join((label_description, result_description))
138138
else:
139-
label_description = self.metadata.label_description[self.result.label]
139+
label_description = self.templates.label_description[self.result.label]
140140
edge_case = (
141141
"OSM has substantivly more buildings than the reference datasets. The "
142142
"reference dataset is likely to miss many buildings."

ohsome_quality_api/indicators/building_comparison/metadata.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,3 @@ building-comparison:
77
quality_dimension: completeness
88
description: >-
99
Comparison of OSM buildings with the buildings of reference datasets.
10-
label_description:
11-
red: >-
12-
The completeness of OSM buildings in your area-of-interest is low.
13-
yellow: >-
14-
The completeness of OSM buildings in your area-of-interest is medium.
15-
green: >-
16-
The completeness of OSM buildings in your area-of-interest is high.
17-
undefined: >-
18-
Comparison could not be made.
19-
result_description: >-
20-
The completeness in comparison to $dataset is $ratio%.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
label_description:
3+
red: >-
4+
The completeness of OSM buildings in your area-of-interest is low.
5+
yellow: >-
6+
The completeness of OSM buildings in your area-of-interest is medium.
7+
green: >-
8+
The completeness of OSM buildings in your area-of-interest is high.
9+
undefined: >-
10+
Comparison could not be made.
11+
result_description: >-
12+
The completeness in comparison to $dataset is $ratio%.

ohsome_quality_api/indicators/currentness/indicator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ def calculate(self):
158158
else:
159159
self.result.class_ = 1
160160

161-
label_description = self.metadata.label_description[self.result.label]
161+
label_description = self.templates.label_description[self.result.label]
162162
self.result.description += Template(
163-
self.metadata.result_description
163+
self.templates.result_description
164164
).substitute(
165165
up_to_date_contrib_rel=f"{sum(self.bin_up_to_date.contrib_rel) * 100:.0f}",
166166
num_of_elements=int(self.contrib_sum),

ohsome_quality_api/indicators/currentness/metadata.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,3 @@ currentness:
77
quality_dimension: currentness
88
description: >-
99
Estimate currentness of features by classifying contributions based on topic specific temporal thresholds into three groups: up-to-date, in-between and out-of-date.
10-
label_description:
11-
red: >-
12-
Many features are out-of-date.
13-
yellow: >-
14-
Some features are up-to-date and some features are out-of-date.
15-
green: >-
16-
Most features are up-to-date.
17-
undefined: >-
18-
The quality level could not be calculated for this indicator.
19-
result_description: >-
20-
In the area of interest $up_to_date_contrib_rel% of the $num_of_elements features were edited (created or modified) for the last time in the period between $from_timestamp and $to_timestamp.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
label_description:
3+
red: >-
4+
Many features are out-of-date.
5+
yellow: >-
6+
Some features are up-to-date and some features are out-of-date.
7+
green: >-
8+
Most features are up-to-date.
9+
undefined: >-
10+
The quality level could not be calculated for this indicator.
11+
result_description: >-
12+
In the area of interest $up_to_date_contrib_rel% of the $num_of_elements features were edited (created or modified) for the last time in the period between $from_timestamp and $to_timestamp.

0 commit comments

Comments
 (0)