def test_check_tree_invalid_key(): """Modify a wx decision tree.""" expected = "Node lightning contains unknown key 'kittens'" tree = wxcode_decision_tree() tree["lightning"]["kittens"] = 0 result = check_tree(tree) assert result == expected
def process( *cubes: cli.inputcube, wxtree: cli.inputjson = None, model_id_attr: str = None, record_run_attr: str = None, target_period: int = None, check_tree: bool = False, ): """ Processes cube for Weather symbols. Args: cubes (iris.cube.CubeList): A cubelist containing the diagnostics required for the weather symbols decision tree, these at co-incident times. wxtree (dict): A JSON file containing a weather symbols decision tree definition. model_id_attr (str): Name of attribute recording source models that should be inherited by the output cube. The source models are expected as a space-separated string. record_run_attr: Name of attribute used to record models and cycles used in constructing the weather symbols. target_period: The period in seconds that the weather symbol being produced should represent. This should correspond with any period diagnostics, e.g. precipitation accumulation, being used as input. This is used to scale any threshold values that are defined with an associated period in the decision tree. It will only be used if the decision tree provided has threshold values defined with an associated period. check_tree (bool): If set, the decision tree will be checked to see if it conforms to the expected format and that all nodes can be reached; the only other argument required is the path to the decision tree. If the tree is found to be valid the required inputs will be listed. Setting this flag will prevent the CLI performing any other actions. Returns: iris.cube.Cube: A cube of weather symbols. """ if check_tree: from improver.wxcode.utilities import check_tree return check_tree(wxtree, target_period=target_period) from iris.cube import CubeList from improver.wxcode.weather_symbols import WeatherSymbols if not cubes: raise RuntimeError("Not enough input arguments. See help for more information.") return WeatherSymbols( wxtree, model_id_attr=model_id_attr, record_run_attr=record_run_attr, target_period=target_period, )(CubeList(cubes))
def test_check_tree_list_requirements(): """ This test simply checks that the expected wrapper text is returned. The listing of the diagnostics is checked in testing the interrogate_decision_tree function. """ expected = "Decision tree OK\nRequired inputs are:" tree = wxcode_decision_tree() result = check_tree(tree) assert expected in result
def process( *cubes: cli.inputcube, wxtree: cli.inputjson = None, model_id_attr: str = None, check_tree: bool = False, ): """ Processes cube for Weather symbols. Args: cubes (iris.cube.CubeList): A cubelist containing the diagnostics required for the weather symbols decision tree, these at co-incident times. wxtree (dict): A JSON file containing a weather symbols decision tree definition. model_id_attr (str): Name of attribute recording source models that should be inherited by the output cube. The source models are expected as a space-separated string. check_tree (bool): If set the decision tree will be checked to see if it conforms to the expected format; the only other argument required is the path to the decision tree. If the tree is found to be valid the required inputs will be listed. Setting this flag will prevent the CLI performing any other actions. Returns: iris.cube.Cube: A cube of weather symbols. """ if check_tree: from improver.wxcode.utilities import check_tree return check_tree(wxtree) from iris.cube import CubeList from improver.wxcode.weather_symbols import WeatherSymbols if not cubes: raise RuntimeError( "Not enough input arguments. " "See help for more information." ) return WeatherSymbols(wxtree, model_id_attr=model_id_attr)(CubeList(cubes))
def test_check_tree_non_dictionary(): """Check ValueError is raised if non-dictionary is passed to check_tree.""" expected = "Decision tree is not a dictionary" with pytest.raises(ValueError, match=expected): check_tree(1.0)
def test_check_tree(modify_tree, expected): """Test that the various possible decision tree problems are identified.""" result = check_tree(modify_tree) assert result == expected