示例#1
0
def load_experiment_description(
        exp_desc_file_path: str = './Resources/EnergyExperiment.json'):
    """
    Method reads the Experiment Description from specified file and performs it validation according to specified
        schema.
    :param exp_desc_file_path: String. Relative path to Experiment Description file from root of main node folder.
    :return: Dictionary. Loaded Experiment Description.
    """
    # Check if main.py running with a specified experiment description file path
    if len(argv) > 1:
        exp_desc_file_path = argv[1]

    # Load Experiment description from json file.
    experiment_description = load_json_file(exp_desc_file_path)

    # Add Parameters data to experiment description
    data = load_json_file(
        experiment_description["DomainDescription"]["DataFile"])
    experiment_data_configurations = []
    for dimension in experiment_description["DomainDescription"][
            "FeatureNames"]:
        experiment_data_configurations.append(data[dimension])
    experiment_description["DomainDescription"][
        "AllConfigurations"] = experiment_data_configurations

    # Validate Experiment description file.
    logging.getLogger(__name__).info(
        "The Experiment Description was loaded from the file '%s'." %
        exp_desc_file_path)

    return experiment_description
示例#2
0
 def test_3_load_None_file(self):
     # Test #3. Try to load content if passed file parameter is None
     # Expected result: an error is raised
     from tools.file_system_io import load_json_file
     input_file = None
     expected_result = "expected str, bytes or os.PathLike object, not NoneType"
     with pytest.raises(TypeError) as excinfo:
         load_json_file(input_file)
     assert expected_result in str(excinfo.value)
示例#3
0
 def test_2_load_non_existing_file(self):
     # Test #2. Try to load content of non-existing file
     # Expected result: an error is raised, that file doesn't exist
     from tools.file_system_io import load_json_file
     expected_result = "No such file or directory"
     input_file = "./Resources/no_such_file.json"
     with pytest.raises(IOError) as excinfo:
         load_json_file(input_file)
     assert expected_result in str(excinfo.value)
示例#4
0
    def test_1_load_invalid_json(self):
        # Test #1. Try to load content of non-json file
        # Expected result: an error is raised, that file can not be decoded
        import json

        from tools.file_system_io import load_json_file
        expected_result = "Expecting value"
        input_file = "./tools/file_system_io.py"
        with pytest.raises(json.JSONDecodeError) as excinfo:
            load_json_file(input_file)
        assert expected_result in str(excinfo.value)
示例#5
0
 def test_4_validate_valid_experiment_description(self, caplog):
     # Test #4. Validate an experiment description, loaded from the valid Experiment description file
     # Expected result: experiment description is valid (logged statement)
     import logging
     caplog.set_level(logging.INFO)
     task_description = load_json_file(
         "./Resources/EnergyExperiment/EnergyExperiment.json")
     framework_settings = load_json_file('./Resources/SettingsBRISE.json')
     input_description = {**task_description, **framework_settings}
     expected_result = "Provided Experiment Description is valid."
     validate_experiment_description(input_description)
     for record in caplog.records:
         assert record.levelname == "INFO"
         assert expected_result in str(record)
示例#6
0
 def test_0_load_valid_json(self):
     # Test #0. Load content of a valid json file
     # Expected result: json file is loaded and can be treated as a python dictionary (get value from key)
     from tools.file_system_io import load_json_file
     input_file = "./Resources/SettingsBRISE.json"
     expected_result = "./Results/"
     actual_result = load_json_file(input_file)
     assert actual_result["General"]["results_storage"] == expected_result
示例#7
0
 def test_7_validate_invalid_experiment_data(self):
     # Test #7. Validate an experiment data, loaded from the invalid Experiment data file
     # Expected result: experiment data is invalid, error is raised
     input_data = load_json_file(
         "./Resources/EnergyExperiment/EnergyExperiment.json")
     expected_result = "Provided Experiment Data has not passed the validation using schema in file"
     with pytest.raises(ValueError) as excinfo:
         validate_experiment_data(input_data)
     assert expected_result in str(excinfo.value)
示例#8
0
 def test_5_validate_invalid_experiment_description(self):
     # Test #5. Validate an experiment description, loaded from the invalid Experiment description file
     # Note: SettingsBRISE.json is not enough to pass the scheme. File is treated as invalid
     # Both (settings and ED) must be provided
     # Expected result: experiment description is invalid, error is raised
     input_description = load_json_file("./Resources/SettingsBRISE.json")
     expected_result = "Some errors caused during validation. Please, check the Experiment Description."
     with pytest.raises(ValueError) as excinfo:
         validate_experiment_description(input_description)
     assert expected_result in str(excinfo.value)
示例#9
0
 def test_6_validate_valid_experiment_data(self, caplog):
     # Test #6. Validate an experiment data, loaded from the valid Experiment data file
     # Expected result: experiment data is valid (logged statement)
     import logging
     caplog.set_level(logging.INFO)
     input_data = load_json_file(
         "./Resources/EnergyExperiment/EnergyExperimentData.json")
     expected_result = "Provided Experiment Data is valid."
     validate_experiment_data(input_data)
     for record in caplog.records:
         assert record.levelname == "INFO"
         assert expected_result in str(record)
示例#10
0
def load_experiment_setup(exp_desc_file_path: str) -> [Dict, Hyperparameter]:
    """
    Method reads the Experiment Description from specified file and performs it validation according to specified
        schema. It also loads a search space for the specified experiment from correspondent data file
    :param exp_desc_file_path: String. Relative path to Experiment Description file from root of main node folder.
    :return: loaded Experiment Description, loaded search space
    """
    # Load Experiment description from json file.
    task_description = load_json_file(exp_desc_file_path)
    framework_settings = load_json_file('./Resources/SettingsBRISE.json')

    experiment_description = {**task_description, **framework_settings}
    # Validate and load Search space
    search_space_description = load_json_file(experiment_description["DomainDescription"]["DataFile"])
    validate_experiment_data(search_space_description)
    search_space = initialize_search_space(search_space_description, experiment_description["SelectionAlgorithm"])
    logging.getLogger(__name__).info(
        f"The Experiment Description was loaded from {exp_desc_file_path}. "
        f"Search space was loaded from {experiment_description['DomainDescription']['DataFile']}."
    )

    return experiment_description, search_space
示例#11
0
def load_global_config(global_config_path: str = './GlobalConfig.json'):
    """
    Method reads configuration for BRISE from GlobalConfig.json configuration file.
    :param global_config_path: sting path to file.
    :return: dict with configuration for BRISE
    """
    logger = logging.getLogger(__name__)

    # Check if main.py running with a specified global configuration file path
    if len(argv) > 2:
        global_config_path = argv[2]

    config = load_json_file(global_config_path)
    create_folder_if_not_exists(config['results_storage'])
    logger.info("Global BRISE configuration loaded from file '%s'" %
                global_config_path)
    return config
示例#12
0
def is_json_file_valid(validated_data: dict, schema_path):
    """
    Validates if dictionary suites provided schema.
    :validated_data: dict. Dictionary for validation.
    :schema_path: string. The template schema for validation.
    :return: boolean. Is dictionary is valid under provided schema.
    """
    schema = load_json_file(schema_path)

    resolver = RefResolver('file:///' + abspath('.').replace("\\", "/") + '/',
                           schema)

    try:
        return Draft4Validator(
            schema, resolver=resolver).validate(validated_data) is None
    except ValidationError as error:
        logging.getLogger(__name__).error(error)
        return False
示例#13
0
# print(getcwd())
# print("----------------")
from tools.file_system_io import load_json_file


def is_json_file_valid(experiment_description: dict,
                       schema_path="Resources/schema/experiment.schema.json"):
    """
    Validates if dictionary suites provided schema.
    :experiment_description: dict. Dictionary for validation.
    :schema_path: string. The template schema for validation.
    :return: boolean. Is dictionary is valid under provided schema.
    """
    schema = load_json_file(schema_path)

    resolver = RefResolver('file:///' + abspath('.').replace("\\", "/") + '/',
                           schema)

    try:
        return Draft4Validator(
            schema, resolver=resolver).validate(experiment_description) is None
    except ValidationError as error:
        logging.getLogger(__name__).error(error)
        return False


if __name__ == "__main__":
    # Basic unit test - load EnergyExperiment and check if it valid (should be valid).
    entity_description = load_json_file("Resources/EnergyExperiment.json")
    print("Valid:", is_json_file_valid(entity_description))