@dra2020/district-analytics 2.0.1 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -429,8 +429,8 @@ class Districts {
429
429
  });
430
430
  // COMPUTE DERIVED VALUES
431
431
  // Population deviation % and equal population (boolean) by district.
432
- // Don't set the values for the dummy unassigned district.
433
- let popDevPct = null;
432
+ // Default the value for the dummy unassigned district to 0%.
433
+ let popDevPct = 0 / targetSize;
434
434
  if (i > 0) {
435
435
  popDevPct = (totalPop - targetSize) / targetSize;
436
436
  bEqualPop = (popDevPct <= deviationThreshold);
@@ -1447,17 +1447,22 @@ const U = __importStar(__webpack_require__(/*! ./utils */ "./src/utils.ts"));
1447
1447
  const D = __importStar(__webpack_require__(/*! ./_data */ "./src/_data.ts"));
1448
1448
  function doPopulationDeviation(s, bLog = false) {
1449
1449
  let test = s.getTest(4 /* PopulationDeviation */);
1450
- // Compute the min, max, and average district populations,
1451
- // excluding the dummy 'unassigned' 0 and N+1 summary "districts."
1450
+ let targetSize = s.state.totalPop / s.state.nDistricts;
1451
+ // Compute the min & max district populations
1452
+ // ... excluding the dummy the 'unassigned' 0 and N+1 summary "districts"
1452
1453
  let totPopByDistrict = s.districts.statistics[D.DistrictField.TotalPop];
1453
1454
  totPopByDistrict = totPopByDistrict.slice(1, -1);
1454
- let min = U.minArray(totPopByDistrict);
1455
- let max = U.maxArray(totPopByDistrict);
1456
- let total = U.sumArray(totPopByDistrict);
1457
- // Calculate the raw population deviation.
1458
- // The target size is the average population.
1459
- let avg = total / s.state.nDistricts;
1460
- let popDev = (max - min) / avg;
1455
+ // Remove empty districts
1456
+ totPopByDistrict = totPopByDistrict.filter(x => x > 0);
1457
+ let min = 0;
1458
+ let max = 0;
1459
+ // If there's more than 1 non-empty district, calculate a non-zero deviation
1460
+ if (totPopByDistrict.length > 1) {
1461
+ min = U.minArray(totPopByDistrict);
1462
+ max = U.maxArray(totPopByDistrict);
1463
+ }
1464
+ // Calculate the raw population deviation
1465
+ let popDev = (max - min) / targetSize;
1461
1466
  // Round the raw value to the desired level of precision
1462
1467
  popDev = U.trim(popDev);
1463
1468
  // Populate the test entry
@@ -1467,7 +1472,7 @@ function doPopulationDeviation(s, bLog = false) {
1467
1472
  let totalPop = s.districts.statistics[D.DistrictField.TotalPop];
1468
1473
  let popDevPct = s.districts.statistics[D.DistrictField.PopDevPct];
1469
1474
  let summaryRow = s.districts.numberOfRows() - 1;
1470
- totalPop[summaryRow] = avg; // aka "target size"
1475
+ totalPop[summaryRow] = targetSize;
1471
1476
  popDevPct[summaryRow] = popDev;
1472
1477
  return test;
1473
1478
  }
@@ -2455,8 +2460,10 @@ const testDefns = {
2455
2460
  // Raw numeric analytics, such as population deviation, compactness, etc. are
2456
2461
  // normalized as part of creating a scorecard, so the code to normalize results
2457
2462
  // is encapsulated here.
2458
- // Configure scale parameters for normalizing each raw test result
2459
- // This needs to be separate from the scorecard configuration info above,
2463
+ // Configure scale parameters for normalizing each raw test result.
2464
+ // Scales consist of a minimum & a maximum *raw* value. If the values get
2465
+ // inverted (to make bigger better), these will switch.
2466
+ // This process needs to be separate from the test configuration info above,
2460
2467
  // because some scales need access to the analytics session object.
2461
2468
  function doConfigureScales(s) {
2462
2469
  // Scale defn for PopulationDeviation