Skip to content

Commit

Permalink
Merge pull request #120 from TwinGan/release_ver.1.0
Browse files Browse the repository at this point in the history
Convert old unit test to url type
  • Loading branch information
FedericoTartarini authored Sep 19, 2024
2 parents 4353993 + 2944fcc commit 42134be
Show file tree
Hide file tree
Showing 25 changed files with 673 additions and 1,133 deletions.
185 changes: 170 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,186 @@

unit_test_data_prefix = "https://raw.githubusercontent.com/TwinGan/validation-data-comfort-models/release_v1.0/"
test_adaptive_en_url = unit_test_data_prefix + "ts_adaptive_en.json"
test_adaptive_ashrae_url = unit_test_data_prefix + "ts_adaptive_ashrae.json"
test_a_pmv_url = unit_test_data_prefix + "ts_a_pmv.json"
test_two_nodes_url = unit_test_data_prefix + "ts_two_nodes.json"
test_solar_gain_url = unit_test_data_prefix + "ts_solar_gain.json"
test_ankle_draft_url = unit_test_data_prefix + "ts_ankle_draft.json"
test_phs_url = unit_test_data_prefix + "ts_phs.json"
test_e_pmv_url = unit_test_data_prefix + "ts_e_pmv.json"
test_at_url = unit_test_data_prefix + "ts_at.json"
test_athb_url = unit_test_data_prefix + "ts_athb.json"
test_clo_tout_url = unit_test_data_prefix + "ts_clo_tout.json"
test_cooling_effect_url = unit_test_data_prefix + "ts_cooling_effect.json"
test_vertical_tmp_grad_ppd_url = unit_test_data_prefix + "ts_vertical_tmp_grad_ppd.json"
test_wbgt_url = unit_test_data_prefix + "ts_wbgt.json"
test_heat_index_url = unit_test_data_prefix + "ts_heat_index.json"
test_net_url = unit_test_data_prefix + "ts_net.json"
test_pmv_pdd_url = unit_test_data_prefix + "ts_pmv_pdd.json"
test_pmv_url = unit_test_data_prefix + "ts_pmv.json"
test_set_url = unit_test_data_prefix + "ts_set.json"
test_humidex_url = unit_test_data_prefix + "ts_humidex.json"
test_use_fans_heatwaves_url = unit_test_data_prefix + "ts_use_fans_heatwaves.json"
test_utci_url = unit_test_data_prefix + "ts_utci.json"
test_wind_chill_url = unit_test_data_prefix + "ts_wind_chill.json"
test_pet_steady_url = unit_test_data_prefix + "ts_pet_steady.json"
test_discomfort_index_url = unit_test_data_prefix + "ts_discomfort_index.json"


def retrieve_data(url):
try:
resp = requests.get(url)
reference_tables = json.loads(resp.text)
return reference_tables
except requests.RequestException as e:
print(f"Error fetching data from {url}: {e}")
@pytest.fixture
def retrieve_data():
def _retrieve_data(url):
try:
response = requests.get(url)
if response.status_code == 200:
return json.loads(response.text)
else:
response.raise_for_status()
except requests.RequestException as e:
print(f"Error fetching data from {url}: {e}")
return None

return _retrieve_data


# Custom equal method
# Json null equal to np.nan
@pytest.fixture
def is_equal():
def compare(a, b):
if (a is None and np.isnan(b)) or (b is None and np.isnan(a)):
if isinstance(a, np.ndarray):
if not isinstance(b, np.ndarray):
b = np.array(b, dtype=a.dtype)
if a.dtype.kind in "UOS": # U = unicode, O = objects, S = string
return np.array_equal(a, b)
else:
b = np.where(b == None, np.nan, b) # Replace None with np.nan
# Return True if arrays are close enough, including handling of NaN values
return np.allclose(a, b, equal_nan=True)
elif (a is None and np.isnan(b)) or (b is None and np.isnan(a)):
return True
return a == b
else:
return a == b

return compare


# get test data for adaptove_en()
@pytest.fixture
def get_adaptive_en_test_data():
return retrieve_data(test_adaptive_en_url)
def get_adaptive_en_url():
return test_adaptive_en_url


@pytest.fixture
def get_adaptive_ashrae_url():
return test_adaptive_ashrae_url


@pytest.fixture
def get_a_pmv_url():
return test_a_pmv_url


@pytest.fixture
def get_two_nodes_url():
return test_two_nodes_url


@pytest.fixture
def get_solar_gain_url():
return test_solar_gain_url


@pytest.fixture
def get_ankle_draft_url():
return test_ankle_draft_url


@pytest.fixture
def get_phs_url():
return test_phs_url


@pytest.fixture
def get_vertical_tmp_grad_ppd_url():
return test_vertical_tmp_grad_ppd_url


@pytest.fixture
def get_humidex_url():
return test_humidex_url


@pytest.fixture
def get_wbgt_url():
return test_wbgt_url


@pytest.fixture
def get_e_pmv_url():
return test_e_pmv_url


@pytest.fixture
def get_at_url():
return test_at_url


@pytest.fixture
def get_athb_url():
return test_athb_url


@pytest.fixture
def get_clo_tout_url():
return test_clo_tout_url


@pytest.fixture
def get_cooling_effect_url():
return test_cooling_effect_url


@pytest.fixture
def get_heat_index_url():
return test_heat_index_url


@pytest.fixture
def get_net_url():
return test_net_url


@pytest.fixture
def get_pmv_pdd_url():
return test_pmv_pdd_url


@pytest.fixture
def get_pmv_url():
return test_pmv_url


@pytest.fixture
def get_set_url():
return test_set_url


@pytest.fixture
def get_use_fans_heatwaves_url():
return test_use_fans_heatwaves_url


@pytest.fixture
def get_utci_url():
return test_utci_url


@pytest.fixture
def get_wind_chill_url():
return test_wind_chill_url


@pytest.fixture
def get_pet_steady_url():
return test_pet_steady_url

@pytest.fixture
def get_discomfort_index_url():
return test_discomfort_index_url
28 changes: 21 additions & 7 deletions tests/test_a_pmv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import numpy as np

from pythermalcomfort.models import a_pmv


def test_a_pmv():
np.testing.assert_equal(
a_pmv([24, 30], 30, vr=0.22, rh=50, met=1.4, clo=0.5, a_coefficient=0.293),
[0.48, 1.09],
)
def test_a_pmv(get_a_pmv_url, retrieve_data, is_equal):
reference_table = retrieve_data(get_a_pmv_url)
for entry in reference_table["data"]:
inputs = entry["inputs"]
outputs = entry["outputs"]
result = a_pmv(
inputs["tdb"],
inputs["tr"],
inputs["vr"],
inputs["rh"],
inputs["met"],
inputs["clo"],
inputs["a_coefficient"],
)
try:
assert is_equal(result, outputs["a_pmv"])
except AssertionError as e:
print(
f"Assertion failed for a_pmv. Expected {outputs}, got {result}, inputs={inputs}\nError: {str(e)}"
)
raise
122 changes: 18 additions & 104 deletions tests/test_adaptive_ashrae.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,21 @@
from pythermalcomfort.models import adaptive_ashrae


def test_adaptive_ashrae():
data_test_adaptive_ashrae = (
[ # I have commented the lines of code that don't pass the test
{
"tdb": 19.6,
"tr": 19.6,
"t_running_mean": 17,
"v": 0.1,
"return": {"acceptability_80": True},
},
{
"tdb": 19.6,
"tr": 19.6,
"t_running_mean": 17,
"v": 0.1,
"return": {"acceptability_90": False},
},
{
"tdb": 19.6,
"tr": 19.6,
"t_running_mean": 25,
"v": 0.1,
"return": {"acceptability_80": False},
},
{
"tdb": 19.6,
"tr": 19.6,
"t_running_mean": 25,
"v": 0.1,
"return": {"acceptability_80": False},
},
{
"tdb": 26,
"tr": 26,
"t_running_mean": 16,
"v": 0.1,
"return": {"acceptability_80": True},
},
{
"tdb": 26,
"tr": 26,
"t_running_mean": 16,
"v": 0.1,
"return": {"acceptability_90": False},
},
{
"tdb": 30,
"tr": 26,
"t_running_mean": 16,
"v": 0.1,
"return": {"acceptability_80": False},
},
{
"tdb": 25,
"tr": 25,
"t_running_mean": 23,
"v": 0.1,
"return": {"acceptability_80": True},
},
{
"tdb": 25,
"tr": 25,
"t_running_mean": 23,
"v": 0.1,
"return": {"acceptability_90": True},
},
]
)
for row in data_test_adaptive_ashrae:
assert (
adaptive_ashrae(row["tdb"], row["tr"], row["t_running_mean"], row["v"])[
list(row["return"].keys())[0]
]
) == row["return"][list(row["return"].keys())[0]]

assert (adaptive_ashrae(77, 77, 68, 0.3, units="ip")["tmp_cmf"]) == 75.2

# test limit_inputs and array input
np.testing.assert_equal(
adaptive_ashrae(tdb=25, tr=25, t_running_mean=[9, 10], v=0.1).__dict__,
{
"tmp_cmf": [np.nan, 20.9],
"tmp_cmf_80_low": [np.nan, 17.4],
"tmp_cmf_80_up": [np.nan, 24.4],
"tmp_cmf_90_low": [np.nan, 18.4],
"tmp_cmf_90_up": [np.nan, 23.4],
"acceptability_80": [False, False],
"acceptability_90": [False, False],
},
)
np.testing.assert_equal(
adaptive_ashrae(
tdb=[77, 74], tr=77, t_running_mean=[48, 68], v=0.3, units="ip"
).__dict__,
{
"tmp_cmf": [np.nan, 75.2],
"tmp_cmf_80_low": [np.nan, 68.9],
"tmp_cmf_80_up": [np.nan, 81.5],
"tmp_cmf_90_low": [np.nan, 70.7],
"tmp_cmf_90_up": [np.nan, 79.7],
"acceptability_80": [False, True],
"acceptability_90": [False, True],
},
)
def test_adaptive_ashrae(get_adaptive_ashrae_url, retrieve_data, is_equal):
reference_table = retrieve_data(get_adaptive_ashrae_url)
for entry in reference_table["data"]:
inputs = entry["inputs"]
outputs = entry["outputs"]
units = inputs.get("units", "SI")
result = adaptive_ashrae(
inputs["tdb"], inputs["tr"], inputs["t_running_mean"], inputs["v"], units
)
for key in outputs:
# Use the custom is_equal for other types
try:
assert is_equal(result[key], outputs[key])
except AssertionError as e:
print(
f"Assertion failed for {key}. Expected {outputs[key]}, got {result[key]}, inputs={inputs}\nError: {str(e)}"
)
raise
27 changes: 12 additions & 15 deletions tests/test_adaptive_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
from pythermalcomfort.models import adaptive_en


def test_adaptive_en(get_adaptive_en_test_data, is_equal):
for entry in get_adaptive_en_test_data["data"]:
def test_adaptive_en(get_adaptive_en_url, retrieve_data, is_equal):
reference_table = retrieve_data(get_adaptive_en_url)
for entry in reference_table["data"]:
inputs = entry["inputs"]
outputs = entry["outputs"]
result = adaptive_en(
inputs["tdb"], inputs["tr"], inputs["t_running_mean"], inputs["v"]
)

assert is_equal(result["tmp_cmf"], outputs["tmp_cmf"])
assert is_equal(result["acceptability_cat_i"], outputs["acceptability_cat_i"])
assert is_equal(result["acceptability_cat_ii"], outputs["acceptability_cat_ii"])
assert is_equal(
result["acceptability_cat_iii"], outputs["acceptability_cat_iii"]
)
assert is_equal(result["tmp_cmf_cat_i_up"], outputs["tmp_cmf_cat_i_up"])
assert is_equal(result["tmp_cmf_cat_ii_up"], outputs["tmp_cmf_cat_ii_up"])
assert is_equal(result["tmp_cmf_cat_iii_up"], outputs["tmp_cmf_cat_iii_up"])
assert is_equal(result["tmp_cmf_cat_i_low"], outputs["tmp_cmf_cat_i_low"])
assert is_equal(result["tmp_cmf_cat_ii_low"], outputs["tmp_cmf_cat_ii_low"])
assert is_equal(result["tmp_cmf_cat_iii_low"], outputs["tmp_cmf_cat_iii_low"])
for key in outputs:
# Use the custom is_equal for other types
try:
assert is_equal(result[key], outputs[key])
except AssertionError as e:
print(
f"Assertion failed for {key}. Expected {outputs[key]}, got {result[key]}, inputs={inputs}\nError: {str(e)}"
)
raise
Loading

0 comments on commit 42134be

Please sign in to comment.