From ccf731e295f557a754be690e4c12ffacf7c7ab57 Mon Sep 17 00:00:00 2001 From: Wentao Li Date: Thu, 7 Jul 2022 15:31:29 +0800 Subject: [PATCH] test: inprove converage (#23) --- cyeva/core/wind.py | 1 + tests/case/wind/__init__.py | 1 + tests/case/wind/wind_directions.py | 8 ++++ tests/case/wind/wind_speed.py | 1 + tests/test_temp.py | 48 +++++++++++++++++++++--- tests/test_wind.py | 60 +++++++++++++++++++++++++++++- 6 files changed, 111 insertions(+), 8 deletions(-) diff --git a/cyeva/core/wind.py b/cyeva/core/wind.py index 72464c0..efca850 100644 --- a/cyeva/core/wind.py +++ b/cyeva/core/wind.py @@ -174,6 +174,7 @@ def get_least_lev_diff( return np.abs(lev1 - lev2) +@convert_to_ndarray def get_least_dir_deflection( dir1: Union[int, np.ndarray], dir2: Union[int, np.ndarray], circle_num: int = 8 ) -> Union[int, np.ndarray]: diff --git a/tests/case/wind/__init__.py b/tests/case/wind/__init__.py index c18f064..3b471bc 100644 --- a/tests/case/wind/__init__.py +++ b/tests/case/wind/__init__.py @@ -4,6 +4,7 @@ LEAST_ANGLE_DEFLECTION_CASE, LEAST_DIR_DEFLECTION_CASE, IDENTIFY_DIRECTION8_CASE, + IDENTIFY_DIRECTION16_CASE, WIND_DIR_SCORE_CASE, WIND_DIR_ACCURACY_RATE, ) diff --git a/tests/case/wind/wind_directions.py b/tests/case/wind/wind_directions.py index 733d590..57eb468 100644 --- a/tests/case/wind/wind_directions.py +++ b/tests/case/wind/wind_directions.py @@ -13,6 +13,13 @@ {"angle": 350, "result": 0}, ] +IDENTIFY_DIRECTION16_CASE = [ + {"angle": 22, "result": 1}, + {"angle": 23, "result": 1}, + {"angle": 91, "result": 4}, + {"angle": 350, "result": 0}, +] + LEAST_DIR_DEFLECTION_CASE = [ {"dir1": 0, "dir2": 1, "result": 1}, {"dir1": 0, "dir2": 7, "result": 1}, @@ -20,6 +27,7 @@ {"dir1": 2, "dir2": 7, "result": 3}, {"dir1": 2, "dir2": 6, "result": 4}, {"dir1": 3, "dir2": 6, "result": 3}, + {"dir1": [3, 2], "dir2": [6, 6], "result": np.array([3, 4])}, ] WIND_DIR_SCORE_CASE = [ diff --git a/tests/case/wind/wind_speed.py b/tests/case/wind/wind_speed.py index 5f9c863..cb4a20d 100644 --- a/tests/case/wind/wind_speed.py +++ b/tests/case/wind/wind_speed.py @@ -4,6 +4,7 @@ {"wspd": 0.1, "result": 0}, {"wspd": 8.1, "result": 5}, {"wspd": 38, "result": 13}, + {"wspd": [38, 8.1], "result": np.array([13, 5])}, ] IDENTIFY_SPEED_LEVEL_MAPPING_CASE = [ diff --git a/tests/test_temp.py b/tests/test_temp.py index 07ef121..0a6133f 100644 --- a/tests/test_temp.py +++ b/tests/test_temp.py @@ -107,9 +107,45 @@ def test_calc_temp_linregress(): def test_gather_all_factors(): - for _, cases in ACCURACY_RATE_CASE.items(): - for case in cases: - obs = case["obs"] - fcst = case["fct"] - - TemperatureComparison(obs, fcst) + np.random.seed(0) + + obs = np.sin(np.arange(100)) * 20 + np.random.random(100) * 5 * np.random.choice( + [1, -1] + ) + fcst = obs + np.random.random(100) * 5 * np.random.choice([1, -1]) + + temp = TemperatureComparison(obs, fcst) + temp.gather_all_factors() + + +def test_temp_obj(): + np.random.seed(0) + + obs = np.sin(np.arange(100)) * 20 + np.random.random(100) * 5 * np.random.choice( + [1, -1] + ) + fcst = obs + np.random.random(100) * 5 * np.random.choice([1, -1]) + + temp = TemperatureComparison(obs, fcst) + temp + print(temp) + assert ( + temp.__str__() + == temp.__repr__() + == """ +data: + observation forecast +0 -2.744068 -5.592160 +1 13.253473 9.734786 +2 15.172132 13.729749 +3 0.097984 -2.068456 +4 -17.254324 -21.034857 +.. ... ... +95 12.749277 7.898094 +96 16.739190 13.336467 +97 7.491617 7.065139 +98 -15.612338 -15.894429 +99 -20.007614 -22.446803 + +[100 rows x 2 columns]""" + ) diff --git a/tests/test_wind.py b/tests/test_wind.py index a234796..65a8903 100644 --- a/tests/test_wind.py +++ b/tests/test_wind.py @@ -1,3 +1,8 @@ +from numbers import Number +from typing import List + +import numpy as np + from cyeva.core.wind import ( get_least_angle_deflection, identify_direction, @@ -12,6 +17,7 @@ LEAST_ANGLE_DEFLECTION_CASE, LEAST_DIR_DEFLECTION_CASE, IDENTIFY_DIRECTION8_CASE, + IDENTIFY_DIRECTION16_CASE, WIND_DIR_SCORE_CASE, WIND_DIR_ACCURACY_RATE, IDENTIFY_SPEED_LEVEL_GENERAL_CASE, @@ -42,7 +48,11 @@ def test_get_least_dir_deflection(): dir1 = case["dir1"] dir2 = case["dir2"] result = case["result"] - assert get_least_dir_deflection(dir1, dir2) == result + _result = get_least_dir_deflection(dir1, dir2) + if isinstance(result, Number): + assert _result == result + elif isinstance(result, List) or isinstance(result, np.ndarray): + assert (_result == result).all() def test_identify_direction8(): @@ -52,11 +62,23 @@ def test_identify_direction8(): assert identify_direction(angle, dnum=8) == result +def test_identify_direction16(): + for case in IDENTIFY_DIRECTION16_CASE: + angle = case["angle"] + result = case["result"] + _result = identify_direction(angle, dnum=16) + assert _result == result + + def test_identify_wind_scale(): for case in IDENTIFY_SPEED_LEVEL_GENERAL_CASE: wspd = case["wspd"] result = case["result"] - assert identify_wind_scale(wspd) == result + _result = identify_wind_scale(wspd) + if isinstance(result, Number): + assert _result == result + elif isinstance(result, List) or isinstance(result, np.ndarray): + assert (_result == result).all() def test_calc_wind_dir_score(): @@ -202,3 +224,37 @@ def test_filter_wind_scales(): _result = filter_wind_scales(obs, fct, **parameters) for i in range(len(result)): assert (_result[i] == result[i]).all() + + +def test_wind_obj(): + np.random.seed(0) + + obs_spd = np.random.random(100) * 10 + obs_dir = np.random.random(100) * 360 + fct_spd = np.random.random(100) * 10 + fct_dir = np.random.random(100) * 360 + + wind = WindComparison(obs_spd, fct_spd, obs_dir, fct_dir) + wind + print(wind) + + assert ( + wind.__str__() + == wind.__repr__() + == """ +data: + observation_speed ... forecast_direction +0 5.488135 ... 326.359980 +1 7.151894 ... 278.657040 +2 6.027634 ... 119.932255 +3 5.448832 ... 29.196500 +4 4.236548 ... 146.606822 +.. ... ... ... +95 1.831914 ... 345.233780 +96 5.865129 ... 127.932785 +97 0.201075 ... 128.414481 +98 8.289400 ... 5.878261 +99 0.046955 ... 66.683637 + +[100 rows x 4 columns]""" + )