From 060968e372aeb5da0122517dd907749a4b8885d7 Mon Sep 17 00:00:00 2001
From: hagrid67 <jdhwatson@gmail.com>
Date: Thu, 1 Oct 2020 22:41:23 +0100
Subject: [PATCH] use importlib_resources in test_service notebook - find path
 to env_data

---
 notebooks/notebook-list      |   1 +
 notebooks/test-service.ipynb | 148 +++++++++++++++++++++++++----------
 2 files changed, 106 insertions(+), 43 deletions(-)

diff --git a/notebooks/notebook-list b/notebooks/notebook-list
index 5bb27272..794db006 100644
--- a/notebooks/notebook-list
+++ b/notebooks/notebook-list
@@ -9,3 +9,4 @@ simple_example_3_manual_control.ipynb
 Simple_Rendering_Demo.ipynb
 test-collision.ipynb
 test-saved-envs.ipynb
+test-service.ipynb
diff --git a/notebooks/test-service.ipynb b/notebooks/test-service.ipynb
index 807ee506..7929b591 100644
--- a/notebooks/test-service.ipynb
+++ b/notebooks/test-service.ipynb
@@ -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": 12,
+   "execution_count": 14,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -216,18 +264,30 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": 15,
    "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": 14,
+   "execution_count": 16,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -237,7 +297,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 17,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -246,7 +306,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": 18,
    "metadata": {},
    "outputs": [
     {
@@ -255,7 +315,7 @@
        "subprocess.Popen"
       ]
      },
-     "execution_count": 16,
+     "execution_count": 18,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -266,7 +326,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 17,
+   "execution_count": 19,
    "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": 19,
+   "execution_count": 21,
    "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": 20,
+   "execution_count": 22,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -310,7 +372,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 21,
+   "execution_count": 23,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -319,7 +381,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 22,
+   "execution_count": 24,
    "metadata": {},
    "outputs": [
     {
@@ -328,7 +390,7 @@
        "True"
       ]
      },
-     "execution_count": 22,
+     "execution_count": 24,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -339,7 +401,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 23,
+   "execution_count": 25,
    "metadata": {
     "scrolled": false
    },
@@ -370,10 +432,10 @@
       "====================================================================================================\n",
       "## Client Performance Stats\n",
       "====================================================================================================\n",
-      "\t - env_creation_wait_time\t => min: 0.0011365413665771484 || mean: 0.007825930913289389 || max: 0.014734029769897461\n",
-      "\t - internal_env_reset_time\t => min: 0.0015034675598144531 || mean: 0.0029495954513549805 || max: 0.004395723342895508\n",
-      "\t - inference_time(approx)\t => min: 2.288818359375e-05 || mean: 6.703675141808588e-05 || max: 0.0007417201995849609\n",
-      "\t - internal_env_step_time\t => min: 0.0003027915954589844 || mean: 0.0009439960557814924 || max: 0.0038805007934570312\n",
+      "\t - env_creation_wait_time\t => min: 0.0010077953338623047 || mean: 0.0071858565012613935 || max: 0.014672040939331055\n",
+      "\t - internal_env_reset_time\t => min: 0.002426624298095703 || mean: 0.0024870634078979492 || max: 0.0025475025177001953\n",
+      "\t - inference_time(approx)\t => min: 2.1696090698242188e-05 || mean: 4.437164953577589e-05 || max: 0.0003075599670410156\n",
+      "\t - internal_env_step_time\t => min: 0.00030541419982910156 || mean: 0.0008465407187478588 || max: 0.0026504993438720703\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": 24,
+   "execution_count": 26,
    "metadata": {},
    "outputs": [],
    "source": [
-- 
GitLab