Skip to content
Snippets Groups Projects
Commit 4417d8ed authored by Andi Gerken's avatar Andi Gerken
Browse files

Changed tests for examples

Validation after context won't be executed when there was an exception
parent cf02c73b
No related branches found
No related tags found
No related merge requests found
Pipeline #35783 passed
...@@ -5,48 +5,53 @@ from robofish.io import utils ...@@ -5,48 +5,53 @@ from robofish.io import utils
import numpy as np import numpy as np
# Create a new io file object with a 100x100cm world def create_example_file(path):
sf = robofish.io.File(world_size_cm=[100, 100], frequency_hz=25.0) # Create a new io file object with a 100x100cm world
sf = robofish.io.File(world_size_cm=[100, 100], frequency_hz=25.0)
# create a simple obstacle, fixed in place, fixed outline # create a simple obstacle, fixed in place, fixed outline
obstacle_outline = [[[-10, -10], [-10, 0], [0, 0], [0, -10]]] obstacle_outline = [[[-10, -10], [-10, 0], [0, 0], [0, -10]]]
obstacle_name = sf.create_entity( obstacle_name = sf.create_entity(
"obstacle", positions=[[50, 50]], orientations=[[0]], outlines=obstacle_outline "obstacle", positions=[[50, 50]], orientations=[[0]], outlines=obstacle_outline
) )
# create a robofish with 100 timesteps and 40ms between the timesteps. If we would not give a name, the name would be generated to be robot_1. # create a robofish with 1000 timesteps. If we would not give a name, the name would be generated to be robot_1.
robofish_timesteps = 1000 robofish_timesteps = 1000
robofish_poses = np.ones((robofish_timesteps, 4)) * 50 robofish_poses = np.ones((robofish_timesteps, 4)) * 50
robot = sf.create_entity("robot", robofish_poses, name="robot") robot = sf.create_entity("robot", robofish_poses, name="robot")
# create multiple fishes with timestamps. Since we don't specify names, but only the type "fish" the fishes will be named ["fish_1", "fish_2", "fish_3"] # create multiple fishes with timestamps. Since we don't specify names, but only the type "fish" the fishes will be named ["fish_1", "fish_2", "fish_3"]
agents = 3 agents = 3
timesteps = 1000 timesteps = 1000
# timestamps = np.linspace(0, timesteps + 1, timesteps) # timestamps = np.linspace(0, timesteps + 1, timesteps)
agent_poses = np.random.random((agents, timesteps, 4)) agent_poses = np.random.random((agents, timesteps, 4))
fishes = sf.create_multiple_entities("fish", agent_poses) fishes = sf.create_multiple_entities("fish", agent_poses)
# This would throw an exception if the file was invalid # This would throw an exception if the file was invalid
sf.validate() sf.validate()
# Save file validates aswell # Save file validates aswell
example_file = utils.full_path(__file__, "example.hdf5")
sf.save_as(example_file)
# Closing and opening files (just for demonstration) sf.save_as(path)
sf.close()
sf = robofish.io.File(path=example_file)
print("\nEntity Names") # Closing and opening files (just for demonstration)
print(sf.entity_names) sf.close()
sf = robofish.io.File(path=path)
# Get an array with all poses. As the length of poses varies per agent, it is filled up with nans. print("\nEntity Names")
print("\nAll poses") print(sf.entity_names)
print(sf.poses)
print("\nFish poses") # Get an array with all poses. As the length of poses varies per agent, it is filled up with nans.
print(sf.select_poses(lambda e: e.category == "fish")) print("\nAll poses")
print(sf.poses)
print("\nFile structure") print("\nFish poses")
print(sf) print(sf.select_poses(lambda e: e.category == "fish"))
print("\nFile structure")
print(sf)
if __name__ == "__main__":
create_example_file("example.hdf5")
...@@ -2,34 +2,35 @@ import robofish.io ...@@ -2,34 +2,35 @@ import robofish.io
import numpy as np import numpy as np
from pathlib import Path from pathlib import Path
path = Path("example.hdf5")
if path.exists(): def create_example_file(path):
path.unlink() # By using the context, the file will be automatically validated
with robofish.io.File(path, "w", world_size_cm=[100, 100], frequency_hz=25.0) as f:
f.attrs["experiment_setup"] = "This is a simple example with made up data."
# By using the context, the file will be automatically validated # Create a single robot with 30 timesteps
with robofish.io.File(path, "w", world_size_cm=[100, 100], frequency_hz=25.0) as f: # positions are passed separately
f.attrs["experiment_setup"] = "This is a simple example with made up data." # orientations are passed as with two columns -> orientation_x and orientation_y
f.create_entity(
category="robot",
name="robot",
positions=np.zeros((100, 2)),
orientations=np.ones((100, 2)) * [0, 1],
)
# Create a single robot with 30 timesteps # Create fishes with 30 poses (x, y, orientation_rad)
# positions are passed separately poses = np.zeros((100, 3))
# orientations are passed as with two columns -> orientation_x and orientation_y poses[:, 0] = np.arange(-50, 50)
f.create_entity( poses[:, 1] = np.arange(-50, 50)
category="robot", poses[:, 2] = np.arange(0, 2 * np.pi, step=2 * np.pi / 100)
name="robot", fish = f.create_entity("fish", poses=poses)
positions=np.zeros((100, 2)), fish.attrs["species"] = "My rotating spaghetti fish"
orientations=np.ones((100, 2)) * [0, 1], fish.attrs["fish_standard_length_cm"] = 10
)
# Create fishes with 30 poses (x, y, orientation_rad) # Show and save the file
poses = np.zeros((100, 3)) print(f)
poses[:, 0] = np.arange(-50, 50) print("Poses Shape: ", f.poses.shape)
poses[:, 1] = np.arange(-50, 50)
poses[:, 2] = np.arange(0, 2 * np.pi, step=2 * np.pi / 100)
fish = f.create_entity("fish", poses=poses)
fish.attrs["species"] = "My rotating spaghetti fish"
fish.attrs["fish_standard_length_cm"] = 10
# Show and save the file
print(f) if __name__ == "__main__":
print("Poses Shape: ", f.poses.shape) path = create_example_file("example.hdf5")
...@@ -129,8 +129,10 @@ class File(h5py.File): ...@@ -129,8 +129,10 @@ class File(h5py.File):
return self return self
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
self.validate() # Check if the context was left under normal circumstances
self.close() if (type, value, traceback) is (None, None, None):
self.validate()
self.close()
def save_as(self, path: Union[str, Path], strict_validate: bool = True): def save_as(self, path: Union[str, Path], strict_validate: bool = True):
""" Save a copy of the file """ Save a copy of the file
......
...@@ -7,9 +7,20 @@ import sys ...@@ -7,9 +7,20 @@ import sys
sys.path.append(str(utils.full_path(__file__, "../../../examples/"))) sys.path.append(str(utils.full_path(__file__, "../../../examples/")))
path = utils.full_path(__file__, "../../../examples/tmp_example.hdf5")
if path.exists():
path.unlink()
def test_example_readme(): def test_example_readme():
import example_readme import example_readme
example_readme.create_example_file(path)
path.unlink()
def test_example_basic(): def test_example_basic():
import example_basic import example_basic
example_basic.create_example_file(path)
path.unlink()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment