File size: 1,849 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
import json

def clean_graph_data(input_file, output_file):
    """

    Clean graph data by removing nodes with mask="None", ensuring sequential node_ids,

    and updating all neighbor references accordingly.

    """
    # Load the JSON data
    with open(input_file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # Identify valid nodes (mask != "None") and their IDs
    valid_nodes = []
    valid_node_ids = set()
    
    for node in data:
        if 'mask' in node and node['mask'] != "None":
            valid_nodes.append(node)
            valid_node_ids.add(node['node_id'])
    
    # Create mapping from old node_id to new node_id
    old_to_new_mapping = {}
    new_id = 0
    
    for node in sorted(valid_nodes, key=lambda x: x['node_id']):
        old_to_new_mapping[node['node_id']] = new_id
        new_id += 1
    
    # Update node_ids and neighbors based on the mapping
    for node in valid_nodes:
        # Update neighbors first (while node_id is still the old one)
        new_neighbors = []
        for neighbor in node['neighbors']:
            if neighbor in valid_node_ids:  # Only keep neighbors that weren't removed
                new_neighbors.append(old_to_new_mapping[neighbor])
        node['neighbors'] = new_neighbors
        
        # Update node_id
        node['node_id'] = old_to_new_mapping[node['node_id']]
    
    # Sort nodes by new node_id for better readability
    valid_nodes.sort(key=lambda x: x['node_id'])
    
    # Save the cleaned data
    with open(output_file, 'w') as f:
        json.dump(valid_nodes, f, indent=2)
    
    return f"Successfully cleaned the graph data. Removed {len(data) - len(valid_nodes)} nodes with mask='None'."

# Usage
result = clean_graph_data('wikics.json', 'wikics_cleaned.json')
print(result)