Last active
December 13, 2015 18:38
-
-
Save Skwerl/4956591 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD); | |
$data_error = false; | |
/* /////// PATIENT VARS ///////////////////////////////////////////////////////////////////////// */ | |
$patient_dob = '10/08/05'; | |
$patient_sex = 'male'; | |
$patient_weight = '60'; | |
$patient_height = '4,0'; | |
$date_measured = '2/13/13'; | |
/* ////////////////////////////////////////////////////////////////////////////////////////////// */ | |
$age_in_months = ((strtotime($date_measured)-strtotime($patient_dob))/(3600*24))/30.4375; | |
$height = explode(',',$patient_height); $height_inches = ($height[0]*12)+$height[1]; | |
$bmi = ($patient_weight/(pow($height_inches,2)))*703.069006; | |
$bmi_category = 'Unknown'; | |
if ($age_in_months >= 222) { | |
$bmi_category = get_category_by_bmi($bmi); | |
} else { | |
// CDC chart uses ages from 23.5 months to 240.5 months, in 1 month increments: | |
$lookup_ages = array(); for ($i = 23; $i <=240; $i++) { $lookup_ages[] = $i+0.5; } | |
$closest_index = nearest_value($age_in_months,$lookup_ages); | |
if ((strtolower($patient_sex)=='m') || (strtolower($patient_sex)=='male')) { $select_columns = 'MALE'; } | |
else { $select_columns = 'FEMALE'; } | |
$sth = $db->prepare("SELECT * FROM CDC_BMI WHERE AGE = ?"); | |
$sth->execute(array($closest_index)); | |
$result = $sth->fetch(PDO::FETCH_ASSOC); | |
$l_index = $result[$select_columns.'_L']; | |
$m_index = $result[$select_columns.'_M']; | |
$s_index = $result[$select_columns.'_S']; | |
$z_score = (pow(($bmi/$m_index),$l_index)-1)/($l_index*$s_index); | |
$percentile = intval(normal_dist($z_score)*1000)/10; | |
$lower_sd = ($m_index-($m_index*pow((1+$l_index*$s_index*(-2)),(1/$l_index))))/2; | |
$upper_sd = ($m_index*pow((1+$l_index*$s_index*2),(1/$l_index))-$m_index)/2; | |
if (((($bmi-$m_index)/$lower_sd) < -4) || ((($bmi-$m_index)/$upper_sd) > 8) || (($height_inches*2.54) < 20) || (($height_inches*2.54) > 250) || (($patient_weight*0.453592) < 0.5) || (($patient_weight*0.453592) > 400)) { | |
$data_error = true; | |
} else { | |
$bmi_category = get_category_by_percentile($percentile); | |
} | |
} | |
echo 'Patient status is '.$bmi_category.'.'; | |
/* ////////////////////////////////////////////////////////////////////////////////////////////// */ | |
function get_category_by_bmi($bmi) { | |
$results = array( | |
'Obese' => 30, | |
'Overweight' => 25, | |
'Normal' => 18.5, | |
'Underweight' => 0 | |
); | |
foreach ($results as $category => $threshold) { | |
if ($bmi >= $threshold) { | |
return $category; | |
} | |
} | |
} | |
function get_category_by_percentile($percentile) { | |
$results = array( | |
'Obese' => 95, | |
'Overweight' => 85, | |
'Normal' => 5, | |
'Underweight' => 0 | |
); | |
foreach ($results as $category => $threshold) { | |
if ($percentile >= $threshold) { | |
return $category; | |
} | |
} | |
} | |
function nearest_value($value, $array) { | |
if (array_search($value, $array)) { | |
return $value; | |
} else { | |
$array[] = $value; | |
sort($array); | |
$key = array_search($value, $array); | |
if ($key == 0) { return $array[$key+1]; } | |
if ($key == sizeof($array)-1) { return $array[$key-1]; } | |
$dist_to_ceil = $array[$key+1]-$value; | |
$dist_to_floor = $array[$key-1]-$value; | |
if ($dist_to_ceil <= $dist_to_floor) { | |
return $array[$key+1]; | |
} else { | |
return $array[$key-1]; | |
} | |
} | |
} | |
function normal_dist($standard_deviation) { | |
$p = floatval(0.2316419); | |
$b1 = floatval(0.319381530); | |
$b2 = floatval(-0.356563782); | |
$b3 = floatval(1.781477937); | |
$b4 = floatval(-1.821255978); | |
$b5 = floatval(1.330274429); | |
$t = 1/(1+($p*floatval($standard_deviation))); | |
$zx = (1/(sqrt(2*pi()))*(exp(0-pow($standard_deviation,2)/2))); | |
$px = 1-floatval($zx)*(($b1*$t)+($b2*pow($t,2))+($b3*pow($t,3))+($b4*pow($t,4))+($b5*pow($t,5))); | |
return $px; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment