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
4bb8c284
Commit
4bb8c284
authored
5 years ago
by
Erik Nygren
Browse files
Options
Downloads
Plain Diff
Merge branch 'master' of gitlab.aicrowd.com:flatland/flatland into dqn_on_flatland
parents
e0808569
d36d70c4
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
flatland/utils/editor.py
+14
-6
14 additions, 6 deletions
flatland/utils/editor.py
notebooks/CanvasEditor.ipynb
+102
-7
102 additions, 7 deletions
notebooks/CanvasEditor.ipynb
with
116 additions
and
13 deletions
flatland/utils/editor.py
+
14
−
6
View file @
4bb8c284
...
@@ -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
,
2
0
])
self
.
yxBase
=
array
([
6
,
2
1
])
# 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
...
...
This diff is collapsed.
Click to expand it.
notebooks/CanvasEditor.ipynb
+
102
−
7
View file @
4bb8c284
...
@@ -52,7 +52,7 @@
...
@@ -52,7 +52,7 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"from ipywidgets import IntSlider, link, VBox"
"from ipywidgets import IntSlider, link, VBox
, RadioButtons, HBox
"
]
]
},
},
{
{
...
@@ -98,8 +98,8 @@
...
@@ -98,8 +98,8 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"oEnv = RailEnv(width=
1
0,\n",
"oEnv = RailEnv(width=
2
0,\n",
" height=
1
0,\n",
" height=
2
0,\n",
" rail_generator=random_rail_generator(cell_type_relative_proportion=[1,1] + [0.5] * 6),\n",
" rail_generator=random_rail_generator(cell_type_relative_proportion=[1,1] + [0.5] * 6),\n",
" number_of_agents=0,\n",
" number_of_agents=0,\n",
" obs_builder_object=TreeObsForRailEnv(max_depth=2))\n",
" obs_builder_object=TreeObsForRailEnv(max_depth=2))\n",
...
@@ -115,7 +115,8 @@
...
@@ -115,7 +115,8 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"sfEnv = \"../flatland/env-data/tests/test1.npy\"\n",
"sfEnv = \"../flatland/env-data/tests/test1.npy\"\n",
"oEnv.rail.load_transition_map(sfEnv)"
"if False:\n",
" oEnv.rail.load_transition_map(sfEnv)"
]
]
},
},
{
{
...
@@ -209,7 +210,8 @@
...
@@ -209,7 +210,8 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"### Edit the map below here by dragging the mouse to create transitions"
"### Edit the map below here by dragging the mouse to create transitions\n",
"You can create a dead-end by dragging foward and backward, ie Cell A -> Adjacent Cell B -> back to Cell A"
]
]
},
},
{
{
...
@@ -222,7 +224,7 @@
...
@@ -222,7 +224,7 @@
{
{
"data": {
"data": {
"application/vnd.jupyter.widget-view+json": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "
3216678923e047dd871b8b10cc87336c
",
"model_id": "
e36f66779f454856882018ee3fa8e8b3
",
"version_major": 2,
"version_major": 2,
"version_minor": 0
"version_minor": 0
},
},
...
@@ -236,6 +238,7 @@
...
@@ -236,6 +238,7 @@
],
],
"source": [
"source": [
"#wid_box\n",
"#wid_box\n",
"#HBox([wid_img, wid_buttons])\n",
"wid_img"
"wid_img"
]
]
},
},
...
@@ -248,12 +251,104 @@
...
@@ -248,12 +251,104 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1
4
,
"execution_count": 1
2
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"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",
"metadata": {},
"source": [
"## Junk below here"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "52bb87bcae69447fb1ecbf06fff971bc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"RadioButtons(options=('Draw', 'Erase'), value='Draw')"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"wid_buttons = ipywidgets.RadioButtons(options=[\"Draw\", \"Erase\"])\n",
"wid_buttons"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Draw'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wid_buttons.get_interact_value()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def evListen(wid, ev):\n",
" x = ev[\"canvasX\"]\n",
" y=ev[\"canvasY\"]\n",
" yxBase = array([6, 21])\n",
" nPixCell = 35\n",
" rcCell = ((array([y, x]) - yxBase) / nPixCell).astype(int)\n",
" print(x, y, (x-21) / 35, (y-6) / 35, rcCell)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"#wid_img.register_click(evListen)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"#wid_img.unregister_all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
}
],
],
"metadata": {
"metadata": {
...
...
%% 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
=
1
0
,
oEnv
=
RailEnv
(
width
=
2
0
,
height
=
1
0
,
height
=
2
0
,
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
```
...
...
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