From 708f3adbcc7e4609525beb584cb9a2f3055a65ee Mon Sep 17 00:00:00 2001
From: hagrid67 <jdhwatson@gmail.com>
Date: Wed, 1 May 2019 13:05:30 +0100
Subject: [PATCH] added separate debug mouse move checkbox added on_click debug
 message containing transition info at the clicked cell

---
 flatland/utils/editor.py     |  16 ++++-
 notebooks/CanvasEditor.ipynb | 127 +++++++++++++++++++++--------------
 2 files changed, 90 insertions(+), 53 deletions(-)

diff --git a/flatland/utils/editor.py b/flatland/utils/editor.py
index 88b6d7a..9c2afe7 100644
--- a/flatland/utils/editor.py
+++ b/flatland/utils/editor.py
@@ -49,6 +49,7 @@ class JupEditor(object):
         self.gRCTrans = array([[-1, 0], [0, 1], [1, 0], [0, -1]])  # NESW in RC
 
         self.debug = False
+        self.debug_move = False
         self.wid_output = None
         self.drawMode = "Draw"
         self.env_filename = "temp.npy"
@@ -65,7 +66,11 @@ class JupEditor(object):
 
     def setDebug(self, dEvent):
         self.debug = dEvent["new"]
-        self.log("Debug:", self.debug)
+        self.log("Set Debug:", self.debug)
+
+    def setDebugMove(self, dEvent):
+        self.debug_move = dEvent["new"]
+        self.log("Set DebugMove:", self.debug)
 
     def setOutput(self, wid_output):
         self.wid_output = wid_output
@@ -89,6 +94,13 @@ class JupEditor(object):
         
         # self.log("agent", self.drawMode, self.iAgent, rcCell)
 
+        if self.debug:
+            self.log("debug:", event)
+            binTrans = self.env.rail.get_transitions(rcCell)
+            sbinTrans = format(binTrans, "#018b")[2:]
+            self.log("cell ", rcCell, "Transitions: ", binTrans, sbinTrans,
+                [sbinTrans[i:i+4] for i in range(0, len(sbinTrans), 4)])
+
         self.redraw()
 
     def event_handler(self, wid, event):
@@ -102,7 +114,7 @@ class JupEditor(object):
         bRedrawn = False
         writableData = None
 
-        if self.debug:
+        if self.debug and (event["buttons"] > 0 or self.debug_move):
             self.log("debug:", len(qEvents), len(rcHistory), event)
 
         assert wid == self.wid_img, "wid not same as wid_img"
diff --git a/notebooks/CanvasEditor.ipynb b/notebooks/CanvasEditor.ipynb
index 144ce7d..ea9bcf0 100644
--- a/notebooks/CanvasEditor.ipynb
+++ b/notebooks/CanvasEditor.ipynb
@@ -156,6 +156,40 @@
     "oEnv.width"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "['/home/jeremy/projects/heating',\n",
+       " '/home/jeremy/projects/aicrowd/rl-trains/MAgent/python',\n",
+       " '/home/jeremy/ve367/lib/python36.zip',\n",
+       " '/home/jeremy/ve367/lib/python3.6',\n",
+       " '/home/jeremy/ve367/lib/python3.6/lib-dynload',\n",
+       " '/usr/lib/python3.6',\n",
+       " '',\n",
+       " '/home/jeremy/ve367/lib/python3.6/site-packages',\n",
+       " '/home/jeremy/projects/aicrowd/rl-trains/torch-rl/torch_rl',\n",
+       " '/home/jeremy/projects/aicrowd/rl-trains/gym-minigrid',\n",
+       " '/home/jeremy/projects/aicrowd/rl-trains/flatland',\n",
+       " '/home/jeremy/projects/aicrowd/rl-trains/Jupyter_Canvas_Widget',\n",
+       " '/home/jeremy/ve367/lib/python3.6/site-packages/IPython/extensions',\n",
+       " '/home/jeremy/.ipython']"
+      ]
+     },
+     "execution_count": 10,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "import sys\n",
+    "sys.path"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
@@ -165,7 +199,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
+   "execution_count": 11,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -182,7 +216,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 11,
+   "execution_count": 12,
    "metadata": {},
    "outputs": [
     {
@@ -222,7 +256,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 12,
+   "execution_count": 13,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -241,7 +275,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": 14,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -249,6 +283,9 @@
     "wid_drawMode.observe(oEditor.setDrawMode, names=\"value\")\n",
     "wid_debug = ipywidgets.Checkbox(description = \"Debug\")\n",
     "wid_debug.observe(oEditor.setDebug, names=\"value\")\n",
+    "wid_debug_move = ipywidgets.Checkbox(description = \"Debug mouse move\")\n",
+    "wid_debug_move.observe(oEditor.setDebugMove, names=\"value\")\n",
+    "\n",
     "wid_output = ipywidgets.Output()\n",
     "oEditor.setOutput(wid_output)\n",
     "wid_filename = ipywidgets.Text(description = \"Filename\")\n",
@@ -281,7 +318,7 @@
     "    \n",
     "\n",
     "#wid_debug = interact(oEditor.setDebug, debug=False)\n",
-    "vbox_controls = VBox([wid_filename, wid_drawMode, *lwid_buttons, wid_size, wid_debug])\n"
+    "vbox_controls = VBox([wid_filename, wid_drawMode, *lwid_buttons, wid_size, wid_debug, wid_debug_move])\n"
    ]
   },
   {
@@ -294,7 +331,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 15,
    "metadata": {
     "scrolled": false
    },
@@ -302,7 +339,7 @@
     {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "0c9b294c3a3d492fa0035935bda407ed",
+       "model_id": "cddcd8e9e5fb48c185a129ff73b1ef40",
        "version_major": 2,
        "version_minor": 0
       },
@@ -312,18 +349,6 @@
      },
      "metadata": {},
      "output_type": "display_data"
-    },
-    {
-     "ename": "IndexError",
-     "evalue": "list assignment index out of range",
-     "output_type": "error",
-     "traceback": [
-      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
-      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
-      "\u001b[0;32m~/projects/aicrowd/rl-trains/flatland/flatland/utils/editor.py\u001b[0m in \u001b[0;36mon_click\u001b[0;34m(self, wid, event)\u001b[0m\n\u001b[1;32m     80\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     81\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdrawMode\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"Origin\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 82\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miAgent\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_agent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrcCell\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrcCell\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     83\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdrawMode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Destination\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     84\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplayer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m  \u001b[0;31m# will need to start a new player\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
-      "\u001b[0;32m~/projects/aicrowd/rl-trains/flatland/flatland/envs/rail_env.py\u001b[0m in \u001b[0;36madd_agent\u001b[0;34m(self, rcPos, rcTarget, iDir)\u001b[0m\n\u001b[1;32m    536\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0miDir\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    537\u001b[0m             \u001b[0miDir\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpick_agent_direction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrcPos\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrcTarget\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 538\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0magents_direction\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0miAgent\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miDir\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    539\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    540\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0magents_direction\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
-      "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
-     ]
     }
    ],
    "source": [
@@ -335,27 +360,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "0"
-      ]
-     },
-     "execution_count": 15,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "oEditor.env.number_of_agents"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": 32,
    "metadata": {
     "scrolled": true
    },
@@ -363,12 +368,12 @@
     {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "d187e6c9f63d43b0a0f6cbc1aae0fc92",
+       "model_id": "b4d521d96fc44ea6bf7cd9e4e299673b",
        "version_major": 2,
        "version_minor": 0
       },
       "text/plain": [
-       "Output()"
+       "Output(outputs=({'output_type': 'stream', 'text': \"Set Debug: True\\ndebug: {'type': 'click', 'shiftKey': False…"
       ]
      },
      "metadata": {},
@@ -376,9 +381,18 @@
     }
    ],
    "source": [
+    "wid_output.clear_output()\n",
     "wid_output"
    ]
   },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Experimental\n",
+    "enable the thread.start below to try background stepping of the agents.  It tends to make a mess."
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": 17,
@@ -389,8 +403,7 @@
     "def bgUpdate(editor):\n",
     "    for i in range(100):\n",
     "        editor.step_event()\n",
-    "        time.sleep(0.2)\n",
-    "        \n"
+    "        time.sleep(0.2)\n"
    ]
   },
   {
@@ -408,7 +421,8 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "#thread.start()"
+    "if False:\n",
+    "    thread.start()"
    ]
   },
   {
@@ -424,7 +438,8 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "oEnv.rail.save_transition_map(\"../flatland/env-data/tests/test-editor.npy\")"
+    "if False: \n",
+    "    oEnv.rail.save_transition_map(\"../flatland/env-data/tests/test-editor.npy\")"
    ]
   },
   {
@@ -477,7 +492,7 @@
     {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "03564681edac4bc6a937b5f3fc4dc8b6",
+       "model_id": "adf698700a634775bfe4da0909912b96",
        "version_major": 2,
        "version_minor": 0
       },
@@ -545,7 +560,7 @@
     {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "cecbaa2e04974ed4bfe10f9e10b8e4ad",
+       "model_id": "e6bf851f84bd471f8f4a4771e9d9b668",
        "version_major": 2,
        "version_minor": 0
       },
@@ -578,7 +593,7 @@
     {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "a950ac7c3b084a2eb565b68c549eb560",
+       "model_id": "4de1e7a8162e460f8b1ecc732d3c1e2e",
        "version_major": 2,
        "version_minor": 0
       },
@@ -616,7 +631,7 @@
     {
      "data": {
       "text/plain": [
-       "False"
+       "['1000', '0000', '0010', '0000']"
       ]
      },
      "execution_count": 31,
@@ -625,7 +640,17 @@
     }
    ],
    "source": [
-    "[2,3] == (2,3)"
+    "binTrans = format(32800, \"#016b\")\n",
+    "[binTrans[i+2:i+6] for i in range(0, len(binTrans)-2, 4)]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "\n"
    ]
   }
  ],
-- 
GitLab