def test_equal_length_single_animation_comparison(self): rec_anim = Animation(np.array([[1, 3, 4], [1, 2, 3]])) lib_anim1 = Animation(np.array([[1, 3, 4], [1, 2, 3]])) lib_anim2 = Animation(np.array([[2, 3, 4], [1, 2, 3]])) lib_anim3 = Animation(np.array([[2, 3, 4], [1, 2, 2.5]])) lib_anim4 = Animation(np.array([[2, 3, 4], [1, 2, 2.5], [1, 2, 2.5]])) ar = AnimationRecognition() score = ar.compare_equal_length_single_animations(rec_anim, lib_anim1) self.assertEqual(score, ScoreSpaceElement(0, anim=lib_anim1)) score = ar.compare_equal_length_single_animations(rec_anim, lib_anim2) self.assertEqual(score, ScoreSpaceElement(1, anim=lib_anim2)) score = ar.compare_equal_length_single_animations(lib_anim2, rec_anim) self.assertEqual(score, ScoreSpaceElement(1, anim=rec_anim)) score = ar.compare_equal_length_single_animations(rec_anim, lib_anim3) self.assertEqual(score, ScoreSpaceElement(1.5, anim=lib_anim3)) score = ar.compare_equal_length_single_animations(lib_anim3, rec_anim) self.assertEqual(score, ScoreSpaceElement(1.5, anim=rec_anim)) # Check that assertion is raised if animation sizes are not equal with self.assertRaises(ValueError): ar.compare_equal_length_single_animations(rec_anim, lib_anim4)
def test_process_and_get_timeline(self): p = AnimationRecognitionProcessor() p.process(self.data) timeline = p.get_timeline_with_placed_animations() desired_timeline = Timeline( length=5, elements=[ ScoreSpaceElement(0, 0, self.lib_anim1), ScoreSpaceElement(4, 2, self.lib_anim2) ]) self.assertEqual(desired_timeline, timeline)
def test_place(self): a1 = Animation(np.ndarray((2, 1))) scores = [ [ScoreSpaceElement(0, 0, anim=a1), ScoreSpaceElement(3, 1, anim=a1), ScoreSpaceElement(5, 2, anim=a1), ScoreSpaceElement(6, 3, anim=a1)], [ScoreSpaceElement(1, 0, anim=a1), ScoreSpaceElement(2, 1, anim=a1), ScoreSpaceElement(4, 2, anim=a1), ScoreSpaceElement(5, 3, anim=a1)], [ScoreSpaceElement(4, 2, anim=a1), ScoreSpaceElement(6, 1, anim=a1), ScoreSpaceElement(10, 0, anim=a1)] ] timeline = Timeline(10) TimelinePlacer.layout_score_space_on_timeline(scores, timeline) desired_timeline = Timeline(length=10, elements=[ScoreSpaceElement(0, 0, anim=a1), ScoreSpaceElement(4, 2, anim=a1)]) self.assertEqual(desired_timeline, timeline)
def test_equal_length_animation_library_comparison(self): rec_anim = Animation(np.array([[1, 3, 4], [1, 2, 3]])) lib_anim1 = Animation(np.array([[1, 3, 4], [1, 2, 3]])) lib_anim2 = Animation(np.array([[2, 3, 4], [1, 2, 3]])) lib_anim3 = Animation(np.array([[2, 3, 4], [1, 2, 2.5]])) lib_anim4 = Animation(np.array([[2, 3, 4], [1, 2, 2.5], [1, 2, 2.5]])) lib_anim5 = Animation(np.array([[2, 3], [1, 2]])) ar = AnimationRecognition() anim_lib = AnimationLibrary() scores = ar.compare_equal_length_library_animations(rec_anim, anim_lib) self.assertListEqual(scores, []) anim_lib = AnimationLibrary([lib_anim3]) scores = ar.compare_equal_length_library_animations(rec_anim, anim_lib) self.assertListEqual(scores, [ScoreSpaceElement(1.5, anim=lib_anim3)]) anim_lib = AnimationLibrary([lib_anim1, lib_anim2, lib_anim3]) scores = ar.compare_equal_length_library_animations(rec_anim, anim_lib) self.assertListEqual(scores, [ ScoreSpaceElement(0, anim=lib_anim1), ScoreSpaceElement(1, anim=lib_anim2), ScoreSpaceElement(1.5, anim=lib_anim3) ]) # Test with List[Animation] scores = ar.compare_equal_length_library_animations(rec_anim, []) self.assertListEqual(scores, []) scores = ar.compare_equal_length_library_animations( rec_anim, [lib_anim3]) self.assertListEqual(scores, [ScoreSpaceElement(1.5, anim=lib_anim3)]) scores = ar.compare_equal_length_library_animations( rec_anim, [lib_anim1, lib_anim2, lib_anim3]) self.assertListEqual(scores, [ ScoreSpaceElement(0, anim=lib_anim1), ScoreSpaceElement(1, anim=lib_anim2), ScoreSpaceElement(1.5, anim=lib_anim3) ]) # The animation with different size with self.assertRaises(ValueError): ar.compare_equal_length_library_animations(rec_anim, [lib_anim4]) # The animation with different num_features with self.assertRaises(ValueError): ar.compare_equal_length_library_animations(rec_anim, [lib_anim5])
def test_construct(self): with self.assertRaises(ValueError): Timeline(length=-1) t = Timeline() self.assertListEqual([], t.elements) self.assertEqual(t.length, 0) t = Timeline(length=5) self.assertListEqual([], t.elements) self.assertEqual(t.length, 5) t = Timeline(elements=[]) self.assertListEqual([], t.elements) self.assertEqual(t.length, 0) els = [ScoreSpaceElement(0), ScoreSpaceElement(1)] t = Timeline(elements=els) self.assertListEqual(els, t.elements)
def test_free_frames_set(self): t = Timeline() self.assertEqual(set(), t.free_frames_set()) t = Timeline(length=5) self.assertEqual(set(range(5)), t.free_frames_set()) anim1 = Animation(np.ndarray((2, 1))) # Anim with 2 frames se1 = ScoreSpaceElement(0, anim=anim1, window_start_frame=0) t.add_element(se1) # Must reserve frames 0 and 1 self.assertEqual(set(range(2, 5)), t.free_frames_set())
def test_eq(self): t1 = Timeline(elements=[ScoreSpaceElement(0), ScoreSpaceElement(1)]) t2 = Timeline(elements=[ScoreSpaceElement(0)]) t3 = Timeline() t4 = Timeline(elements=[ScoreSpaceElement(0), ScoreSpaceElement(1)]) t5 = Timeline(elements=[ScoreSpaceElement(1), ScoreSpaceElement(1)]) self.assertEqual(t1, t4) self.assertNotEqual(t1, t2) self.assertNotEqual(t1, t3) self.assertNotEqual(t2, t3) self.assertNotEqual(t4, t5)
def compare_equal_length_single_animations(self, rec_anim: Animation, lib_anim: Animation) -> ScoreSpaceElement: """ Compare 2 animations with equal length and return score Parameters ---------- rec_anim: Animation First animation to compare lib_anim: Animation Second animation to compare Returns ------- score: ScoreSpaceElement The comparison score (0 is equal), the higher the score - the bigger the difference """ score = rec_anim.diff_score(lib_anim, diff_type="abs", score_type="sum") return ScoreSpaceElement(score, anim=lib_anim)
def test_single_animation_moving_window_no_scale(self): """ Take a single recorded animation and single library animation. Slide the library animation through the recorded animation. Return the resulting scores. """ rec_anim = Animation( np.array([[1, 3, 4], [1, 2, 3], [1, 2, 2], [1, 2, 1], [1, 2, 1]])) lib_anim = Animation(np.array([[1, 3, 4], [1, 2, 3]])) wrong_length_lib_anim = Animation(np.array([[2, 3], [1, 2]])) ar = AnimationRecognition() desired_scores = [ ScoreSpaceElement(0, 0, lib_anim), ScoreSpaceElement(3, 1, lib_anim), ScoreSpaceElement(5, 2, lib_anim), ScoreSpaceElement(6, 3, lib_anim) ] calculated_scores = ar.get_single_anim_moving_window_scores( rec_anim, lib_anim) self.assertEqual(desired_scores, calculated_scores) desired_scores = [ ScoreSpaceElement(0, 0, lib_anim), ScoreSpaceElement(3, 1, lib_anim), ScoreSpaceElement(5, 2, lib_anim), ScoreSpaceElement(6, 3, lib_anim) ] calculated_scores = ar.get_single_anim_moving_window_scores( rec_anim, lib_anim, window_step=1) self.assertEqual(desired_scores, calculated_scores) desired_scores = [ ScoreSpaceElement(0, 0, lib_anim), ScoreSpaceElement(5, 2, lib_anim) ] calculated_scores = ar.get_single_anim_moving_window_scores( rec_anim, lib_anim, window_step=2) self.assertEqual(desired_scores, calculated_scores) desired_scores = [ScoreSpaceElement(0, 0, lib_anim)] calculated_scores = ar.get_single_anim_moving_window_scores( rec_anim, lib_anim, window_step=5) self.assertEqual(desired_scores, calculated_scores) desired_scores = [ScoreSpaceElement(0, 0, rec_anim)] calculated_scores = ar.get_single_anim_moving_window_scores( rec_anim, rec_anim, window_step=5) self.assertEqual(desired_scores, calculated_scores) # The length of rec_anim is less than anim - raise ValueError with self.assertRaises(ValueError): ar.get_single_anim_moving_window_scores(rec_anim=lib_anim, anim=rec_anim) # The animations with different num_features - raise ValueError with self.assertRaises(ValueError): ar.compare_equal_length_library_animations(rec_anim, [wrong_length_lib_anim])
def test_multiple_library_animations_moving_window_no_scale(self): """ Take a single recorded animation and several library animations. Slide the library animations through the recorded animation. Return the resulting scores. """ rec_anim = Animation( np.array([[1, 3, 4], [1, 2, 3], [1, 2, 2], [1, 2, 1], [1, 2, 1]])) lib_anim1 = Animation(np.array([[1, 3, 4], [1, 2, 3]])) lib_anim2 = Animation(np.array([[1, 2, 4], [1, 2, 3]])) lib_anim3 = Animation(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])) anim_lib = AnimationLibrary([lib_anim1, lib_anim2, lib_anim3]) ar = AnimationRecognition() desired_scores = [[ ScoreSpaceElement(0, 0, lib_anim1), ScoreSpaceElement(3, 1, lib_anim1), ScoreSpaceElement(5, 2, lib_anim1), ScoreSpaceElement(6, 3, lib_anim1) ], [ ScoreSpaceElement(1, 0, lib_anim2), ScoreSpaceElement(2, 1, lib_anim2), ScoreSpaceElement(4, 2, lib_anim2), ScoreSpaceElement(5, 3, lib_anim2) ], [ ScoreSpaceElement(10, 0, lib_anim3), ScoreSpaceElement(6, 1, lib_anim3), ScoreSpaceElement(4, 2, lib_anim3) ]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib) self.assertEqual(desired_scores, calculated_scores) desired_scores = [[ ScoreSpaceElement(0, 0, lib_anim1), ScoreSpaceElement(3, 1, lib_anim1), ScoreSpaceElement(5, 2, lib_anim1), ScoreSpaceElement(6, 3, lib_anim1) ], [ ScoreSpaceElement(1, 0, lib_anim2), ScoreSpaceElement(2, 1, lib_anim2), ScoreSpaceElement(4, 2, lib_anim2), ScoreSpaceElement(5, 3, lib_anim2) ], [ ScoreSpaceElement(10, 0, lib_anim3), ScoreSpaceElement(6, 1, lib_anim3), ScoreSpaceElement(4, 2, lib_anim3) ]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib, window_step=1) self.assertEqual(desired_scores, calculated_scores) desired_scores = [[ ScoreSpaceElement(0, 0, lib_anim1), ScoreSpaceElement(5, 2, lib_anim1) ], [ ScoreSpaceElement(1, 0, lib_anim2), ScoreSpaceElement(4, 2, lib_anim2) ], [ ScoreSpaceElement(10, 0, lib_anim3), ScoreSpaceElement(4, 2, lib_anim3) ]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib, window_step=2) self.assertEqual(desired_scores, calculated_scores) desired_scores = [[ScoreSpaceElement(0, 0, lib_anim1)], [ScoreSpaceElement(1, 0, lib_anim2)], [ScoreSpaceElement(10, 0, lib_anim3)]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib, window_step=5) self.assertEqual(desired_scores, calculated_scores) desired_scores = [[ScoreSpaceElement(0, 0, lib_anim1)], [ScoreSpaceElement(1, 0, lib_anim2)], [ScoreSpaceElement(10, 0, lib_anim3)]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib, window_step=5) self.assertEqual(desired_scores, calculated_scores) # lib_anim4 has wrong num features - raise ValueError lib_anim4 = Animation(np.array([[1, 1], [1, 1], [1, 1]])) anim_lib = AnimationLibrary( [lib_anim1, lib_anim2, lib_anim3, lib_anim4]) with self.assertRaises(ValueError): ar.get_anim_library_moving_window_scores(rec_anim, anim_lib) # lib_anim5 is longer than rec_anim - raise ValueError lib_anim5 = Animation( np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]])) anim_lib = AnimationLibrary( [lib_anim1, lib_anim2, lib_anim3, lib_anim5]) with self.assertRaises(ValueError): ar.get_anim_library_moving_window_scores(rec_anim, anim_lib)
def test_single_library_animation_moving_window_no_scale(self): """ Take a single recorded animation and a single library animation. Slide the library animation through the recorded animation. Return the resulting scores. """ rec_anim = Animation( np.array([[1, 3, 4], [1, 2, 3], [1, 2, 2], [1, 2, 1], [1, 2, 1]])) lib_anim1 = Animation(np.array([[1, 3, 4], [1, 2, 3]])) anim_lib = AnimationLibrary([lib_anim1]) ar = AnimationRecognition() desired_scores = [[ ScoreSpaceElement(0, 0, lib_anim1), ScoreSpaceElement(3, 1, lib_anim1), ScoreSpaceElement(5, 2, lib_anim1), ScoreSpaceElement(6, 3, lib_anim1) ]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib) self.assertEqual(desired_scores, calculated_scores) desired_scores = [[ ScoreSpaceElement(0, 0, lib_anim1), ScoreSpaceElement(3, 1, lib_anim1), ScoreSpaceElement(5, 2, lib_anim1), ScoreSpaceElement(6, 3, lib_anim1) ]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib, window_step=1) self.assertEqual(desired_scores, calculated_scores) desired_scores = [[ ScoreSpaceElement(0, 0, lib_anim1), ScoreSpaceElement(5, 2, lib_anim1) ]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib, window_step=2) self.assertEqual(desired_scores, calculated_scores) desired_scores = [[ScoreSpaceElement(0, 0, lib_anim1)]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib, window_step=5) self.assertEqual(desired_scores, calculated_scores) desired_scores = [[ScoreSpaceElement(0, 0, lib_anim1)]] calculated_scores = ar.get_anim_library_moving_window_scores( rec_anim, anim_lib, window_step=5) self.assertEqual(desired_scores, calculated_scores)
def test_constructor(self): scores = [[ ScoreSpaceElement(0, 0), ScoreSpaceElement(3, 1), ScoreSpaceElement(5, 2), ScoreSpaceElement(6, 3) ], [ ScoreSpaceElement(1, 0), ScoreSpaceElement(2, 1), ScoreSpaceElement(4, 2), ScoreSpaceElement(5, 3) ], [ ScoreSpaceElement(4, 2), ScoreSpaceElement(6, 1), ScoreSpaceElement(10, 0) ]] score_space = ScoreSpaceSimple2D(scores) self.assertListEqual(score_space.elements, scores) score_space = ScoreSpaceSimple2D() self.assertListEqual(score_space.elements, []) score_space = ScoreSpaceSimple2D([]) self.assertListEqual(score_space.elements, [])
def test_get_best_score_element(self): scores = [[ ScoreSpaceElement(0, 0), ScoreSpaceElement(3, 1), ScoreSpaceElement(5, 2), ScoreSpaceElement(6, 3) ], [ ScoreSpaceElement(1, 0), ScoreSpaceElement(2, 1), ScoreSpaceElement(4, 2), ScoreSpaceElement(5, 3) ], [ ScoreSpaceElement(4, 2), ScoreSpaceElement(6, 1), ScoreSpaceElement(10, 0) ]] s = ScoreSpaceSimple2D(scores) self.assertEqual(ScoreSpaceElement(0, 0), s.get_best_score_element()) self.assertEqual(ScoreSpaceElement(0, 0), s.get_best_score_element(remove=True)) self.assertEqual(ScoreSpaceElement(1, 0), s.get_best_score_element()) self.assertEqual(ScoreSpaceElement(1, 0), s.get_best_score_element(remove=True)) self.assertEqual(ScoreSpaceElement(2, 1), s.get_best_score_element(remove=True)) self.assertEqual(ScoreSpaceElement(3, 1), s.get_best_score_element()) s = ScoreSpaceSimple2D([[ScoreSpaceElement(0, 0)], []]) self.assertEqual(ScoreSpaceElement(0, 0), s.get_best_score_element(remove=True)) with self.assertRaises(NoElementsAvailableError): s.get_best_score_element()
def test_empty(self): scores = [[ ScoreSpaceElement(0, 0), ScoreSpaceElement(3, 1), ScoreSpaceElement(5, 2), ScoreSpaceElement(6, 3) ], [ ScoreSpaceElement(1, 0), ScoreSpaceElement(2, 1), ScoreSpaceElement(4, 2), ScoreSpaceElement(5, 3) ], [ ScoreSpaceElement(4, 2), ScoreSpaceElement(6, 1), ScoreSpaceElement(10, 0) ]] self.assertFalse(ScoreSpaceSimple2D(scores).empty()) self.assertFalse( ScoreSpaceSimple2D([[ScoreSpaceElement(0, 0)], []]).empty()) self.assertTrue(ScoreSpaceSimple2D().empty()) self.assertTrue(ScoreSpaceSimple2D([]).empty()) self.assertTrue(ScoreSpaceSimple2D([[]]).empty()) self.assertTrue(ScoreSpaceSimple2D([[], []]).empty())
def test_add_element(self): # ========== PART 1 ========== # t = Timeline(length=3) # Check for various forms of incomplete instances of ScoreSpaceElement with self.assertRaises(IncompleteScoreSpaceElementError): t.add_element(ScoreSpaceElement(0)) with self.assertRaises(IncompleteScoreSpaceElementError): t.add_element( ScoreSpaceElement(0, anim=Animation(np.ndarray((2, 1))))) with self.assertRaises(IncompleteScoreSpaceElementError): t.add_element(ScoreSpaceElement(0, window_start_frame=1)) # ========== PART 2 ========== # t = Timeline(length=3) # Create 2 valid instances of ScoreSpaceElement el1 = ScoreSpaceElement(0, anim=Animation(), window_start_frame=0) el2 = ScoreSpaceElement(1, anim=Animation(np.ndarray((2, 1))), window_start_frame=1) # Animation with 0 frames raises error with self.assertRaises(EmptyAnimationError): t.add_element(el1) # Nothing added, just check it self.assertEqual([], t.elements) self.assertEqual({0, 1, 2}, t.free_frames_set()) # Animation with 2 frames starting at frame 1 t.add_element(el2) self.assertEqual([el2], t.elements) self.assertEqual({0}, t.free_frames_set()) self.assertEqual(False, t.full()) # ========== PART 3 ========== # t = Timeline(length=1) # Adding an animation that exceeds the timeline length raises error with self.assertRaises(FramesOutOfRangeError): t.add_element( ScoreSpaceElement(0, window_start_frame=0, anim=Animation(np.ndarray((2, 1))))) # ========== PART 4 ========== # t = Timeline(length=5) # Add a valid ScoreSpaceElement with 2 frames starting at frame 1 t.add_element( ScoreSpaceElement(1, anim=Animation(np.ndarray((2, 1))), window_start_frame=1)) self.assertEqual({0, 3, 4}, t.free_frames_set()) # Check for various forms of adding ScoreSpaceElement to the reserved timeline frames with self.assertRaises(FramesAlreadyReservedError): t.add_element( ScoreSpaceElement(1, anim=Animation(np.ndarray((2, 1))), window_start_frame=2)) with self.assertRaises(FramesAlreadyReservedError): t.add_element( ScoreSpaceElement(1, anim=Animation(np.ndarray((2, 1))), window_start_frame=0)) # Check for various forms of incomplete instances of ScoreSpaceElement. Again. with self.assertRaises(FramesOutOfRangeError): t.add_element( ScoreSpaceElement(1, anim=Animation(np.ndarray((5, 1))), window_start_frame=2))
def test_process_and_get_scores(self): # Process data and get scores p = AnimationRecognitionProcessor() p.process(self.data) scores = p.get_best_scores(num_scores=10) desired_scores = [[ ScoreSpaceElement(0, 0, self.lib_anim1), ScoreSpaceElement(3, 1, self.lib_anim1), ScoreSpaceElement(5, 2, self.lib_anim1), ScoreSpaceElement(6, 3, self.lib_anim1) ], [ ScoreSpaceElement(1, 0, self.lib_anim2), ScoreSpaceElement(2, 1, self.lib_anim2), ScoreSpaceElement(4, 2, self.lib_anim2), ScoreSpaceElement(5, 3, self.lib_anim2) ], [ ScoreSpaceElement(4, 2, self.lib_anim3), ScoreSpaceElement(6, 1, self.lib_anim3), ScoreSpaceElement(10, 0, self.lib_anim3) ]] self.assertEqual(desired_scores, scores) scores = p.get_best_scores(num_scores=2) desired_scores = [[ ScoreSpaceElement(0, 0, self.lib_anim1), ScoreSpaceElement(3, 1, self.lib_anim1) ], [ ScoreSpaceElement(1, 0, self.lib_anim2), ScoreSpaceElement(2, 1, self.lib_anim2) ], [ ScoreSpaceElement(4, 2, self.lib_anim3), ScoreSpaceElement(6, 1, self.lib_anim3) ]] self.assertEqual(desired_scores, scores) scores = p.get_best_scores(num_scores=0) desired_scores = [[], [], []] self.assertEqual(desired_scores, scores)