Beispiel #1
0
def _run_tests(args):
    logging.info(f"loading entity labels...")
    entity_labels = EntityLabelFileParser.create_from_file(args.entity_labels)

    logging.info(f"loading task categories...")
    categories = TaskCategoryFileParser.create_categories_from_file(
        args.test_set_config, entity_labels)

    logging.info(f"loading task configurations...")
    task_configurations = TaskConfigurationFileParser.create_configurations_from_file(
        args.test_set_config)

    logging.info(f"loading entity linkings...")
    entity_linkings = EntityLinkingFileParser.create_from_file(
        args.entity_mapping)

    logging.info(f"loading embeddings...")
    embeddings = EmbeddingFileParser.create_from_file(args.embeddings)

    test_configuration = TestConfiguration(embeddings, entity_linkings,
                                           entity_labels, categories,
                                           task_configurations)

    logging.info(f"starting test execution...")
    test_executor = TestExecutor(test_configuration)
    test_category_results = list(test_executor.run())

    logging.info(f"storing results...")

    logging.info(f"Result size: {len(test_category_results)}")
    args.test_results.parent.mkdir(parents=True, exist_ok=True)
    with bz2.BZ2File(str(args.test_results.absolute()), "w") as output_stream:
        pickle.dump(test_category_results, output_stream)
    def test_create_categories_from_file(self):
        file = self._random_test_file()
        with open(file, "w+") as file_output:
            print(f"""
            configuration:
                categories:
                    - category:
                        name: category_name_1
                        enabled: true
                        entities: {self.empty_file.absolute()}
                        tasks:
                            - task:
                                name: task_name_1
                                type: analogy
                                test_set: {self.empty_file.absolute()}
                            - task:
                                name: task_name_2
                                type: cosine_similarity
                                test_set: {self.empty_file.absolute()}
                        categories:
                            - category:
                                name: sub_category_name_1
                                enabled: false
                                entities: {self.empty_file.absolute()}
                                tasks:
                                categories:""",
                  file=file_output)

        entity_labels = EntityLabels()
        categories = TaskCategoryFileParser.create_categories_from_file(
            file, entity_labels)
        assert len(categories) == 1

        category = categories[0]
        assert category.name == "category_name_1"
        assert category.enabled
        assert len(category.tasks) == 2
        assert len(categories) == 1

        task = category.tasks[0]
        assert task.name == "analogy: task_name_1"
        assert task.__class__ == AnalogyTask
        assert task.test_set == self.empty_file

        task = category.tasks[1]
        assert task.name == "cosine_similarity: task_name_2"
        assert task.__class__ == CosineSimilarityTask
        assert task.test_set == self.empty_file

        subcategory = category.categories[0]
        assert subcategory.name == "sub_category_name_1"
        assert not subcategory.enabled
        assert len(subcategory.tasks) == 0
        assert len(subcategory.categories) == 0
    def test_create_from_file_empty_categories(self):
        file = self._random_test_file()
        with open(file, "w+") as file_output:
            print(f"""
            configuration:
                categories:""",
                  file=file_output)

        entity_labels = EntityLabels()
        categories = TaskCategoryFileParser.create_categories_from_file(
            file, entity_labels)
        assert len(categories) == 0
 def _create_test_configuration(self):
     task_configs = TaskConfigurationFileParser.create_configurations_from_file(
         Path(self.resource_directory, "configuration.yaml"))
     categories = TaskCategoryFileParser.create_categories_from_file(
         Path(self.resource_directory, "configuration.yaml"),
         EntityLabels())
     entity_linking = EntityLinkingFileParser.create_from_file(
         Path(self.resource_directory, "linking.csv"))
     embedding = EmbeddingFileParser.create_from_file(
         Path(self.resource_directory, "embedding"))
     return TestConfiguration(embedding, entity_linking, EntityLabels(),
                              categories, task_configs)
    def test_enabled_is_missing(self):
        file = self._random_test_file()
        with open(file, "w+") as file_output:
            print(f"""
                    configuration:
                        categories:
                            - category:
                                name: category_name_1
                                tasks:
                                categories:""",
                  file=file_output)

        with pytest.raises(KeyError):
            entity_labels = EntityLabels()
            categories = TaskCategoryFileParser.create_categories_from_file(
                file, entity_labels)