Skip to content

Commit

Permalink
add midparental centiles
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourpeas committed Oct 1, 2021
1 parent aed7f4a commit 32a64aa
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 20 deletions.
63 changes: 60 additions & 3 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "RCPCH Digital Growth API",
"description": "Returns SDS and centiles for child growth measurements using growth references. Currently provides calculations based on the UK-WHO, Turner's Syndrome and Trisomy-21 references.",
"version": "3.5.0"
"version": "4.0.0"
},
"paths": {
"/uk-who/calculation": {
Expand Down Expand Up @@ -438,7 +438,7 @@
"utilities"
],
"summary": "Mid Parental Height Endpoint",
"description": "## Mid-parental-height Endpoint\n\n* Calculates mid-parental-height",
"description": "## Mid-parental-height Endpoint\n\n* Calculates mid-parental-height\n* Returns mid-parental centile and SDS, as well as centile lines for mid-parental height\n* and +2 SD and -SD",
"operationId": "mid_parental_height_endpoint_utilities_mid_parental_height_post",
"requestBody": {
"content": {
Expand All @@ -455,7 +455,9 @@
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
"schema": {
"$ref": "#/components/schemas/MidParentalHeightResponse"
}
}
}
},
Expand Down Expand Up @@ -1401,6 +1403,61 @@
}
}
},
"MidParentalHeightResponse": {
"title": "MidParentalHeightResponse",
"required": [
"mid_parental_height",
"mid_parental_height_sds",
"mid_parental_height_centile",
"mid_parental_height_centile_data",
"mid_parental_height_lower_centile_data",
"mid_parental_height_upper_centile_data"
],
"type": "object",
"properties": {
"mid_parental_height": {
"title": "Mid Parental Height",
"type": "number"
},
"mid_parental_height_sds": {
"title": "Mid Parental Height Sds",
"type": "number"
},
"mid_parental_height_centile": {
"title": "Mid Parental Height Centile",
"type": "number"
},
"mid_parental_height_centile_data": {
"title": "Mid Parental Height Centile Data",
"type": "array",
"items": {
"$ref": "#/components/schemas/ReferenceCreate"
}
},
"mid_parental_height_lower_centile_data": {
"title": "Mid Parental Height Lower Centile Data",
"type": "array",
"items": {
"$ref": "#/components/schemas/ReferenceCreate"
}
},
"mid_parental_height_upper_centile_data": {
"title": "Mid Parental Height Upper Centile Data",
"type": "array",
"items": {
"$ref": "#/components/schemas/ReferenceCreate"
}
},
"mid_parental_height_upper_value": {
"title": "Mid Parental Height Upper Value",
"type": "number"
},
"mid_parental_height_lower_value": {
"title": "Mid Parental Height Lower Value",
"type": "number"
}
}
},
"PlottableData": {
"title": "PlottableData",
"required": [
Expand Down
4 changes: 2 additions & 2 deletions routers/trisomy21.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
# Third party imports
from fastapi import APIRouter, Body, HTTPException
from typing import List
from rcpchgrowth import Measurement, constants, generate_fictional_child_data, create_chart, generate_custom_centile
from rcpchgrowth import Measurement, constants, generate_fictional_child_data, create_chart
from rcpchgrowth.constants.reference_constants import TRISOMY_21

# local imports
from schemas import MeasurementRequest, ChartCoordinateRequest, FictionalChildRequest, CustomCentileRequest
from schemas import MeasurementRequest, ChartCoordinateRequest, FictionalChildRequest

# set up the API router
trisomy_21 = APIRouter(
Expand Down
4 changes: 2 additions & 2 deletions routers/turner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from schemas.response_schema_classes import Centile_Data, MeasurementObject

# RCPCH imports
from rcpchgrowth import Measurement, constants, generate_fictional_child_data, create_chart, generate_custom_centile
from rcpchgrowth import Measurement, constants, generate_fictional_child_data, create_chart
from rcpchgrowth.constants.reference_constants import TURNERS
from schemas import MeasurementRequest, ChartCoordinateRequest, FictionalChildRequest, CustomCentileRequest
from schemas import MeasurementRequest, ChartCoordinateRequest, FictionalChildRequest

# set up the API router
turners = APIRouter(
Expand Down
1 change: 0 additions & 1 deletion routers/ukwho.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from schemas.request_validation_classes import CustomCentileRequest

# RCPCH imports
from rcpchgrowth.chart_functions import generate_custom_centile
from rcpchgrowth import Measurement, constants, generate_fictional_child_data, create_chart
from rcpchgrowth.constants.reference_constants import UK_WHO
from schemas import MeasurementRequest, ChartCoordinateRequest, FictionalChildRequest
Expand Down
73 changes: 70 additions & 3 deletions routers/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@

# RCPCH imports
from rcpchgrowth import mid_parental_height, sds_for_measurement, constants, centile
from schemas import MidParentalHeightRequest
from rcpchgrowth.constants.reference_constants import UK_WHO
from rcpchgrowth.global_functions import measurement_from_sds
from rcpchgrowth.chart_functions import create_chart
from schemas import MidParentalHeightRequest, MidParentalHeightResponse

# set up the API router
utilities = APIRouter(
prefix="/utilities",
)


@utilities.post('/mid-parental-height', tags=['utilities'])
@utilities.post('/mid-parental-height', tags=['utilities'], response_model=MidParentalHeightResponse)
def mid_parental_height_endpoint(mid_parental_height_request: MidParentalHeightRequest):
"""
## Mid-parental-height Endpoint
* Calculates mid-parental-height
* Returns mid-parental centile and SDS, as well as centile lines for mid-parental height
* and +2 SD and -SD
"""
height = mid_parental_height(mid_parental_height_request.height_paternal,
mid_parental_height_request.height_maternal,
Expand All @@ -32,6 +37,11 @@ def mid_parental_height_endpoint(mid_parental_height_request: MidParentalHeightR
"""
mph_sds = None
mph_centile = None
upper_height = None
lower_height = None
mph_centile_data = None
mph_lower_centile_data = None
mph_upper_centile_data = None
try:
mph_sds = sds_for_measurement(
reference=constants.UK_WHO,
Expand All @@ -40,16 +50,73 @@ def mid_parental_height_endpoint(mid_parental_height_request: MidParentalHeightR
observation_value=height,
sex=mid_parental_height_request.sex
)

except Exception:
print("It was not possible to calculate midparental SDS.")

try:
mph_centile = centile(mph_sds)
except:
print("It was not possible to calculate a centile from midparental height.")

try:
mph_centile_data = create_chart(
reference=UK_WHO,
centile_format=[mph_centile],
measurement_method=constants.HEIGHT,
sex=mid_parental_height_request.sex
)
except Exception as e:
print(e)

try:
lower_centile = centile(mph_sds - 2)
mph_lower_centile_data = create_chart(
reference=UK_WHO,
centile_format=[lower_centile],
measurement_method=constants.HEIGHT,
sex=mid_parental_height_request.sex
)

except Exception as e:
print(e)

try:
upper_centile = centile(mph_sds + 2)
mph_upper_centile_data = create_chart(
reference=UK_WHO,
centile_format=[upper_centile],
measurement_method=constants.HEIGHT,
sex=mid_parental_height_request.sex
)
except Exception as e:
print(e)

try:
upper_height = measurement_from_sds(
reference=constants.UK_WHO,
age=20,
sex=mid_parental_height_request.sex,
measurement_method=constants.HEIGHT,
requested_sds=mph_sds + 2
)
lower_height = measurement_from_sds(
reference=constants.UK_WHO,
age=20,
sex=mid_parental_height_request.sex,
measurement_method=constants.HEIGHT,
requested_sds=mph_sds - 2
)
except Exception as e:
print(e)

return {
"mid_parental_height": height,
"mid_parental_height_sds": mph_sds,
"mid_parental_height_centile": mph_centile
"mid_parental_height_centile": mph_centile,
"mid_parental_height_centile_data": mph_centile_data,
"mid_parental_height_lower_centile_data": mph_lower_centile_data,
"mid_parental_height_upper_centile_data": mph_upper_centile_data,
"mid_parental_height_lower_value": lower_height,
"mid_parental_height_upper_value": upper_height
}
4 changes: 4 additions & 0 deletions schemas/request_validation_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def custom_centiles_must_not_exceed_fifteen(cls, v):
raise ValueError("Centile formats cannot exceed 15 items.")
if (type(v) is list and len(v) < 1):
raise ValueError("Empty list. Please provide at least one value or one of the standard collection flags.")
if(type(v) is list):
for cent in v:
if cent < 0:
raise ValueError("Centile values cannot be negative.")
return v

class FictionalChildRequest(BaseModel):
Expand Down
18 changes: 11 additions & 7 deletions schemas/response_schema_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ class MeasurementObject(BaseModel):
plottable_data: PlottableData
bone_age: BoneAge
events_data: EventsData

class MidParentalHeight(BaseModel):
mid_parental_height: float
mid_parental_height_sds: float
mid_parental_height_centile: float

class Data(BaseModel):
l: str
x: float
Expand All @@ -140,4 +134,14 @@ class ReferenceCreate(BaseModel):
__root__: Dict[str, Sex]

class Centile_Data(BaseModel):
centile_data: List[ReferenceCreate]
centile_data: List[ReferenceCreate]

class MidParentalHeightResponse(BaseModel):
mid_parental_height: float
mid_parental_height_sds: float
mid_parental_height_centile: float
mid_parental_height_centile_data: List[ReferenceCreate]
mid_parental_height_lower_centile_data: List[ReferenceCreate]
mid_parental_height_upper_centile_data: List[ReferenceCreate]
mid_parental_height_upper_value: Optional[float]
mid_parental_height_lower_value: Optional[float]
61 changes: 59 additions & 2 deletions tests/test_data/test_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
"utilities"
],
"summary": "Mid Parental Height Endpoint",
"description": "## Mid-parental-height Endpoint\n\n* Calculates mid-parental-height",
"description": "## Mid-parental-height Endpoint\n\n* Calculates mid-parental-height\n* Returns mid-parental centile and SDS, as well as centile lines for mid-parental height\n* and +2 SD and -SD",
"operationId": "mid_parental_height_endpoint_utilities_mid_parental_height_post",
"requestBody": {
"content": {
Expand All @@ -455,7 +455,9 @@
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
"schema": {
"$ref": "#/components/schemas/MidParentalHeightResponse"
}
}
}
},
Expand Down Expand Up @@ -1401,6 +1403,61 @@
}
}
},
"MidParentalHeightResponse": {
"title": "MidParentalHeightResponse",
"required": [
"mid_parental_height",
"mid_parental_height_sds",
"mid_parental_height_centile",
"mid_parental_height_centile_data",
"mid_parental_height_lower_centile_data",
"mid_parental_height_upper_centile_data"
],
"type": "object",
"properties": {
"mid_parental_height": {
"title": "Mid Parental Height",
"type": "number"
},
"mid_parental_height_sds": {
"title": "Mid Parental Height Sds",
"type": "number"
},
"mid_parental_height_centile": {
"title": "Mid Parental Height Centile",
"type": "number"
},
"mid_parental_height_centile_data": {
"title": "Mid Parental Height Centile Data",
"type": "array",
"items": {
"$ref": "#/components/schemas/ReferenceCreate"
}
},
"mid_parental_height_lower_centile_data": {
"title": "Mid Parental Height Lower Centile Data",
"type": "array",
"items": {
"$ref": "#/components/schemas/ReferenceCreate"
}
},
"mid_parental_height_upper_centile_data": {
"title": "Mid Parental Height Upper Centile Data",
"type": "array",
"items": {
"$ref": "#/components/schemas/ReferenceCreate"
}
},
"mid_parental_height_upper_value": {
"title": "Mid Parental Height Upper Value",
"type": "number"
},
"mid_parental_height_lower_value": {
"title": "Mid Parental Height Lower Value",
"type": "number"
}
}
},
"PlottableData": {
"title": "PlottableData",
"required": [
Expand Down

0 comments on commit 32a64aa

Please sign in to comment.