Skip to content

Commit

Permalink
avniproject/avni-client#1217 - fixed logic...added more scenarios fro…
Browse files Browse the repository at this point in the history
…m the excel
  • Loading branch information
petmongrels committed Dec 15, 2023
1 parent 1350a45 commit f7e1049
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/health_modules/child/zScoreCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const getHfaReference = (gender, ageInMonths) => {

function calc_sd(reference, sd) {
const {L, M, S} = reference;
return M * ((1 + L * S * sd) ^ (1 / L));
return M * Math.pow(1 + (L * S * sd), (1 / L));
}

function roundToOneDecimal(value) {
Expand All @@ -58,7 +58,7 @@ const calculate = (value, reference) => {
const sd3pos = calc_sd(reference, 3);
const sd3neg = calc_sd(reference, -3);
const sd23pos = sd3pos - calc_sd(reference, 2);
const sd23neg = calc_sd(reference, 2) - sd3neg;
const sd23neg = calc_sd(reference, -2) - sd3neg;

let zScore = roundToOneDecimal((Math.pow(value / reference.M, reference.L) - 1) / (reference.S * reference.L));

Expand Down
41 changes: 16 additions & 25 deletions src/test/child/zScoreCalculatorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,31 @@ describe("zScoreCalculator", () => {
});

it("calculates weight for age z-scores for boys and girls between 0 and 5", () => {
let zScores, today = new Date();
const today = new Date();
individual.dateOfBirth = moment(today).subtract(1, 'month');

zScores = zScore(individual, today, 1.2, 54.7);
assert.equal(zScores.wfa, -6.7);
// check for numbers outside -4 and 4
assert.equal(zScore(individual, today, 1.2).wfa, -6.8);
assert.equal(zScore(individual, today, 4.5).wfa, 0);
assert.equal(zScore(individual, today, 5.1).wfa, 1);

zScores = zScore(individual, today, 4.5, 54.7);
assert.equal(zScores.wfa, 0);
individual.dateOfBirth = moment(today).subtract(26, 'month');
assert.equal(zScore(individual, today, 7.0).wfa, -4.7);

zScores = zScore(individual, today, 5.1, 54.7);
assert.equal(zScores.wfa, 1);
individual.dateOfBirth = moment(today).subtract(26, 'month');
assert.equal(zScore(individual, today, 6.3).wfa, -5.4);

individual.gender = female;

//Notice this calculation may not exactly match the zscores as provided in the reference
zScores = zScore(individual, today, 2.7, 54.7);
assert.equal(zScores.wfa, -3.1);
individual.dateOfBirth = moment(today).subtract(1, 'month');
assert.equal(zScore(individual, today, 2.7).wfa, -3.1);

//Notice this calculation may not exactly match the zscores as provided in the reference
zScores = zScore(individual, today, 3.2, 54.7);
assert.equal(zScores.wfa, -1.9);

zScores = zScore(individual, today, 4.2, 54.7);
assert.equal(zScores.wfa, 0);

zScores = zScore(individual, today, 4.8, 54.7);
assert.equal(zScores.wfa, 1);

zScores = zScore(individual, today, 5.5, 54.7);
assert.equal(zScores.wfa, 2);

zScores = zScore(individual, today, 6.2, 54.7);
assert.equal(zScores.wfa, 3);

assert.equal(zScore(individual, today, 3.2).wfa, -1.9);
assert.equal(zScore(individual, today, 4.2).wfa, 0);
assert.equal(zScore(individual, today, 4.8).wfa, 1);
assert.equal(zScore(individual, today, 5.5).wfa, 2);
assert.equal(zScore(individual, today, 6.2).wfa, 3);

//2 year old girl
individual.dateOfBirth = moment(today).subtract(24, 'month');
Expand Down

0 comments on commit f7e1049

Please sign in to comment.