File size: 1,458 Bytes
7375cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# filename: isco.py
import csv


def create_hierarchy(filename):
    """
    Creates a dictionary where keys are nodes and values are sets of parent nodes representing the hierarchy of the ISCO-08 codes from the "unit" column of the isco_structure.csv file.

    Args:
    - filename: A string representing the path to the CSV file containing the ISCO-08 codes and their hierarchy.

    Returns:
    - A dictionary where keys are ISCO-08 unit codes and values are sets of their parent codes.
    """
    isco_hierarchy = {}

    with open(filename, newline="") as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            # Extract unit group level code (4 digits)
            unit_code = row["unit"].zfill(4)
            # Extract the parent code for the unit group level, which is the minor group level (3 digits)
            parent_code = unit_code[:3]

            # Add the unit code to the hierarchy with its parent code
            isco_hierarchy[unit_code] = {parent_code}

            # Additionally, we can add the parent's parent codes if needed
            # For example, the major group level (1 digit) and sub-major group level (2 digits)
            major_code = unit_code[0]
            sub_major_code = unit_code[:2]
            isco_hierarchy[unit_code].update({major_code, sub_major_code})

    return isco_hierarchy


# Example usage:
# hierarchy = create_hierarchy("ISCO_structure.csv")
# print(hierarchy)