Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Flatland
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sfwatergit
Flatland
Commits
bed1d101
Commit
bed1d101
authored
5 years ago
by
hagrid67
Browse files
Options
Downloads
Patches
Plain Diff
small editor bugfix
parent
a5d4ec9f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
flatland/utils/editor.py
+8
-2
8 additions, 2 deletions
flatland/utils/editor.py
notebooks/CanvasEditor.ipynb
+10
-30
10 additions, 30 deletions
notebooks/CanvasEditor.ipynb
with
18 additions
and
32 deletions
flatland/utils/editor.py
+
8
−
2
View file @
bed1d101
...
...
@@ -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
],
Tr
ue
)
env
.
rail
.
grid
[
tuple
(
rcMiddle
)],
liTrans
[
0
],
liTrans
[
1
],
b
Tr
ansition
)
# Also set the reverse transition
iValCell
=
env
.
rail
.
transitions
.
set_transition
(
iValCell
,
(
liTrans
[
1
]
+
2
)
%
4
,
(
liTrans
[
0
]
+
2
)
%
4
,
Tr
ue
)
b
Tr
ansition
)
# Write the cell transition value back into the grid
env
.
rail
.
grid
[
tuple
(
rcMiddle
)]
=
iValCell
...
...
This diff is collapsed.
Click to expand it.
notebooks/CanvasEditor.ipynb
+
10
−
30
View file @
bed1d101
...
...
@@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count":
52
,
"execution_count":
3
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -174,7 +174,7 @@
},
{
"cell_type": "code",
"execution_count": 1
44
,
"execution_count": 1
1
,
"metadata": {},
"outputs": [
{
...
...
@@ -214,7 +214,7 @@
},
{
"cell_type": "code",
"execution_count":
15
6,
"execution_count":
2
6,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -232,7 +232,7 @@
},
{
"cell_type": "code",
"execution_count":
15
7,
"execution_count":
2
7,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -285,7 +285,7 @@
},
{
"cell_type": "code",
"execution_count":
15
8,
"execution_count":
2
8,
"metadata": {
"scrolled": false
},
...
...
@@ -293,7 +293,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "
2fceb907aab945788d32e2c4555d5071
",
"model_id": "
4c71c3622edc4d1b8d28916b65ae12fc
",
"version_major": 2,
"version_minor": 0
},
...
...
@@ -314,7 +314,7 @@
},
{
"cell_type": "code",
"execution_count": 1
38
,
"execution_count": 1
5
,
"metadata": {
"scrolled": false
},
...
...
@@ -322,12 +322,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "
b9c28e5dab4e46b49ab1fb7dd9f383
4b",
"model_id": "
58e49268613640a581df588132692c
4b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output(
outputs=({'output_type': 'stream', 'text': 'Debug: True\\n', 'name': 'stdout'},)
)"
"Output()"
]
},
"metadata": {},
...
...
@@ -347,7 +347,7 @@
},
{
"cell_type": "code",
"execution_count": 1
5
,
"execution_count": 1
6
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -361,26 +361,6 @@
"## Junk below here"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Draw'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wid_buttons.get_interact_value()"
]
},
{
"cell_type": "code",
"execution_count": 17,
...
...
%% 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
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment