From 4faa4f84cf0cd0e890e01095b2326778f97da54a Mon Sep 17 00:00:00 2001
From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch>
Date: Wed, 5 Jun 2019 08:26:29 +0200
Subject: [PATCH] Renderer (Editor) simplified . ready for testing

---
 flatland/utils/editor.py                      |  24 +++--
 flatland/utils/rendertools.py                 | 100 +-----------------
 .../{Editor2.ipynb => Scene_Editor.ipynb}     |  80 +++-----------
 ...der1.ipynb => Simple_Rendering_Demo.ipynb} |   0
 4 files changed, 27 insertions(+), 177 deletions(-)
 rename notebooks/{Editor2.ipynb => Scene_Editor.ipynb} (66%)
 rename notebooks/{render1.ipynb => Simple_Rendering_Demo.ipynb} (100%)

diff --git a/flatland/utils/editor.py b/flatland/utils/editor.py
index e5e2bc9a..52561369 100644
--- a/flatland/utils/editor.py
+++ b/flatland/utils/editor.py
@@ -6,7 +6,6 @@ import ipywidgets
 import jpy_canvas
 import numpy as np
 from ipywidgets import IntSlider, VBox, HBox, Checkbox, Output, Text, RadioButtons, Tab
-from matplotlib import pyplot as plt
 from numpy import array
 
 import flatland.utils.rendertools as rt
@@ -67,21 +66,16 @@ class View(object):
         self.new_env()
         self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False)
         img = self.oRT.getImage()
-        plt.clf()  # TODO: remove this plt.clf() call
         self.wImage = jpy_canvas.Canvas(img)
         self.yxSize = self.wImage.data.shape[:2]
         self.writableData = np.copy(self.wImage.data)  # writable copy of image - wid_img.data is somehow readonly
         self.wImage.register_move(self.controller.on_mouse_move)
         self.wImage.register_click(self.controller.on_click)
 
-        # TODO: These are currently estimated values
-        # self.yxBase = array([6, 21])  # pixel offset
-        # self.nPixCell = 700 / self.model.env.rail.width  # 35
         self.yxBase = self.oRT.gl.yxBase
         self.nPixCell = self.oRT.gl.nPixCell
 
     def init_widgets(self):
-        # Radiobutton for drawmode - TODO: replace with shift/ctrl/alt keys
         # self.wDrawMode = RadioButtons(options=["Draw", "Erase", "Origin", "Destination"])
         # self.wDrawMode.observe(self.editor.setDrawMode, names="value")
 
@@ -95,6 +89,7 @@ class View(object):
 
         # Checkbox for rendering observations
         self.wShowObs = Checkbox(description="Show Agent Observations")
+        self.wShowObs.observe(self.controller.refresh, names="value")
 
         # This is like a cell widget where loggin goes
         self.wOutput = Output()
@@ -122,13 +117,18 @@ class View(object):
         self.wReplaceAgents = Checkbox(value=True, description="Replace Agents")
 
         self.wTab = Tab()
-        tab_contents = ["Debug", "Regen"]
+        tab_contents = ["Regen", "Observation"]
         for i, title in enumerate(tab_contents):
             self.wTab.set_title(i, title)
+        # self.wTab.children = [
+        #             VBox([self.wRegenSizeWidth, self.wRegenSizeHeight, self.wRegenNAgents,
+        #               self.wRegenMethod, self.wReplaceAgents]),
+        #        VBox([self.wDebug, self.wDebug_move, self.wShowObs]),
+        #   ]
         self.wTab.children = [
-            VBox([self.wDebug, self.wDebug_move, self.wShowObs]),
-            VBox([self.wRegenSizeWidth, self.wRegenSizeHeight, self.wRegenNAgents,
-                  self.wRegenMethod, self.wReplaceAgents])]
+            VBox([self.wRegenSizeWidth, self.wRegenSizeHeight, self.wRegenNAgents]),
+            VBox([self.wShowObs]),
+        ]
 
         # Progress bar intended for stepping in the background (not yet working)
         self.wProg_steps = ipywidgets.IntProgress(value=0, min=0, max=20, step=1, description="Step")
@@ -301,7 +301,6 @@ class Controller(object):
 
         # Process the events in our queue:
         # Draw a black square to indicate a trail
-        # TODO: infer a vector of moves between these squares to avoid gaps
         # Convert the xy position to a cell rc
         # Enqueue transitions across cells in another queue
         if len(qEvents) > 0:
@@ -499,6 +498,9 @@ class EditorModel(object):
             self.debug("lrcStroke ", len(lrcStroke), rcCell)
 
     def mod_path(self, bAddRemove):
+        # disabled functionality (no longer required)
+        if bAddRemove == False:
+            return
         # This elif means we wait until all the mouse events have been processed (black square drawn)
         # before trying to draw rails.  (We could change this behaviour)
         # Equivalent to waiting for mouse button to be lifted (and a mouse event is necessary:
diff --git a/flatland/utils/rendertools.py b/flatland/utils/rendertools.py
index 139d3afc..c73278a9 100644
--- a/flatland/utils/rendertools.py
+++ b/flatland/utils/rendertools.py
@@ -2,101 +2,15 @@ import time
 from collections import deque
 
 # import xarray as xr
-import matplotlib.pyplot as plt
 import numpy as np
 from numpy import array
 from recordtype import recordtype
 
-from flatland.utils.graphics_layer import GraphicsLayer
 from flatland.utils.graphics_pil import PILGL, PILSVG
 
 
 # TODO: suggested renaming to RailEnvRenderTool, as it will only work with RailEnv!
 
-class MPLGL(GraphicsLayer):
-    def __init__(self, width, height, jupyter=False):
-        self.width = width
-        self.height = height
-        self.yxBase = array([6, 21])  # pixel offset
-        self.nPixCell = 700 / width
-        self.img = None
-
-    def open_window(self):
-        plt.figure(figsize=(10, 10))
-
-    def plot(self, *args, **kwargs):
-        plt.plot(*args, **kwargs)
-
-    def scatter(self, *args, **kwargs):
-        plt.scatter(*args, **kwargs)
-
-    def text(self, *args, **kwargs):
-        plt.text(*args, **kwargs)
-
-    def prettify(self, *args, **kwargs):
-        ax = plt.gca()
-        plt.xticks(range(int(ax.get_xlim()[1]) + 1))
-        plt.yticks(range(int(ax.get_ylim()[1]) + 1))
-        plt.grid()
-        plt.xlabel("Euclidean distance")
-        plt.ylabel("Tree / Transition Depth")
-
-    def prettify2(self, width, height, cell_size):
-        plt.xlim([0, width * cell_size])
-        plt.ylim([-height * cell_size, 0])
-
-        gTicks = (np.arange(0, height) + 0.5) * cell_size
-        gLabels = np.arange(0, height)
-        plt.xticks(gTicks, gLabels)
-
-        gTicks = np.arange(-height * cell_size, 0) + cell_size / 2
-        gLabels = np.arange(height - 1, -1, -1)
-        plt.yticks(gTicks, gLabels)
-
-        plt.xlim([0, width * cell_size])
-        plt.ylim([-height * cell_size, 0])
-
-    def show(self, block=False):
-        plt.show(block=block)
-
-    def pause(self, seconds=0.00001):
-        plt.pause(seconds)
-
-    def clf(self):
-        plt.clf()
-        plt.close()
-
-    def get_cmap(self, *args, **kwargs):
-        return plt.get_cmap(*args, **kwargs)
-
-    def beginFrame(self):
-        self.img = None
-        plt.figure(figsize=(10, 10))
-        plt.clf()
-        pass
-
-    def endFrame(self):
-        self.img = self.getImage(force=True)
-        plt.clf()
-        plt.close()
-
-    def getImage(self, force=False):
-        if self.img is None or force:
-            ax = plt.gca()
-            fig = ax.get_figure()
-            fig.tight_layout(pad=0)
-            fig.canvas.draw()
-            data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
-            data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
-            self.img = data
-        return self.img
-
-    def adaptColor(self, color, lighten=False):
-        color = super(self.__class__, self).adaptColor(color, lighten)
-        # MPL has RGBA in [0,1]^4 not \mathbb{N} \cap [0,255]^4
-        color = tuple([iRGBA / 255 for iRGBA in color])
-        return color
-
 
 class RenderTool(object):
     Visit = recordtype("Visit", ["rc", "iDir", "iDepth", "prev"])
@@ -118,15 +32,13 @@ class RenderTool(object):
     gTheta = np.linspace(0, np.pi / 2, 5)
     gArc = array([np.cos(gTheta), np.sin(gTheta)]).T  # from [1,0] to [0,1]
 
-    def __init__(self, env, gl="MPL", jupyter=False):
+    def __init__(self, env, gl="PILSVG", jupyter=False):
         self.env = env
         self.iFrame = 0
         self.time1 = time.time()
         self.lTimes = deque()
 
-        if gl == "MPL":
-            self.gl = MPLGL(env.width, env.height, jupyter)
-        elif gl == "PIL":
+        if gl == "PIL":
             self.gl = PILGL(env.width, env.height, jupyter)
         elif gl == "PILSVG":
             self.gl = PILSVG(env.width, env.height, jupyter)
@@ -630,10 +542,6 @@ class RenderTool(object):
         if type(self.gl) is PILGL:
             self.gl.beginFrame()
 
-        if type(self.gl) is MPLGL:
-            # self.gl.clf()
-            self.gl.beginFrame()
-            pass
 
         # self.gl.clf()
         # if oFigure is None:
@@ -674,10 +582,6 @@ class RenderTool(object):
         # TODO: for MPL, we don't want to call clf (called by endframe)
         # if not show:
 
-        if type(self.gl) is MPLGL:
-            if show:
-                self.gl.show(block=False)
-            # self.gl.endFrame()
 
         if show and type(self.gl) is PILGL:
             self.gl.show()
diff --git a/notebooks/Editor2.ipynb b/notebooks/Scene_Editor.ipynb
similarity index 66%
rename from notebooks/Editor2.ipynb
rename to notebooks/Scene_Editor.ipynb
index fb27f98b..278a12aa 100644
--- a/notebooks/Editor2.ipynb
+++ b/notebooks/Scene_Editor.ipynb
@@ -4,12 +4,12 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "# Rail Editor v0.2"
+    "# Railway Scene Editor "
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 1,
+   "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -19,7 +19,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 2,
+   "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -32,67 +32,27 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": null,
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/html": [
-       "<style>.container { width:95% !important; }</style>"
-      ],
-      "text/plain": [
-       "<IPython.core.display.HTML object>"
-      ]
-     },
-     "metadata": {},
-     "output_type": "display_data"
-    }
-   ],
+   "outputs": [],
    "source": [
     "display(HTML(\"<style>.container { width:95% !important; }</style>\"))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": null,
    "metadata": {},
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "cairo installed: OK\n"
-     ]
-    }
-   ],
+   "outputs": [],
    "source": [
-    "from flatland.utils.editor import EditorMVC, EditorModel, View, Controller"
+    "from flatland.utils.editor import EditorMVC"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": null,
    "metadata": {},
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "<flatland.utils.graphics_pil.PILSVG object at 0x000002D1B5216C18> <class 'flatland.utils.graphics_pil.PILSVG'>\n",
-      "<super: <class 'PILSVG'>, <PILSVG object>> <class 'super'>\n",
-      "Clear rails\n"
-     ]
-    },
-    {
-     "data": {
-      "text/plain": [
-       "<Figure size 432x288 with 0 Axes>"
-      ]
-     },
-     "metadata": {},
-     "output_type": "display_data"
-    }
-   ],
+   "outputs": [],
    "source": [
     "mvc = EditorMVC(sGL=\"PILSVG\" ) "
    ]
@@ -103,7 +63,6 @@
    "source": [
     "## Instructions\n",
     "- Drag to draw (improved dead-ends)\n",
-    "- Shift-Drag to erase rails (erasing dead ends not yet automated - drag right across them)\n",
     "- ctrl-click to add agent or select agent\n",
     "  - if agent is selected:\n",
     "    - ctrl-click to move agent position\n",
@@ -117,26 +76,11 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": null,
    "metadata": {
     "scrolled": false
    },
-   "outputs": [
-    {
-     "data": {
-      "application/vnd.jupyter.widget-view+json": {
-       "model_id": "6c7674fa4b684cf9bbdcc68284df5b49",
-       "version_major": 2,
-       "version_minor": 0
-      },
-      "text/plain": [
-       "HBox(children=(Canvas(), VBox(children=(Text(value='temp.pkl', description='Filename'), Button(description='Re…"
-      ]
-     },
-     "metadata": {},
-     "output_type": "display_data"
-    }
-   ],
+   "outputs": [],
    "source": [
     "mvc.view.display()"
    ]
diff --git a/notebooks/render1.ipynb b/notebooks/Simple_Rendering_Demo.ipynb
similarity index 100%
rename from notebooks/render1.ipynb
rename to notebooks/Simple_Rendering_Demo.ipynb
-- 
GitLab