Skip to content

Commit

Permalink
test: inprove converage (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarmy committed Oct 27, 2023
1 parent 808d384 commit ccf731e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 8 deletions.
1 change: 1 addition & 0 deletions cyeva/core/wind.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
1 change: 1 addition & 0 deletions tests/case/wind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
8 changes: 8 additions & 0 deletions tests/case/wind/wind_directions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@
{"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},
{"dir1": 1, "dir2": 7, "result": 2},
{"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 = [
Expand Down
1 change: 1 addition & 0 deletions tests/case/wind/wind_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
48 changes: 42 additions & 6 deletions tests/test_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
== """<Object:TemperatureComparison(degC)>
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]"""
)
60 changes: 58 additions & 2 deletions tests/test_wind.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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__()
== """<Object:WindComparison>
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]"""
)

0 comments on commit ccf731e

Please sign in to comment.