Basic usage¶
This Jupyter notebook gives an overview of the Python package mevis. The .ipynb file can be found here.
[1]:
import mevis as mv
from opencog.type_constructors import *
Create an AtomSpace¶
[2]:
atomspace1 = mv.create()
n1 = ConceptNode('Vertebrates')
n2 = ConceptNode('Fish')
n3 = ConceptNode('Tetrapods')
n4 = ConceptNode('Amphibians')
n5 = ConceptNode('Reptiles')
n6 = ConceptNode('Mammals')
n7 = ConceptNode('Birds')
n8 = ConceptNode('Marsupialia')
n9 = ConceptNode('Placentalia')
e1 = InheritanceLink(n2, n1)
e2 = InheritanceLink(n3, n1)
e3 = InheritanceLink(n4, n3)
e4 = InheritanceLink(n5, n3)
e5 = InheritanceLink(n6, n3)
e6 = InheritanceLink(n7, n3)
e7 = InheritanceLink(n8, n6)
e8 = InheritanceLink(n9, n6)
e9 = ListLink(n3, n4, n5, n6, n7)
e10 = ListLink(n8, n9)
Load an AtomSpace¶
[3]:
atomspace2 = mv.load('moses.scm')
Store an AtomSpace¶
[4]:
mv.store(atomspace1, 'vertebrates.scm', overwrite=True)
Inspect an AtomSpace¶
[5]:
mv.inspect(atomspace1)
[5]:
{'atoms': 19,
'nodes': 9,
'links': 10,
'types': {'ConceptNode': 9, 'ListLink': 2, 'InheritanceLink': 8}}
[6]:
mv.inspect(atomspace2, count_details=False)
[6]:
{'atoms': 16, 'nodes': 5, 'links': 11}
Convert an AtomSpace to a graph¶
[7]:
graph1 = mv.convert(atomspace1)
[8]:
graph2 = mv.convert(
atomspace2,
node_color='blue',
node_size=lambda atom: 20 if atom.is_link() else 10,
node_shape='hexagon')
Inspect a graph¶
[9]:
mv.inspect(graph1)
[9]:
{'nodes': 19,
'edges': 23,
'node_properties': {'label': 11, 'color': 1, 'shape': 1, 'hover': 19},
'edge_properties': {'color': 1}}
[10]:
mv.inspect(graph2, count_details=False)
[10]:
{'nodes': 16, 'edges': 21}
Export a graph¶
[11]:
mv.export(graph2, 'vertebrates.gml', overwrite=True)
mv.export(graph2, 'vertebrates.gml.gz', overwrite=True)
mv.export(graph2, 'vertebrates.gml.bz2', overwrite=True)
Plot an AtomSpace¶
When plot is the last command in a cell, the figure is automatically displayed as output of the cell.
[12]:
mv.plot(atomspace1, node_click='b')
[12]:
It is also possible to display multiple figures as output of one cell. Other outputs such as text can be put in between.
[13]:
fig1 = mv.plot(atomspace1)
fig2 = mv.plot(atomspace2)
print('AtomSpace 1')
fig1.display(inline=True)
print('AtomSpace 2')
fig2.display(inline=True)
AtomSpace 1
AtomSpace 2
There are three JavaScript backends (d3.js, three.js, vis.js) and various layout algorithms available.
[14]:
mv.plot(atomspace1, 'd3', 'bipartite')
[14]:
[15]:
mv.plot(atomspace2, 'vis', 'dot')
[15]:
[16]:
mv.plot(atomspace2, 'three')
[16]:
Graph annotations can be used to change visual elements of the plot.
[17]:
def calc_node_color(atom):
if atom.type_name == 'AndLink':
return 'blue'
elif atom.type_name == 'OrLink':
return 'green'
elif atom.type_name == 'NotLink':
return 'black'
else:
return 'red'
def calc_node_shape(atom):
if atom.is_node():
return 'rectangle'
elif atom.type_name in ('AndLink', 'OrLink'):
return 'hexagon'
else:
return 'circle'
mv.plot(atomspace2, layout_method='dot', filter_target='$2', filter_context=('in', 3),
node_color=calc_node_color, node_shape=calc_node_shape)
[17]:
Export a plot¶
A plot can be exported as standalone HTML file that contains an interactive visualization, or as JPG/PNG/SVG file that contains a static image captured with Selenium, which is an optional dependency of mevis. Note that only plots with d3 support the SVG format.
[18]:
fig = mv.plot(atomspace2, 'd3', 'dot', zoom_factor=0.5)
fig.export_html('moses.html')
fig.export_jpg('moses.jpg')
fig.export_png('moses.png')
fig.export_svg('moses.svg')