| | import json
|
| | import random
|
| |
|
| | path = r"C:\Code_Compiling\02_bit_Li\07_LLM4GDA\data\arxiv2023_label_16_10.json"
|
| |
|
| | with open(path, 'r', encoding='utf-8') as f:
|
| | data = json.load(f)
|
| |
|
| |
|
| | label_counts = {}
|
| | for node in data:
|
| | label = node['label']
|
| | if node['mask'] == 'Train':
|
| | if label not in label_counts:
|
| | label_counts[label] = 0
|
| | label_counts[label] += 1
|
| |
|
| |
|
| | print("Train Label counts:", label_counts)
|
| |
|
| |
|
| | x = int(input("Enter label value (x): "))
|
| | y = int(input("Enter number of nodes to keep (y): "))
|
| |
|
| |
|
| | train_x_nodes = [node for node in data if node['label'] == x and node['mask'] == 'Train']
|
| |
|
| |
|
| | if len(train_x_nodes) < y:
|
| | print(f"Warning: There are fewer than {y} nodes with label {x} and mask 'train'. All {len(train_x_nodes)} nodes will be kept.")
|
| | selected_nodes = train_x_nodes
|
| | else:
|
| |
|
| | selected_nodes = random.sample(train_x_nodes, y)
|
| |
|
| |
|
| | deleted_nodes = set(node['node_id'] for node in train_x_nodes if node not in selected_nodes)
|
| |
|
| |
|
| | new_data = []
|
| | for node in data:
|
| |
|
| | if node['label'] != x or (node['mask'] != 'Train' or node in selected_nodes):
|
| | new_data.append(node)
|
| |
|
| |
|
| | for node in new_data:
|
| | if 'neighbors' in node:
|
| |
|
| | node['neighbors'] = [neighbor for neighbor in node['neighbors'] if neighbor not in deleted_nodes]
|
| |
|
| |
|
| | id_mapping = {}
|
| | new_node_id = 0
|
| |
|
| |
|
| | for node in new_data:
|
| | id_mapping[node['node_id']] = new_node_id
|
| | node['node_id'] = new_node_id
|
| | new_node_id += 1
|
| |
|
| |
|
| | for node in new_data:
|
| | if 'neighbors' in node:
|
| |
|
| | updated_neighbors = []
|
| | for neighbor in node['neighbors']:
|
| | if neighbor in id_mapping:
|
| | updated_neighbors.append(id_mapping[neighbor])
|
| | node['neighbors'] = updated_neighbors
|
| |
|
| |
|
| | output_filename = f"arxiv2023_label_{x}_{y}.json"
|
| | with open(output_filename, 'w', encoding='utf-8') as f:
|
| | json.dump(new_data, f, indent=4)
|
| |
|
| | print(f"Modified data saved to {output_filename}")
|
| |
|