Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasschaub committed Aug 20, 2022
1 parent e28f9ed commit b487d04
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 69 deletions.
20 changes: 9 additions & 11 deletions workers/ohsome_quality_analyst/api/request_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@

import pydantic
from geojson import Feature, FeatureCollection
from pydantic import BaseModel

from ohsome_quality_analyst.base.layer import LayerData
from ohsome_quality_analyst.definitions import (
INDICATOR_LAYER,
get_dataset_names,
get_fid_fields,
get_indicator_names,
get_layer_keys,
get_report_names,
)
from ohsome_quality_analyst.utils.helper import loads_geojson, snake_to_lower_camel
from ohsome_quality_analyst.definitions import (INDICATOR_LAYER_THRESHOLDS,
get_dataset_names,
get_fid_fields,
get_indicator_names,
get_layer_keys,
get_report_names)
from ohsome_quality_analyst.utils.helper import (loads_geojson,
snake_to_lower_camel)
from pydantic import BaseModel

IndicatorEnum = Enum("IndicatorEnum", {name: name for name in get_indicator_names()})
ReportEnum = Enum("ReportEnum", {name: name for name in get_report_names()})
Expand Down
14 changes: 12 additions & 2 deletions workers/ohsome_quality_analyst/base/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import asdict, dataclass
from datetime import datetime, timezone
from io import StringIO
from typing import Dict, Literal, Optional
from typing import Dict, Literal, Optional, Tuple

import matplotlib.pyplot as plt
from dacite import from_dict
Expand Down Expand Up @@ -63,15 +63,25 @@ def label(self) -> Literal["green", "yellow", "red", "undefined"]:


class BaseIndicator(metaclass=ABCMeta):
"""The base class of every indicator."""
"""The base class of every indicator.
Attributes:
thresholds (tuple): A tuple with four float values representing the thresholds
between the result classes. The first element is the threshold between the
result class 1 and 2, the second element is the threshold between the result
class 2 and 3 and so on.
"""

def __init__(
self,
layer: Layer,
feature: Feature,
thresholds: Tuple[float, float, float, float],
) -> None:
self.layer: Layer = layer
self.feature: Feature = feature
self.thresholds: Tuple[float, float, float, float] = thresholds

# setattr(object, key, value) could be used instead of relying on from_dict.
metadata = get_metadata("indicators", type(self).__name__)
self.metadata: Metadata = from_dict(data_class=Metadata, data=metadata)
Expand Down
130 changes: 74 additions & 56 deletions workers/ohsome_quality_analyst/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import os
from dataclasses import dataclass
from types import MappingProxyType
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Tuple

import yaml

from ohsome_quality_analyst.base.layer import LayerDefinition
from ohsome_quality_analyst.config import get_config_value
from ohsome_quality_analyst.utils.exceptions import RasterDatasetUndefinedError
from ohsome_quality_analyst.utils.helper import flatten_sequence, get_module_dir
from ohsome_quality_analyst.utils.helper import (flatten_sequence,
get_module_dir)


@dataclass(frozen=True)
Expand All @@ -30,6 +30,22 @@ class RasterDataset:
nodata: Optional[int]


@dataclass(frozen=True)
class IndicatorLayerThreshold:
"""Valid Indicator/Layer combinations with optional threshold values.
Args:
indicator (str): Indicator name
layer (str): Layer name
thresholds (tuple): A tuple with thresholds values.
First element of the tuple is the threshold between result class 1 and 2.
"""

indicator: str
layer: str
threshold: Tuple[float, float, float, float] = None


RASTER_DATASETS = (
RasterDataset(
"GHS_BUILT_R2018A",
Expand Down Expand Up @@ -58,57 +74,57 @@ class RasterDataset:
)

# Possible indicator layer combinations
INDICATOR_LAYER = (
("BuildingCompleteness", "building_area"),
("GhsPopComparisonBuildings", "building_count"),
("GhsPopComparisonRoads", "jrc_road_length"),
("GhsPopComparisonRoads", "major_roads_length"),
("MappingSaturation", "building_count"),
("MappingSaturation", "major_roads_length"),
("MappingSaturation", "amenities"),
("MappingSaturation", "jrc_health_count"),
("MappingSaturation", "jrc_mass_gathering_sites_count"),
("MappingSaturation", "jrc_railway_length"),
("MappingSaturation", "jrc_road_length"),
("MappingSaturation", "jrc_education_count"),
("MappingSaturation", "mapaction_settlements_count"),
("MappingSaturation", "mapaction_major_roads_length"),
("MappingSaturation", "mapaction_rail_length"),
("MappingSaturation", "mapaction_lakes_area"),
("MappingSaturation", "mapaction_rivers_length"),
("MappingSaturation", "ideal_vgi_infrastructure"),
("MappingSaturation", "poi"),
("MappingSaturation", "lulc"),
("Currentness", "major_roads_count"),
("Currentness", "building_count"),
("Currentness", "amenities"),
("Currentness", "jrc_health_count"),
("Currentness", "jrc_education_count"),
("Currentness", "jrc_road_count"),
("Currentness", "jrc_railway_count"),
("Currentness", "jrc_airport_count"),
("Currentness", "jrc_water_treatment_plant_count"),
("Currentness", "jrc_power_generation_plant_count"),
("Currentness", "jrc_cultural_heritage_site_count"),
("Currentness", "jrc_bridge_count"),
("Currentness", "jrc_mass_gathering_sites_count"),
("Currentness", "mapaction_settlements_count"),
("Currentness", "mapaction_major_roads_length"),
("Currentness", "mapaction_rail_length"),
("Currentness", "mapaction_lakes_count"),
("Currentness", "mapaction_rivers_length"),
("PoiDensity", "poi"),
("TagsRatio", "building_count"),
("TagsRatio", "major_roads_length"),
("TagsRatio", "jrc_health_count"),
("TagsRatio", "jrc_education_count"),
("TagsRatio", "jrc_road_length"),
("TagsRatio", "jrc_airport_count"),
("TagsRatio", "jrc_power_generation_plant_count"),
("TagsRatio", "jrc_cultural_heritage_site_count"),
("TagsRatio", "jrc_bridge_count"),
("TagsRatio", "jrc_mass_gathering_sites_count"),
("Minimal", "minimal"),
INDICATOR_LAYER_THRESHOLDS = (
IndicatorLayerThreshold("BuildingCompleteness", "building_area"),
IndicatorLayerThreshold("GhsPopComparisonBuildings", "building_count"),
IndicatorLayerThreshold("GhsPopComparisonRoads", "jrc_road_length"),
IndicatorLayerThreshold("GhsPopComparisonRoads", "major_roads_length"),
IndicatorLayerThreshold("MappingSaturation", "building_count"),
IndicatorLayerThreshold("MappingSaturation", "major_roads_length"),
IndicatorLayerThreshold("MappingSaturation", "amenities"),
IndicatorLayerThreshold("MappingSaturation", "jrc_health_count"),
IndicatorLayerThreshold("MappingSaturation", "jrc_mass_gathering_sites_count"),
IndicatorLayerThreshold("MappingSaturation", "jrc_railway_length"),
IndicatorLayerThreshold("MappingSaturation", "jrc_road_length"),
IndicatorLayerThreshold("MappingSaturation", "jrc_education_count"),
IndicatorLayerThreshold("MappingSaturation", "mapaction_settlements_count"),
IndicatorLayerThreshold("MappingSaturation", "mapaction_major_roads_length"),
IndicatorLayerThreshold("MappingSaturation", "mapaction_rail_length"),
IndicatorLayerThreshold("MappingSaturation", "mapaction_lakes_area"),
IndicatorLayerThreshold("MappingSaturation", "mapaction_rivers_length"),
IndicatorLayerThreshold("MappingSaturation", "ideal_vgi_infrastructure"),
IndicatorLayerThreshold("MappingSaturation", "poi"),
IndicatorLayerThreshold("MappingSaturation", "lulc"),
IndicatorLayerThreshold("Currentness", "major_roads_count"),
IndicatorLayerThreshold("Currentness", "building_count"),
IndicatorLayerThreshold("Currentness", "amenities"),
IndicatorLayerThreshold("Currentness", "jrc_health_count"),
IndicatorLayerThreshold("Currentness", "jrc_education_count"),
IndicatorLayerThreshold("Currentness", "jrc_road_count"),
IndicatorLayerThreshold("Currentness", "jrc_railway_count"),
IndicatorLayerThreshold("Currentness", "jrc_airport_count"),
IndicatorLayerThreshold("Currentness", "jrc_water_treatment_plant_count"),
IndicatorLayerThreshold("Currentness", "jrc_power_generation_plant_count"),
IndicatorLayerThreshold("Currentness", "jrc_cultural_heritage_site_count"),
IndicatorLayerThreshold("Currentness", "jrc_bridge_count"),
IndicatorLayerThreshold("Currentness", "jrc_mass_gathering_sites_count"),
IndicatorLayerThreshold("Currentness", "mapaction_settlements_count"),
IndicatorLayerThreshold("Currentness", "mapaction_major_roads_length"),
IndicatorLayerThreshold("Currentness", "mapaction_rail_length"),
IndicatorLayerThreshold("Currentness", "mapaction_lakes_count"),
IndicatorLayerThreshold("Currentness", "mapaction_rivers_length"),
IndicatorLayerThreshold("PoiDensity", "poi"),
IndicatorLayerThreshold("TagsRatio", "building_count"),
IndicatorLayerThreshold("TagsRatio", "major_roads_length"),
IndicatorLayerThreshold("TagsRatio", "jrc_health_count"),
IndicatorLayerThreshold("TagsRatio", "jrc_education_count"),
IndicatorLayerThreshold("TagsRatio", "jrc_road_length"),
IndicatorLayerThreshold("TagsRatio", "jrc_airport_count"),
IndicatorLayerThreshold("TagsRatio", "jrc_power_generation_plant_count"),
IndicatorLayerThreshold("TagsRatio", "jrc_cultural_heritage_site_count"),
IndicatorLayerThreshold("TagsRatio", "jrc_bridge_count"),
IndicatorLayerThreshold("TagsRatio", "jrc_mass_gathering_sites_count"),
IndicatorLayerThreshold("Minimal", "minimal"),
)

ATTRIBUTION_TEXTS = MappingProxyType(
Expand Down Expand Up @@ -274,9 +290,11 @@ def get_attribution(data_keys: list) -> str:

def get_valid_layers(indcator_name: str) -> tuple:
"""Get valid Indicator/Layer combination of an Indicator."""
return tuple([tup[1] for tup in INDICATOR_LAYER if tup[0] == indcator_name])
return tuple(
[tup[1] for tup in INDICATOR_LAYER_THRESHOLDS if tup[0] == indcator_name]
)


def get_valid_indicators(layer_key: str) -> tuple:
"""Get valid Indicator/Layer combination of a Layer."""
return tuple([tup[0] for tup in INDICATOR_LAYER if tup[1] == layer_key])
return tuple([tup[0] for tup in INDICATOR_LAYER_THRESHOLDS if tup[1] == layer_key])
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from io import StringIO
from string import Template
from typing import Optional, Tuple

import dateutil.parser
import geojson
Expand Down Expand Up @@ -58,10 +59,14 @@ def __init__(
self,
layer: Layer,
feature: Feature,
thresholds: Optional[Tuple[float, float, float, float]],
) -> None:
if thresholds is None:
thresholds = (0.2, 0.5, 0.8, 0.9)
super().__init__(
layer=layer,
feature=feature,
thresholds=thresholds,
)
self.model_name: str = "Random Forest Regressor"
# Lists of elements per hexagonal cell
Expand Down

0 comments on commit b487d04

Please sign in to comment.