This function takes in a normalized Ontology and returns a flattened map that is in the form of {name: featureSchemaId
}. Here is a function that may help:
def map_features(ontology_normalized):
""" Creates a dictionary where keys = tool/classification/option name : values = featureSchemaId
Args:
ontology_normalized : Queried from a project using project.ontology().normalized
Returns:
Dictionary with the specified keys and values
"""
feature_map = {}
tools = ontology_normalized["tools"]
classifications = ontology_normalized["classifications"]
if tools:
feature_map, next_layer = layer_iterator(feature_map, tools)
if classifications:
feature_map, next_layer = layer_iterator(feature_map, classifications)
return feature_map
def layer_iterator(feature_map, node_layer):
for node in node_layer:
if "tool" in node.keys():
node_name = node["name"]
next_layer = node["classifications"]
elif "instructions" in node.keys():
node_name = node["instructions"]
next_layer = node["options"]
else:
node_name = node["label"]
if "options" in node.keys():
next_layer = node["options"]
else:
next_layer = []
feature_map.update({node_name : node['featureSchemaId']})
feature_map = copy.deepcopy(feature_map)
if next_layer:
feature_map, next_layer = layer_iterator(feature_map, next_layer)
return feature_map, next_layer