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
pranjal_dhole
Flatland
Commits
060968e3
Commit
060968e3
authored
4 years ago
by
hagrid67
Browse files
Options
Downloads
Patches
Plain Diff
use importlib_resources in test_service notebook - find path to env_data
parent
89dd3132
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
notebooks/notebook-list
+1
-0
1 addition, 0 deletions
notebooks/notebook-list
notebooks/test-service.ipynb
+105
-43
105 additions, 43 deletions
notebooks/test-service.ipynb
with
106 additions
and
43 deletions
notebooks/notebook-list
+
1
−
0
View file @
060968e3
...
...
@@ -9,3 +9,4 @@ simple_example_3_manual_control.ipynb
Simple_Rendering_Demo.ipynb
test-collision.ipynb
test-saved-envs.ipynb
test-service.ipynb
This diff is collapsed.
Click to expand it.
notebooks/test-service.ipynb
+
105
−
43
View file @
060968e3
...
...
@@ -90,16 +90,6 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#sPack, sResource = \"env_data.tests\", \"Test_2_Level_0.pkl\"\n",
"sPack, sResource = \"env_data.tests\", \"Test_9_Level_1.pkl\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {},
"colab_type": "code",
...
...
@@ -120,7 +110,7 @@
},
{
"cell_type": "code",
"execution_count":
7
,
"execution_count":
6
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -129,12 +119,15 @@
"import redis\n",
"import subprocess as sp\n",
"import shlex\n",
"import time"
"import time\n",
"import pkg_resources as pr\n",
"import importlib_resources as ir\n",
"import sys"
]
},
{
"cell_type": "code",
"execution_count":
8
,
"execution_count":
7
,
"metadata": {},
"outputs": [
{
...
...
@@ -153,16 +146,26 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"###
Clear any old redis keys
"
"###
Find the real path of the `env_data` package (should be copied by tox)
"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pathlib.PosixPath'> /home3/jeremy/projects/aicrowd/rl-trains/flatland5/env_data/tests/test_001.pkl\n"
]
}
],
"source": [
"oRedis = redis.Redis()"
"with ir.path(\"env_data.tests\", \"test_001.pkl\") as oPath:\n",
" sPath = oPath\n",
"print(type(sPath), sPath)"
]
},
{
...
...
@@ -173,7 +176,7 @@
{
"data": {
"text/plain": [
"
[]
"
"
'/home3/jeremy/projects/aicrowd/rl-trains/flatland5/env_data/tests/service_test/'
"
]
},
"execution_count": 10,
...
...
@@ -182,8 +185,15 @@
}
],
"source": [
"lKeys = oRedis.keys(\"flatland*\")\n",
"lKeys"
"sDirRoot = \"/\" + \"/\".join(sPath.parts[1:-1] + (\"service_test\",\"\"))\n",
"sDirRoot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Clear any old redis keys"
]
},
{
...
...
@@ -191,6 +201,44 @@
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"oRedis = redis.Redis()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[b'flatland-rl::FLATLAND_RL_SERVICE_ID::response::65c5cdafbda515c05db3af5b2c7800ce']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lKeys = oRedis.keys(\"flatland*\")\n",
"lKeys"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deleting: b'flatland-rl::FLATLAND_RL_SERVICE_ID::response::65c5cdafbda515c05db3af5b2c7800ce'\n"
]
}
],
"source": [
"for sKey in lKeys:\n",
" print(\"Deleting:\", sKey)\n",
...
...
@@ -207,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": 1
2
,
"execution_count": 1
4
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -216,18 +264,30 @@
},
{
"cell_type": "code",
"execution_count": 1
3
,
"execution_count": 1
5
,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"python -m flatland.evaluators.service --test_folder /home3/jeremy/projects/aicrowd/rl-trains/flatland5/env_data/tests/service_test/ --pickle\n",
"['python', '-m', 'flatland.evaluators.service', '--test_folder', '/home3/jeremy/projects/aicrowd/rl-trains/flatland5/env_data/tests/service_test/', '--pickle']\n"
]
}
],
"source": [
"#sCmd = \"python -m flatland.evaluators.service --test_folder ../env_data/tests/service_test --mergeDir ./tmp/merge --actionDir ./tmp/actions --pickle --missingOnly\"\n",
"sCmd = \"python -m flatland.evaluators.service --test_folder ../env_data/tests/service_test --pickle\" # --verbose\"\n",
"lsCmd = shlex.split(sCmd)"
"#sCmd = \"python -m flatland.evaluators.service --test_folder ../env_data/tests/service_test --pickle\" # --verbose\"\n",
"sCmd = f\"python -m flatland.evaluators.service --test_folder {sDirRoot} --pickle\" # --verbose\"\n",
"lsCmd = shlex.split(sCmd)\n",
"print(sCmd)\n",
"print(lsCmd)"
]
},
{
"cell_type": "code",
"execution_count": 1
4
,
"execution_count": 1
6
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -237,7 +297,7 @@
},
{
"cell_type": "code",
"execution_count": 1
5
,
"execution_count": 1
7
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -246,7 +306,7 @@
},
{
"cell_type": "code",
"execution_count": 1
6
,
"execution_count": 1
8
,
"metadata": {},
"outputs": [
{
...
...
@@ -255,7 +315,7 @@
"subprocess.Popen"
]
},
"execution_count": 1
6
,
"execution_count": 1
8
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -266,7 +326,7 @@
},
{
"cell_type": "code",
"execution_count": 1
7
,
"execution_count": 1
9
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -275,26 +335,28 @@
},
{
"cell_type": "code",
"execution_count":
18
,
"execution_count":
20
,
"metadata": {},
"outputs": [],
"source": [
"oFRC = FlatlandRemoteClient(test_envs_root=\"../env_data/tests/service_test/\", verbose=False, use_pickle=True)"
"#oFRC = FlatlandRemoteClient(test_envs_root=\"../env_data/tests/service_test/\", verbose=False, use_pickle=True)\n",
"oFRC = FlatlandRemoteClient(test_envs_root=sDirRoot, verbose=False, use_pickle=True)"
]
},
{
"cell_type": "code",
"execution_count": 1
9
,
"execution_count":
2
1,
"metadata": {},
"outputs": [],
"source": [
"env, env_dict = RailEnvPersister.load_new(\"../env_data/tests/service_test/Test_0/Level_0.pkl\") # env_file)\n",
"#env, env_dict = RailEnvPersister.load_new(\"../env_data/tests/service_test/Test_0/Level_0.pkl\") # env_file)\n",
"env, env_dict = RailEnvPersister.load_new(f\"{sDirRoot}/Test_0/Level_0.pkl\") # env_file)\n",
"ldActions = env_dict[\"actions\"]"
]
},
{
"cell_type": "code",
"execution_count": 2
0
,
"execution_count": 2
2
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -310,7 +372,7 @@
},
{
"cell_type": "code",
"execution_count": 2
1
,
"execution_count": 2
3
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -319,7 +381,7 @@
},
{
"cell_type": "code",
"execution_count": 2
2
,
"execution_count": 2
4
,
"metadata": {},
"outputs": [
{
...
...
@@ -328,7 +390,7 @@
"True"
]
},
"execution_count": 2
2
,
"execution_count": 2
4
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -339,7 +401,7 @@
},
{
"cell_type": "code",
"execution_count": 2
3
,
"execution_count": 2
5
,
"metadata": {
"scrolled": false
},
...
...
@@ -370,10 +432,10 @@
"====================================================================================================\n",
"## Client Performance Stats\n",
"====================================================================================================\n",
"\t - env_creation_wait_time\t => min: 0.001
1365413665771484 || mean: 0.007825930913289389 || max: 0.014734029769897461
\n",
"\t - internal_env_reset_time\t => min: 0.00
15034675598144531 || mean: 0.0029495954513549805 || max: 0.004395723342895508
\n",
"\t - inference_time(approx)\t => min: 2.
288818359375e-05 || mean: 6.703675141808588
e-05 || max: 0.000
7417201995849609
\n",
"\t - internal_env_step_time\t => min: 0.00030
27915954589844 || mean: 0.0009439960557814924 || max: 0.00388050079345
703
12
\n",
"\t - env_creation_wait_time\t => min: 0.001
0077953338623047 || mean: 0.0071858565012613935 || max: 0.014672040939331055
\n",
"\t - internal_env_reset_time\t => min: 0.00
2426624298095703 || mean: 0.0024870634078979492 || max: 0.0025475025177001953
\n",
"\t - inference_time(approx)\t => min: 2.
1696090698242188e-05 || mean: 4.437164953577589
e-05 || max: 0.000
3075599670410156
\n",
"\t - internal_env_step_time\t => min: 0.00030
541419982910156 || mean: 0.0008465407187478588 || max: 0.0026504993438720
703\n",
"====================================================================================================\n",
"{'mean_reward': -944.0, 'mean_normalized_reward': 0.80735, 'mean_percentage_complete': 1.0}\n"
]
...
...
@@ -434,7 +496,7 @@
},
{
"cell_type": "code",
"execution_count": 2
4
,
"execution_count": 2
6
,
"metadata": {},
"outputs": [],
"source": [
...
...
%% Cell type:markdown id: tags:
# Test Service
Intended to test the service.py evaluator.
Runs the service.py and a simple client.
%% Cell type:markdown id: tags:
# Setup
%% Cell type:code id: tags:
```
python
%
load_ext
autoreload
%
autoreload
2
```
%% Cell type:code id: tags:
```
python
import
pandas
as
pd
import
numpy
as
np
import
matplotlib.pyplot
as
plt
```
%% Cell type:code id: tags:
```
python
import
PIL
from
flatland.utils.rendertools
import
RenderTool
import
imageio
import
os
```
%% Cell type:code id: tags:
```
python
from
IPython.display
import
clear_output
from
IPython.core
import
display
import
ipywidgets
as
ipw
display
.
display
(
display
.
HTML
(
"
<style>.container { width:95% !important; }</style>
"
))
```
%% Output
%% Cell type:code id: tags:
```
python
#sPack, sResource = "env_data.tests", "Test_2_Level_0.pkl"
sPack
,
sResource
=
"
env_data.tests
"
,
"
Test_9_Level_1.pkl
"
```
%% Cell type:code id: tags:
```
python
import
pickle
from
flatland.envs.rail_env
import
RailEnv
from
flatland.envs.rail_generators
import
sparse_rail_generator
from
flatland.envs.schedule_generators
import
sparse_schedule_generator
from
flatland.envs.malfunction_generators
import
malfunction_from_file
,
no_malfunction_generator
from
flatland.envs.rail_generators
import
rail_from_file
from
flatland.envs.schedule_generators
import
schedule_from_file
from
flatland.core.env_observation_builder
import
DummyObservationBuilder
```
%% Cell type:code id: tags:
```
python
from
flatland.envs.persistence
import
RailEnvPersister
from
flatland.evaluators.client
import
FlatlandRemoteClient
import
redis
import
subprocess
as
sp
import
shlex
import
time
import
pkg_resources
as
pr
import
importlib_resources
as
ir
import
sys
```
%% Cell type:code id: tags:
```
python
!
pwd
```
%% Output
/home/jeremy/projects/aicrowd/rl-trains/flatland5/notebooks
%% Cell type:markdown id: tags:
### Find the real path of the `env_data` package (should be copied by tox)
%% Cell type:code id: tags:
```
python
with
ir
.
path
(
"
env_data.tests
"
,
"
test_001.pkl
"
)
as
oPath
:
sPath
=
oPath
print
(
type
(
sPath
),
sPath
)
```
%% Output
<class 'pathlib.PosixPath'> /home3/jeremy/projects/aicrowd/rl-trains/flatland5/env_data/tests/test_001.pkl
%% Cell type:code id: tags:
```
python
sDirRoot
=
"
/
"
+
"
/
"
.
join
(
sPath
.
parts
[
1
:
-
1
]
+
(
"
service_test
"
,
""
))
sDirRoot
```
%% Output
'/home3/jeremy/projects/aicrowd/rl-trains/flatland5/env_data/tests/service_test/'
%% Cell type:markdown id: tags:
### Clear any old redis keys
%% Cell type:code id: tags:
```
python
oRedis
=
redis
.
Redis
()
```
%% Cell type:code id: tags:
```
python
lKeys
=
oRedis
.
keys
(
"
flatland*
"
)
lKeys
```
%% Output
[]
[
b'flatland-rl::FLATLAND_RL_SERVICE_ID::response::65c5cdafbda515c05db3af5b2c7800ce'
]
%% Cell type:code id: tags:
```
python
for
sKey
in
lKeys
:
print
(
"
Deleting:
"
,
sKey
)
oRedis
.
delete
(
sKey
)
```
%% Output
Deleting: b'flatland-rl::FLATLAND_RL_SERVICE_ID::response::65c5cdafbda515c05db3af5b2c7800ce'
%% Cell type:markdown id: tags:
## Service python command
### Kill any old service process
%% Cell type:code id: tags:
```
python
!
ps
-
ef
|
grep
-
i
python
|
grep
-
i
flatland
.
evaluators
.
service
|
awk
'
{print $2}
'
|
xargs
kill
```
%% Cell type:code id: tags:
```
python
#sCmd = "python -m flatland.evaluators.service --test_folder ../env_data/tests/service_test --mergeDir ./tmp/merge --actionDir ./tmp/actions --pickle --missingOnly"
sCmd
=
"
python -m flatland.evaluators.service --test_folder ../env_data/tests/service_test --pickle
"
# --verbose"
#sCmd = "python -m flatland.evaluators.service --test_folder ../env_data/tests/service_test --pickle" # --verbose"
sCmd
=
f
"
python -m flatland.evaluators.service --test_folder
{
sDirRoot
}
--pickle
"
# --verbose"
lsCmd
=
shlex
.
split
(
sCmd
)
print
(
sCmd
)
print
(
lsCmd
)
```
%% Output
python -m flatland.evaluators.service --test_folder /home3/jeremy/projects/aicrowd/rl-trains/flatland5/env_data/tests/service_test/ --pickle
['python', '-m', 'flatland.evaluators.service', '--test_folder', '/home3/jeremy/projects/aicrowd/rl-trains/flatland5/env_data/tests/service_test/', '--pickle']
%% Cell type:code id: tags:
```
python
#wOut = ipw.Output()
#wOut
```
%% Cell type:code id: tags:
```
python
oPipe
=
sp
.
Popen
(
lsCmd
)
```
%% Cell type:code id: tags:
```
python
type
(
oPipe
)
```
%% Output
subprocess.Popen
%% Cell type:code id: tags:
```
python
oPipe
.
poll
()
```
%% Cell type:code id: tags:
```
python
oFRC
=
FlatlandRemoteClient
(
test_envs_root
=
"
../env_data/tests/service_test/
"
,
verbose
=
False
,
use_pickle
=
True
)
#oFRC = FlatlandRemoteClient(test_envs_root="../env_data/tests/service_test/", verbose=False, use_pickle=True)
oFRC
=
FlatlandRemoteClient
(
test_envs_root
=
sDirRoot
,
verbose
=
False
,
use_pickle
=
True
)
```
%% Cell type:code id: tags:
```
python
env
,
env_dict
=
RailEnvPersister
.
load_new
(
"
../env_data/tests/service_test/Test_0/Level_0.pkl
"
)
# env_file)
#env, env_dict = RailEnvPersister.load_new("../env_data/tests/service_test/Test_0/Level_0.pkl") # env_file)
env
,
env_dict
=
RailEnvPersister
.
load_new
(
f
"
{
sDirRoot
}
/Test_0/Level_0.pkl
"
)
# env_file)
ldActions
=
env_dict
[
"
actions
"
]
```
%% Cell type:code id: tags:
```
python
def
expert_controller
(
obs
,
_env
):
return
ldActions
[
_env
.
_elapsed_steps
]
def
random_controller
(
obs
,
_env
):
dAct
=
{}
for
iAg
in
range
(
len
(
_env
.
agents
)):
dAct
[
iAg
]
=
np
.
random
.
randint
(
0
,
5
)
return
dAct
```
%% Cell type:code id: tags:
```
python
oObsB
=
DummyObservationBuilder
()
```
%% Cell type:code id: tags:
```
python
oObsB
.
get
()
```
%% Output
True
%% Cell type:code id: tags:
```
python
if
True
:
episode
=
0
obs
=
True
while
obs
:
obs
,
info
=
oFRC
.
env_create
(
obs_builder_object
=
oObsB
)
if
not
obs
:
print
(
"
null observation!
"
)
"""
The remote env returns False as the first obs
when it is done evaluating all the individual episodes
"""
break
print
(
"
Episode : {}
"
.
format
(
episode
))
episode
+=
1
print
(
oFRC
.
env
.
dones
[
'
__all__
'
])
while
True
:
if
episode
<
3
:
action
=
expert_controller
(
obs
,
oFRC
.
env
)
else
:
action
=
random_controller
(
obs
,
oFRC
.
env
)
time_start
=
time
.
time
()
try
:
observation
,
all_rewards
,
done
,
info
=
oFRC
.
env_step
(
action
)
time_diff
=
time
.
time
()
-
time_start
#print("Step Time : ", time_diff)
print
(
"
.
"
,
end
=
""
)
if
done
[
'
__all__
'
]:
print
(
"
\n
Current Episode :
"
,
episode
)
print
(
"
Episode Done
"
)
print
(
"
Reward :
"
,
sum
(
list
(
all_rewards
.
values
())))
break
except
TimeoutException
as
err
:
print
(
"
Timeout:
"
,
err
)
break
print
(
"
Evaluation Complete...
"
)
print
(
oFRC
.
submit
())
```
%% Output
DEPRECATED - use FileMalfunctionGen instead of malfunction_from_file
DEPRECATED - RailEnv arg: malfunction_and_process_data - use malfunction_generator
Episode : 0
False
...........................................................................................................................................................................
Current Episode : 1
Episode Done
Reward : 10.0
DEPRECATED - use FileMalfunctionGen instead of malfunction_from_file
DEPRECATED - RailEnv arg: malfunction_and_process_data - use malfunction_generator
Episode : 1
False
...........................................................................................................................................................................
Current Episode : 2
Episode Done
Reward : 10.0
null observation!
Evaluation Complete...
====================================================================================================
====================================================================================================
## Client Performance Stats
====================================================================================================
- env_creation_wait_time => min: 0.001
1365413665771484 || mean: 0.007825930913289389 || max: 0.014734029769897461
- internal_env_reset_time => min: 0.00
15034675598144531 || mean: 0.0029495954513549805 || max: 0.004395723342895508
- inference_time(approx) => min: 2.
288818359375e-05 || mean: 6.703675141808588
e-05 || max: 0.000
7417201995849609
- internal_env_step_time => min: 0.00030
27915954589844 || mean: 0.0009439960557814924 || max: 0.00388050079345
703
12
- env_creation_wait_time => min: 0.001
0077953338623047 || mean: 0.0071858565012613935 || max: 0.014672040939331055
- internal_env_reset_time => min: 0.00
2426624298095703 || mean: 0.0024870634078979492 || max: 0.0025475025177001953
- inference_time(approx) => min: 2.
1696090698242188e-05 || mean: 4.437164953577589
e-05 || max: 0.000
3075599670410156
- internal_env_step_time => min: 0.00030
541419982910156 || mean: 0.0008465407187478588 || max: 0.0026504993438720
703
====================================================================================================
{'mean_reward': -944.0, 'mean_normalized_reward': 0.80735, 'mean_percentage_complete': 1.0}
%% Cell type:markdown id: tags:
### Kill the evaluator process we started earlier
%% Cell type:code id: tags:
```
python
!
ps
-
ef
|
grep
-
i
python
|
grep
-
i
flatland
.
evaluators
.
service
|
awk
'
{print $2}
'
|
xargs
kill
```
...
...
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