Commit 296a66d3 authored by Marius Kriegerowski's avatar Marius Kriegerowski
Browse files

Added gem taxonomy evaluation and tests

parent 29ccaee1
#!/usr/bin/env python3
# Copyright (C) 2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
import csv
from collections import defaultdict
def group_taxonomy(taxonomy: list[dict[str, str]]) -> dict[str, dict[str, str]]:
"""Extract groups of identical keys and group them in nested dicts."""
grouped_taxonomy = defaultdict(dict)
for row in taxonomy:
key = row.pop("key")
value = row.pop("value")
grouped_taxonomy[key].update({value: row})
return grouped_taxonomy
def read_csv(fn: str) -> list[dict[str, str]]:
"""Read content from csv into list of dicts."""
with open(fn) as csvfile:
taxonomy_reader = list(csv.DictReader(csvfile))
return taxonomy_reader
class TaxonomyMapper:
"""Map building tags to `GEM_taxonomy_occupancy`"""
def __init__(self, mapping):
self.mapping = mapping
def tags_to_taxonomy(self, building_tags: dict[dict[str, str]]) -> list[str]:
"""Map `building_tags` to lists of GEM_taxonomy_occupancy as defined in
building_and_POIs_tags.csv and landuse_tags.csv. Tags may be duplicated.
Args:
building_tags: list of building tag strings
Returns:
list of `GEM_taxonomy_occupancy`s
"""
occupancies = []
for building_tag in building_tags:
for key, value in building_tag.items():
occupancy = self.mapping.get(key, {}).get(value, None)
if occupancy is not None:
occupancies.append(occupancy["GEM_taxonomy_occupancy"])
return occupancies
@classmethod
def from_csv(cls, fn: str):
"""Read a csv and initialize a `TaxonomyMapper`."""
taxonomy = read_csv(fn)
grouped_taxonomy = group_taxonomy(taxonomy)
return cls(mapping=grouped_taxonomy)
#!/usr/bin/env python3
# Copyright (C) 2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
from rabotnikobm.taxonomy.gem_taxonomy import group_taxonomy, TaxonomyMapper
def test_group_taxonomy():
rows = [
{"key": "a", "value": 0},
{"key": "a", "value": 1},
{"key": "b", "value": 2},
]
grouped = group_taxonomy(rows)
assert grouped == {"a": {0: {}, 1: {}}, "b": {2: {}}}
def test_mapper_from_csv(pytestconfig):
demo_file = pytestconfig.rootpath / "data/rules/occupancy/building_and_PoIs_tags.csv"
mapper = TaxonomyMapper.from_csv(demo_file)
assert mapper
def test_taxonomy_mapper_building_pois(pytestconfig):
demo_file = pytestconfig.rootpath / "data/rules/occupancy/building_and_PoIs_tags.csv"
mapper = TaxonomyMapper.from_csv(demo_file)
sample_tags = [{"amenity": "community_centre"}, {"amenity": "cafe"}, {"x": "y"}]
assert mapper.tags_to_taxonomy(sample_tags) == ["ASS4", "COM5"]
def test_taxonomy_mapper_landuse(pytestconfig):
demo_file = pytestconfig.rootpath / "data/rules/occupancy/landuse_tags.csv"
mapper = TaxonomyMapper.from_csv(demo_file)
sample_tags = [{"amenity": "university", "a": "b"}, {"landuse": "brownfield"}, {"x": "y"}]
assert mapper.tags_to_taxonomy(sample_tags) == ["EDU3", "UNDECIDABLE"]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment