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
Branches
Tags
No related merge requests found
Pipeline #35783 passed
...@@ -5,6 +5,7 @@ from robofish.io import utils ...@@ -5,6 +5,7 @@ from robofish.io import utils
import numpy as np import numpy as np
def create_example_file(path):
# Create a new io file object with a 100x100cm world # Create a new io file object with a 100x100cm world
sf = robofish.io.File(world_size_cm=[100, 100], frequency_hz=25.0) sf = robofish.io.File(world_size_cm=[100, 100], frequency_hz=25.0)
...@@ -14,7 +15,7 @@ obstacle_name = sf.create_entity( ...@@ -14,7 +15,7 @@ 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")
...@@ -31,12 +32,12 @@ fishes = sf.create_multiple_entities("fish", agent_poses) ...@@ -31,12 +32,12 @@ fishes = sf.create_multiple_entities("fish", agent_poses)
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) sf.save_as(path)
# Closing and opening files (just for demonstration) # Closing and opening files (just for demonstration)
sf.close() sf.close()
sf = robofish.io.File(path=example_file) sf = robofish.io.File(path=path)
print("\nEntity Names") print("\nEntity Names")
print(sf.entity_names) print(sf.entity_names)
...@@ -50,3 +51,7 @@ print(sf.select_poses(lambda e: e.category == "fish")) ...@@ -50,3 +51,7 @@ print(sf.select_poses(lambda e: e.category == "fish"))
print("\nFile structure") print("\nFile structure")
print(sf) print(sf)
if __name__ == "__main__":
create_example_file("example.hdf5")
...@@ -2,11 +2,8 @@ import robofish.io ...@@ -2,11 +2,8 @@ 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():
path.unlink()
def create_example_file(path):
# By using the context, the file will be automatically validated # 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: 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." f.attrs["experiment_setup"] = "This is a simple example with made up data."
...@@ -33,3 +30,7 @@ with robofish.io.File(path, "w", world_size_cm=[100, 100], frequency_hz=25.0) as ...@@ -33,3 +30,7 @@ with robofish.io.File(path, "w", world_size_cm=[100, 100], frequency_hz=25.0) as
# Show and save the file # Show and save the file
print(f) print(f)
print("Poses Shape: ", f.poses.shape) print("Poses Shape: ", f.poses.shape)
if __name__ == "__main__":
path = create_example_file("example.hdf5")
...@@ -129,6 +129,8 @@ class File(h5py.File): ...@@ -129,6 +129,8 @@ class File(h5py.File):
return self return self
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
# Check if the context was left under normal circumstances
if (type, value, traceback) is (None, None, None):
self.validate() self.validate()
self.close() self.close()
......
...@@ -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