Skip to content
Snippets Groups Projects
Commit d36d70c4 authored by hagrid67's avatar hagrid67
Browse files

Updated Notebook Editor to 20x20 rails.

Applied workaround for spurious matplotlib console output
parent a0636d2a
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,9 @@ from numpy import array ...@@ -3,6 +3,9 @@ from numpy import array
import time import time
from collections import deque from collections import deque
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from contextlib import redirect_stdout
import os
# import io # import io
# from PIL import Image # from PIL import Image
# from ipywidgets import IntSlider, link, VBox # from ipywidgets import IntSlider, link, VBox
...@@ -19,8 +22,8 @@ class JupEditor(object): ...@@ -19,8 +22,8 @@ class JupEditor(object):
self.qEvents = deque() self.qEvents = deque()
# TODO: These are currently estimated values # TODO: These are currently estimated values
self.yxBase = array([20, 20]) self.yxBase = array([6, 21]) # pixel offset
self.nPixCell = 70 self.nPixCell = 35
self.rcHistory = [] self.rcHistory = []
self.iTransLast = -1 self.iTransLast = -1
...@@ -62,6 +65,7 @@ class JupEditor(object): ...@@ -62,6 +65,7 @@ class JupEditor(object):
if len(rcHistory) > 1: if len(rcHistory) > 1:
rcLast = rcHistory[-1] rcLast = rcHistory[-1]
if not np.array_equal(rcLast, rcCell): # only save at transition if not np.array_equal(rcLast, rcCell): # only save at transition
# print(y, x, rcCell)
rcHistory.append(rcCell) rcHistory.append(rcCell)
else: else:
rcHistory.append(rcCell) rcHistory.append(rcCell)
...@@ -98,10 +102,14 @@ class JupEditor(object): ...@@ -98,10 +102,14 @@ class JupEditor(object):
# Write the cell transition value back into the grid # Write the cell transition value back into the grid
env.rail.grid[tuple(rcMiddle)] = iValCell env.rail.grid[tuple(rcMiddle)] = iValCell
plt.figure(figsize=(10, 10)) # TODO: bit of a hack - can we suppress the console messages from MPL at source?
self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False) with redirect_stdout(os.devnull):
img = self.oRT.getImage() plt.figure(figsize=(10, 10))
plt.clf() self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False)
img = self.oRT.getImage()
plt.clf()
plt.close()
# This updates the image in the browser with the new rendered image # This updates the image in the browser with the new rendered image
wid.data = img wid.data = img
bRedrawn = True bRedrawn = True
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Jupyter Canvas Widget - Rail Editor # Jupyter Canvas Widget - Rail Editor
From - https://github.com/Who8MyLunch/Jupyter_Canvas_Widget/blob/master/notebooks/example%20mouse%20events.ipynb From - https://github.com/Who8MyLunch/Jupyter_Canvas_Widget/blob/master/notebooks/example%20mouse%20events.ipynb
Follow his instructions to do a local dev install and enable the widget. Follow his instructions to do a local dev install and enable the widget.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## You need to run all cells before trying to edit the rails! ## You need to run all cells before trying to edit the rails!
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%load_ext autoreload %load_ext autoreload
%autoreload 2 %autoreload 2
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import image_attendant as imat import image_attendant as imat
import ipywidgets import ipywidgets
import IPython import IPython
import jpy_canvas import jpy_canvas
import numpy as np import numpy as np
from numpy import array from numpy import array
import time import time
from collections import deque from collections import deque
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
import io import io
from PIL import Image from PIL import Image
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from ipywidgets import IntSlider, link, VBox from ipywidgets import IntSlider, link, VBox, RadioButtons, HBox
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import flatland.core.env import flatland.core.env
from flatland.envs.rail_env import RailEnv, random_rail_generator from flatland.envs.rail_env import RailEnv, random_rail_generator
from flatland.core.transitions import RailEnvTransitions from flatland.core.transitions import RailEnvTransitions
from flatland.core.env_observation_builder import TreeObsForRailEnv from flatland.core.env_observation_builder import TreeObsForRailEnv
import flatland.utils.rendertools as rt import flatland.utils.rendertools as rt
from flatland.utils.editor import JupEditor from flatland.utils.editor import JupEditor
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from IPython.core.display import display, HTML from IPython.core.display import display, HTML
display(HTML("<style>.container { width:90% !important; }</style>")) display(HTML("<style>.container { width:90% !important; }</style>"))
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
oEnv = RailEnv(width=10, oEnv = RailEnv(width=20,
height=10, height=20,
rail_generator=random_rail_generator(cell_type_relative_proportion=[1,1] + [0.5] * 6), rail_generator=random_rail_generator(cell_type_relative_proportion=[1,1] + [0.5] * 6),
number_of_agents=0, number_of_agents=0,
obs_builder_object=TreeObsForRailEnv(max_depth=2)) obs_builder_object=TreeObsForRailEnv(max_depth=2))
obs = oEnv.reset() obs = oEnv.reset()
oRT = rt.RenderTool(oEnv) oRT = rt.RenderTool(oEnv)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
sfEnv = "../flatland/env-data/tests/test1.npy" sfEnv = "../flatland/env-data/tests/test1.npy"
oEnv.rail.load_transition_map(sfEnv) if False:
oEnv.rail.load_transition_map(sfEnv)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Clear the rails ### Clear the rails
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
oEnv.rail.grid[:,:] = 0 oEnv.rail.grid[:,:] = 0
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Render the env in the usual way, and take an image snapshot as numpy array ### Render the env in the usual way, and take an image snapshot as numpy array
If you have already edited the env in the cell below, these changes should be reflected here. If you have already edited the env in the cell below, these changes should be reflected here.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
oFig = plt.figure(figsize=(10,10)) oFig = plt.figure(figsize=(10,10))
oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False) oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False)
img = oRT.getImage() img = oRT.getImage()
print(type(img)) print(type(img))
plt.clf() # if you don't want the image to appear here plt.clf() # if you don't want the image to appear here
pass pass
wid_img = jpy_canvas.Canvas(img) wid_img = jpy_canvas.Canvas(img)
``` ```
%% Output %% Output
<class 'numpy.ndarray'> <class 'numpy.ndarray'>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Update the function - in case external code updated ### Update the function - in case external code updated
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
wid_img.unregister_all() wid_img.unregister_all()
oEditor = JupEditor(oEnv) oEditor = JupEditor(oEnv)
wid_img.register_move(oEditor.event_handler) wid_img.register_move(oEditor.event_handler)
oEditor.rcHistory, oEditor.qEvents oEditor.rcHistory, oEditor.qEvents
``` ```
%% Output %% Output
([], deque([])) ([], deque([]))
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Edit the map below here by dragging the mouse to create transitions ### Edit the map below here by dragging the mouse to create transitions
You can create a dead-end by dragging foward and backward, ie Cell A -> Adjacent Cell B -> back to Cell A
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#wid_box #wid_box
#HBox([wid_img, wid_buttons])
wid_img wid_img
``` ```
%% Output %% Output
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Save the image (change the filename...) ### Save the image (change the filename...)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
oEnv.rail.save_transition_map("../flatland/env-data/tests/test-editor.npy") oEnv.rail.save_transition_map("../flatland/env-data/tests/test-editor.npy")
``` ```
%% Cell type:markdown id: tags:
## Junk below here
%% Cell type:code id: tags:
``` python
wid_buttons = ipywidgets.RadioButtons(options=["Draw", "Erase"])
wid_buttons
```
%% Output
%% Cell type:code id: tags:
``` python
wid_buttons.get_interact_value()
```
%% Output
'Draw'
%% Cell type:code id: tags:
``` python
def evListen(wid, ev):
x = ev["canvasX"]
y=ev["canvasY"]
yxBase = array([6, 21])
nPixCell = 35
rcCell = ((array([y, x]) - yxBase) / nPixCell).astype(int)
print(x, y, (x-21) / 35, (y-6) / 35, rcCell)
```
%% Cell type:code id: tags:
``` python
#wid_img.register_click(evListen)
```
%% Cell type:code id: tags:
``` python
#wid_img.unregister_all()
```
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment