@dra2020/dra-analytics 4.1.5 → 4.1.7
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.
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2021-
|
|
3
|
+
Copyright (c) 2021-2025 Social Good Fund for Dave's Redistricting.
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/dist/dra-analytics.js
CHANGED
|
@@ -345,6 +345,8 @@ function makeCompactnessScorecard(shapes, bLog = false) {
|
|
|
345
345
|
// For returning compactness by district to DRA
|
|
346
346
|
// Note, these use the Cartesian (flat earth) measurements
|
|
347
347
|
let byDistrict = [];
|
|
348
|
+
// Give hint to featureize cache about how many features we are working with
|
|
349
|
+
(0, features_1.featureizeCacheSize)(shapes.features.length);
|
|
348
350
|
for (let i = 0; i < shapes.features.length; i++) {
|
|
349
351
|
const f = shapes.features[i];
|
|
350
352
|
const features = (0, features_1.featureizePoly)(f, options, { bKIWYSIFeatures: bKIWYSIFeatures });
|
|
@@ -450,6 +452,7 @@ exports.calcBoundingBox = calcBoundingBox;
|
|
|
450
452
|
exports.calcPolsbyPopper = calcPolsbyPopper;
|
|
451
453
|
exports.calcConvexHullFeature = calcConvexHullFeature;
|
|
452
454
|
exports.calcSchwartzberg = calcSchwartzberg;
|
|
455
|
+
exports.featureizeCacheSize = featureizeCacheSize;
|
|
453
456
|
exports.featureizePoly = featureizePoly;
|
|
454
457
|
const baseclient_1 = __webpack_require__(/*! @dra2020/baseclient */ "@dra2020/baseclient");
|
|
455
458
|
// FEATURES (FOR AN ML MODEL)
|
|
@@ -615,8 +618,48 @@ function calcConvexHullFeature(area, chArea) {
|
|
|
615
618
|
function calcSchwartzberg(area, perimeter) {
|
|
616
619
|
return perimeter / ((2 * Math.PI) * Math.sqrt(area / Math.PI));
|
|
617
620
|
}
|
|
618
|
-
|
|
621
|
+
class featureizeCache {
|
|
622
|
+
constructor() {
|
|
623
|
+
this.map = new Map();
|
|
624
|
+
this.size = 200;
|
|
625
|
+
this.lru = 0;
|
|
626
|
+
}
|
|
627
|
+
setSize(size) {
|
|
628
|
+
this.size = Math.max(this.size, size);
|
|
629
|
+
}
|
|
630
|
+
find(hash) {
|
|
631
|
+
let e = this.map.get(hash);
|
|
632
|
+
if (e) {
|
|
633
|
+
e.lru = this.lru++; // refresh LRU value to most current
|
|
634
|
+
//console.log(`featureize: saved ${e.ms} milliseconds`);
|
|
635
|
+
return e.features;
|
|
636
|
+
}
|
|
637
|
+
return undefined;
|
|
638
|
+
}
|
|
639
|
+
add(hash, features, ms) {
|
|
640
|
+
this.cull();
|
|
641
|
+
this.map.set(hash, { lru: this.lru++, ms, features });
|
|
642
|
+
}
|
|
643
|
+
cull() {
|
|
644
|
+
// Let grow to twice size before culling since we do sort by lru to cull and don't want to do too often
|
|
645
|
+
if (this.map.size > 2 * this.size) {
|
|
646
|
+
let keys = Array.from(this.map.entries())
|
|
647
|
+
.sort((a, b) => a[1].lru - b[1].lru)
|
|
648
|
+
.map((a) => a[0])
|
|
649
|
+
.slice(0, this.map.size - this.size);
|
|
650
|
+
keys.forEach(key => this.map.delete(key));
|
|
651
|
+
//console.log(`featureizeCache:cull: culled to ${this.map.size}`);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
let cache = new featureizeCache();
|
|
656
|
+
function featureizeCacheSize(size) { cache.setSize(size); }
|
|
619
657
|
function featureizePoly(poly, options, { bKIWYSIFeatures = true } = {}) {
|
|
658
|
+
let h = baseclient_1.Poly.polyHash32(poly);
|
|
659
|
+
let r = cache.find(h);
|
|
660
|
+
if (r)
|
|
661
|
+
return r;
|
|
662
|
+
let e = new baseclient_1.Util.Elapsed();
|
|
620
663
|
if (options === undefined)
|
|
621
664
|
options = baseclient_1.Poly.DefaultOptions;
|
|
622
665
|
const area = baseclient_1.Poly.polyArea(poly);
|
|
@@ -642,6 +685,7 @@ function featureizePoly(poly, options, { bKIWYSIFeatures = true } = {}) {
|
|
|
642
685
|
reockFlat: calcReock(areaFlat, diameterFlat),
|
|
643
686
|
polsbyFlat: calcPolsbyPopper(areaFlat, perimeterFlat)
|
|
644
687
|
};
|
|
688
|
+
cache.add(h, result, e.ms());
|
|
645
689
|
return result;
|
|
646
690
|
}
|
|
647
691
|
|