@dra2020/dra-analytics 3.3.5 → 4.1.0

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.
@@ -1,2 +1,2 @@
1
- export declare type Vector = number[];
1
+ export type Vector = number[];
2
2
  export declare function dotProduct(a: Vector, b: Vector): number;
@@ -1,4 +1,4 @@
1
1
  import { PopulationScorecard } from '../types/all';
2
2
  export declare function calcPopulationDeviation(max: number, min: number, targetSize: number): number;
3
3
  export declare function isRoughlyEqual(devation: number, bLegislative: boolean): boolean;
4
- export declare function makePopulationScorecard(totPopByDistrict: number[], targetSize: number, bLegislative: boolean, bLog?: boolean): PopulationScorecard;
4
+ export declare function makePopulationScorecard(totPopByDistrict: number[], targetSize: number, bLegislative: boolean, repsByDistrict?: number[], bLog?: boolean): PopulationScorecard;
@@ -11,7 +11,7 @@ import { CompactnessScorecard } from './compactness';
11
11
  import { SplittingScorecard } from './splitting';
12
12
  import { PopulationScorecard } from './population';
13
13
  import { Dict } from './general';
14
- export declare type Scorecard = {
14
+ export type Scorecard = {
15
15
  partisan: PartisanScorecard;
16
16
  minority: MinorityScorecard;
17
17
  compactness: CompactnessScorecard;
@@ -1,6 +1,6 @@
1
1
  import * as T from './general';
2
- export declare type arrPoint = [number, number];
3
- export declare type CompactnessFeatures = {
2
+ export type arrPoint = [number, number];
3
+ export type CompactnessFeatures = {
4
4
  sym_x: number;
5
5
  sym_y: number;
6
6
  reock: number;
@@ -11,7 +11,7 @@ export declare type CompactnessFeatures = {
11
11
  reockFlat: number;
12
12
  polsbyFlat: number;
13
13
  };
14
- export declare type FeaturesEntry = {
14
+ export type FeaturesEntry = {
15
15
  n: number;
16
16
  features: CompactnessFeatures;
17
17
  score: number;
@@ -20,12 +20,12 @@ export declare const enum PCAModel {
20
20
  Revised = 0,
21
21
  Original = 1
22
22
  }
23
- export declare type GeoProperties = {
23
+ export type GeoProperties = {
24
24
  area: number;
25
25
  perimeter: number;
26
26
  diameter: number;
27
27
  };
28
- export declare type CompactnessScorecard = {
28
+ export type CompactnessScorecard = {
29
29
  avgReock: number;
30
30
  avgPolsby: number;
31
31
  avgKWIWYSI: number;
@@ -33,25 +33,25 @@ export declare type CompactnessScorecard = {
33
33
  details: T.Dict;
34
34
  score?: number;
35
35
  };
36
- export declare type Compactness = {
36
+ export type Compactness = {
37
37
  rawReock: number;
38
38
  normalizedReock: number;
39
39
  rawPolsby: number;
40
40
  normalizedPolsby: number;
41
41
  kiwysiScore?: number;
42
42
  };
43
- export declare type CompactnessAlt = {
43
+ export type CompactnessAlt = {
44
44
  reock: number;
45
45
  polsby: number;
46
46
  kiwysiRank: number;
47
47
  };
48
- export declare type CompactnessJSONReady = {
48
+ export type CompactnessJSONReady = {
49
49
  avgReock: number;
50
50
  avgPolsby: number;
51
51
  avgKWIWYSI: number;
52
52
  byDistrict: CompactnessAlt[];
53
53
  };
54
- export declare type KiwysiFeatures = {
54
+ export type KiwysiFeatures = {
55
55
  sym_x: number;
56
56
  sym_y: number;
57
57
  reock: number;
@@ -61,7 +61,7 @@ export declare type KiwysiFeatures = {
61
61
  schwartzberg: number;
62
62
  kiwysiRank: number;
63
63
  };
64
- export declare type KiwysiJSONReady = {
64
+ export type KiwysiJSONReady = {
65
65
  avgKWIWYSI: number;
66
66
  byDistrict: KiwysiFeatures[];
67
67
  };
@@ -1,4 +1,4 @@
1
- export declare type Dict = {
1
+ export type Dict = {
2
2
  [key: string]: any;
3
3
  };
4
4
  export declare const enum Party {
@@ -1,14 +1,14 @@
1
- export declare type ContiguityGraph = {
1
+ export type ContiguityGraph = {
2
2
  [geoID: string]: UnweightedNeighbors | WeightedNeighbors;
3
3
  };
4
- export declare type WeightedNeighbors = {
4
+ export type WeightedNeighbors = {
5
5
  [geoID: string]: number;
6
6
  };
7
- export declare type UnweightedNeighbors = string[];
8
- export declare type PlanByGeoID = {
7
+ export type UnweightedNeighbors = string[];
8
+ export type PlanByGeoID = {
9
9
  [geoID: string]: number;
10
10
  };
11
- export declare type PlanByDistrictID = {
11
+ export type PlanByDistrictID = {
12
12
  [districtID: number]: Set<string>;
13
13
  };
14
- export declare type FeatureGroup = Set<string>;
14
+ export type FeatureGroup = Set<string>;
@@ -1,5 +1,5 @@
1
1
  import * as T from './general';
2
- export declare type MinorityFilter = {
2
+ export type MinorityFilter = {
3
3
  white: boolean;
4
4
  hispanic: boolean;
5
5
  black: boolean;
@@ -9,11 +9,11 @@ export declare type MinorityFilter = {
9
9
  minority: boolean;
10
10
  invertSelection: boolean;
11
11
  };
12
- export declare type dictPoint = {
12
+ export type dictPoint = {
13
13
  x: number;
14
14
  y: number;
15
15
  };
16
- export declare type DemographicVotingByFeature = {
16
+ export type DemographicVotingByFeature = {
17
17
  ids: string[];
18
18
  comparison: dictPoint[];
19
19
  minority: dictPoint[];
@@ -23,18 +23,18 @@ export declare type DemographicVotingByFeature = {
23
23
  asian: dictPoint[];
24
24
  native: dictPoint[];
25
25
  };
26
- export declare type LinearRegression = {
26
+ export type LinearRegression = {
27
27
  slope: number;
28
28
  intercept: number;
29
29
  r2: number;
30
30
  sterrs: StErrs;
31
31
  };
32
- export declare type StErrs = {
32
+ export type StErrs = {
33
33
  slope: number;
34
34
  intercept: number;
35
35
  regression: number;
36
36
  };
37
- export declare type RPVFactor = {
37
+ export type RPVFactor = {
38
38
  slope: number;
39
39
  intercept: number;
40
40
  r2: number;
@@ -42,7 +42,7 @@ export declare type RPVFactor = {
42
42
  sterr: number;
43
43
  points: dictPoint[];
44
44
  };
45
- export declare type RPVAnalysis = {
45
+ export type RPVAnalysis = {
46
46
  ids: string[] | undefined;
47
47
  comparison: RPVFactor | undefined;
48
48
  minority: RPVFactor | undefined;
@@ -52,7 +52,7 @@ export declare type RPVAnalysis = {
52
52
  asian: RPVFactor | undefined;
53
53
  native: RPVFactor | undefined;
54
54
  };
55
- export declare type Demographics = {
55
+ export type Demographics = {
56
56
  white: number;
57
57
  minority: number;
58
58
  black: number;
@@ -61,7 +61,7 @@ export declare type Demographics = {
61
61
  asian: number;
62
62
  native: number;
63
63
  };
64
- export declare type DemoBreakdown = {
64
+ export type DemoBreakdown = {
65
65
  pct35_40: number;
66
66
  pct40_45: number;
67
67
  pct45_50: number;
@@ -71,7 +71,7 @@ export declare type DemoBreakdown = {
71
71
  vapPct: number;
72
72
  propSeats: number;
73
73
  };
74
- export declare type DemographicPivot = {
74
+ export type DemographicPivot = {
75
75
  minority: DemoBreakdown;
76
76
  black: DemoBreakdown;
77
77
  hispanic: DemoBreakdown;
@@ -79,7 +79,7 @@ export declare type DemographicPivot = {
79
79
  asian: DemoBreakdown;
80
80
  native: DemoBreakdown;
81
81
  };
82
- export declare type MinorityScorecard = {
82
+ export type MinorityScorecard = {
83
83
  pivotByDemographic: DemographicPivot;
84
84
  opportunityDistricts: number;
85
85
  coalitionDistricts: number;
@@ -1,10 +1,10 @@
1
1
  import * as T from './general';
2
- export declare type VfArray = number[];
3
- export declare type SVpoint = {
2
+ export type VfArray = number[];
3
+ export type SVpoint = {
4
4
  v: number;
5
5
  s: number;
6
6
  };
7
- export declare type rVpoints = {
7
+ export type rVpoints = {
8
8
  Sb: number;
9
9
  Ra: number;
10
10
  Rb: number;
@@ -15,7 +15,7 @@ export declare const enum Shift {
15
15
  Proportional = 0,
16
16
  Uniform = 1
17
17
  }
18
- export declare type Bias = {
18
+ export type Bias = {
19
19
  bestS: number;
20
20
  bestSf: number;
21
21
  estS: number;
@@ -37,11 +37,11 @@ export declare type Bias = {
37
37
  mMd: number;
38
38
  lO: number | undefined;
39
39
  };
40
- export declare type Impact = {
40
+ export type Impact = {
41
41
  unearnedS: number;
42
42
  score?: number;
43
43
  };
44
- export declare type Responsiveness = {
44
+ export type Responsiveness = {
45
45
  bigR?: number;
46
46
  littleR?: number;
47
47
  mIR?: number;
@@ -52,13 +52,13 @@ export declare type Responsiveness = {
52
52
  cDf: number;
53
53
  score?: number;
54
54
  };
55
- export declare type Experimental = {
55
+ export type Experimental = {
56
56
  lSym?: number;
57
57
  lProp?: number;
58
58
  lPropAlt?: number;
59
59
  lUE?: number;
60
60
  };
61
- export declare type PartisanScorecard = {
61
+ export type PartisanScorecard = {
62
62
  bias: Bias;
63
63
  impact: Impact;
64
64
  responsiveness: Responsiveness;
@@ -69,7 +69,7 @@ export declare type PartisanScorecard = {
69
69
  experimental: Experimental;
70
70
  details: T.Dict;
71
71
  };
72
- export declare type PartisanJSONReady = {
72
+ export type PartisanJSONReady = {
73
73
  bias: Bias;
74
74
  responsiveness: Responsiveness;
75
75
  dSVpoints: SVpoint[];
@@ -1,5 +1,5 @@
1
1
  import * as T from './general';
2
- export declare type PopulationScorecard = {
2
+ export type PopulationScorecard = {
3
3
  deviation: number;
4
4
  roughlyEqual: boolean;
5
5
  score?: number;
@@ -1,25 +1,25 @@
1
1
  import * as T from './general';
2
- export declare type CountyProfile = CxD;
3
- export declare type CxD = number[][];
4
- export declare type SplittingScorecard = {
2
+ export type CountyProfile = CxD;
3
+ export type CxD = number[][];
4
+ export type SplittingScorecard = {
5
5
  county: number;
6
6
  district: number;
7
7
  details: T.Dict;
8
8
  score?: number;
9
9
  };
10
- export declare type SplittingJSONReady = {
10
+ export type SplittingJSONReady = {
11
11
  county: number;
12
12
  district: number;
13
13
  };
14
- export declare type COISplits = {
14
+ export type COISplits = {
15
15
  name: string;
16
16
  splits: number[];
17
17
  };
18
- export declare type COISplitting = {
18
+ export type COISplitting = {
19
19
  name: string;
20
20
  effectiveSplits: number;
21
21
  uncertainty: number;
22
22
  };
23
- export declare type COISplittingJSONReady = {
23
+ export type COISplittingJSONReady = {
24
24
  byCOI: COISplitting[];
25
25
  };
@@ -9,6 +9,7 @@ import * as T from '../types/all';
9
9
  import * as U from '../utils/all';
10
10
 
11
11
 
12
+ // MMD - This is the same for SMD & MMD. It's the calculation of min, max, and target size that differs.
12
13
  export function calcPopulationDeviation(max: number, min: number, targetSize: number): number
13
14
  {
14
15
  return (max - min) / targetSize; // Don't trim the result here!
@@ -21,13 +22,42 @@ export function isRoughlyEqual(devation: number, bLegislative: boolean): boolean
21
22
  return (devation <= threshold) ? true : false;
22
23
  }
23
24
 
24
- export function makePopulationScorecard(totPopByDistrict: number[], targetSize: number, bLegislative: boolean, bLog: boolean = false): PopulationScorecard
25
+ // MMD
26
+ // - Add optional # of reps per district.
27
+ // - Assume targetSize has been calculated correctly per # of reps not districts.
28
+ // - If it exists, handle the MMD-specific calculations.
29
+ export function makePopulationScorecard(totPopByDistrict: number[], targetSize: number, bLegislative: boolean, repsByDistrict?: number[], bLog: boolean = false): PopulationScorecard
25
30
  {
26
- const nonEmptyDistricts = totPopByDistrict.filter(x => x > 0);
31
+ const nDistricts = totPopByDistrict.length;
32
+
33
+ // MMD - Validate reps per district input
34
+ if (repsByDistrict)
35
+ {
36
+ if (repsByDistrict.length != nDistricts) throw new Error("Mismatched #'s of districts passed to makePopulationScorecard()!");
37
+ if (repsByDistrict.includes(0)) throw new Error("Zero reps for a district passed to makePopulationScorecard()!");
38
+ // Assume a positive integer # of reps per district
39
+ }
40
+
41
+ // MMD - Figure out the type of districts, SMD or MMD.
42
+ const nReps = (repsByDistrict) ? repsByDistrict.reduce((a, b) => a + b, 0) : nDistricts;
43
+ const bSMD = (!repsByDistrict || (nReps == nDistricts)) ? true : false;
44
+
45
+ // MMD - Generalize populations for non-empty districts to be per rep.
46
+ // const nonEmptyDistricts = totPopByDistrict.filter(x => x > 0);
47
+ let popPerRep: number[] = U.deepCopy(totPopByDistrict);
48
+ if (!bSMD && repsByDistrict)
49
+ {
50
+ for (let i = 0; i < nDistricts; i += 1)
51
+ {
52
+ popPerRep[i] = totPopByDistrict[i] / repsByDistrict[i];
53
+ }
54
+ }
55
+ const nonEmptyDistricts = popPerRep.filter(x => x > 0);
27
56
 
28
57
  let min = 0;
29
58
  let max = 0;
30
59
 
60
+ // MMD - This is already generalized, because nonEmptyDistricts is generalized.
31
61
  if (nonEmptyDistricts.length > 1)
32
62
  {
33
63
  min = U.minArray(nonEmptyDistricts);
@@ -8,6 +8,7 @@ import {avgSVError} from '../partisan/method';
8
8
  import * as T from '../types/all'
9
9
 
10
10
 
11
+ // MMD - This is already generalized, if deviations have been calculated based on # of reps instead of districts.
11
12
  // RATE POPULATION DEVIATION
12
13
 
13
14
  export function ratePopulationDeviation(rawDeviation: number, bLegislative: boolean): number
@@ -4,7 +4,6 @@
4
4
 
5
5
  import * as T from './general';
6
6
 
7
-
8
7
  export type PopulationScorecard = {
9
8
  deviation: number,
10
9
  roughlyEqual: boolean,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/dra-analytics",
3
- "version": "3.3.5",
3
+ "version": "4.1.0",
4
4
  "description": "DRA analytics",
5
5
  "main": "dist/dra-analytics.js",
6
6
  "types": "./dist/lib/all/all.d.ts",
@@ -30,40 +30,30 @@
30
30
  "bugs": {
31
31
  "url": "https://github.com/dra2020/dra-analytics/issues"
32
32
  },
33
- "jest": {
34
- "moduleFileExtensions": [
35
- "js",
36
- "jsx"
37
- ],
38
- "moduleDirectories": [
39
- "node_modules",
40
- "shared"
41
- ]
42
- },
43
33
  "homepage": "https://github.com/dra2020/dra-analytics#readme",
44
34
  "devDependencies": {
45
- "@types/geojson": "^7946.0.8",
46
- "@types/jest": "^27.0.3",
47
- "@types/node": "^12.20.20",
48
- "@types/yargs": "^16.0.4",
49
- "csv-parse": "^4.16.0",
50
- "jest": "^27.4.3",
35
+ "@types/geojson": "^7946.0.10",
36
+ "@types/node": "^12.20.55",
37
+ "@types/yargs": "^12.0.20",
38
+ "csv-parse": "^4.16.3",
51
39
  "json-loader": "^0.5.7",
52
- "prettier": "^2.3.2",
40
+ "prettier": "^2.7.1",
53
41
  "shapefile": "^0.6.6",
54
- "source-map-loader": "^3.0.0",
55
- "ts-jest": "^27.1.1",
56
- "ts-loader": "^9.2.6",
42
+ "source-map-loader": "^4.0.1",
43
+ "ts-loader": "^9.4.1",
57
44
  "tsify": "^5.0.4",
58
45
  "tslint": "^6.1.3",
59
46
  "tslint-config-prettier": "^1.18.0",
60
- "typescript": "^4.5.2",
61
- "webpack": "^5.65.0",
62
- "webpack-cli": "^4.9.1",
63
- "yargs": "^17.1.1"
47
+ "typescript": "^4.9.3",
48
+ "webpack": "^5.75.0",
49
+ "webpack-cli": "^4.10.0",
50
+ "yargs": "^12.0.5"
64
51
  },
65
52
  "dependencies": {
66
- "@dra2020/baseclient": "^1.0.67",
67
- "geojson": "^0.5.0"
53
+ "@dra2020/baseclient": "^1.0.89",
54
+ "@types/jest": "^26.0.24",
55
+ "geojson": "^0.5.0",
56
+ "jest": "^26.6.3",
57
+ "ts-jest": "^26.5.6"
68
58
  }
69
- }
59
+ }