From 282ec4b2777a3759ff779da7a58d54ec68880a40 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik Date: Wed, 18 Apr 2018 16:38:40 +0200 Subject: [PATCH] do not normalize datasets with too less data --- src/helpers.js | 2 +- test/helpers_spec.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/helpers.js b/src/helpers.js index de1b756..e8d97eb 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -9,7 +9,7 @@ export function weightFunc (d, dmax, degree) { export function normalize (referenceArr) { const cutoff = Math.ceil(0.1 * referenceArr.length) const trimmed_arr = sort(referenceArr).slice(cutoff, referenceArr.length - cutoff) - const sd = math.std(trimmed_arr) + const sd = trimmed_arr.length > 3 ? math.std(trimmed_arr) : 1 return function (outputArr) { return outputArr.map(val => val / sd) } diff --git a/test/helpers_spec.js b/test/helpers_spec.js index ede64c9..fa7e477 100644 --- a/test/helpers_spec.js +++ b/test/helpers_spec.js @@ -44,10 +44,20 @@ describe('function normalize', function () { expect: [44.499, 3.266, 2.858, 2.449, 2.041, 1.633, 1.225, 0.816, 0.408, -40.825] } + const caseTwo = { + test: [109, 8, 7], + expect: [109, 8, 7] + } + it('should return array divided by 10% trimmed sample deviation', function () { const normalizedArr = normalize(caseOne.test)(caseOne.test) expect(math.round(normalizedArr, 3)).to.eql(caseOne.expect) }) + + it('should return same array if sample count is too small (lt 4)', function () { + const normalizedArr = normalize(caseTwo.test)(caseTwo.test) + expect(normalizedArr).to.eql(caseTwo.expect) + }) }) describe('function euclideanDist', function () {