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

small editor bugfix

parent a5d4ec9f
No related branches found
No related tags found
No related merge requests found
......@@ -38,6 +38,7 @@ class JupEditor(object):
self.wid_output = None
self.drawMode = "Draw"
self.env_filename = "temp.npy"
self.set_env(env)
def set_env(self, env):
self.env = env
......@@ -104,6 +105,11 @@ class JupEditor(object):
# If we have already touched 3 cells
# We have a transition into a cell, and out of it.
if self.drawMode == "Draw":
bTransition = True
elif self.drawMode == "Erase":
bTransition = False
while len(rcHistory) >= 3:
rc3Cells = array(rcHistory[:3]) # the 3 cells
rcMiddle = rc3Cells[1] # the middle cell which we will update
......@@ -122,14 +128,14 @@ class JupEditor(object):
# 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)
env.rail.grid[tuple(rcMiddle)], liTrans[0], liTrans[1], bTransition)
# Also set the reverse transition
iValCell = env.rail.transitions.set_transition(
iValCell,
(liTrans[1] + 2) % 4,
(liTrans[0] + 2) % 4,
True)
bTransition)
# Write the cell transition value back into the grid
env.rail.grid[tuple(rcMiddle)] = iValCell
......
%% 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, 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()
```
%% Cell type:code id: tags:
``` python
sfEnv = "../flatland/env-data/tests/test1.npy"
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, wid_img)
wid_img.register_move(oEditor.event_handler)
```
%% Cell type:markdown id: tags:
### 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
wid_main = HBox([wid_img, vbox_controls])
wid_output.clear_output()
wid_main
```
%% Output
%% Cell type:code id: tags:
``` python
wid_output
```
%% 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.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(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