Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add get_template function #673

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions ohsome_quality_analyst/indicators/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import json
import os
from abc import ABCMeta, abstractmethod

import plotly.graph_objects as go
import yaml
from geojson import Feature

from ohsome_quality_analyst.definitions import get_attribution, get_metadata
from ohsome_quality_analyst.definitions import get_attribution
from ohsome_quality_analyst.indicators.models import IndicatorMetadata, Result
from ohsome_quality_analyst.topics.models import BaseTopic as Topic
from ohsome_quality_analyst.utils.helper import json_serialize
from ohsome_quality_analyst.utils.helper import (
camel_to_hyphen,
camel_to_snake,
get_module_dir,
json_serialize,
)


class BaseIndicator(metaclass=ABCMeta):
Expand All @@ -18,9 +25,7 @@ def __init__(
topic: Topic,
feature: Feature,
) -> None:
self.metadata: IndicatorMetadata = get_metadata(
"indicators", type(self).__name__
)
self.metadata: IndicatorMetadata = self.get_template()
self.topic: Topic = topic
self.feature: Feature = feature
self.result: Result = Result(
Expand Down Expand Up @@ -145,3 +150,17 @@ def _get_default_figure(self) -> None:
raw = fig.to_dict()
raw["layout"].pop("template") # remove boilerplate
self.result.figure = raw

def get_template(self) -> None:
"""Get template for indicator."""
indicator_key = camel_to_snake(type(self).__name__)
directory = get_module_dir(
"ohsome_quality_analyst.indicators.{}".format(indicator_key)
)
file = os.path.join(directory, "metadata.yaml")
with open(file, "r") as file:
raw = yaml.safe_load(file)
metadata = {}
for k, v in raw.items():
metadata[k] = IndicatorMetadata(**v)
return metadata[camel_to_hyphen(type(self).__name__)]
8 changes: 7 additions & 1 deletion tests/integrationtests/test_base_indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from ohsome_quality_analyst.indicators.minimal.indicator import Minimal
from ohsome_quality_analyst.indicators.models import Result
from ohsome_quality_analyst.indicators.models import IndicatorMetadata, Result

from .utils import get_geojson_fixture, get_topic_fixture

Expand Down Expand Up @@ -62,6 +62,12 @@ def test_figure(self, feature, topic):
# comment out for manual test
pio.show(indicator.result.figure)

def test_get_template(self, feature, topic):
indicator = Minimal(feature=feature, topic=topic)
indicator.get_template()
assert isinstance(indicator.metadata, IndicatorMetadata)
assert isinstance(indicator.result, Result)


class TestBaseResult:
def test_label(self):
Expand Down