{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualising Dialectical Structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`theodias.util` offers some basic helper functions to export dialectical structures as tex or dot file. These can then be used to further transform them into other formats as is shown here.\n", "\n", "*Source:* You can download this notebook from [here](https://github.com/re-models/theodias/blob/master/docs-source/tutorials/visualisation.ipynb). To execute this notebook, you need the used [dot2tex-template](https://github.com/re-models/theodias/blob/master/docs-source/tutorials/dot2tex_sw-template.tex) and you have to installed all other required software (see below). " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# imports\n", "from theodias.util import create_random_arguments, write_as_tex, write_as_dot\n", "from IPython.display import SVG\n", "from subprocess import call, run\n", "from os import environ, getcwd, chdir, remove, path\n", "import sys\n", "# some helper functions\n", "def execute_command(command, program_name):\n", " #print(command)\n", " try:\n", " retcode = call(command, shell=True)\n", " if retcode != 0:\n", " print(program_name + \" terminated with signal\", retcode, file=sys.stderr)\n", " except OSError as e:\n", " print(program_name + \" execution failed:\", e, file=sys.stderr)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the dot format to create automatically layouted graphs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By using the dot-format we can use dot-based programs to render the dot-file in other file-formats (e.g. svg, pdf or what have you). The following function creates an svg-file by using other programs (the function relies on the programs [dot2tex](https://dot2tex.readthedocs.io/en/latest/), [pdflatex](https://ctan.org/pkg/pdftex) and [pdf2svg](https://github.com/dawbarton/pdf2svg) and their dependencies - see e.g. [here](https://dot2tex.readthedocs.io/en/latest/installation_guide.html#dependencies))." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def dot2svg(dot_file_name, directory = None, keep_temporary_files = False, dot2tex_template = 'dot2tex_sw-template.tex'):\n", " \"\"\" Creating an svg file from the given dot file.\n", " \n", " This function will create a svg file of the arguments. It will additionally create a temporary tex and a pdf file\n", " which will be deleted if :code:`keep_temporary_files` is set to :code:`false`. The function relies one the programs \n", " `dot2tex`, `pdflatex` and `pdf2svg` to be installed. (The might be more specific latex dependencies - e.g. tikz - which\n", " can be find in the used :code:`dot2tex_template`.)\n", "\n", " \"\"\"\n", " if directory is None:\n", " directory = getcwd()\n", " \n", " current_dir = getcwd()\n", " # changing working directory that files generated are being put there\n", " chdir(directory)\n", " file_name = dot_file_name.split('.')[0]\n", " \n", " # dot2tex\n", " command = \"dot2tex -ftikz --crop --margin 5pt -tmath --autosize --template \"+ dot2tex_template + \" \" + dot_file_name + \" -o \" + file_name + \".tex\"\n", " execute_command(command, \"dot2tex\")\n", "\n", " # tex2pdf\n", " command = \"pdflatex --shell-escape \" + file_name + \".tex\" + \" -o \" + file_name + \".pdf\"\n", " execute_command(command, \"pdflatex\")\n", "\n", " # pdf2svg\n", " command = \"pdf2svg \" + file_name + \".pdf\" + \" \" + file_name + \".svg\"\n", " execute_command(command, \"pdf2svg\")\n", " \n", " # ToDo: remove other temp file (aux, log?)\n", " if keep_temporary_files is False:\n", " if path.exists(file_name + \".pdf\"):\n", " remove(file_name + \".pdf\")\n", " if path.exists(file_name + \".tex\"):\n", " remove(file_name + \".tex\")\n", " \n", " chdir(current_dir)\n", " return path.join(directory,file_name + '.svg')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "args = create_random_arguments(5, 3, 3, variation=True, connected=True)\n", "\n", "file_name = \"nice-graph.dot\"\n", "\n", "write_as_dot(getcwd(), file_name, args)\n", "# if we set keep_temporary_files generated pdf and tex files will be kept.\n", "svg_file_name = dot2svg(dot_file_name = file_name, directory = getcwd(), keep_temporary_files = True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of just exporting the graph into an svg file you can show the graph right here by using [IPython's Rich Display System](https://nbviewer.ipython.org/github/ipython/ipython/blob/2.x/examples/Notebook/Display%20System.ipynb)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def show_arguments(args, file_name_without_suffix = \"img_tmp\", directory = getcwd()):\n", " dot_file_name = file_name_without_suffix + \".dot\"\n", " \n", " write_as_dot(directory, dot_file_name, args)\n", " svg_file_name = dot2svg(dot_file_name, directory)\n", " return SVG(filename = svg_file_name)\n", "\n", "args = create_random_arguments(5, 3, 3, variation=True, connected=True)\n", "show_arguments(args, directory = getcwd())" ] } ], "metadata": { "kernelspec": { "display_name": "py38-kernel", "language": "python", "name": "py38-kernel" }, "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.8" } }, "nbformat": 4, "nbformat_minor": 4 }