"""Reading data and printing.""" import json import pandas as pd import networkx as nx from texttable import Texttable from gensim.models.doc2vec import TaggedDocument def tab_printer(args): """ Function to print the logs in a nice tabular format. :param args: Parameters used for the model. """ args = vars(args) keys = sorted(args.keys()) t = Texttable() t.add_rows([["Parameter", "Value"]]) t.add_rows([[k.replace("_", " ").capitalize(), args[k]] for k in keys]) print(t.draw()) def load_graph(graph_path): """ Reading a NetworkX graph. :param graph_path: Path to the edge list. :return graph: NetworkX object. """ data = pd.read_csv(graph_path) edges = data.values.tolist() edges = [[int(edge[0]), int(edge[1])] for edge in edges] graph = nx.from_edgelist(edges) graph.remove_edges_from(nx.selfloop_edges(graph)) return graph def load_features(features_path): """ Reading the features from disk. :param features_path: Location of feature JSON. :return features: Feature hash table. """ features = json.load(open(features_path)) features = {str(k): [str(val) for val in v] for k, v in features.items()} return features def create_documents(features): """ From a feature hash create a list of TaggedDocuments. :param features: Feature hash table - keys are nodes, values are feature lists. :return docs: Tagged Documents list. """ docs = [TaggedDocument(words=v, tags=[str(k)]) for k, v in features.items()] return docs