Skip to content
Snippets Groups Projects
Commit 56221a75 authored by toyota's avatar toyota
Browse files

add splitting nodes for node classification

parent b8b80e2f
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Utaemon Toyota
@project: Software Projekt
@date: 25.12.2018
"""
import random
import cora
def get_graph(path_nodes, path_edges):
return cora.get_graph(path_nodes, path_edges)
def dict_of_node_classes(path_nodes, path_edges):
cora = get_graph(path_nodes, path_edges)
class_dict = {}
for node in cora.node:
node_class = cora.node[node]["class"]
if not node_class in class_dict:
class_dict[node_class] = set()
class_dict[node_class].add(node)
return class_dict
def get_random_num_nodes(set_elm, num):
return set(random.sample(set_elm, num))
def get_num_random_nodes_for_all_classes(num = 3, path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites"):
"""get specific number of nodes per class, same number for all classes"""
cora_dict = dict_of_node_classes(path_nodes, path_edges)
sampled_random_id_set = set()
for key in cora_dict:
for id in get_random_num_nodes(cora_dict[key], num):
sampled_random_id_set.add(id)
# if you want to return dict of class:random_ids
# cora_dict = dict_of_node_classes(path_nodes, path_edges)
# sampled_random_dict = {}
# for key in cora_dict:
# sampled_random_dict[key] = get_random_num_nodes(cora_dict[key], num)
# return sampled_random_dict
return sampled_random_id_set
#get random nodes
def get_num_of_random_nodes(num = 3, path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites"):
"""Get random nodes."""
cora_nodes = set(get_graph(path_nodes, path_edges).nodes)
return set(random.sample(cora_nodes, num))
#get a dictionary with class: ids
def get_dict_of_nodes(*args):
"""*args: Tuple of Set_name and number of nodes in it, for example ("a", 10)."""
dict_of_sets = {}
for arg in args:
ids = get_num_of_random_nodes(arg[1])
dict_of_sets[arg[0]] = ids
return dict_of_sets
#get features from nodes
def get_features_for_nodes(path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites"):
cora_graph = get_graph(path_nodes, path_edges)
set_of_cora_nodes = get_num_of_random_nodes()
dict_of_id_and_features = {}
for id in set_of_cora_nodes:
dict_of_id_and_features[id] = cora_graph.node[id]
return dict_of_id_and_features
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment