Download data_clear.py from EnjunDu/GraphMaster: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/datasets/EnjunDu/GraphMaster/resolve/main/data_clear.py
- Command line
-
hf download hf://datasets/EnjunDu/GraphMaster/data_clear.py
-
curl -L -o data_clear.py https://huggingface.co/datasets/EnjunDu/GraphMaster/resolve/main/data_clear.py
1.85 kB
| 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) |