{ "cells": [ { "cell_type": "markdown", "id": "cc191d89", "metadata": {}, "source": [ "# Basic usage\n", "\n", "This Jupyter notebook gives an overview of the Python package [mevis](https://pypi.org/project/mevis). The .ipynb file can be found [here](https://github.com/robert-haas/mevis/tree/master/examples)." ] }, { "cell_type": "code", "execution_count": null, "id": "51a0be7d", "metadata": {}, "outputs": [], "source": [ "import mevis as mv\n", "from opencog.type_constructors import *" ] }, { "cell_type": "markdown", "id": "b4762356", "metadata": {}, "source": [ "## Create an AtomSpace" ] }, { "cell_type": "code", "execution_count": null, "id": "28f78c1e", "metadata": {}, "outputs": [], "source": [ "atomspace1 = mv.create()\n", "\n", "n1 = ConceptNode('Vertebrates')\n", "n2 = ConceptNode('Fish')\n", "n3 = ConceptNode('Tetrapods')\n", "n4 = ConceptNode('Amphibians')\n", "n5 = ConceptNode('Reptiles')\n", "n6 = ConceptNode('Mammals')\n", "n7 = ConceptNode('Birds')\n", "n8 = ConceptNode('Marsupialia')\n", "n9 = ConceptNode('Placentalia')\n", "\n", "e1 = InheritanceLink(n2, n1)\n", "e2 = InheritanceLink(n3, n1)\n", "e3 = InheritanceLink(n4, n3)\n", "e4 = InheritanceLink(n5, n3)\n", "e5 = InheritanceLink(n6, n3)\n", "e6 = InheritanceLink(n7, n3)\n", "e7 = InheritanceLink(n8, n6)\n", "e8 = InheritanceLink(n9, n6)\n", "e9 = ListLink(n3, n4, n5, n6, n7)\n", "e10 = ListLink(n8, n9)" ] }, { "cell_type": "markdown", "id": "65620e8e", "metadata": {}, "source": [ "## Load an AtomSpace" ] }, { "cell_type": "code", "execution_count": null, "id": "0d5896c7", "metadata": {}, "outputs": [], "source": [ "atomspace2 = mv.load('moses.scm')" ] }, { "cell_type": "markdown", "id": "be880115", "metadata": {}, "source": [ "## Store an AtomSpace" ] }, { "cell_type": "code", "execution_count": null, "id": "43d2f205", "metadata": {}, "outputs": [], "source": [ "mv.store(atomspace1, 'vertebrates.scm', overwrite=True)" ] }, { "cell_type": "markdown", "id": "c5dc5ffa", "metadata": {}, "source": [ "## Inspect an AtomSpace" ] }, { "cell_type": "code", "execution_count": null, "id": "cdf9707f", "metadata": {}, "outputs": [], "source": [ "mv.inspect(atomspace1)" ] }, { "cell_type": "code", "execution_count": null, "id": "1dfe3b6b", "metadata": {}, "outputs": [], "source": [ "mv.inspect(atomspace2, count_details=False)" ] }, { "cell_type": "markdown", "id": "2c1b6367", "metadata": {}, "source": [ "## Convert an AtomSpace to a graph" ] }, { "cell_type": "code", "execution_count": null, "id": "7f1806c3", "metadata": {}, "outputs": [], "source": [ "graph1 = mv.convert(atomspace1)" ] }, { "cell_type": "code", "execution_count": null, "id": "c10306c4", "metadata": {}, "outputs": [], "source": [ "graph2 = mv.convert(\n", " atomspace2,\n", " node_color='blue',\n", " node_size=lambda atom: 20 if atom.is_link() else 10,\n", " node_shape='hexagon')" ] }, { "cell_type": "markdown", "id": "a7247f2c", "metadata": {}, "source": [ "## Inspect a graph" ] }, { "cell_type": "code", "execution_count": null, "id": "c49d14a9", "metadata": {}, "outputs": [], "source": [ "mv.inspect(graph1)" ] }, { "cell_type": "code", "execution_count": null, "id": "fdc7f777", "metadata": {}, "outputs": [], "source": [ "mv.inspect(graph2, count_details=False)" ] }, { "cell_type": "markdown", "id": "7901cabe", "metadata": {}, "source": [ "## Export a graph" ] }, { "cell_type": "code", "execution_count": null, "id": "90010899", "metadata": {}, "outputs": [], "source": [ "mv.export(graph2, 'vertebrates.gml', overwrite=True)\n", "mv.export(graph2, 'vertebrates.gml.gz', overwrite=True)\n", "mv.export(graph2, 'vertebrates.gml.bz2', overwrite=True)" ] }, { "cell_type": "markdown", "id": "2639378b", "metadata": {}, "source": [ "## Plot an AtomSpace\n", "\n", "When plot is the last command in a cell, the figure is automatically displayed as output of the cell." ] }, { "cell_type": "code", "execution_count": null, "id": "9e0f8ad2", "metadata": {}, "outputs": [], "source": [ "mv.plot(atomspace1, node_click='b')" ] }, { "cell_type": "markdown", "id": "4325dc6a", "metadata": {}, "source": [ "It is also possible to display multiple figures as output of one cell. Other outputs such as text can be put in between." ] }, { "cell_type": "code", "execution_count": null, "id": "7a7181a1", "metadata": {}, "outputs": [], "source": [ "fig1 = mv.plot(atomspace1)\n", "fig2 = mv.plot(atomspace2)\n", "\n", "print('AtomSpace 1')\n", "fig1.display(inline=True)\n", "\n", "print('AtomSpace 2')\n", "fig2.display(inline=True)" ] }, { "cell_type": "markdown", "id": "ad4b2d42", "metadata": {}, "source": [ "There are three JavaScript backends (d3.js, three.js, vis.js) and various layout algorithms available." ] }, { "cell_type": "code", "execution_count": null, "id": "50317a8c", "metadata": {}, "outputs": [], "source": [ "mv.plot(atomspace1, 'd3', 'bipartite')" ] }, { "cell_type": "code", "execution_count": null, "id": "85f80937", "metadata": {}, "outputs": [], "source": [ "mv.plot(atomspace2, 'vis', 'dot')" ] }, { "cell_type": "code", "execution_count": null, "id": "4c0247e7", "metadata": {}, "outputs": [], "source": [ "mv.plot(atomspace2, 'three')" ] }, { "cell_type": "markdown", "id": "d8ccab53", "metadata": {}, "source": [ "Graph annotations can be used to change visual elements of the plot." ] }, { "cell_type": "code", "execution_count": null, "id": "f52fd902", "metadata": {}, "outputs": [], "source": [ "def calc_node_color(atom):\n", " if atom.type_name == 'AndLink':\n", " return 'blue'\n", " elif atom.type_name == 'OrLink':\n", " return 'green'\n", " elif atom.type_name == 'NotLink':\n", " return 'black'\n", " else:\n", " return 'red'\n", "\n", "def calc_node_shape(atom):\n", " if atom.is_node():\n", " return 'rectangle'\n", " elif atom.type_name in ('AndLink', 'OrLink'):\n", " return 'hexagon'\n", " else:\n", " return 'circle'\n", "\n", "mv.plot(atomspace2, layout_method='dot', filter_target='$2', filter_context=('in', 3),\n", " node_color=calc_node_color, node_shape=calc_node_shape)" ] }, { "cell_type": "markdown", "id": "275ca6e5", "metadata": {}, "source": [ "## Export a plot\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "id": "518a9929", "metadata": {}, "outputs": [], "source": [ "fig = mv.plot(atomspace2, 'd3', 'dot', zoom_factor=0.5)\n", "\n", "fig.export_html('moses.html')\n", "\n", "fig.export_jpg('moses.jpg')\n", "fig.export_png('moses.png')\n", "fig.export_svg('moses.svg')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }