File size: 2,207 Bytes
593899c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
import networkx as nx
import community as community_louvain  # You may need to install this package using: pip install python-louvain

def main():
    # Load the JSON file
    with open('arxiv2023_1624-10.json', 'r', encoding='utf-8') as f:
        data = json.load(f)

    # Initialize counters and sets
    nodes_count = len(data)
    classes = set()
    train_nodes_count = 0
    validation_nodes_count = 0
    test_nodes_count = 0

    # Build an undirected graph
    G = nx.Graph()

    # Iterate over each element in the dataset
    for entry in data:
        node_id = entry['node_id']
        label = entry['label']
        mask = entry['mask']

        # Add label to the classes set
        classes.add(label)

        # Add the node with its attributes to the graph
        G.add_node(node_id, label=label, mask=mask)

        # Count nodes by mask type
        if mask == 'Train':
            train_nodes_count += 1
        elif mask == 'Validation':
            validation_nodes_count += 1
        elif mask == 'Test':
            test_nodes_count += 1

        # Process neighbors and add edges (using set to remove duplicates)
        neighbors = set(entry['neighbors'])
        for neighbor in neighbors:
            # Avoid self-loop if desired (optional)
            if neighbor != node_id:
                G.add_edge(node_id, neighbor)
            # If you want to add self-loops, remove the above condition

    # Compute the number of edges in the graph
    edge_count = G.number_of_edges()
    classes_count = len(classes)

    # Perform Louvain community detection
    partition = community_louvain.best_partition(G)
    communities = set(partition.values())
    community_count = len(communities)

    # Print out the statistics
    print("Nodes count:", nodes_count)
    print("Edges count:", edge_count)
    print("Classes count:", classes_count)
    print("Train nodes count:", train_nodes_count)
    print("Validation nodes count:", validation_nodes_count)
    print("Test nodes count:", test_nodes_count)
    print("Louvain community count:", community_count)

if __name__ == '__main__':
    main()