danieldux's picture
Refactor create_hierarchy_dict function to concisely include additional hierarchy levels
7fc2f52
raw
history blame
1.45 kB
"""This module provides functionality for creating a hierarchy tree and a mapping from ISCO code to node name."""
import csv
def create_hierarchy_dict(file: str) -> dict:
"""
Creates a dictionary where keys are nodes and values are sets of parent nodes representing the group level hierarchy of the ISCO-08 structure.
The function assumes that the input CSV file has a column named 'unit' with the 4-digit ISCO-08 codes.
A csv file with the ISCO-08 structure can be downloaded from the International Labour Organization (ILO) at [https://www.ilo.org/ilostat-files/ISCO/newdocs-08-2021/ISCO-08/ISCO-08 EN.csv](https://www.ilo.org/ilostat-files/ISCO/newdocs-08-2021/ISCO-08/ISCO-08%20EN.csv)
Args:
- file: A string representing the path to the CSV file containing the 4-digit ISCO-08 codes.
Returns:
- A dictionary where keys are ISCO-08 unit codes and values are sets of their parent codes.
"""
isco_hierarchy = {}
with open(file, newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
unit_code = row["unit"].zfill(4)
minor_code = unit_code[0:3]
sub_major_code = unit_code[0:2]
major_code = unit_code[0]
isco_hierarchy[unit_code] = {minor_code, major_code, sub_major_code}
return isco_hierarchy
# Example usage:
# hierarchy_dict = create_hierarchy_dict("ISCO_structure.csv")
# print(hierarchy)