def load_models(self):
now = datetime.now()
self.rec_candidates = []
if self.last_model_update is None or (now - self.last_model_update).days >= 1:
print('loading model', now)
model_path = '{0}/it-topics'.format(settings.PORTRAIT_FOLDER)
lda_filename = os.readlink('{0}/current_lda_model.gensim'.format(model_path))
self.lda_model = gensim.models.ldamulticore.LdaMulticore.load(lda_filename)
self.topic_graph = nx.read_gpickle('{0}/current_topic_graph.nx'.format(model_path))
with gzip.open('{0}/current_candidates.json.gz'.format(model_path), 'rt') as f:
self.rec_candidates = json.load(f)
print('loaded', len(self.rec_candidates), 'candidates')
self.last_model_update = datetime.now()
python类read_gpickle()的实例源码
def graph2png(infile, outdir, fname=None):
'''
infile: input .gpickle or .graphml file
outdir: path to directory to store output png files
'''
# if file is .gpickle, otherwise load .graphml
try:
graph = nx.read_gpickle(infile)
except:
graph = nx.read_graphml(infile)
# get numpy array equivalent of adjacency matrix
g = nx.adj_matrix(graph).todense()
fig = plt.figure(figsize=(7, 7))
# plot adjacency matrix
p = plt.imshow(g, interpolation='None', cmap='jet')
if fname is None:
fname = os.path.split(infile)[1].split('.')[0] + '.png'
save_location = outdir + fname
plt.savefig(save_location, format='png')
print(fname + ' done!')
def loadGraphs(filenames, verb=False):
"""
Given a list of files, returns a dictionary of graphs
Required parameters:
filenames:
- List of filenames for graphs
Optional parameters:
verb:
- Toggles verbose output statements
"""
# Initializes empty dictionary
if type(filenames) is not list:
filenames = [filenames]
gstruct = OrderedDict()
for idx, files in enumerate(filenames):
if verb:
print("Loading: " + files)
# Adds graphs to dictionary with key being filename
fname = os.path.basename(files)
try:
gstruct[fname] = nx.read_graphml(files)
except:
gstruct[fname] = nx.read_gpickle(files)
return gstruct
def load_saved_pickle(cls, graph_path):
"""Loads a graph saved as pickle
Parameters
----------
graph_path: The path of the graph that should be loaded
Returns
-------
NxGraph: Graph object
Examples
--------
>>> g.load_saved_pickle("graph.bz2")
"""
return cls(graph_obj=nx.read_gpickle(graph_path))
def call_exps(params, data_set):
print('Dataset: %s' % data_set)
model_hyp = json.load(
open('gem/experiments/config/%s.conf' % data_set, 'r')
)
if bool(params["node_labels"]):
node_labels = cPickle.load(
open('gem/data/%s/node_labels.pickle' % data_set, 'rb')
)
else:
node_labels = None
di_graph = nx.read_gpickle('gem/data/%s/graph.gpickle' % data_set)
for d, meth in itertools.product(params["dimensions"], params["methods"]):
dim = int(d)
MethClass = getattr(
importlib.import_module("gem.embedding.%s" % meth),
methClassMap[meth]
)
hyp = {"d": dim}
hyp.update(model_hyp[meth])
MethObj = MethClass(hyp)
run_exps(MethObj, di_graph, data_set, node_labels, params)
def from_pickle(path, check_version=True):
"""Reads a graph from a gpickle file.
:param file or str path: File or filename to read. Filenames ending in .gz or .bz2 will be uncompressed.
:param bool check_version: Checks if the graph was produced by this version of PyBEL
:return: A BEL graph
:rtype: BELGraph
"""
graph = read_gpickle(path)
raise_for_not_bel(graph)
if check_version:
raise_for_old_graph(graph)
return graph
def read_graph(self, infile):
self.graph = nx.read_gpickle(infile)
def compare_graph_outputs(generated_output, stored_output_file_name):
expected_output = nx.read_gpickle(expected_output_directory+stored_output_file_name)
if(nx.is_isomorphic(generated_output, expected_output)):
return True
return False
def loadSBMGraph(file_prefix):
graph_file = file_prefix + '_graph.gpickle'
G = nx.read_gpickle(graph_file)
node_file = file_prefix + '_node.pkl'
with open(node_file, 'rb') as fp:
node_community = pickle.load(fp)
return (G, node_community)
def loadRealGraphSeries(file_prefix, startId, endId):
graphs = []
for file_id in range(startId, endId + 1):
graph_file = file_prefix + str(file_id) + '_graph.gpickle'
graphs.append(nx.read_gpickle(graph_file))
return graphs
def loadDynamicSBmGraph(file_perfix, length):
graph_files = ['%s_%d_graph.gpickle' % (file_perfix, i) for i in xrange(length)]
info_files = ['%s_%d_node.pkl' % (file_perfix, i) for i in xrange(length)]
graphs = [nx.read_gpickle(graph_file) for graph_file in graph_files]
nodes_comunities = []
perturbations = []
for info_file in info_files:
with open(info_file, 'rb') as fp:
node_infos = pickle.load(fp)
nodes_comunities.append(node_infos['community'])
perturbations.append(node_infos['perturbation'])
return zip(graphs, nodes_comunities, perturbations)
def cache_read(self, cache_folder):
self.contentProvider = pickle.load(
open(cache_folder + '/contentProvider.cache', 'rb')
)
self.contentNodes = pickle.load(
open(cache_folder + '/contentNodes.cache', 'rb')
)
self.accessNodes = pickle.load(
open(cache_folder + '/accessNodes.cache', 'rb')
)
self.netGraph = nx.read_gpickle(cache_folder + '/asGraph.cache')
self.as2ip = pickle.load(
open(cache_folder + '/as2ip.cache', 'rb')
)
return None
def load_verb_res():
''' Load verb semantics related resources. '''
global G, w2v_model, w2v_model_gf
sys.stderr.write('Loading graph...')
G=nx.read_gpickle('../../sdewac_graph/verbs_and_args_no_subcat.gpickle')
sys.stderr.write(' done.\nLoading word2vec models...')
w2v_model_gf=gensim.models.Word2Vec.load('../../word2vec/vectors_sdewac_gf_skipgram_min50_new.gensim')
sys.stderr.write(' done.\n')