def build_graph(conf_name, year, H, min_topic_lift, min_ngram_lift, exclude=[], force=False, save=True, load=False):
"""
Utility method to build and return the graph model. First we check if a graph file
exists. If not, we check if the builder class is already instantiated. If not, we do
it and proceed to build the graph.
"""
global builder
model_folder = config.IN_MODELS_FOLDER % (config.DATASET, H)
# Creates model folder if non existing
if not os.path.exists(model_folder):
os.makedirs(model_folder)
graph_file = utils.get_graph_file_name(conf_name, model_folder)
if force or (not os.path.exists(graph_file)):
if not builder:
builder = kddcup_model.ModelBuilder()
# Builds the graph file
graph = builder.build(conf_name, year, H, min_topic_lift, min_ngram_lift, exclude)
# Stores gexf copy for caching purposes
if save:
nx.write_gexf(graph, graph_file)
return graph
else:
# A gexf copy already exists in disk. Just load it and return
# print graph_file
try:
graph = nx.read_gexf(graph_file, node_type=int)
except:
print "Problem opening '%s'." % graph_file
sys.exit(1)
return graph
评论列表
文章目录