Module rodentia.test.rodentia_module_test
Expand source code
import os
import unittest
import numpy as np
from PIL import Image
import sys
sys.path.insert(0, os.getcwd())
import rodentia
def to_nd_float_array(list_obj):
return np.array(list_obj, dtype=np.float32)
def to_nd_int_array(list_obj):
return np.array(list_obj, dtype=np.int32)
def imsave(path, image):
pimage = Image.fromarray(image)
pimage.save(path)
class RodentiaModuleTest(unittest.TestCase):
def testVersion(self):
version = rodentia.rodentia_module.version()
self.assertEqual(version, rodentia.__version__)
def testEnv(self):
width = 84 * 4
height = 84 * 4
env = rodentia.rodentia_module.Env()
camera_id = env.add_camera_view(width, height,
bg_color=to_nd_float_array([1.0, 0.0, 0.0]),
near=0.05, far=80.0, focal_length=50.0,
shadow_buffer_width=0)
self.assertEqual(camera_id, 0)
agent_id = env.add_agent(radius=1.0,
pos=to_nd_float_array([0,0,0]),
rot_y=0.0,
mass=1.0,
detect_collision=False,
color=to_nd_float_array([0,0,1]))
self.assertEqual(agent_id, 0)
# Check setup interfaces
# Add box
env.add_box(texture_path="",
color=to_nd_float_array([0.0, 0.0, 0.0]),
half_extent=to_nd_float_array([30.0, 1.0, 30.0]),
pos=to_nd_float_array([0.0, -1.0, 0.0]),
rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0]),
mass=0.0,
detect_collision=False,
visible=True)
# Add Sphere
sphere_id = env.add_sphere(texture_path="",
color=to_nd_float_array([0.0, 0.0, 0.0]),
radius=1.0,
pos=to_nd_float_array([0.0, 2.0, -5.0]),
rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0]),
mass=1.0,
detect_collision=True,
visible=True)
print("sphere_id={}".format(sphere_id))
# Locate agent
env.locate_agent(id=agent_id,
pos=to_nd_float_array([0.0, 1.0, 0.0]),
rot_y=0.0)
# Locate object
env.locate_object(sphere_id,
pos=to_nd_float_array([0.0, 2.0, -6.0]),
rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0]))
# Set light parameters
env.set_light(dir=to_nd_float_array([-1.0, -1.0, 0.0]),
color=to_nd_float_array([1.0, 1.0, 1.0]),
ambient_color=to_nd_float_array([0.4, 0.4, 0.4]),
shadow_rate=0.2)
# Check step with action
action = np.array([1, 0, 0], dtype=np.int32)
env.control(id=agent_id, action=action)
collision_ids = env.step()
self.assertEqual( type(collision_ids), dict )
# Get object info
info = env.get_obj_info(agent_id)
# Check shape
self.assertEqual( (3,), info["pos"].shape )
self.assertEqual( (3,), info["velocity"].shape )
self.assertEqual( (4,), info["rot"].shape )
obs_render = env.render(camera_id, info["pos"], info["rot"],
ignore_ids=to_nd_int_array([]))
screen = obs_render["screen"]
imsave("debug.png", screen)
# Check shape
self.assertEqual( (width,height,3), screen.shape )
# dtype should be uint8
self.assertEqual(np.uint8, screen.dtype)
# Get object info
info = env.get_obj_info(sphere_id)
# Check shape
self.assertEqual( (3,), info["pos"].shape )
self.assertEqual( (3,), info["velocity"].shape )
self.assertEqual( (4,), info["rot"].shape )
# Get agent info
info = env.get_obj_info(agent_id)
# Check shape
self.assertEqual( (3,), info["pos"].shape )
self.assertEqual( (3,), info["velocity"].shape )
self.assertEqual( (4,), info["rot"].shape )
# Replace object texture
texture_path = [""]
env.replace_obj_texture(sphere_id, texture_path)
# Apply impulse
env.apply_impulse(sphere_id, to_nd_float_array([1, 0, 0]))
if __name__ == '__main__':
unittest.main()
Functions
def imsave(path, image)
-
Expand source code
def imsave(path, image): pimage = Image.fromarray(image) pimage.save(path)
def to_nd_float_array(list_obj)
-
Expand source code
def to_nd_float_array(list_obj): return np.array(list_obj, dtype=np.float32)
def to_nd_int_array(list_obj)
-
Expand source code
def to_nd_int_array(list_obj): return np.array(list_obj, dtype=np.int32)
Classes
class RodentiaModuleTest (methodName='runTest')
-
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Expand source code
class RodentiaModuleTest(unittest.TestCase): def testVersion(self): version = rodentia.rodentia_module.version() self.assertEqual(version, rodentia.__version__) def testEnv(self): width = 84 * 4 height = 84 * 4 env = rodentia.rodentia_module.Env() camera_id = env.add_camera_view(width, height, bg_color=to_nd_float_array([1.0, 0.0, 0.0]), near=0.05, far=80.0, focal_length=50.0, shadow_buffer_width=0) self.assertEqual(camera_id, 0) agent_id = env.add_agent(radius=1.0, pos=to_nd_float_array([0,0,0]), rot_y=0.0, mass=1.0, detect_collision=False, color=to_nd_float_array([0,0,1])) self.assertEqual(agent_id, 0) # Check setup interfaces # Add box env.add_box(texture_path="", color=to_nd_float_array([0.0, 0.0, 0.0]), half_extent=to_nd_float_array([30.0, 1.0, 30.0]), pos=to_nd_float_array([0.0, -1.0, 0.0]), rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0]), mass=0.0, detect_collision=False, visible=True) # Add Sphere sphere_id = env.add_sphere(texture_path="", color=to_nd_float_array([0.0, 0.0, 0.0]), radius=1.0, pos=to_nd_float_array([0.0, 2.0, -5.0]), rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0]), mass=1.0, detect_collision=True, visible=True) print("sphere_id={}".format(sphere_id)) # Locate agent env.locate_agent(id=agent_id, pos=to_nd_float_array([0.0, 1.0, 0.0]), rot_y=0.0) # Locate object env.locate_object(sphere_id, pos=to_nd_float_array([0.0, 2.0, -6.0]), rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0])) # Set light parameters env.set_light(dir=to_nd_float_array([-1.0, -1.0, 0.0]), color=to_nd_float_array([1.0, 1.0, 1.0]), ambient_color=to_nd_float_array([0.4, 0.4, 0.4]), shadow_rate=0.2) # Check step with action action = np.array([1, 0, 0], dtype=np.int32) env.control(id=agent_id, action=action) collision_ids = env.step() self.assertEqual( type(collision_ids), dict ) # Get object info info = env.get_obj_info(agent_id) # Check shape self.assertEqual( (3,), info["pos"].shape ) self.assertEqual( (3,), info["velocity"].shape ) self.assertEqual( (4,), info["rot"].shape ) obs_render = env.render(camera_id, info["pos"], info["rot"], ignore_ids=to_nd_int_array([])) screen = obs_render["screen"] imsave("debug.png", screen) # Check shape self.assertEqual( (width,height,3), screen.shape ) # dtype should be uint8 self.assertEqual(np.uint8, screen.dtype) # Get object info info = env.get_obj_info(sphere_id) # Check shape self.assertEqual( (3,), info["pos"].shape ) self.assertEqual( (3,), info["velocity"].shape ) self.assertEqual( (4,), info["rot"].shape ) # Get agent info info = env.get_obj_info(agent_id) # Check shape self.assertEqual( (3,), info["pos"].shape ) self.assertEqual( (3,), info["velocity"].shape ) self.assertEqual( (4,), info["rot"].shape ) # Replace object texture texture_path = [""] env.replace_obj_texture(sphere_id, texture_path) # Apply impulse env.apply_impulse(sphere_id, to_nd_float_array([1, 0, 0]))
Ancestors
- unittest.case.TestCase
Methods
def testEnv(self)
-
Expand source code
def testEnv(self): width = 84 * 4 height = 84 * 4 env = rodentia.rodentia_module.Env() camera_id = env.add_camera_view(width, height, bg_color=to_nd_float_array([1.0, 0.0, 0.0]), near=0.05, far=80.0, focal_length=50.0, shadow_buffer_width=0) self.assertEqual(camera_id, 0) agent_id = env.add_agent(radius=1.0, pos=to_nd_float_array([0,0,0]), rot_y=0.0, mass=1.0, detect_collision=False, color=to_nd_float_array([0,0,1])) self.assertEqual(agent_id, 0) # Check setup interfaces # Add box env.add_box(texture_path="", color=to_nd_float_array([0.0, 0.0, 0.0]), half_extent=to_nd_float_array([30.0, 1.0, 30.0]), pos=to_nd_float_array([0.0, -1.0, 0.0]), rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0]), mass=0.0, detect_collision=False, visible=True) # Add Sphere sphere_id = env.add_sphere(texture_path="", color=to_nd_float_array([0.0, 0.0, 0.0]), radius=1.0, pos=to_nd_float_array([0.0, 2.0, -5.0]), rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0]), mass=1.0, detect_collision=True, visible=True) print("sphere_id={}".format(sphere_id)) # Locate agent env.locate_agent(id=agent_id, pos=to_nd_float_array([0.0, 1.0, 0.0]), rot_y=0.0) # Locate object env.locate_object(sphere_id, pos=to_nd_float_array([0.0, 2.0, -6.0]), rot=to_nd_float_array([0.0, 0.0, 0.0, 1.0])) # Set light parameters env.set_light(dir=to_nd_float_array([-1.0, -1.0, 0.0]), color=to_nd_float_array([1.0, 1.0, 1.0]), ambient_color=to_nd_float_array([0.4, 0.4, 0.4]), shadow_rate=0.2) # Check step with action action = np.array([1, 0, 0], dtype=np.int32) env.control(id=agent_id, action=action) collision_ids = env.step() self.assertEqual( type(collision_ids), dict ) # Get object info info = env.get_obj_info(agent_id) # Check shape self.assertEqual( (3,), info["pos"].shape ) self.assertEqual( (3,), info["velocity"].shape ) self.assertEqual( (4,), info["rot"].shape ) obs_render = env.render(camera_id, info["pos"], info["rot"], ignore_ids=to_nd_int_array([])) screen = obs_render["screen"] imsave("debug.png", screen) # Check shape self.assertEqual( (width,height,3), screen.shape ) # dtype should be uint8 self.assertEqual(np.uint8, screen.dtype) # Get object info info = env.get_obj_info(sphere_id) # Check shape self.assertEqual( (3,), info["pos"].shape ) self.assertEqual( (3,), info["velocity"].shape ) self.assertEqual( (4,), info["rot"].shape ) # Get agent info info = env.get_obj_info(agent_id) # Check shape self.assertEqual( (3,), info["pos"].shape ) self.assertEqual( (3,), info["velocity"].shape ) self.assertEqual( (4,), info["rot"].shape ) # Replace object texture texture_path = [""] env.replace_obj_texture(sphere_id, texture_path) # Apply impulse env.apply_impulse(sphere_id, to_nd_float_array([1, 0, 0]))
def testVersion(self)
-
Expand source code
def testVersion(self): version = rodentia.rodentia_module.version() self.assertEqual(version, rodentia.__version__)