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

Refactored taxonomies

parent d9443876
...@@ -20,46 +20,46 @@ import csv ...@@ -20,46 +20,46 @@ import csv
from collections import defaultdict from collections import defaultdict
def group_taxonomy(taxonomy: list[dict[str, str]]) -> dict[str, dict[str, str]]: def group_tags(occupancy_mapping: list[dict[str, str]]) -> dict[str, dict[str, str]]:
"""Extract groups of identical keys and group them in nested dicts.""" """Extract groups of identical tags and group them in nested dicts."""
grouped_taxonomy = defaultdict(dict) grouped_tag_mapping = defaultdict(dict)
for row in taxonomy: for row in occupancy_mapping:
key = row.pop("key") key = row.pop("key")
value = row.pop("value") 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]]: def read_csv(fn: str) -> list[dict[str, str]]:
"""Read content from csv into list of dicts.""" """Read content from csv into list of dicts."""
with open(fn) as csvfile: 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`""" """Map building tags to `GEM_taxonomy_occupancy`"""
def __init__(self, mapping): def __init__(self, mapping):
self.mapping = mapping self.mapping = mapping
def tags_to_taxonomy(self, building_tags: dict[dict[str, str]]) -> list[str]: def tags_to_occupancy(self, osm_tags: dict[dict[str, str]]) -> list[str]:
"""Map `building_tags` to lists of GEM_taxonomy_occupancy as defined in """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. building_and_POIs_tags.csv and landuse_tags.csv. Tags may be duplicated.
Args: Args:
building_tags: list of building tag strings osm_tags: list of OSM building tag strings associated with a specific building
Returns: Returns:
list of `GEM_taxonomy_occupancy`s list of `GEM_taxonomy_occupancy`s
""" """
occupancies = [] occupancies = []
for building_tag in building_tags: for osm_tag in osm_tags:
for key, value in building_tag.items(): for key, value in osm_tag.items():
occupancy = self.mapping.get(key, {}).get(value, None) occupancy = self.mapping.get(key, {}).get(value, None)
if occupancy is not None: if occupancy is not None:
occupancies.append(occupancy["GEM_taxonomy_occupancy"]) occupancies.append(occupancy["GEM_taxonomy_occupancy"])
...@@ -68,8 +68,8 @@ class TaxonomyOccupancyMapper: ...@@ -68,8 +68,8 @@ class TaxonomyOccupancyMapper:
@classmethod @classmethod
def from_csv(cls, fn: str): def from_csv(cls, fn: str):
"""Read a csv and initialize a `TaxonomyOccupancyMapper`.""" """Read a csv and initialize a `OccupancyMapper`."""
taxonomy = read_csv(fn) occupancy_mapping = read_csv(fn)
grouped_taxonomy = group_taxonomy(taxonomy) occupancy_mapping_grouped = group_tags(occupancy_mapping)
return cls(mapping=grouped_taxonomy) return cls(mapping=occupancy_mapping_grouped)
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
tests_require = ["pytest"] tests_require = ["pytest", "pytest-asyncio"]
linters_require = ["pylint", "pre-commit"] linters_require = ["pylint", "pre-commit"]
setup( setup(
......
...@@ -16,39 +16,39 @@ ...@@ -16,39 +16,39 @@
# You should have received a copy of the GNU Affero General Public License # 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/. # 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 = [ rows = [
{"key": "a", "value": 0}, {"key": "a", "value": 0},
{"key": "a", "value": 1}, {"key": "a", "value": 1},
{"key": "b", "value": 2}, {"key": "b", "value": 2},
] ]
grouped = group_taxonomy(rows) grouped = group_tags(rows)
assert grouped == {"a": {0: {}, 1: {}}, "b": {2: {}}} assert grouped == {"a": {0: {}, 1: {}}, "b": {2: {}}}
def test_mapper_from_csv(pytestconfig): def test_mapper_from_csv(pytestconfig):
demo_file = pytestconfig.rootpath / "data/rules/occupancy/building_and_PoIs_tags.csv" 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 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" 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"}] 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" 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"}] 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