Commit af39dccf authored by Marius Kriegerowski's avatar Marius Kriegerowski
Browse files

Refactored taxonomies

parent d9443876
......@@ -20,46 +20,46 @@ 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)
def group_tags(occupancy_mapping: list[dict[str, str]]) -> dict[str, dict[str, str]]:
"""Extract groups of identical tags and group them in nested dicts."""
grouped_tag_mapping = defaultdict(dict)
for row in taxonomy:
for row in occupancy_mapping:
key = row.pop("key")
value = row.pop("value")
grouped_taxonomy[key].update({value: row})
grouped_tag_mapping[key].update({value: row})
return grouped_taxonomy
return grouped_tag_mapping
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))
mapping = list(csv.DictReader(csvfile))
return taxonomy_reader
return mapping
class TaxonomyOccupancyMapper:
class OccupancyMapper:
"""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
def tags_to_occupancy(self, osm_tags: dict[dict[str, str]]) -> list[str]:
"""Map `osm_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
osm_tags: list of OSM building tag strings associated with a specific building
Returns:
list of `GEM_taxonomy_occupancy`s
"""
occupancies = []
for building_tag in building_tags:
for key, value in building_tag.items():
for osm_tag in osm_tags:
for key, value in osm_tag.items():
occupancy = self.mapping.get(key, {}).get(value, None)
if occupancy is not None:
occupancies.append(occupancy["GEM_taxonomy_occupancy"])
......@@ -68,8 +68,8 @@ class TaxonomyOccupancyMapper:
@classmethod
def from_csv(cls, fn: str):
"""Read a csv and initialize a `TaxonomyOccupancyMapper`."""
taxonomy = read_csv(fn)
grouped_taxonomy = group_taxonomy(taxonomy)
"""Read a csv and initialize a `OccupancyMapper`."""
occupancy_mapping = read_csv(fn)
occupancy_mapping_grouped = group_tags(occupancy_mapping)
return cls(mapping=grouped_taxonomy)
return cls(mapping=occupancy_mapping_grouped)
......@@ -18,7 +18,7 @@
from setuptools import setup, find_packages
tests_require = ["pytest"]
tests_require = ["pytest", "pytest-asyncio"]
linters_require = ["pylint", "pre-commit"]
setup(
......
......@@ -16,39 +16,39 @@
# 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, TaxonomyOccupancyMapper
from rabotnikobm.occupancy.mapping import group_tags, OccupancyMapper
def test_group_taxonomy():
def test_group_tags():
rows = [
{"key": "a", "value": 0},
{"key": "a", "value": 1},
{"key": "b", "value": 2},
]
grouped = group_taxonomy(rows)
grouped = group_tags(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 = TaxonomyOccupancyMapper.from_csv(demo_file)
mapper = OccupancyMapper.from_csv(demo_file)
assert mapper
def test_taxonomy_mapper_building_pois(pytestconfig):
def test_occupancy_mapper_building_pois(pytestconfig):
demo_file = pytestconfig.rootpath / "data/rules/occupancy/building_and_PoIs_tags.csv"
mapper = TaxonomyOccupancyMapper.from_csv(demo_file)
mapper = OccupancyMapper.from_csv(demo_file)
sample_tags = [{"amenity": "community_centre"}, {"amenity": "cafe"}, {"x": "y"}]
assert mapper.tags_to_taxonomy(sample_tags) == ["ASS4", "COM5"]
assert mapper.tags_to_occupancy(sample_tags) == ["ASS4", "COM5"]
def test_taxonomy_mapper_landuse(pytestconfig):
def test_occupancy_mapper_landuse(pytestconfig):
demo_file = pytestconfig.rootpath / "data/rules/occupancy/landuse_tags.csv"
mapper = TaxonomyOccupancyMapper.from_csv(demo_file)
mapper = OccupancyMapper.from_csv(demo_file)
sample_tags = [{"amenity": "university", "a": "b"}, {"landuse": "brownfield"}, {"x": "y"}]
assert mapper.tags_to_taxonomy(sample_tags) == ["EDU3", "UNDECIDABLE"]
assert mapper.tags_to_occupancy(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