chore(growth): apply scorer behaviour in indicators

This commit is contained in:
João Paulo Dubas 2024-06-11 11:41:31 +00:00
parent fa022b9592
commit 88b8e811a8
Signed by: joao.dubas
SSH Key Fingerprint: SHA256:V1mixgOGRc/YMhGx/DNkOSmJxgA2vHNrDZEk3wt/kOA
7 changed files with 86 additions and 61 deletions

View File

@ -2,4 +2,17 @@ defmodule Growth.Score.ArmCircumference do
@moduledoc """
Calculate z-score for arm circumference for age.
"""
@behaviour Growth.Score.Scorer
alias Growth.Score.Scorer
@impl Scorer
@spec measure_name() :: atom()
@doc """
Name of the measurement used in arm circumference indicator.
"""
def measure_name do
:arm_circumference
end
end

View File

@ -3,69 +3,16 @@ defmodule Growth.Score.BMI do
Calculate z-score for body mass index for age.
"""
alias Growth.Calc.Centile
alias Growth.Calc.ZScore
@behaviour Growth.Score.Scorer
@spec result(Growth.t()) :: Growth.t()
alias Growth.Score.Scorer
@impl Scorer
@spec measure_name() :: atom()
@doc """
Calculate z-score and centile values and add them to the growth measurement results.
Name of the measurement used in BMI indicator.
"""
def result(%Growth{bmi: nil} = measure) do
measure
end
def result(%Growth{results: results} = measure) do
result =
measure
|> lms()
|> Enum.map(fn {precision, {l, m, s}} ->
{precision, {z_score(measure.bmi, l, m, s), centile(measure.bmi, l, m, s)}}
end)
%{measure | results: [%{bmi: result} | results]}
end
@spec lms(Growth.t()) :: [{String.t(), {number(), number(), number()}}]
@doc """
Get the fitted values of Box-Cox:
* power (`l`)
* median (`m`)
* coefficient of variation (`s`)
"""
def lms(%Growth{} = measure) do
[
{measure.gender, "day", measure.age_in_days},
{measure.gender, "week", measure.age_in_weeks},
{measure.gender, "month", measure.age_in_months}
]
|> Enum.map(fn {_, precision, _} = key ->
case :ets.lookup(__MODULE__, key) do
[{^key, %{l: l, m: m, s: s}} | _] ->
{precision, {l, m, s}}
_ ->
nil
end
end)
|> Enum.reject(&is_nil/1)
end
@spec z_score(number(), number(), number(), number()) :: number()
@doc """
Check `Growth.Calc.ZScore.compute/4`.
"""
def z_score(value, l, m, s) do
ZScore.compute(value, l, m, s)
end
@spec centile(number(), number(), number(), number()) :: number() | :na
@doc """
Check `Growth.Calc.Centile.compute/4`.
"""
def centile(value, l, m, s) do
Centile.compute(value, l, m, s)
def measure_name do
:bmi
end
end

View File

@ -2,4 +2,17 @@ defmodule Growth.Score.HeadCircumference do
@moduledoc """
Calculate z-score for head circumference for age.
"""
@behaviour Growth.Score.Scorer
alias Growth.Score.Scorer
@impl Scorer
@spec measure_name() :: atom()
@doc """
Name of the measurement used in head circumference indicator.
"""
def measure_name do
:head_circumference
end
end

View File

@ -2,4 +2,17 @@ defmodule Growth.Score.Height do
@moduledoc """
Calculate z-score for height for age.
"""
@behaviour Growth.Score.Scorer
alias Growth.Score.Scorer
@impl Scorer
@spec measure_name() :: atom()
@doc """
Name of the measurement used in height indicator.
"""
def measure_name do
:height
end
end

View File

@ -2,4 +2,17 @@ defmodule Growth.Score.SubscapularSkinfold do
@moduledoc """
Calculate z-score for subscapular skinfold for age.
"""
@behaviour Growth.Score.Scorer
alias Growth.Score.Scorer
@impl Scorer
@spec measure_name() :: atom()
@doc """
Name of the measurement used in subscapular skinfold indicator.
"""
def measure_name do
:subscapular_circumference
end
end

View File

@ -2,4 +2,17 @@ defmodule Growth.Score.TricepsSkinfold do
@moduledoc """
Calculate z-score for triceps skinfold for age.
"""
@behaviour Growth.Score.Scorer
alias Growth.Score.Scorer
@impl Scorer
@spec measure_name() :: atom()
@doc """
Name of the measurement used in triceps skinfold indicator.
"""
def measure_name do
:triceps_skinfold
end
end

View File

@ -2,4 +2,17 @@ defmodule Growth.Score.Weight do
@moduledoc """
Calculate z-score for weight for age.
"""
@behaviour Growth.Score.Scorer
alias Growth.Score.Scorer
@impl Scorer
@spec measure_name() :: atom()
@doc """
Name of the measurement used in weight indicator.
"""
def measure_name do
:weight
end
end