Skip to content

Commit 9fbd70d

Browse files
author
Tom Pearson
authored
Bug fix/deal with indicators over 9 (#22)
* proper checks for indicator length based on elements rather than string length
1 parent 3c33220 commit 9fbd70d

File tree

6 files changed

+225
-19
lines changed

6 files changed

+225
-19
lines changed

data/inclusiveinternet/2022/entities.csv

+101
Large diffs are not rendered by default.

data/inclusiveinternet/2022/indicators.csv

+103
Large diffs are not rendered by default.

index.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ import {csvParse} from 'd3';
22
import fs from 'fs';
33
import indexCore from './src/index-core.js';
44

5-
const simpleRootDir = 'data/simple-index-set';
5+
const iiiRoot = 'data/inclusiveinternet/2022';
66

7-
const simpleIndicators = csvParse(fs.readFileSync(`${simpleRootDir}/indicators.csv`, 'utf-8'));
8-
const simpleEntities = csvParse(fs.readFileSync(`${simpleRootDir}/entities.csv`, 'utf-8'));
7+
const iiiIndicators = csvParse(fs.readFileSync(`${iiiRoot}/indicators.csv`, 'utf-8'));
8+
const iiiEntities = csvParse(fs.readFileSync(`${iiiRoot}/entities.csv`, 'utf-8'));
99

10-
const simpleIndex = indexCore(simpleIndicators, simpleEntities, 100 ,true, true);
11-
const simpleIndexUnrestricted = indexCore(simpleIndicators, simpleEntities);
10+
const iii = indexCore(iiiIndicators, iiiEntities, 100 ,true, true);
11+
// const iiiComparison = indexCore(iiiIndicators, iiiEntities, 100, false);
1212

1313
//console.log( simpleIndex.getEntity('Chinatown') )
14-
console.log( simpleIndex.getEntity('Chinatown').value, 'vs', simpleIndexUnrestricted.getEntity('Chinatown').value );
15-
console.log( simpleIndex.getEntity('Chinatown')['1'], 'vs', simpleIndexUnrestricted.getEntity('Chinatown')['1'] )
16-
console.log( simpleIndex.getEntity('Chinatown')['1.4'], 'vs', simpleIndexUnrestricted.getEntity('Chinatown')['1.4'] )
14+
// console.log( simpleIndex.getEntity('Chinatown').value, 'vs', simpleIndexUnrestricted.getEntity('Chinatown').value );
15+
// console.log( simpleIndex.getEntity('Chinatown')['1'], 'vs', simpleIndexUnrestricted.getEntity('Chinatown')['1'] )
16+
// console.log( simpleIndex.getEntity('Chinatown')['1.4'], 'vs', simpleIndexUnrestricted.getEntity('Chinatown')['1.4'] )
1717

18+
console.log('i', iii.getEntityIndicator('Algeria', '1.1'));
19+
console.log('i', iii.getEntityIndicator('Algeria', '1'));

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@economist/index-core",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "",
55
"main": "src/index-core.js",
66
"type": "module",

src/index-core.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function indexCore(
7070

7171
// format an indicator for passing to the weighted mean function
7272
function formatIndicator(indicator, entity, max) {
73-
const diverging = !!indicator.diverging;
73+
const diverging = (indicator.diverging === true || String(indicator.diverging).toLocaleLowerCase() === 'true');
7474
let value = entity.user && entity.user[indicator.id]
7575
? Number(entity.user[indicator.id])
7676
: Number(entity[indicator.id]);
@@ -87,7 +87,7 @@ function indexCore(
8787
if (indicator.max) {
8888
range = [0, indicator.max];
8989
if (indicator.min) {
90-
range = [0, Math.max(Math.abs(indicator.min), indicator.max)];
90+
range = [0, Math.max(Math.abs(indicator.min), Math.abs(indicator.max))];
9191
}
9292
} else {
9393
range = [0, max];
@@ -108,22 +108,23 @@ function indexCore(
108108

109109
function indexEntity(entity, calculationList, overwrite = allowOverwrite) {
110110
const newEntity = clone(entity);
111-
calculationList.forEach((indicatorID) => {
112-
if ((newEntity[indicatorID] && overwrite === true) || !newEntity[indicatorID]) {
111+
calculationList.forEach((parentIndicatorID) => {
112+
if ((newEntity[parentIndicatorID] && overwrite === true) || !newEntity[parentIndicatorID]) {
113113
// get the required component indicators to calculate the parent value
114114
// this is a bit brittle maybe?
115115

116116
const componentIndicators = indicatorsData
117-
.filter((indicator) => (indicator.id.indexOf(indicatorID) === 0
118-
&& indicator.id.length === indicatorID.length + 2))
117+
.filter((indicator) => (
118+
indicator.id.indexOf(parentIndicatorID) === 0 // the
119+
&& indicator.id.split('.').length === parentIndicatorID.split('.').length + 1))
119120
.filter((indicator) => excludeIndicator(indicator) === false)
120121
.map((indicator) => formatIndicator(indicator, newEntity, indexMax));
121122
// calculate the weighted mean of the component indicators on the newEntity
122123
// assign that value to the newEntity
123-
newEntity[indicatorID] = calculateWeightedMean(componentIndicators, indexMax, clamp);
124+
newEntity[parentIndicatorID] = calculateWeightedMean(componentIndicators, indexMax, clamp);
124125
} else {
125-
console.warn(`retaining existing value for ${newEntity.name} - ${indicatorID} : ${Number(entity[indicatorID])}`);
126-
newEntity[indicatorID] = Number(entity[indicatorID]);
126+
console.warn(`retaining existing value for ${newEntity.name} - ${parentIndicatorID} : ${Number(entity[parentIndicatorID])}`);
127+
newEntity[parentIndicatorID] = Number(entity[parentIndicatorID]);
127128
}
128129
});
129130

src/utils.js

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export function calculateWeightedMean(weightedValues, normaliseTo = 100, clamp =
2323
const weightedValue = indicator.invert
2424
? ((normaliseTo - normalisedValue) * indicator.weight)
2525
: (normalisedValue * indicator.weight);
26-
2726
weightedSum += weightedValue;
2827
cumulativeWeight += indicator.weight;
2928
}

0 commit comments

Comments
 (0)