Skip to content

Commit

Permalink
Adds cdc tests for height/weight. 1819 passing, 1 fail out by 0.001
Browse files Browse the repository at this point in the history
eatyourpeas committed Sep 26, 2024
1 parent 201cdec commit bb7a1d2
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions rcpchgrowth/tests/cdc/height_weight_validation.json

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions rcpchgrowth/tests/test_cdc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# standard imports
from datetime import datetime
import json
import os
from pprint import pprint

# third-party imports
import pytest

# rcpch imports
from rcpchgrowth import Measurement

# the ACCURACY constant defines the accuracy of the test comparisons
# owing to variations in statistical calculations it's impossible to get exact
# agreement between R and Python, so our statistician feels we can set a tolerance
# within which we will accept a result as correct.

ACCURACY = 1e-3


def load_valid_data_set():
"""
Loads in the testing data from JSON file
"""
with open(os.path.abspath(os.path.dirname(__file__)) + "/cdc/height_weight_validation.json") as f:
return json.load(f)


@pytest.mark.parametrize("line", load_valid_data_set())
def test_measurement_class_cdc_data(line):
"""
Test which iterates through a JSON file of 4000 fictional children and known calculated (TC via R)
correct SDS values. Compares the output of the Measurement.measurement method with the known
correct value.
"""

measurement_object = Measurement(
sex=str(line["sex"]),
birth_date=datetime.strptime(line["birth_date"], "%d/%m/%Y"),
observation_date=datetime.strptime(
line["observation_date"], "%d/%m/%Y"),
measurement_method=str(line["measurement_method"]),
observation_value=float(line["observation_value"]),
gestation_weeks=40,
gestation_days=0,
reference="cdc"
)

pprint(vars(measurement_object))
pprint(line)

# all comparisons using absolute tolerance (not relative)

# this conditional guards against failure of pytest.approx with NoneTypes
# if line["corrected_sds"] is None:
# assert measurement_object.measurement[
# "measurement_calculated_values"]['corrected_sds'] is None
# else:
# assert measurement_object.measurement[
# "measurement_calculated_values"]['corrected_sds'] == pytest.approx(
# line["corrected_sds"], abs=ACCURACY)

# this conditional guards against failure of pytest.approx with NoneTypes
if line["z_score"] is None:
assert measurement_object.measurement[
"measurement_calculated_values"]['chronological_sds'] is None
else:
assert measurement_object.measurement[
"measurement_calculated_values"]['chronological_sds'] == pytest.approx(
line["z_score"], abs=ACCURACY)

0 comments on commit bb7a1d2

Please sign in to comment.