From f1c6922df77a3224107c7f27bb0b40e34e9f253e Mon Sep 17 00:00:00 2001
From: JasonJiazhiZhang <21229070+JasonJiazhiZhang@users.noreply.github.com>
Date: Wed, 29 May 2019 14:12:42 -0700
Subject: [PATCH] Static PointGoal Sensor test (#93)

  * Add `test_static_pointgoal_senosr()` in `test/test_sensor.py`
  * Fix overriden configs in `test_sensors.py` (now using default config)
  * Change default config to include static pointgoal sensor
  * Fix `StaticPointGoalSensor._get_sensor_type()` to return `PATH`
---
 habitat/config/default.py     |  6 ++++
 habitat/tasks/nav/nav_task.py |  2 +-
 test/test_sensors.py          | 52 ++++++++++++++++++++++++++++++-----
 3 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/habitat/config/default.py b/habitat/config/default.py
index 3a165ceca..18020b970 100644
--- a/habitat/config/default.py
+++ b/habitat/config/default.py
@@ -37,6 +37,12 @@ _C.TASK.POINTGOAL_SENSOR = CN()
 _C.TASK.POINTGOAL_SENSOR.TYPE = "PointGoalSensor"
 _C.TASK.POINTGOAL_SENSOR.GOAL_FORMAT = "POLAR"
 # -----------------------------------------------------------------------------
+# # STATIC POINTGOAL SENSOR
+# -----------------------------------------------------------------------------
+_C.TASK.STATIC_POINTGOAL_SENSOR = CN()
+_C.TASK.STATIC_POINTGOAL_SENSOR.TYPE = "StaticPointGoalSensor"
+_C.TASK.STATIC_POINTGOAL_SENSOR.GOAL_FORMAT = "CARTESIAN"
+# -----------------------------------------------------------------------------
 # # HEADING SENSOR
 # -----------------------------------------------------------------------------
 _C.TASK.HEADING_SENSOR = CN()
diff --git a/habitat/tasks/nav/nav_task.py b/habitat/tasks/nav/nav_task.py
index d6c8382cb..4f5d8f823 100644
--- a/habitat/tasks/nav/nav_task.py
+++ b/habitat/tasks/nav/nav_task.py
@@ -233,7 +233,7 @@ class StaticPointGoalSensor(habitat.Sensor):
         return "static_pointgoal"
 
     def _get_sensor_type(self, *args: Any, **kwargs: Any):
-        return SensorTypes.STATIC_GOAL_VECTOR
+        return SensorTypes.PATH
 
     def _get_observation_space(self, *args: Any, **kwargs: Any):
         if self._goal_format == "CARTESIAN":
diff --git a/test/test_sensors.py b/test/test_sensors.py
index 3dc5e5afb..a24c978e5 100644
--- a/test/test_sensors.py
+++ b/test/test_sensors.py
@@ -14,10 +14,13 @@ from habitat.config.default import get_config
 from habitat.tasks.nav.nav_task import (
     NavigationEpisode,
     COLLISION_PROXIMITY_TOLERANCE,
+    NavigationGoal,
 )
 from habitat.sims.habitat_simulator import SimulatorActions
 
-CFG_TEST = "configs/test/habitat_all_sensors_test.yaml"
+NON_STOP_ACTIONS = [
+    v for v in range(len(SimulatorActions)) if v != SimulatorActions.STOP.value
+]
 
 
 def _random_episode(env, config):
@@ -41,10 +44,9 @@ def _random_episode(env, config):
 
 
 def test_heading_sensor():
-    config = get_config(CFG_TEST)
+    config = get_config()
     if not os.path.exists(config.SIMULATOR.SCENE):
         pytest.skip("Please download Habitat test data to data folder.")
-    config = get_config()
     config.defrost()
     config.TASK.SENSORS = ["HEADING_SENSOR"]
     config.freeze()
@@ -79,10 +81,9 @@ def test_heading_sensor():
 
 
 def test_tactile():
-    config = get_config(CFG_TEST)
+    config = get_config()
     if not os.path.exists(config.SIMULATOR.SCENE):
         pytest.skip("Please download Habitat test data to data folder.")
-    config = get_config()
     config.defrost()
     config.TASK.SENSORS = ["PROXIMITY_SENSOR"]
     config.TASK.MEASUREMENTS = ["COLLISIONS"]
@@ -111,10 +112,9 @@ def test_tactile():
 
 
 def test_collisions():
-    config = get_config(CFG_TEST)
+    config = get_config()
     if not os.path.exists(config.SIMULATOR.SCENE):
         pytest.skip("Please download Habitat test data to data folder.")
-    config = get_config()
     config.defrost()
     config.TASK.MEASUREMENTS = ["COLLISIONS"]
     config.freeze()
@@ -156,3 +156,41 @@ def test_collisions():
             prev_collisions = collisions
 
     env.close()
+
+
+def test_static_pointgoal_sensor():
+    config = get_config()
+    if not os.path.exists(config.SIMULATOR.SCENE):
+        pytest.skip("Please download Habitat test data to data folder.")
+    config.defrost()
+    config.TASK.SENSORS = ["STATIC_POINTGOAL_SENSOR"]
+    config.freeze()
+    env = habitat.Env(config=config, dataset=None)
+
+    # start position is checked for validity for the specific test scene
+    valid_start_position = [-1.3731, 0.08431, 8.60692]
+    expected_static_pointgoal = [0.1, 0.2, 0.3]
+    goal_position = np.add(valid_start_position, expected_static_pointgoal)
+
+    # starting quaternion is rotated 180 degree along z-axis, which
+    # corresponds to simulator using z-negative as forward action
+    start_rotation = [0, 0, 0, 1]
+
+    env.episodes = [
+        NavigationEpisode(
+            episode_id="0",
+            scene_id=config.SIMULATOR.SCENE,
+            start_position=valid_start_position,
+            start_rotation=start_rotation,
+            goals=[NavigationGoal(goal_position)],
+        )
+    ]
+
+    obs = env.reset()
+    for _ in range(5):
+        env.step(np.random.choice(NON_STOP_ACTIONS))
+    static_pointgoal = obs["static_pointgoal"]
+
+    # check to see if taking non-stop actions will affect static point_goal
+    assert np.allclose(static_pointgoal, expected_static_pointgoal)
+    env.close()
-- 
GitLab