def test_parser_speed(string, n): start_time = time.time() for i in range(n): parse(string) elapsed_time = time.time() - start_time print( f"\nelapsed time: {elapsed_time}; time per parse: {elapsed_time / n}")
def is_rp5_streamflow_ensemble_percentile(p, cell_methods): cell_methods = parse_if_str(cell_methods) return ( len(cell_methods) == 4 and is_rp5_streamflow_climatology_single_model(cell_methods[0:3]) and cell_methods[4] == parse(f"models: percentile[{p}]")[0] )
def check_final_cell_method(cell_methods, target_method, default_to_mean=True): """Determines whether the final method in a cell methods string (corresponding to a statistical aggregation) matches the target. If default_to_mean is true, treats errors and unrecognized methods as though they are climatological means. This compensates for some noisy cell_methods attributes in our data, all of which are climatological means. """ parsed = parse(cell_methods) if target_method == "mean" and default_to_mean: # determine means by process of elimination nonmeans = [m for m in VALID_CELL_METHODS_PARAMETERS if m != "mean"] return parsed is None or parsed[-1].method.name not in nonmeans elif parsed is not None: return parsed[-1].method.name == target_method else: # unparsable cell methods string return False
def test_is_conventional(cell_method_str, expected): cell_methods = parse(cell_method_str) assert is_conventional(cell_methods) is expected
def test_single(cell_method_str, conventional, extended): cell_methods = parse(cell_method_str) assert is_conventional_1(cell_methods[0]) is conventional assert is_extended_1(cell_methods[0]) is extended
def parse_if_str(cell_methods): return parse(cell_methods) if isinstance(cell_methods, str) \ else cell_methods