{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Parallel Plots using JSON files \n", "\n", "
\n", "The goal of this exercise is to take the json files saved by the optimizer in OPAL,\n", "and use pyOPALTools to produce a parallel plot. \n", "
\n", "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append('../')\n", "\n", "import numpy as np\n", "from opal.parser.OptimizerParser import OptimizerParser\n", "\n", "import plotly\n", "import plotly.plotly as py\n", "import plotly.graph_objs as go\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 99 json files.\n" ] } ], "source": [ "files = './json-reader/data/'\n", "\n", "#Find all .json files in a directory\n", "optjson = OptimizerParser(files)\n", "optjson.readGeneration(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load names of design variables and objectives" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Design variables: ['benergy', 'phiinit', 'prinit', 'rinit']\n", "number of design variables = 4\n" ] } ], "source": [ "dvars = optjson.getDesignVariables()\n", "print ( \"Design variables: \", dvars)\n", "print ( \"number of design variables = \", np.size(dvars))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Objectives: ['dpeak1', 'dpeak2', 'dpeak3_5']\n", "number of objectives 3\n" ] } ], "source": [ "objs = optjson.getObjectives()\n", "print ( \"Objectives: \", objs)\n", "print ( \"number of objectives\", np.size(objs))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Find number of generations run by optimizer\n", "This should match number of files found by json reader." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of generations = 99\n" ] } ], "source": [ "n = optjson.getNumOfGenerations()\n", "print('Number of generations = ', n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load all data in first generation\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "optjson.readGeneration(1)\n", "dvarsall = optjson.getAllInput()\n", "objsall = optjson.getAllOutput()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Assign labels, data, and plot paralllel coord\n", "I will try to add a function (in visualize script) to do this without explicitly making Parcoord obj. \n", "\n", "You can adjust data range by changing min and max: range = [1.5,10] \n", "You can adjust tick marks as well: tickvals = [1.5,3,4.5]," ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "pardata = [go.Parcoords(\n", " line = dict(color = 'blue'),\n", " dimensions = list([\n", " dict(#range = [1.5,10],\n", " label = dvars[0], values = dvarsall[:,0]),\n", "\n", " dict(#range = [-30,0],\n", " label = dvars[1], values = dvarsall[:,1]),\n", "\n", " dict(#range = [200,500],\n", " #tickvals = [1.5,3,4.5],\n", " label = dvars[2], values = dvarsall[:,2]),\n", "\n", " dict(#range = [170,260],\n", " #tickvals = [1,2,4,5],\n", " label = objs[0], values = objsall[:,0]),\n", " #ticktext = ['text 1', 'text 2', 'text 3', 'text 4']),\n", "\n", " dict(#range = [-8.0, 8.0],\n", " label = objs[1], values = objsall[:,1]),\n", "\n", " dict(#range = [-8.0,8.0],\n", " label = objs[2], values = objsall[:,2])\n", "\n", " ]))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot parallel coord plot and open in new tab:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'file:///home/matthias/Documents/projects/pyOPALTools/tests/testParallel.html'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotly.offline.plot(pardata, filename = 'testParallel.html')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }