def test_edit_workload():
    # Workloads to compare against
    workload1 = Workload("test_workload(I_v3)", "", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", 4000, 0.01, 10, 10, 10, 100, -1, 100, False, 0, 0, 0, "")
    workload2 = Workload("Small_Workload", "", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", 200, 0.01, 10, 10, 10, 100, -1, 100, False, 0, 0, 0, "")
    # Call get_workloads 
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 2

    # Check that both workloads exist in the system
    assert compare_workloads(WL_data["test_workload(I_v3)"], workload1)
    assert compare_workloads(WL_data["Small_Workload"], workload2)

    # Now edit the first workload
    aixprt.edit_workload("test_workload(I_v3)", "workload_edited", "edited", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", "5000", "0.01", "10", "10", "20", "100", "-2", "100", "True", "0", "0", "0", "")
    workload1_edited = Workload("workload_edited", "edited", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", 5000, 0.01, 10, 10, 20, 100, -2, 100, True, 0, 0, 0, "")
    
    # Enure it was edited properly
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 2
    assert not "test_workload(I_v3)" in WL_data
    assert "workload_edited" in WL_data
    assert compare_workloads(WL_data["workload_edited"], workload1_edited)
    
    # Now try to edit a workload to have all invalid parameters (with the exception of name, comment, and command)
    actual_invalid = aixprt.edit_workload("Small_Workload", "invalidWL", "", '', "cat", "0.0.0.0", "1.0", "False", "1.0", "asdga100", "--2", "1%00", "FTrue", "z", "e", "ro", "")
    expected_invalid = ["tfhub_model", "training_steps", "learning_rate", "testing_percentage", "validation_percentage", "eval_step_interval", "train_batch_size", "test_batch_size", "validation_batch_size", "flip_left_right", "random_crop", "random_scale", "random_brightness"]
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 2
    assert len(expected_invalid) == 13
    assert expected_invalid == actual_invalid
    
    # Ensure that the workload was not edited
    assert compare_workloads(WL_data["Small_Workload"], workload2)
class AixprtApp(App):
    """
    Main application, the central part of a kivy application.
    """
    # List of Workload objects
    workloads_obj = aixprt.get_workloads()

    # Names of all the workloads in the system
    workloads = aixprt.get_workloads().keys()

    # List of suites objects
    suites_obj = aixprt.load_suites()

    # Names of all the suites in the system
    suites = aixprt.load_suites().keys()

    # wl to edit on edit suite page
    wl_to_edit = None

    # List of suite statuses
    suite_status_list = []

    # Text to display on the message page
    message_text = ''

    # Passed to ProgressScreen to run
    suite_to_run = ''

    # Passed to ProgressScreen to run
    workload_to_run = ''

    # Number of workload iterations
    workload_iterations = 1

    # Count of workloads on the page
    wl_counter = 0

    # Page you just came from.  ScreenManager has a previous() function, but it only works based
    # on the order of the screens in the ScreenManager.  Thus, for dynamic screen returning we need to store
    # the name ourselves.
    source_page = ''

    # Need to manually refresh the workload list.  This does that every half-a-second
    def refresh_workloads(self):
        """
        Refresh the workloads in the App.
        """
        App.get_running_app().workloads_obj = aixprt.get_workloads()
        App.get_running_app().workloads = aixprt.get_workloads().keys()

    # Need to manually refresh the suites list.  This does that every half-a-second
    def refresh_suites(self):
        """
        Refresh the suites in the App.
        """
        App.get_running_app().suites_obj = aixprt.load_suites()
        App.get_running_app().suites = aixprt.load_suites().keys()

    Clock.schedule_interval(refresh_workloads, 0.5)
    Clock.schedule_interval(refresh_suites, 0.5)
def test_remove_workload():
    expected_workload1 = Workload("test_workload(I_v3)", "", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", 4000, 0.01, 10, 10, 10, 100, -1, 100, False, 0, 0, 0, "")
    expected_workload2 = Workload("Small_Workload", "", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", 200, 0.01, 10, 10, 10, 100, -1, 100, False, 0, 0, 0, "")
    expected_workload3 = Workload("Smaller_Workload", "", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", 100, 0.01, 10, 10, 10, 100, -1, 100, False, 0, 0, 0, "")

    # Ensure file is setup properly
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 2
    assert "test_workload(I_v3)" in WL_data
    assert "Small_Workload" in WL_data
    
    # Try to remove a non-existent workload
    assert aixprt.remove_workload("Not_in_file") == 0
    assert len(WL_data) == 2
    assert "test_workload(I_v3)" in WL_data
    assert "Small_Workload" in WL_data
    
    # Remove an existing workload
    actual_workload1 = aixprt.remove_workload("test_workload(I_v3)")
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 1
    assert not "test_workload(I_v3)" in WL_data 
    assert compare_workloads(expected_workload1, actual_workload1)

    # Remove an existing workload that is used in a suite
    suite1 = aixprt.get_suite("Small_Suite")
    assert len(suite1) == 2
    actual_workload2 = aixprt.remove_workload("Small_Workload")
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 0
    assert not "Small_Workload" in WL_data 
    assert compare_workloads(expected_workload2, actual_workload2)
    suite1 = aixprt.get_suite("Small_Suite")
    assert len(suite1) == 1
    assert not "Small_Workload" in suite1 

    # Remove a workload that occurs multiple times in multiple suites
    # Setup for this particular test:
    aixprt.add_workload("test_workload(I_v3)", "", 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1', "4100", "0.01", "10", "10", "10", "100", "-1", "100", "False", "0", "0", "0", "")
    aixprt.add_workload("Small_Workload", "", 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1', "200", "0.01", "10", "10", "10", "100", "-1", "100", "False", "0", "0", "0", "")
    aixprt.add_workload("Smaller_Workload", "", 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1', "100", "0.01", "10", "10", "10", "100", "-1", "100", "False", "0", "0", "0", "")
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 3
    suite2_A = aixprt.get_suite("Small_Suite")
    assert len(suite2_A) == 1
    suite2_B = aixprt.get_suite("Smaller_Suite")
    assert len(suite2_B) == 2
    # Now remove 
    actual_workload3 = aixprt.remove_workload("Smaller_Workload")
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 2
    assert not "Smaller_Workload" in WL_data
    assert compare_workloads(expected_workload3, actual_workload3)
    suite2_A = aixprt.get_suite("Small_Suite")
    assert len(suite2_A) == 0
    assert not "Smaller_Workload" in suite2_A
    suite2_B = aixprt.get_suite("Smaller_Suite")
    assert len(suite2_B) == 0
    assert not "Smaller_Workload" in suite2_B
    def add_workload(self):
        """
        Add's a workload to the suite
        """
        if self.ids.spin_wls_to_add.text != 'Choose...':
            self.ids.edit_suite_add_btn.disabled = False
            self.active_wl = aixprt.get_workloads()[
                self.ids.spin_wls_to_add.text]
            self.order_list()
            # get iterations
            self.active_wl_iterations = self.ids.edit_suite_wl_it
            # add to list of suites
            wl_to_add = {
                "name": self.active_wl.name,
                "iterations": self.active_wl_iterations.text
            }

            self.active_suite.append(wl_to_add)
            App.get_running_app().suite_status_list.append(
                't'
            )  # t means uneditable. Don't remember why I picked t. Might change it.

            # display workload on label -- changed
            self.display_wl_labels()

            self.count_workloads()
            self.refresh_wls()
    def add_workload(self):
        """
        Adds the selected Workload to the Suite you are creating.
        """
        if self.ids.spin_suite_add.text != 'Choose...' and App.get_running_app(
        ).wl_counter <= 10:
            self.ids.suite_add_btn.disabled = False
            # get wl
            self.active_workload = aixprt.get_workloads()[
                self.ids.spin_suite_add.text]
            # get iterations
            self.active_wl_iterations = self.ids.suite_it_add.text
            # add to list of suites
            wl_to_add = {
                "name": self.active_workload.name,
                "iterations": self.active_wl_iterations
            }
            self.suite_to_add.append(wl_to_add)
            # display workload on label
            self.ids["add_wl_" + str(
                App.get_running_app().wl_counter
            )].text = self.active_workload.name + ", Iterations: " + self.active_wl_iterations
            App.get_running_app().wl_counter += 1
            if App.get_running_app().wl_counter >= 10:
                self.ids.suite_add_btn.disabled = True
                self.ids.spin_suite_add.disabled = True
                self.ids.suite_max_add.text = "Max workloads added!"

            self.refresh_page()
 def update_fields(self):
     """
     Auto-fills TextInput boxes with data from the selected workload.
     """
     # Update active workload with our selected. Probably needs to be
     # changed to use the workload list in AIXPRT class.
     if self.ids.spin_add.text != 'Choose...':
         self.active_workload = aixprt.get_workloads()[
             self.ids.spin_add.text]
         self.ids.name.text = self.active_workload.name
         self.ids.tfhub_model.text = self.active_workload.tfhub_model
         self.ids.training_steps.text = str(
             self.active_workload.training_steps)
         self.ids.learning_rate.text = str(
             self.active_workload.learning_rate)
         self.ids.testing_percentage.text = str(
             self.active_workload.testing_percentage)
         self.ids.validation_percentage.text = str(
             self.active_workload.validation_percentage)
         self.ids.eval_step_interval.text = str(
             self.active_workload.eval_step_interval)
         self.ids.train_batch_size.text = str(
             self.active_workload.train_batch_size)
         self.ids.test_batch_size.text = str(
             self.active_workload.test_batch_size)
         self.ids.validation_batch_size.text = str(
             self.active_workload.validation_batch_size)
         self.ids.flip_left_right.text = str(
             self.active_workload.flip_left_right)
         self.ids.random_crop.text = str(self.active_workload.random_crop)
         self.ids.random_scale.text = str(self.active_workload.random_scale)
         self.ids.random_brightness.text = str(
             self.active_workload.random_brightness)
         self.ids.comment.text = str(self.active_workload.comment)
         self.ids.command.text = str(self.active_workload.command)
def test_get_workloads():
    workload1 = Workload("test_workload(I_v3)", "", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", 4000, 0.01, 10, 10, 10, 100, -1, 100, False, 0, 0, 0, "")
    workload2 = Workload("Small_Workload", "", "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1", 200, 0.01, 10, 10, 10, 100, -1, 100, False, 0, 0, 0, "")
    # Call get_workloads 
    WL_data = aixprt.get_workloads()
    # Check that there are only 2 workloads in the list and ensure both are correct
    assert len(WL_data) == 2
    assert "test_workload(I_v3)" in WL_data
    assert "Small_Workload" in WL_data
    assert compare_workloads(WL_data["test_workload(I_v3)"], workload1)
    assert compare_workloads(WL_data["Small_Workload"], workload2)
def test_add_workload():
    WL_data = aixprt.get_workloads()
    workload1 = Workload("test_workload1", "test comment", 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1', 4100, 0.02, 10, 20, 10, 100, -2, 100, True, 0, 0, 0, "")
    
    # Test to ensure that the workload to be added does not exist in the file
    assert not "test_workload1" in WL_data
    # Then add and assert that it does exist in it
    aixprt.add_workload("test_workload1", "test comment", 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1', "4100", "0.02", "10", "20", "10", "100", "-2", "100", "True", "0", "0", "0", "")
    WL_data = aixprt.get_workloads()
    assert "test_workload1" in WL_data
    assert compare_workloads(WL_data["test_workload1"], workload1)

    # Try to add the same workload again and ensure it does not
    assert len(WL_data) == 3
    assert aixprt.add_workload("test_workload1", "test comment", 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1', "4100", "0.02", "10", "20", "10", "100", "-2", "100", "True", "0", "0", "0", "") == 1
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 3

    # Try to add invalid workloads and ensure it does not
    # Workload with 1 thing wrong
    invalid_1 = aixprt.add_workload("wrong_workload", "test comment", 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1', "4100", "0.02", "1.0", "20", "10", "100", "-2", "100", "True", "0", "0", "0", "")
    expected_invalid_1 = ["testing_percentage"]
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 3
    assert len(invalid_1) == 1
    assert invalid_1 == expected_invalid_1
    # Workload with everything wrong (except for the comment and command parameters, which can both be empty strings)
    invalid_2 = aixprt.add_workload("", "", '', "cat", "0..02", "1.0", "False", "1.0", "asdga100", "--2", "1%00", "FTrue", "z", "e", "ro", "")
    expected_invalid_2 = ["name", "tfhub_model", "training_steps", "learning_rate", "testing_percentage", "validation_percentage", "eval_step_interval", "train_batch_size", "test_batch_size", "validation_batch_size", "flip_left_right", "random_crop", "random_scale", "random_brightness"]
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 3
    assert len(invalid_2) == 14 #TODO fix after regex is corrected
    assert invalid_2 == expected_invalid_2

    # Add a workload with a command
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 3
    assert aixprt.add_workload("test_workload2", "", '', "", "", "", "", "", "", "", "", "", "", "", "", "help dir") == 0
    WL_data = aixprt.get_workloads()
    assert len(WL_data) == 4    
    def update_fields(self):
        """
        Auto-fills TextInput boxes with data from the selected workload.
        """
        # Update active workload with our selected.
        if self.ids.spin_edit.text != 'Choose...':
            self.active_workload = aixprt.get_workloads()[
                self.ids.spin_edit.text]
            self.ids.name.text = self.active_workload.name
            self.ids.tfhub_model.text = self.active_workload.tfhub_model
            self.ids.training_steps.text = str(
                self.active_workload.training_steps)
            self.ids.learning_rate.text = str(
                self.active_workload.learning_rate)
            self.ids.testing_percentage.text = str(
                self.active_workload.testing_percentage)
            self.ids.validation_percentage.text = str(
                self.active_workload.validation_percentage)
            self.ids.eval_step_interval.text = str(
                self.active_workload.eval_step_interval)
            self.ids.train_batch_size.text = str(
                self.active_workload.train_batch_size)
            self.ids.test_batch_size.text = str(
                self.active_workload.test_batch_size)
            self.ids.validation_batch_size.text = str(
                self.active_workload.validation_batch_size)
            self.ids.flip_left_right.text = str(
                self.active_workload.flip_left_right)
            self.ids.random_crop.text = str(self.active_workload.random_crop)
            self.ids.random_scale.text = str(self.active_workload.random_scale)
            self.ids.random_brightness.text = str(
                self.active_workload.random_brightness)
            self.ids.comment.text = str(self.active_workload.comment)
            self.ids.command.text = str(self.active_workload.command)

            # Status should be initially blank
            self.ids.edit_status.text = ''
 def refresh_workloads(self):
     """
     Refresh the workloads in the App.
     """
     App.get_running_app().workloads_obj = aixprt.get_workloads()
     App.get_running_app().workloads = aixprt.get_workloads().keys()