Пример #1
0
    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 layout_score_space_on_timeline(score_space: List[
        List[ScoreSpaceElement]], timeline: Timeline) -> Timeline:
        # TODO Replace List[List[ScoreSpaceElement]] with LinearScoreSpace or smth. everywhere
        """
        Grab elements from the score space and drop them onto the timeline using some algorithm,
        that may (and will) change. So consider it to be sort of a callback or mixin or...

        
        So, the algorithm is:
        1. Find the highest score through all the ScoreSpaceElements
        
        2. If each of its frames is not reserved on the timeline
           2.1. Add it to the timeline
           2.2. Add the frames occupied by the animation to the reserved frames list
           
        3. Drop it from the list
        
        4. Repeat step 1 till the timeline is full or till the score list is empty.
        - 
        """

        # This is a temporary trick while I don't use the ScoreSpace everywhere in the code
        # instead of list of lists
        from score_space_simple_2d import ScoreSpaceSimple2D
        score_space = ScoreSpaceSimple2D(score_space)

        while not score_space.empty():
            score_element = score_space.get_best_score_element(remove=True)

            try:
                timeline.add_element(score_element)
                print(f"Added {score_element} to timeline")
            except FramesAlreadyReservedError:
                # Just drop the elements which can't be placed to the Timeline and move on
                pass

        return timeline
Пример #3
0
    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))