Skip to content
Snippets Groups Projects
Commit 92eae8f3 authored by toyota's avatar toyota
Browse files

little bugfixes: id to int, uniform to uniform-random instead of normal

parent aa4d80c4
No related branches found
No related tags found
No related merge requests found
......@@ -8,9 +8,9 @@ Arrays are initialized in normal or uniform random format (default = normal).
get_graph(path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites")
-> return graph with nodes and edges
To write the graph informations in file:
def write_graph_to_file(path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites", path_output_graph = "/home/utaemon/SP/graph.txt")
def write_graph_to_file(path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites", path_output_graph = "/home/utaemon/SP/")
To write the dictionary with initalizing Embeddings in file:
def write_dict_to_file(quantity=1433, path_output_emb = "/home/utaemon/SP/init.txt")
def write_dict_to_file(rand_type="normal_random", dimension = 128, quantity=1433, path_output_emb = "/home/utaemon/SP/")
"""
import networkx as nx
......@@ -29,9 +29,9 @@ def read_file_and_get_nodes(graph_name, path="/home/utaemon/SP/cora/cora.content
paper_bow = np.where(all_bow_of_paper == "1") #get indices which words occur
#add infos to Graph
graph_name.add_node(paper_id)
graph_name.node[paper_id]["class"] = paper_class
graph_name.node[paper_id]["bow"] = paper_bow
graph_name.add_node(int(paper_id))
graph_name.node[int(paper_id)]["class"] = paper_class
graph_name.node[int(paper_id)]["bow"] = paper_bow
def read_file_and_get_edges(graph_name, path="/home/utaemon/SP/cora/cora.cites"):
with open(path) as file:
......@@ -45,8 +45,8 @@ def get_graph(path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home
read_file_and_get_edges(Cora_graph, path_edges)
return Cora_graph
def get_init_emb(quantity=1433):
return initialize.get_embeddings(quantity=quantity)
def get_init_emb(rand_type="normal_random", dimension = 128, quantity=1433):
return initialize.get_embeddings(rand_type=rand_type, dimension = dimension, quantity=quantity)
#This class is from: https://stackoverflow.com/a/47626762
class NumpyEncoder(json.JSONEncoder):
......@@ -55,22 +55,19 @@ class NumpyEncoder(json.JSONEncoder):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
def write_graph_to_file(path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites", path_output_graph = "/home/utaemon/SP/graph.txt"):
def write_graph_to_file(path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites", path_output_graph = "/home/utaemon/SP/"):
"""
JSON kann kein numpy-array verarbeiten. Lösung: zuvor zu einer List umformen und später zurück.
"""
g = get_graph(path_nodes, path_edges)
with open(path_output_graph, "w") as output1:
with open(path_output_graph + "graph.txt", "w") as output1:
output1.write(json.dumps(json_graph.node_link_data(g), cls=NumpyEncoder)) #care, numpy arrays are converted to lists!
def write_dict_to_file(quantity=1433, path_output_emb = "/home/utaemon/SP/init.txt"):
with open(path_output_emb, "w") as output2:
output2.write(str(get_init_emb(quantity)))
write_dict_to_file()
#write_graph_to_file()
def write_dict_to_file(rand_type="normal_random", dimension = 128, quantity=1433, path_output_emb = "/home/utaemon/SP/"):
with open(path_output_emb + rand_type +"_" + "init.txt", "w") as output2:
output2.write(str(get_init_emb(rand_type=rand_type, dimension=dimension, quantity=quantity)))
if __name__ == "__main__":
# execute only if run as a script
get_graph(path_nodes="/home/utaemon/SP/cora/cora.content", path_edges="/home/utaemon/SP/cora/cora.cites")
get_init_emb(quantity=1433)
\ No newline at end of file
get_init_emb(rand_type="normal_random", dimension = 128, quantity=1433)
\ No newline at end of file
This diff is collapsed.
......@@ -31,7 +31,5 @@ def get_embeddings(rand_type="normal_random", dimension = 128, quantity=1):
emb_dict[i] = Embeddings.normal_random(dimension = dimension, seed = i)
elif rand_type == "uniform_random":
for i in range(0, quantity):
emb_dict[i] = Embeddings.normal_random(dimension = dimension, seed = i)
return emb_dict
#print (get_embeddings())
\ No newline at end of file
emb_dict[i] = Embeddings.uniform_random(dimension = dimension, seed = i)
return emb_dict
\ No newline at end of file
This diff is collapsed.
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