In [1]:
# We learn how to create a graph minor on chemical structure graphs
In [12]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
DEBUG=False
NJOBS=1
BABELDRAW=False
from eden.util import configure_logging
import logging
configure_logging(logging.getLogger(),verbosity=1+DEBUG)
if DEBUG: NJOBS=1
In [3]:
# get data
from eden.converter.graph.gspan import gspan_to_eden
from itertools import islice
def get_graphs(dataset_fname='../toolsdata/bursi.pos.gspan', size=100):
return islice(gspan_to_eden(dataset_fname),size)
In [4]:
from graphlearn.utils import draw
import graphlearn.abstract_graphs.minortransform as transform
import graphlearn.abstract_graphs.minordecompose as decompose
from eden.graph import Vectorizer
from sklearn.cluster import KMeans
import math
#preparing
v=Vectorizer(complexity=3)
''' decomposer default options
make_decomposer = decompose.make_decomposer(
include_base=False,
base_thickness_list=[2])
'''
decomposergen=decompose.make_decomposergen()
class shape_cluster:
def fit(self,li):
pass
def predict(self,i):
return [math.ceil(i)]
pp=transform.GraphMinorTransformer(
#core_shape_cluster=shape_cluster(),#KMeans(n_clusters=3),
core_shape_cluster=KMeans(n_clusters=3),
name_cluster=False,
graph_to_minor=transform.GraphToAbstractTransformer(min_size=3,score_threshold=0,debug=False))
pp.set_param(v)
# the magic happens here
decomposers= [decomposergen(v,x) for x in pp.fit_transform(get_graphs(size=500))]
#lets draw what we did there
for i in range(2):
draw.graphlearn([decomposers[i+5].pre_vectorizer_graph(nested=True),decomposers[i+5].base_graph(),decomposers[i+5].abstract_graph()],
size=13,
contract=True,
abstract_color='red',
vertex_label='label'
)
In [5]:
#parameters
radius_list=[0,2]
thickness_list=[2,4]
base_thickness_list=[2]
#extract
cips=decomposers[0].all_core_interface_pairs(thickness_list=[2],radius_list=[0,1],hash_bitmask=2**20-1)
#draw
print cips
draw.graphlearn(cips[0][0].graph, contract=False)
draw.graphlearn(cips[0][1].graph, contract=False)
In [13]:
%%time
from graphlearn.graphlearn import Sampler as GLS
graphs = get_graphs(size=1000)
sampler=GLS(radius_list=[0,1],
thickness_list=[1],
min_cip_count=2,
min_interface_count=2,
decomposergen=decomposergen,
graphtransformer=transform.GraphMinorTransformer(name_cluster=False))
sampler.fit(graphs,grammar_n_jobs=NJOBS,grammar_batch_size=50)
print 'done'
In [7]:
n_productions=20
draw.draw_grammar(sampler.lsgg.productions,n_productions=n_productions,n_graphs_per_production=5,
n_graphs_per_line=5, size=9, contract=False,
colormap='Paired', invert_colormap=False,node_border=1,
vertex_alpha=0.6, edge_alpha=0.5, node_size=450, abstract_interface=True)
In [8]:
%%time
import graphlearn.utils.draw as draw
import itertools
#parameters
graphs = get_graphs()
id_start=15
id_end=id_start+6
graphs = itertools.islice(graphs,id_start,id_end)
n_steps=20
# sampling with many arguments.
graphs = sampler.sample(graphs,
n_samples=10,
batch_size=1,
n_steps=n_steps,
n_jobs=NJOBS,
quick_skip_orig_cip=True,
probabilistic_core_choice=False,
burnin=0,
improving_threshold=0.5,
select_cip_max_tries=100,
keep_duplicates=True,
include_seed=True)
scores=[]
ids=range(id_start,id_end)
for i,path_graphs in enumerate(graphs):
# for each sampling path:
print 'Graph id: %d'%(ids[i])
#collect scores so that we can display the score graph later
scores.append(sampler.monitors[i].sampling_info['score_history'])
# show graphs
if BABELDRAW:
from graphlearn.utils import openbabel
openbabel.draw(path_graphs,n_graphs_per_line=5)
else:
draw.graphlearn(path_graphs,
n_graphs_per_line=5,
size=10,
colormap='Paired',
invert_colormap=False,
node_border=0.5,
#vertex_color='color_level',
vertex_alpha=0.5,
edge_alpha=0.7,
node_size=450,
edge_label=None)
In [9]:
%matplotlib inline
from itertools import islice
import numpy as np
import matplotlib.pyplot as plt
step=1
num_graphs_per_plot=3
num_plots=np.ceil([len(scores)/num_graphs_per_plot])
for i in range(num_plots):
plt.figure(figsize=(10,5))
for j,score in enumerate(scores[i*num_graphs_per_plot:i*num_graphs_per_plot+num_graphs_per_plot]):
data = list(islice(score,None, None, step))
plt.plot(data, label='graph %d'%(j+i*num_graphs_per_plot+id_start))
plt.legend(loc='lower right')
plt.grid()
plt.ylim(-0.1,1.1)
plt.show()
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: