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
import time
from collections import deque
from matplotlib import pyplot as plt
from contextlib import redirect_stdout
import os
# import io
# from PIL import Image
# from ipywidgets import IntSlider, link, VBox
......@@ -19,8 +22,8 @@ class JupEditor(object):
self.qEvents = deque()
# TODO: These are currently estimated values
self.yxBase = array([20, 20])
self.nPixCell = 70
self.yxBase = array([6, 21]) # pixel offset
self.nPixCell = 35
self.rcHistory = []
self.iTransLast = -1
......@@ -62,6 +65,7 @@ class JupEditor(object):
if len(rcHistory) > 1:
rcLast = rcHistory[-1]
if not np.array_equal(rcLast, rcCell): # only save at transition
# print(y, x, rcCell)
rcHistory.append(rcCell)
else:
rcHistory.append(rcCell)
......@@ -98,10 +102,14 @@ class JupEditor(object):
# Write the cell transition value back into the grid
env.rail.grid[tuple(rcMiddle)] = iValCell
plt.figure(figsize=(10, 10))
self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False)
img = self.oRT.getImage()
plt.clf()
# TODO: bit of a hack - can we suppress the console messages from MPL at source?
with redirect_stdout(os.devnull):
plt.figure(figsize=(10, 10))
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
wid.data = img
bRedrawn = True
......
%% Cell type:markdown id: tags:
# Jupyter Canvas Widget - Rail Editor
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.
%% Cell type:markdown id: tags:
## You need to run all cells before trying to edit the rails!
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
```
%% Cell type:code id: tags:
``` python
import image_attendant as imat
import ipywidgets
import IPython
import jpy_canvas
import numpy as np
from numpy import array
import time
from collections import deque
from matplotlib import pyplot as plt
import io
from PIL import Image
```
%% Cell type:code id: tags:
``` python
from ipywidgets import IntSlider, link, VBox
from ipywidgets import IntSlider, link, VBox, RadioButtons, HBox
```
%% Cell type:code id: tags:
``` python
import flatland.core.env
from flatland.envs.rail_env import RailEnv, random_rail_generator
from flatland.core.transitions import RailEnvTransitions
from flatland.core.env_observation_builder import TreeObsForRailEnv
import flatland.utils.rendertools as rt
from flatland.utils.editor import JupEditor
```
%% Cell type:code id: tags:
``` python
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:90% !important; }</style>"))
```
%% Output
%% Cell type:code id: tags:
``` python
oEnv = RailEnv(width=10,
height=10,
oEnv = RailEnv(width=20,
height=20,
rail_generator=random_rail_generator(cell_type_relative_proportion=[1,1] + [0.5] * 6),
number_of_agents=0,
obs_builder_object=TreeObsForRailEnv(max_depth=2))
obs = oEnv.reset()
oRT = rt.RenderTool(oEnv)
```
%% Cell type:code id: tags:
``` python
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:
### Clear the rails
%% Cell type:code id: tags:
``` python
oEnv.rail.grid[:,:] = 0
```
%% Cell type:markdown id: tags:
### 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.
%% Cell type:code id: tags:
``` python
oFig = plt.figure(figsize=(10,10))
oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False)
img = oRT.getImage()
print(type(img))
plt.clf() # if you don't want the image to appear here
pass
wid_img = jpy_canvas.Canvas(img)
```
%% Output
<class 'numpy.ndarray'>
%% Cell type:markdown id: tags:
### Update the function - in case external code updated
%% Cell type:code id: tags:
``` python
wid_img.unregister_all()
oEditor = JupEditor(oEnv)
wid_img.register_move(oEditor.event_handler)
oEditor.rcHistory, oEditor.qEvents
```
%% Output
([], deque([]))
%% Cell type:markdown id: tags:
### 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:
``` python
#wid_box
#HBox([wid_img, wid_buttons])
wid_img
```
%% Output
%% Cell type:markdown id: tags:
### Save the image (change the filename...)
%% Cell type:code id: tags:
``` python
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