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

add various buttons to editor and update notebook

parent 3dc53dac
No related branches found
No related tags found
No related merge requests found
......@@ -5,31 +5,56 @@ from collections import deque
from matplotlib import pyplot as plt
from contextlib import redirect_stdout
import os
import sys
# import io
# from PIL import Image
# from ipywidgets import IntSlider, link, VBox
# 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.env_observation_builder import TreeObsForRailEnv
from flatland.core.env_observation_builder import TreeObsForRailEnv
import flatland.utils.rendertools as rt
class JupEditor(object):
def __init__(self, env):
def __init__(self, env, wid_img):
self.env = env
self.wid_img = wid_img
self.qEvents = deque()
self.regen_size = 10
# TODO: These are currently estimated values
self.yxBase = array([6, 21]) # pixel offset
self.nPixCell = 35
self.nPixCell = 700 / self.env.rail.width # 35
self.rcHistory = []
self.iTransLast = -1
self.gRCTrans = array([[-1, 0], [0, 1], [1, 0], [0, -1]]) # NESW in RC
self.debug = False
self.wid_output = None
self.drawMode = "Draw"
self.env_filename = "temp.npy"
def set_env(self, env):
self.env = env
self.yxBase = array([6, 21]) # pixel offset
self.nPixCell = 700 / self.env.rail.width # 35
self.oRT = rt.RenderTool(env)
def setDebug(self, dEvent):
self.debug = dEvent["new"]
self.log("Debug:", self.debug)
def setOutput(self, wid_output):
self.wid_output = wid_output
def setDrawMode(self, dEvent):
self.drawMode = dEvent["new"]
def event_handler(self, wid, event):
"""Mouse motion event handler
"""
......@@ -41,6 +66,11 @@ class JupEditor(object):
bRedrawn = False
writableData = None
if self.debug:
self.log("debug:", len(qEvents), len(rcHistory), event)
assert wid == self.wid_img, "wid not same as wid_img"
# If the mouse is held down, enqueue an event in our own queue
if event["buttons"] > 0:
qEvents.append((time.time(), x, y))
......@@ -49,9 +79,9 @@ class JupEditor(object):
tNow = time.time()
if tNow - qEvents[0][0] > 0.1: # wait before trying to draw
height, width = wid.data.shape[:2]
writableData = np.copy(wid.data) # writable copy of image - wid.data is somehow readonly
writableData = np.copy(self.wid_img.data) # writable copy of image - wid_img.data is somehow readonly
with wid.hold_sync():
with self.wid_img.hold_sync():
while len(qEvents) > 0:
t, x, y = qEvents.popleft() # get events from our queue
......@@ -70,53 +100,119 @@ class JupEditor(object):
else:
rcHistory.append(rcCell)
# If we have already touched 3 cells
# We have a transition into a cell, and out of it.
if len(rcHistory) >= 3:
rc3Cells = array(rcHistory[:3]) # the 3 cells
rcMiddle = rc3Cells[1] # the middle cell which we will update
# get the 2 row, col deltas between the 3 cells, eg [-1,0] = North
rc2Trans = np.diff(rc3Cells, axis=0)
elif len(rcHistory) >= 3:
# If we have already touched 3 cells
# We have a transition into a cell, and out of it.
# get the direction index for the 2 transitions
liTrans = []
for rcTrans in rc2Trans:
iTrans = np.argwhere(np.all(self.gRCTrans - rcTrans == 0, axis=1))
if len(iTrans) > 0:
iTrans = iTrans[0][0]
liTrans.append(iTrans)
if len(liTrans) == 2:
# Set the transition
# oEnv.rail.set_transition((*rcLast, iTransLast), iTrans, True) # does nothing
iValCell = env.rail.transitions.set_transition(
env.rail.grid[tuple(rcMiddle)], liTrans[0], liTrans[1], True)
# Also set the reverse transition
iValCell = env.rail.transitions.set_transition(
iValCell,
(liTrans[1] + 2) % 4,
(liTrans[0] + 2) % 4,
True)
# Write the cell transition value back into the grid
env.rail.grid[tuple(rcMiddle)] = iValCell
while len(rcHistory) >= 3:
rc3Cells = array(rcHistory[:3]) # the 3 cells
rcMiddle = rc3Cells[1] # the middle cell which we will update
# get the 2 row, col deltas between the 3 cells, eg [-1,0] = North
rc2Trans = np.diff(rc3Cells, axis=0)
# 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
rcHistory.pop(0) # remove the last-but-one
# get the direction index for the 2 transitions
liTrans = []
for rcTrans in rc2Trans:
iTrans = np.argwhere(np.all(self.gRCTrans - rcTrans == 0, axis=1))
if len(iTrans) > 0:
iTrans = iTrans[0][0]
liTrans.append(iTrans)
if len(liTrans) == 2:
# Set the transition
# oEnv.rail.set_transition((*rcLast, iTransLast), iTrans, True) # does nothing
iValCell = env.rail.transitions.set_transition(
env.rail.grid[tuple(rcMiddle)], liTrans[0], liTrans[1], True)
# Also set the reverse transition
iValCell = env.rail.transitions.set_transition(
iValCell,
(liTrans[1] + 2) % 4,
(liTrans[0] + 2) % 4,
True)
# Write the cell transition value back into the grid
env.rail.grid[tuple(rcMiddle)] = iValCell
rcHistory.pop(0) # remove the last-but-one
self.redraw()
bRedrawn = True
# only redraw with the dots/squares if necessary
if not bRedrawn and writableData is not None:
# This updates the image in the browser to be the new edited version
wid.data = writableData
self.wid_img.data = writableData
def on_click(self, event):
pass
def redraw(self, hide_stdout=True, update=True):
if hide_stdout:
stdout_dest = os.devnull
else:
stdout_dest = sys.stdout
# TODO: bit of a hack - can we suppress the console messages from MPL at source?
with redirect_stdout(stdout_dest):
plt.figure(figsize=(10, 10))
self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False)
img = self.oRT.getImage()
plt.clf()
plt.close()
if update:
self.wid_img.data = img
return img
def redraw_event(self, event):
img = self.redraw()
self.wid_img.data = img
def clear(self, event):
self.env.rail.grid[:, :] = 0
self.redraw_event(event)
def setFilename(self, filename):
self.log("filename = ", filename, type(filename))
self.env_filename = filename
def setFilename_event(self, event):
self.setFilename(event["new"])
def load(self, event):
self.env.rail.load_transition_map(self.env_filename, override_gridsize=True)
self.fix_env()
self.set_env(self.env)
self.wid_img.data = self.redraw()
def save(self, event):
self.log("save to ", self.env_filename)
self.env.rail.save_transition_map(self.env_filename)
def regenerate_event(self, event):
self.env = RailEnv(width=self.regen_size,
height=self.regen_size,
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))
self.env.reset()
self.set_env(self.env)
self.redraw()
def setRegenSize_event(self, event):
self.regen_size = event["new"]
def fix_env(self):
self.env.width = self.env.rail.width
self.env.height = self.env.rail.height
def log(self, *args, **kwargs):
if self.wid_output:
with self.wid_output:
print(*args, **kwargs)
else:
print(*args, **kwargs)
%% 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, RadioButtons, HBox
from ipywidgets import IntSlider, link, VBox, RadioButtons, HBox, interact
```
%% 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=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"
if False:
if True:
oEnv.rail.load_transition_map(sfEnv)
oEnv.width = oEnv.rail.width
oEnv.height = oEnv.rail.height
```
%% Cell type:code id: tags:
``` python
oRT = rt.RenderTool(oEnv)
```
%% Cell type:code id: tags:
``` python
oEnv.width
```
%% Output
10
%% 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)
oEditor = JupEditor(oEnv, wid_img)
wid_img.register_move(oEditor.event_handler)
oEditor.rcHistory, oEditor.qEvents
```
%% Output
%% Cell type:markdown id: tags:
([], deque([]))
### Some more widgets
%% Cell type:code id: tags:
``` python
wid_drawMode = ipywidgets.RadioButtons(options=["Draw", "Erase", "Origin", "Destination"])
wid_drawMode.observe(oEditor.setDrawMode, names="value")
wid_refresh = ipywidgets.Button(description="Refresh")
wid_refresh.on_click(oEditor.redraw_event)
wid_clear = ipywidgets.Button(description = "Clear")
wid_clear.on_click(oEditor.clear)
wid_debug = ipywidgets.Checkbox(description = "Debug")
wid_debug.observe(oEditor.setDebug, names="value")
wid_output = ipywidgets.Output()
oEditor.setOutput(wid_output)
wid_regen = ipywidgets.Button(description = "Regenerate")
wid_filename = ipywidgets.Text(description = "Filename")
wid_filename.value = sfEnv
oEditor.setFilename(sfEnv)
wid_filename.observe(oEditor.setFilename_event, names="value")
wid_size = ipywidgets.IntSlider(min=5, max=30, step=5, description="Regen Size")
wid_size.observe(oEditor.setRegenSize_event, names="value")
ldButtons = [
dict(name = "Refresh", method = oEditor.redraw_event),
dict(name = "Clear", method = oEditor.clear),
dict(name = "Regenerate", method = oEditor.regenerate_event),
dict(name = "Load", method = oEditor.load),
dict(name = "Save", method = oEditor.save)
]
lwid_buttons = []
for dButton in ldButtons:
wid_button = ipywidgets.Button(description = dButton["name"])
wid_button.on_click(dButton["method"])
lwid_buttons.append(wid_button)
#wid_debug = interact(oEditor.setDebug, debug=False)
vbox_controls = VBox([wid_filename, wid_drawMode, *lwid_buttons, wid_size, wid_debug])
```
%% 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
# wid_box
wid_main = HBox([wid_img, vbox_controls])
wid_output.clear_output()
wid_main
```
%% 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")
wid_output
```
%% Output
%% Cell type:markdown id: tags:
## Junk below here
### Save the image (change the filename...)
%% Cell type:code id: tags:
``` python
wid_buttons = ipywidgets.RadioButtons(options=["Draw", "Erase"])
wid_buttons
oEnv.rail.save_transition_map("../flatland/env-data/tests/test-editor.npy")
```
%% Output
%% Cell type:markdown id: tags:
## Junk below here
%% 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(ev)
print(x, y, (x-21) / 35, (y-6) / 35, rcCell)
```
%% Cell type:code id: tags:
``` python
#wid_img.register_click(evListen)
# wid_img.register_click(evListen)
#wid_img.register(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