We have a provider who is requesting the percentiles for height/weight/head circumference come into the text translation of his pediatric visits. Has anyone happened to have tackled this coding yet?
{fn CalcPercentiles()
{
calculate_height_percentile()
calculate_weight_percentile()
calculate_headcirc_percentile()
calculate_bmi_percentile()
}
}
{fn CALCULATE_HEIGHT_PERCENTILE()
{
local htP
htP = str(GetHeightPercentile(GetAgeInMonths(PATIENT.DATEOFBIRTH), OBSNOW("HEIGHT (CM)")))
OBSNOW("HEIGHT %TILE", htP)
}
}
{fn GetHeightPercentile(ageInMonths, htCm)
{
local lmsArr
local p = ""
if ageInMonths<240 and size(htCm)<>0 then
lmsArr = GetLms("height_age", ageInMonths, TRUE, 0)
if size(lmsArr)<>0 then
p = GetPercentile(GetZScore(htCm, lmsArr[2], lmsArr[3], lmsArr[4]))
endif
endif
return p
}
}
{fn CALCULATE_WEIGHT_PERCENTILE()
{
local wtP
wtP=str(GetWeightPercentile(GetAgeInMonths(PATIENT.DATEOFBIRTH), OBSNOW("WEIGHT (KG)")))
OBSNOW("WEIGHT %TILE", wtP)
}
}
{fn GetWeightPercentile(ageInMonths, wtKg)
{
local lmsArr
local p = ""
if size(wtKg)<>0 then
lmsArr = GetLms("weight_age", ageInMonths, TRUE, 0)
if size(lmsArr)<>0 then
p = GetPercentile(GetZScore(wtKg, lmsArr[2], lmsArr[3], lmsArr[4]))
endif
endif
return p
}
}
{fn CALCULATE_HEADCIRC_PERCENTILE()
{
local hcP
hcP = str(GetHeadCircPercentile(GetAgeInMonths(PATIENT.DATEOFBIRTH), OBSNOW("HEADCIRC(CM)")))
OBSNOW("HC %tile", hcP)
}
}
{fn GetHeadCircPercentile(ageInMonths, hcCm)
{
local lmsArr
local p = ""
if ageInMonths<=36 and size(hcCm)<>0 then
lmsArr = GetLms("head_circ_36", ageInMonths, FALSE, 0)
if size(lmsArr)<>0 then
p = GetPercentile(GetZScore(hcCm, lmsArr[2], lmsArr[3], lmsArr[4]))
endif
endif
return p
}
}
{fn Calculate_BMI_Percentile()
{
local bmiP
local wtForHtP
local dob
local ageInMonths
local bmi
local htCm
local wtKg
htCm = OBSNOW("HEIGHT (CM)")
wtKg = OBSNOW("WEIGHT (KG)")
bmi = OBSNOW("BMI")
dob = PATIENT.DATEOFBIRTH
ageInMonths = GetAgeInMonths(dob)
bmiP = GetBMIPercentile(ageInMonths, bmi)
bmiP = str(bmiP)
OBSNOW("bmi%ile", bmiP)
wtForHtP = GetWeightForStaturePercentile(ageInMonths, wtKg, htCm)
wtForHtP = str(wtForHtP)
OBSNOW("wt lgth-ht%", wtForHtP)
}
}
{fn GetBMIPercentile(ageInMonths, bmi)
{
local lmsArr
local p = ""
local index = 0
if ageInMonths>=24 and ageInMonths<240 and size(bmi)<>0 then
if ageInMonths<60 then
index = 24
endif
lmsArr = GetLms("bmi_age", ageInMonths-index, TRUE, index)
if size(lmsArr)<>0 then
p = GetPercentile(GetZScore(bmi, lmsArr[2], lmsArr[3],lmsArr[4]))
endif
endif
return p
}
}
{fn GetWeightForStaturePercentile(ageInMonths, wtKg, htCm)
{
local lmsArr
local p = ""
local indexVal
if ageInMonths<=60 and size(wtKg)<>0 and size(htCm)<>0 then
if ageInMonths<24 and htCm>45 and htCm<103 then
indexVal = htCm-45-0
lmsArr = GetLms("weight_length_infant", indexVal, FALSE, 45)
if size(lmsArr)<>0 then
p = GetPercentile(GetZScore(wtKg, lmsArr[2], lmsArr[3], lmsArr[4]))
endif
else
if ageInMonths>=24 and htCm>77 and htCm<121 then
indexVal = htCm-77-0
lmsArr = GetLms("weight_length_child", indexVal, FALSE, 77)
if size(lmsArr)<>0 then
p = GetPercentile(GetZScore(wtKg, lmsArr[2], lmsArr[3], lmsArr[4]))
endif
endif
endif
endif
return p
}
}
{fn GetAgeInMonths(dob)
{
local ageArr
local ageInMos
ageArr = GetAgeArray(dob)
ageInMos = 12 * get(ageArr, 1) + 1 * get(ageArr, 2) + 1 / 31 * get(ageArr, 3)
return ageInMos
}
}
{fn GetAgeArray(dob)
{
local years
local months
local days
local today
local ageArr
today = str(DOCUMENT.CLINICALDATE)
dob = str(dob)
years = 0 + sub(today, 7, 4) - sub(dob, 7, 4)
months = 0 + sub(today, 1, 2) - sub(dob, 1, 2)
days = 0 + sub(today, 4, 2) - sub(dob, 4, 2)
if days<0 then
days = days + GetDaysInMonth(dob)
months = months - 1
endif
if months<0 then
months = months + 12
years = years - 1
endif
ageArr = array(years, months, days)
return ageArr
}
}
{fn GetDaysInMonth(date)
{
local day
local month
local year
local nextMo
date==str(date)
month = sub(date, 1, 2)
day = sub(date, 4, 2)
year = sub(date, 7, 4)
nextMo = ADDDATES(month + "/01/" + year, "0", "1", "0" )
nextMo = SUBTRACTDATES(str(nextMo), "0", "0", "1")
day = sub(str(nextMo), 4, 2)
day = 0 + day
return day
}
}
{fn GetLms(fileName, ageInMonths, addAgeToFileName, adjIndex)
{
local fileNameAge = 0
local rawLmsArr
local lmsArr
local index
local rawHi
local rawLo
if addAgeToFileName then
if ageInMonths<60 then
fileNameAge = 60
else
if ageInMonths<120 then
fileNameAge = 120
else
if ageInMonths<180 then
fileNameAge = 180
else
if ageInMonths<240 then
fileNameAge = 240
else
if ageInMonths>=240 then
return ""
endif
endif
endif
endif
endif
fileName = fileName + "_" + str(fileNameAge)
endif
lmsArr = GetRawLmsArray(fileName)
if size(lmsArr)==0 then
return ""
else
index = ageInMonths - fileNameAge + 1.5
if addAgeToFileName then
index = index + 60
endif
index = div(index, 1)
if size(lmsArr)<index then
return ""
else
rawLo = get(lmsArr,index)
if size(lmsArr)>index then
index = index + 1
endif
rawHi = get(lmsArr, index)
rawLo = getfield(rawLo, "|")
rawHi = getfield(rawHi, "|")
ageInMonths = ageInMonths + adjIndex
lmsArr = GetLMSArray(ageInMonths, rawLo, rawHi)
return lmsArr
endif
endif
}
}
{fn GetLMSArray(ptAge, lmsArrLow, lmsArrHigh )
{
local ageLow
local ageHigh
local L
local M
local S
local LMSArr
local fractAbove
ageLow = 1 * lmsArrLow[4]
ageHigh = 1 * lmsArrHigh[4]
if (ageHigh-ageLow)==0 then
fractAbove = 1
else
fractAbove = (ptAge-ageLow) / (ageHigh-ageLow)
endif
L = 1 * lmsArrLow[1] + (1 * lmsArrHigh[1] - 1 * lmsArrLow[1]) * fractAbove
M = 1 * lmsArrLow[2] + (1 * lmsArrHigh[2] - 1 * lmsArrLow[2]) * fractAbove
S = 1 * lmsArrLow[3] + (1 * lmsArrHigh[3] - 1 * lmsArrLow[3]) * fractAbove
LMSArr = array(ptAge, L, M, S)
return LMSArr
}
}
{fn GetRawLmsArray(fileName)
{
local path
local file
local text = ""
local lmsArr = ""
path = GetGrowthChartLmsDataPath() + fileName
file = FILEOPEN(path, "r")
if file<>NULL then
text = FILEREAD(file)
FILECLOSE(file)
if size(text)<>0 then
lmsArr = getfield(text, ",")
endif
else
USEROK("Unable to open file '" + path + "'")
endif
return lmsArr
}
}
{fn GetGrowthChartLmsDataPath()
{
local path
local backslashes = "\\\"
local backslash = sub(backslashes, 2, 1)
path = DIR_EMR() + backslash + "clinical content" + backslash
+ "peds_growth_data" + backslash + PATIENT.SEX + backslash
return path
}
}
{fn GetZScore(val, lVal, mVal, sVal)
{
local z
z = val / mVal
if lVal==0 then
lVal = 0.000000001
endif
z = z ^ lVal
z = z - 1
z = z / (1 * lVal * 1 * sVal)
return z
}
}
{fn GetPercentile(z)
{
local p10
local p9
local p4
local p3
local p2
local p1
local p
local pp
p10 = 1 + (0.33267 * AbsValue(z))
p9 = 1 / p10
p4 = (0.4361836 * p9) - (0.1201676 * (p9 ^ 2)) + (0.937298 * (p9 ^ 3))
p1= 2 * 3.14159265
p3 = (AbsValue(z) ^ 2)
p2 = -1 * p3 / 2
pp = 1 / (p1 ^ 0.5) * (2.71828182845905 ^ p2) * p4
p = 1 - pp
if z>0 then
p = p * 100
else
p = 100 - p * 100
endif
p = div((p + 0.5), 1)
return p
}
}
{fn AbsValue(value)
{
if value < 0 then
return -1 * value
else
return value
endif
}
}