Spaces:
Build error
Build error
Add create_hierarchy_dict and create_hierarchy_tree functions
Browse files
isco.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
# filename: isco.py
|
|
|
2 |
import csv
|
3 |
|
4 |
|
5 |
-
def
|
6 |
"""
|
7 |
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.
|
8 |
|
@@ -34,6 +35,37 @@ def create_hierarchy(filename):
|
|
34 |
return isco_hierarchy
|
35 |
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# Example usage:
|
38 |
-
#
|
|
|
39 |
# print(hierarchy)
|
|
|
|
|
|
1 |
# filename: isco.py
|
2 |
+
from collections import defaultdict
|
3 |
import csv
|
4 |
|
5 |
|
6 |
+
def create_hierarchy_dict(filename):
|
7 |
"""
|
8 |
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.
|
9 |
|
|
|
35 |
return isco_hierarchy
|
36 |
|
37 |
|
38 |
+
def create_hierarchy_tree(hierarchy_dict: dict) -> tuple:
|
39 |
+
"""
|
40 |
+
Builds the hierarchy tree and a mapping from name to ISCO code.
|
41 |
+
|
42 |
+
Args:
|
43 |
+
- hierarchy_json: A dictionary representing the hierarchical structure.
|
44 |
+
|
45 |
+
Returns:
|
46 |
+
- tree: A dictionary representing the hierarchical structure.
|
47 |
+
- code_to_node: A dictionary mapping from ISCO code to node name.
|
48 |
+
"""
|
49 |
+
|
50 |
+
tree = defaultdict(lambda: {"children": [], "parent": None})
|
51 |
+
code_to_node = {}
|
52 |
+
|
53 |
+
def add_node(parent_code, node):
|
54 |
+
code = node["name"].split("=")[0].strip()
|
55 |
+
code_to_node[code] = node["name"]
|
56 |
+
tree[code]["parent"] = parent_code
|
57 |
+
if parent_code:
|
58 |
+
tree[parent_code]["children"].append(code)
|
59 |
+
for child in node.get("children", []):
|
60 |
+
add_node(code, child)
|
61 |
+
|
62 |
+
add_node(None, hierarchy_dict) # Root node has no parent
|
63 |
+
return tree, code_to_node
|
64 |
+
|
65 |
+
|
66 |
# Example usage:
|
67 |
+
# hierarchy_dict = create_hierarchy("ISCO_structure.csv")
|
68 |
+
# tree, code_to_node = create_hierarchy_tree(hierarchy_dict)
|
69 |
# print(hierarchy)
|
70 |
+
# print(code_to_node)
|
71 |
+
# print(tree)
|