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
d36d70c4
Commit
d36d70c4
authored
5 years ago
by
hagrid67
Browse files
Options
Downloads
Patches
Plain Diff
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
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 @
d36d70c4
...
...
@@ -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
,
2
0
])
self
.
nPixCell
=
70
self
.
yxBase
=
array
([
6
,
2
1
])
# 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
...
...
This diff is collapsed.
Click to expand it.
notebooks/CanvasEditor.ipynb
+
102
−
7
View file @
d36d70c4
...
...
@@ -52,7 +52,7 @@
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import IntSlider, link, VBox"
"from ipywidgets import IntSlider, link, VBox
, RadioButtons, HBox
"
]
},
{
...
...
@@ -98,8 +98,8 @@
"metadata": {},
"outputs": [],
"source": [
"oEnv = RailEnv(width=
1
0,\n",
" height=
1
0,\n",
"oEnv = RailEnv(width=
2
0,\n",
" height=
2
0,\n",
" rail_generator=random_rail_generator(cell_type_relative_proportion=[1,1] + [0.5] * 6),\n",
" number_of_agents=0,\n",
" obs_builder_object=TreeObsForRailEnv(max_depth=2))\n",
...
...
@@ -115,7 +115,8 @@
"outputs": [],
"source": [
"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 @@
"cell_type": "markdown",
"metadata": {},
"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 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "
3216678923e047dd871b8b10cc87336c
",
"model_id": "
e36f66779f454856882018ee3fa8e8b3
",
"version_major": 2,
"version_minor": 0
},
...
...
@@ -236,6 +238,7 @@
],
"source": [
"#wid_box\n",
"#HBox([wid_img, wid_buttons])\n",
"wid_img"
]
},
...
...
@@ -248,12 +251,104 @@
},
{
"cell_type": "code",
"execution_count": 1
4
,
"execution_count": 1
2
,
"metadata": {},
"outputs": [],
"source": [
"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": {
...
...
%% 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
=
1
0
,
height
=
1
0
,
oEnv
=
RailEnv
(
width
=
2
0
,
height
=
2
0
,
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
```
...
...
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