@dra2020/dra-analytics 4.1.6 → 4.1.8

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.
@@ -7,6 +7,7 @@ export declare function calcBoundingBox(poly: any): number;
7
7
  export declare function calcPolsbyPopper(area: number, perimeter: number): number;
8
8
  export declare function calcConvexHullFeature(area: number, chArea: number): number;
9
9
  export declare function calcSchwartzberg(area: number, perimeter: number): number;
10
+ export declare function featureizeCacheSize(size: number): void;
10
11
  export declare function featureizePoly(poly: any, options?: Poly.PolyOptions, { bKIWYSIFeatures }?: {
11
12
  bKIWYSIFeatures?: boolean;
12
13
  }): T.CompactnessFeatures;
@@ -5,7 +5,7 @@
5
5
  import * as GeoJSON from 'geojson';
6
6
  import {Poly} from '@dra2020/baseclient';
7
7
 
8
- import {featureizePoly} from './features';
8
+ import {featureizePoly, featureizeCacheSize} from './features';
9
9
  import {scoreFeatureSet} from './kiwysi';
10
10
  import * as T from '../types/all';
11
11
  import {ratePolsby, rateReock} from '../rate/dra-ratings';
@@ -52,6 +52,9 @@ export function makeCompactnessScorecard(shapes: GeoJSON.FeatureCollection, bLog
52
52
  // Note, these use the Cartesian (flat earth) measurements
53
53
  let byDistrict: T.Compactness[] = [];
54
54
 
55
+ // Give hint to featureize cache about how many features we are working with
56
+ featureizeCacheSize(shapes.features.length);
57
+
55
58
  for (let i = 0; i < shapes.features.length; i++)
56
59
  {
57
60
  const f: any = shapes.features[i];
@@ -229,9 +229,74 @@ export function calcSchwartzberg(area: number, perimeter: number): number
229
229
 
230
230
 
231
231
  // CALCULATE THE 7 COMPACTNESS "FEATURES" FOR A POLYGON FOR THE KIWYSI COMPACTNESS MODEL
232
+ interface CacheEntry
233
+ {
234
+ lru: number,
235
+ ms: number,
236
+ features: T.CompactnessFeatures,
237
+ }
238
+
239
+ class featureizeCache
240
+ {
241
+ map: Map<any, CacheEntry>;
242
+ size: number;
243
+ lru: number;
244
+
245
+ constructor()
246
+ {
247
+ this.map = new Map<any, CacheEntry>();
248
+ this.size = 200;
249
+ this.lru = 0;
250
+ }
251
+
252
+ setSize(size: number): void
253
+ {
254
+ this.size = Math.max(this.size, size);
255
+ }
256
+
257
+ find(hash: number): T.CompactnessFeatures|undefined
258
+ {
259
+ let e = this.map.get(hash);
260
+ if (e)
261
+ {
262
+ e.lru = this.lru++; // refresh LRU value to most current
263
+ //console.log(`featureize: saved ${e.ms} milliseconds`);
264
+ return e.features;
265
+ }
266
+ return undefined;
267
+ }
268
+
269
+ add(hash: number, features: T.CompactnessFeatures, ms: number): void
270
+ {
271
+ this.cull();
272
+ this.map.set(hash, { lru: this.lru++, ms, features });
273
+ }
274
+
275
+ cull(): void
276
+ {
277
+ // Let grow to twice size before culling since we do sort by lru to cull and don't want to do too often
278
+ if (this.map.size > 2*this.size)
279
+ {
280
+ let keys = Array.from(this.map.entries())
281
+ .sort((a: [any, CacheEntry], b: [any, CacheEntry]) => a[1].lru - b[1].lru)
282
+ .map((a: [any, CacheEntry]) => a[0])
283
+ .slice(0, this.map.size - this.size);
284
+ keys.forEach(key => this.map.delete(key));
285
+ //console.log(`featureizeCache:cull: culled to ${this.map.size}`);
286
+ }
287
+ }
288
+ }
289
+
290
+ let cache = new featureizeCache();
291
+ export function featureizeCacheSize(size: number): void { cache.setSize(size) }
232
292
 
233
293
  export function featureizePoly(poly: any, options?: Poly.PolyOptions, {bKIWYSIFeatures = true}: {bKIWYSIFeatures?: boolean} = {}): T.CompactnessFeatures
234
294
  {
295
+ let h = Poly.polyHash32(poly);
296
+ let r = cache.find(h);
297
+ if (r) return r;
298
+ let e = new Util.Elapsed();
299
+
235
300
  if (options === undefined) options = Poly.DefaultOptions;
236
301
 
237
302
  const area: number = Poly.polyArea(poly);
@@ -262,6 +327,7 @@ export function featureizePoly(poly: any, options?: Poly.PolyOptions, {bKIWYSIFe
262
327
  reockFlat: calcReock(areaFlat, diameterFlat),
263
328
  polsbyFlat: calcPolsbyPopper(areaFlat, perimeterFlat)
264
329
  };
330
+ cache.add(h, result, e.ms());
265
331
 
266
332
  return result;
267
333
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/dra-analytics",
3
- "version": "4.1.6",
3
+ "version": "4.1.8",
4
4
  "description": "DRA analytics",
5
5
  "main": "dist/dra-analytics.js",
6
6
  "types": "./dist/lib/all/all.d.ts",
@@ -37,16 +37,16 @@
37
37
  "@types/node": "^20.11.30",
38
38
  "@types/yargs": "^11.1.8",
39
39
  "csv-parse": "^4.16.3",
40
+ "elliptic": "^6.6.1",
40
41
  "jest": "^29.5.0",
41
42
  "json-loader": "^0.5.7",
43
+ "pbkdf2": "^3.1.3",
42
44
  "prettier": "^2.8.8",
43
45
  "shapefile": "^0.6.6",
44
46
  "source-map-loader": "^5.0.0",
45
47
  "ts-jest": "^29.1.0",
46
48
  "ts-loader": "^9.5.2",
47
49
  "tsify": "^5.0.4",
48
- "elliptic": "^6.6.1",
49
- "pbkdf2": "^3.1.3",
50
50
  "tslint": "^6.1.3",
51
51
  "tslint-config-prettier": "^1.18.0",
52
52
  "typescript": "^5.8.3",
@@ -54,7 +54,7 @@
54
54
  "webpack-cli": "^6.0.1"
55
55
  },
56
56
  "dependencies": {
57
- "@dra2020/baseclient": "^1.0.154",
57
+ "@dra2020/baseclient": "^1.0.166",
58
58
  "cjs": "^0.0.11",
59
59
  "geojson": "^0.5.0",
60
60
  "yargs": "^12.0.5"