🔍 Code Extractor

function analyze_hierarchy

Maturity: 49

Analyzes a hierarchical database structure to extract statistics about nodes, their relationships, depths, and types.

File:
/tf/active/vicechatdev/e-ink-llm/cloudtest/analyze_replica.py
Lines:
35 - 67
Complexity:
simple

Purpose

This function processes a database dictionary containing nodes and hierarchy information to provide comprehensive statistics including root nodes identification, depth distribution analysis, node type categorization, and parent-child relationship counts. It's useful for understanding the structure and composition of hierarchical data systems, tree structures, or organizational charts stored in dictionary format.

Source Code

def analyze_hierarchy(database: Dict[str, Any]) -> Dict[str, Any]:
    """Analyze the hierarchy structure"""
    nodes = database.get('nodes', {})
    hierarchy = database.get('hierarchy', {})
    
    # Find root nodes (nodes with no parent)
    root_nodes = []
    for uuid, node in nodes.items():
        if not node.get('parent_uuid'):
            root_nodes.append(uuid)
    
    # Calculate depth statistics
    depth_counts = {}
    max_depth = 0
    
    for uuid, node in nodes.items():
        depth = node.get('depth', 0)
        depth_counts[depth] = depth_counts.get(depth, 0) + 1
        max_depth = max(max_depth, depth)
    
    # Count node types
    type_counts = {}
    for uuid, node in nodes.items():
        node_type = node.get('node_type', 'unknown')
        type_counts[node_type] = type_counts.get(node_type, 0) + 1
    
    return {
        'root_nodes': root_nodes,
        'depth_counts': depth_counts,
        'max_depth': max_depth,
        'type_counts': type_counts,
        'total_parent_relationships': len(hierarchy)
    }

Parameters

Name Type Default Kind
database Dict[str, Any] - positional_or_keyword

Parameter Details

database: A dictionary containing the hierarchical database structure. Expected to have two keys: 'nodes' (dict mapping UUIDs to node dictionaries with properties like 'parent_uuid', 'depth', and 'node_type') and 'hierarchy' (dict representing parent-child relationships). The 'nodes' dict should contain node objects with optional fields: 'parent_uuid' (string or None), 'depth' (integer), and 'node_type' (string).

Return Value

Type: Dict[str, Any]

Returns a dictionary with five keys: 'root_nodes' (list of UUIDs for nodes without parents), 'depth_counts' (dict mapping depth levels to count of nodes at that depth), 'max_depth' (integer representing the maximum depth in the hierarchy), 'type_counts' (dict mapping node types to their counts), and 'total_parent_relationships' (integer count of entries in the hierarchy dict).

Required Imports

from typing import Dict, Any

Usage Example

from typing import Dict, Any

def analyze_hierarchy(database: Dict[str, Any]) -> Dict[str, Any]:
    nodes = database.get('nodes', {})
    hierarchy = database.get('hierarchy', {})
    root_nodes = []
    for uuid, node in nodes.items():
        if not node.get('parent_uuid'):
            root_nodes.append(uuid)
    depth_counts = {}
    max_depth = 0
    for uuid, node in nodes.items():
        depth = node.get('depth', 0)
        depth_counts[depth] = depth_counts.get(depth, 0) + 1
        max_depth = max(max_depth, depth)
    type_counts = {}
    for uuid, node in nodes.items():
        node_type = node.get('node_type', 'unknown')
        type_counts[node_type] = type_counts.get(node_type, 0) + 1
    return {
        'root_nodes': root_nodes,
        'depth_counts': depth_counts,
        'max_depth': max_depth,
        'type_counts': type_counts,
        'total_parent_relationships': len(hierarchy)
    }

# Example usage
database = {
    'nodes': {
        'uuid1': {'parent_uuid': None, 'depth': 0, 'node_type': 'root'},
        'uuid2': {'parent_uuid': 'uuid1', 'depth': 1, 'node_type': 'branch'},
        'uuid3': {'parent_uuid': 'uuid1', 'depth': 1, 'node_type': 'branch'},
        'uuid4': {'parent_uuid': 'uuid2', 'depth': 2, 'node_type': 'leaf'}
    },
    'hierarchy': {
        'uuid1': ['uuid2', 'uuid3'],
        'uuid2': ['uuid4']
    }
}

result = analyze_hierarchy(database)
print(result)
# Output: {'root_nodes': ['uuid1'], 'depth_counts': {0: 1, 1: 2, 2: 1}, 'max_depth': 2, 'type_counts': {'root': 1, 'branch': 2, 'leaf': 1}, 'total_parent_relationships': 2}

Best Practices

  • Ensure the database dictionary contains both 'nodes' and 'hierarchy' keys for complete analysis
  • Node dictionaries should include 'parent_uuid', 'depth', and 'node_type' fields for accurate statistics
  • The function handles missing keys gracefully using .get() with defaults, but providing complete data yields better results
  • Root nodes are identified by the absence of 'parent_uuid' or a falsy value for that field
  • The function assumes depth values are pre-calculated in the node data; it does not compute depth from hierarchy
  • For large hierarchies, consider the memory implications of storing all statistics in the returned dictionary

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_folder_hierarchy 58.1% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file system path, connecting each folder level with PATH relationships.

    From: /tf/active/vicechatdev/offline_docstore_multi_vice.py
  • function print_database_analysis 56.6% similar

    Prints a comprehensive, formatted analysis of a reMarkable tablet replica database, including statistics, hierarchy information, file types, and a content tree visualization.

    From: /tf/active/vicechatdev/e-ink-llm/cloudtest/analyze_replica.py
  • function get_database_statistics 56.6% similar

    Retrieves statistical information about a Neo4j graph database, including counts of nodes grouped by label and counts of relationships grouped by type.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_folder_hierarchy_v2 56.6% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file path, establishing parent-child relationships between folders.

    From: /tf/active/vicechatdev/offline_parser_docstore.py
  • function create_folder_hierarchy_v1 56.1% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file path, connecting each folder level with PATH relationships.

    From: /tf/active/vicechatdev/offline_docstore_multi.py
← Back to Browse