@mailwoman/registry 9.1.0 → 9.3.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.
- package/README.md +2 -2
- package/{address-key.ts → lib/address-key.ts} +1 -1
- package/{geocode-handler.ts → lib/geocode-handler.ts} +7 -7
- package/{geojson.ts → lib/geojson.ts} +12 -6
- package/{index.ts → lib/index.ts} +10 -10
- package/{ingest.ts → lib/ingest.ts} +77 -69
- package/{learned-scorer.ts → lib/learned-scorer.ts} +1 -1
- package/{map-html.ts → lib/map-html.ts} +10 -16
- package/{reconcile.ts → lib/reconcile.ts} +9 -24
- package/{resolve.ts → lib/resolve.ts} +31 -13
- package/{tools → lib/tools}/coverage-reconciliation.ts +13 -15
- package/{tools → lib/tools}/cross-dataset-correlation.ts +64 -139
- package/{tools → lib/tools}/cross-source-threshold-sweep.ts +21 -32
- package/{tools → lib/tools}/dedup-ceiling.ts +33 -144
- package/{tools → lib/tools}/eval-geocoder.ts +10 -11
- package/{tools → lib/tools}/geocoder-namesake-probe.ts +5 -12
- package/{tools → lib/tools}/geocoder-vs-provided-coords.ts +12 -11
- package/lib/tools/gold-set-sample.ts +141 -0
- package/lib/tools/index.ts +31 -0
- package/{tools → lib/tools}/learned-scorer-clustering-eval.ts +71 -260
- package/{tools → lib/tools}/learned-scorer-crossstate-eval.ts +62 -245
- package/{tools → lib/tools}/learned-scorer-eval.ts +64 -263
- package/{tools → lib/tools}/matcher-scale.ts +5 -5
- package/lib/tools/nppes/org-name.ts +23 -0
- package/lib/tools/nppes/overmerge-packet.ts +94 -0
- package/lib/tools/nppes/report.ts +373 -0
- package/lib/tools/nppes/sample.ts +245 -0
- package/lib/tools/nppes/scoring.ts +114 -0
- package/lib/tools/nppes/settings.ts +102 -0
- package/lib/tools/nppes/truth-grains.ts +161 -0
- package/lib/tools/nppes-dedup-benchmark.ts +408 -0
- package/lib/tools/shared.ts +763 -0
- package/lib/tools/train-cross-gbt.ts +209 -0
- package/{tools → lib/tools}/train-gbt.ts +38 -156
- package/lib/tools/train-org-cross-gbt.ts +179 -0
- package/{tools → lib/tools}/txhhsc-to-oarow.ts +10 -28
- package/lib/tools/viz/browser.ts +50 -0
- package/{tools → lib/tools}/viz/cross-dataset-map.ts +16 -39
- package/{tools → lib/tools}/viz/geocode-first-surface.ts +13 -39
- package/{tools → lib/tools}/viz/render-map.ts +10 -23
- package/lib/tools/viz/render.ts +98 -0
- package/{tools → lib/tools}/viz/source-provenance-map.ts +20 -39
- package/{tools → lib/tools}/viz/yardstick-figure.ts +6 -5
- package/{types.ts → lib/types.ts} +10 -1
- package/out/address-key.d.ts +1 -1
- package/out/address-key.d.ts.map +1 -1
- package/out/address-key.js.map +1 -1
- package/out/geocode-handler.d.ts +6 -6
- package/out/geocode-handler.d.ts.map +1 -1
- package/out/geocode-handler.js +6 -6
- package/out/geocode-handler.js.map +1 -1
- package/out/geojson.d.ts +7 -2
- package/out/geojson.d.ts.map +1 -1
- package/out/geojson.js +8 -5
- package/out/geojson.js.map +1 -1
- package/out/index.d.ts +10 -10
- package/out/index.d.ts.map +1 -1
- package/out/index.js +10 -10
- package/out/index.js.map +1 -1
- package/out/ingest.d.ts +64 -46
- package/out/ingest.d.ts.map +1 -1
- package/out/ingest.js +33 -46
- package/out/ingest.js.map +1 -1
- package/out/learned-scorer.d.ts +1 -1
- package/out/learned-scorer.d.ts.map +1 -1
- package/out/learned-scorer.js.map +1 -1
- package/out/map-html.d.ts +4 -4
- package/out/map-html.d.ts.map +1 -1
- package/out/map-html.js +6 -11
- package/out/map-html.js.map +1 -1
- package/out/models/crosssource-gbt-en-us.d.ts.map +1 -1
- package/out/models/crosssource-gbt-en-us.js.map +1 -1
- package/out/models/dedup-gbt-en-us.d.ts.map +1 -1
- package/out/models/dedup-gbt-en-us.js.map +1 -1
- package/out/models/org-crosssource-gbt-en-us.d.ts.map +1 -1
- package/out/models/org-crosssource-gbt-en-us.js.map +1 -1
- package/out/reconcile.d.ts +6 -1
- package/out/reconcile.d.ts.map +1 -1
- package/out/reconcile.js +7 -21
- package/out/reconcile.js.map +1 -1
- package/out/resolve.d.ts +19 -7
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +20 -6
- package/out/resolve.js.map +1 -1
- package/out/tools/coverage-reconciliation.d.ts +1 -1
- package/out/tools/coverage-reconciliation.d.ts.map +1 -1
- package/out/tools/coverage-reconciliation.js +12 -12
- package/out/tools/coverage-reconciliation.js.map +1 -1
- package/out/tools/cross-dataset-correlation.d.ts +4 -4
- package/out/tools/cross-dataset-correlation.d.ts.map +1 -1
- package/out/tools/cross-dataset-correlation.js +60 -111
- package/out/tools/cross-dataset-correlation.js.map +1 -1
- package/out/tools/cross-source-threshold-sweep.d.ts +2 -2
- package/out/tools/cross-source-threshold-sweep.d.ts.map +1 -1
- package/out/tools/cross-source-threshold-sweep.js +19 -27
- package/out/tools/cross-source-threshold-sweep.js.map +1 -1
- package/out/tools/dedup-ceiling.d.ts.map +1 -1
- package/out/tools/dedup-ceiling.js +36 -118
- package/out/tools/dedup-ceiling.js.map +1 -1
- package/out/tools/eval-geocoder.d.ts +9 -13
- package/out/tools/eval-geocoder.d.ts.map +1 -1
- package/out/tools/eval-geocoder.js +2 -2
- package/out/tools/eval-geocoder.js.map +1 -1
- package/out/tools/geocoder-namesake-probe.d.ts +2 -2
- package/out/tools/geocoder-namesake-probe.d.ts.map +1 -1
- package/out/tools/geocoder-namesake-probe.js +4 -8
- package/out/tools/geocoder-namesake-probe.js.map +1 -1
- package/out/tools/geocoder-vs-provided-coords.d.ts +1 -1
- package/out/tools/geocoder-vs-provided-coords.d.ts.map +1 -1
- package/out/tools/geocoder-vs-provided-coords.js +11 -9
- package/out/tools/geocoder-vs-provided-coords.js.map +1 -1
- package/out/tools/gold-set-sample.d.ts +5 -5
- package/out/tools/gold-set-sample.d.ts.map +1 -1
- package/out/tools/gold-set-sample.js +37 -121
- package/out/tools/gold-set-sample.js.map +1 -1
- package/out/tools/index.d.ts +24 -29
- package/out/tools/index.d.ts.map +1 -1
- package/out/tools/index.js +24 -29
- package/out/tools/index.js.map +1 -1
- package/out/tools/learned-scorer-clustering-eval.d.ts +1 -1
- package/out/tools/learned-scorer-clustering-eval.d.ts.map +1 -1
- package/out/tools/learned-scorer-clustering-eval.js +35 -170
- package/out/tools/learned-scorer-clustering-eval.js.map +1 -1
- package/out/tools/learned-scorer-crossstate-eval.d.ts +5 -5
- package/out/tools/learned-scorer-crossstate-eval.d.ts.map +1 -1
- package/out/tools/learned-scorer-crossstate-eval.js +44 -166
- package/out/tools/learned-scorer-crossstate-eval.js.map +1 -1
- package/out/tools/learned-scorer-eval.d.ts +1 -1
- package/out/tools/learned-scorer-eval.d.ts.map +1 -1
- package/out/tools/learned-scorer-eval.js +37 -173
- package/out/tools/learned-scorer-eval.js.map +1 -1
- package/out/tools/matcher-scale.d.ts.map +1 -1
- package/out/tools/matcher-scale.js +5 -5
- package/out/tools/matcher-scale.js.map +1 -1
- package/out/tools/nppes/org-name.d.ts +21 -0
- package/out/tools/nppes/org-name.d.ts.map +1 -0
- package/out/tools/nppes/org-name.js +13 -0
- package/out/tools/nppes/org-name.js.map +1 -0
- package/out/tools/nppes/overmerge-packet.d.ts +39 -0
- package/out/tools/nppes/overmerge-packet.d.ts.map +1 -0
- package/out/tools/nppes/overmerge-packet.js +55 -0
- package/out/tools/nppes/overmerge-packet.js.map +1 -0
- package/out/tools/nppes/report.d.ts +93 -0
- package/out/tools/nppes/report.d.ts.map +1 -0
- package/out/tools/nppes/report.js +186 -0
- package/out/tools/nppes/report.js.map +1 -0
- package/out/tools/nppes/sample.d.ts +90 -0
- package/out/tools/nppes/sample.d.ts.map +1 -0
- package/out/tools/nppes/sample.js +131 -0
- package/out/tools/nppes/sample.js.map +1 -0
- package/out/tools/nppes/scoring.d.ts +36 -0
- package/out/tools/nppes/scoring.d.ts.map +1 -0
- package/out/tools/nppes/scoring.js +80 -0
- package/out/tools/nppes/scoring.js.map +1 -0
- package/out/tools/nppes/settings.d.ts +38 -0
- package/out/tools/nppes/settings.d.ts.map +1 -0
- package/out/tools/nppes/settings.js +79 -0
- package/out/tools/nppes/settings.js.map +1 -0
- package/out/tools/nppes/truth-grains.d.ts +50 -0
- package/out/tools/nppes/truth-grains.d.ts.map +1 -0
- package/out/tools/nppes/truth-grains.js +123 -0
- package/out/tools/nppes/truth-grains.js.map +1 -0
- package/out/tools/nppes-dedup-benchmark.d.ts +7 -3
- package/out/tools/nppes-dedup-benchmark.d.ts.map +1 -1
- package/out/tools/nppes-dedup-benchmark.js +106 -673
- package/out/tools/nppes-dedup-benchmark.js.map +1 -1
- package/out/tools/shared.d.ts +244 -7
- package/out/tools/shared.d.ts.map +1 -1
- package/out/tools/shared.js +353 -21
- package/out/tools/shared.js.map +1 -1
- package/out/tools/train-cross-gbt.d.ts +7 -7
- package/out/tools/train-cross-gbt.d.ts.map +1 -1
- package/out/tools/train-cross-gbt.js +47 -145
- package/out/tools/train-cross-gbt.js.map +1 -1
- package/out/tools/train-gbt.d.ts +7 -7
- package/out/tools/train-gbt.d.ts.map +1 -1
- package/out/tools/train-gbt.js +31 -111
- package/out/tools/train-gbt.js.map +1 -1
- package/out/tools/train-org-cross-gbt.d.ts +7 -5
- package/out/tools/train-org-cross-gbt.d.ts.map +1 -1
- package/out/tools/train-org-cross-gbt.js +45 -141
- package/out/tools/train-org-cross-gbt.js.map +1 -1
- package/out/tools/txhhsc-to-oarow.d.ts +2 -2
- package/out/tools/txhhsc-to-oarow.d.ts.map +1 -1
- package/out/tools/txhhsc-to-oarow.js +9 -23
- package/out/tools/txhhsc-to-oarow.js.map +1 -1
- package/out/tools/viz/browser.d.ts +34 -0
- package/out/tools/viz/browser.d.ts.map +1 -0
- package/out/tools/viz/browser.js +33 -0
- package/out/tools/viz/browser.js.map +1 -0
- package/out/tools/viz/cross-dataset-map.d.ts +4 -25
- package/out/tools/viz/cross-dataset-map.d.ts.map +1 -1
- package/out/tools/viz/cross-dataset-map.js +12 -33
- package/out/tools/viz/cross-dataset-map.js.map +1 -1
- package/out/tools/viz/geocode-first-surface.d.ts +6 -35
- package/out/tools/viz/geocode-first-surface.d.ts.map +1 -1
- package/out/tools/viz/geocode-first-surface.js +10 -37
- package/out/tools/viz/geocode-first-surface.js.map +1 -1
- package/out/tools/viz/render-map.d.ts +3 -6
- package/out/tools/viz/render-map.d.ts.map +1 -1
- package/out/tools/viz/render-map.js +9 -19
- package/out/tools/viz/render-map.js.map +1 -1
- package/out/tools/viz/render.d.ts +4 -9
- package/out/tools/viz/render.d.ts.map +1 -1
- package/out/tools/viz/render.js +24 -39
- package/out/tools/viz/render.js.map +1 -1
- package/out/tools/viz/source-provenance-map.d.ts +4 -24
- package/out/tools/viz/source-provenance-map.d.ts.map +1 -1
- package/out/tools/viz/source-provenance-map.js +12 -32
- package/out/tools/viz/source-provenance-map.js.map +1 -1
- package/out/tools/viz/yardstick-figure.d.ts +2 -2
- package/out/tools/viz/yardstick-figure.d.ts.map +1 -1
- package/out/tools/viz/yardstick-figure.js +5 -4
- package/out/tools/viz/yardstick-figure.js.map +1 -1
- package/out/types.d.ts +11 -1
- package/out/types.d.ts.map +1 -1
- package/out/types.js.map +1 -1
- package/package.json +180 -11
- package/tools/gold-set-sample.ts +0 -252
- package/tools/index.ts +0 -36
- package/tools/nppes-dedup-benchmark.ts +0 -1180
- package/tools/shared.ts +0 -160
- package/tools/train-cross-gbt.ts +0 -358
- package/tools/train-org-cross-gbt.ts +0 -329
- package/tools/viz/render.ts +0 -113
- /package/{models → lib/models}/crosssource-gbt-en-us.ts +0 -0
- /package/{models → lib/models}/dedup-gbt-en-us.ts +0 -0
- /package/{models → lib/models}/org-crosssource-gbt-en-us.ts +0 -0
|
@@ -1,329 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright Sister Software
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Train the ORG-LEVEL cross-source link scorer (#655 follow-on, 2026-07-06). The practitioner
|
|
7
|
-
* cross-source GBT does not transfer to organization records (its person-name features go dark),
|
|
8
|
-
* so the org-level cross-dataset flows pin the FS baseline. The org anchor: **CMS Provider of
|
|
9
|
-
* Services joins Care Compare by CCN** — the same facility in two separately-maintained CMS
|
|
10
|
-
* systems (certification vs quality reporting), each with independently-entered name + address.
|
|
11
|
-
* Measured drift across the national join (n≈5.4k): 12.2% name, 4.9% address — the rename /
|
|
12
|
-
* system-vs-facility / acquisition class the org objective exists for.
|
|
13
|
-
*
|
|
14
|
-
* Pipeline: national CCN join → one record per source per facility → geocode → block the UNION,
|
|
15
|
-
* keep only CROSS-source pairs → the SHARED featurizer → label by CCN → held-out-CCN calibration
|
|
16
|
-
* (max recall s.t. precision ≥ bar) → train on all pairs → emit
|
|
17
|
-
* `registry/models/org-crosssource-gbt-en-us.ts`.
|
|
18
|
-
*
|
|
19
|
-
* Sources (both public domain, direct CSVs):
|
|
20
|
-
*
|
|
21
|
-
* - `cms-pos_hospital-other_*.csv` — Provider of Services (PRVDR_NUM, FAC_NAME, ST_ADR…).
|
|
22
|
-
* - `cms-carecompare_hospital-general_*.csv` — Care Compare (Facility ID, Facility Name, Address…).
|
|
23
|
-
*
|
|
24
|
-
* Run: `mailwoman registry train-scorer org-cross-gbt [--cap 6000] [--precision-bar 0.95]
|
|
25
|
-
* [--wof <admin.db>] [--data-root <dir>] [--out registry/models/org-crosssource-gbt-en-us.ts]`
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
import { mkdirSync, writeFileSync } from "node:fs"
|
|
29
|
-
import { dirname } from "node:path"
|
|
30
|
-
|
|
31
|
-
import { dataRootPath, makeLcg } from "@mailwoman/core/utils"
|
|
32
|
-
import { block, gbtScore, trainGBT } from "@mailwoman/match"
|
|
33
|
-
import {
|
|
34
|
-
addressFrequencyKey,
|
|
35
|
-
buildDefaultModel,
|
|
36
|
-
createMatchFeaturizer,
|
|
37
|
-
defaultBlockingKeys,
|
|
38
|
-
ingestRows,
|
|
39
|
-
streamRows,
|
|
40
|
-
type ColumnMapping,
|
|
41
|
-
type SourceRecord,
|
|
42
|
-
} from "@mailwoman/registry"
|
|
43
|
-
|
|
44
|
-
import type { EvalGeocoderFactory } from "./eval-geocoder.ts"
|
|
45
|
-
import { addr, norm, uniqueQuantiles } from "./shared.ts"
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Share of entities assigned to fit; the rest are held out.
|
|
49
|
-
*/
|
|
50
|
-
const FIT_SPLIT_FRACTION = 0.8
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Options for {@linkcode trainOrgCrossSourceGBT}.
|
|
54
|
-
*/
|
|
55
|
-
export interface TrainOrgCrossSourceGBTOptions {
|
|
56
|
-
/**
|
|
57
|
-
* The injected geocoder factory (the command wires `mailwoman/geocode-core`; see `./eval-geocoder.ts`).
|
|
58
|
-
*/
|
|
59
|
-
createGeocoder: EvalGeocoderFactory
|
|
60
|
-
/**
|
|
61
|
-
* Record-matcher sources directory. Default `$MAILWOMAN_DATA_ROOT/record-matcher/sources`.
|
|
62
|
-
*/
|
|
63
|
-
sources?: string
|
|
64
|
-
/**
|
|
65
|
-
* Care Compare facilities sampled. Default 6000.
|
|
66
|
-
*/
|
|
67
|
-
cap?: number
|
|
68
|
-
/**
|
|
69
|
-
* Output TS module path. Default `registry/models/org-crosssource-gbt-en-us.ts`.
|
|
70
|
-
*/
|
|
71
|
-
out?: string
|
|
72
|
-
/**
|
|
73
|
-
* Locale recorded in the model meta. Default en-US.
|
|
74
|
-
*/
|
|
75
|
-
locale?: string
|
|
76
|
-
/**
|
|
77
|
-
* #655 threshold rule: max cross-source recall subject to this held-out pairwise precision. Default 0.95.
|
|
78
|
-
*/
|
|
79
|
-
precisionBar?: number
|
|
80
|
-
/**
|
|
81
|
-
* Training date stamped into the meta. Default today.
|
|
82
|
-
*/
|
|
83
|
-
date?: string
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
interface MessyRow {
|
|
87
|
-
/**
|
|
88
|
-
* The CCN — the cross-system facility key; rides `record.id` as the held-out label.
|
|
89
|
-
*/
|
|
90
|
-
npi: string
|
|
91
|
-
name: string
|
|
92
|
-
org: string
|
|
93
|
-
address: string
|
|
94
|
-
source: string
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Train + emit the org-level cross-source link GBT — see the module doc.
|
|
99
|
-
*/
|
|
100
|
-
export async function trainOrgCrossSourceGBT(
|
|
101
|
-
options: TrainOrgCrossSourceGBTOptions,
|
|
102
|
-
report?: (line: string) => void
|
|
103
|
-
): Promise<{ out: string; pairs: number; recommendedThreshold: number }> {
|
|
104
|
-
const SOURCES = options.sources || String(dataRootPath("record-matcher", "sources"))
|
|
105
|
-
const CAP = options.cap ?? 6000
|
|
106
|
-
const OUT = options.out || "registry/models/org-crosssource-gbt-en-us.ts"
|
|
107
|
-
const LOCALE = options.locale || "en-US"
|
|
108
|
-
// #655 threshold rule: max cross-source recall subject to this held-out pairwise precision.
|
|
109
|
-
const PRECISION_BAR = options.precisionBar ?? 0.95
|
|
110
|
-
const TRAIN_DATE = options.date || new Date().toISOString().slice(0, 10)
|
|
111
|
-
|
|
112
|
-
const POS = `${SOURCES}/cms-pos_hospital-other_2026q1.csv`
|
|
113
|
-
const CARE_COMPARE = `${SOURCES}/cms-carecompare_hospital-general_20260706.csv`
|
|
114
|
-
|
|
115
|
-
// --- Phase A: Care Compare (Facility ID + name + address). ---
|
|
116
|
-
report?.("[A] streaming Care Compare…")
|
|
117
|
-
const ccByID = new Map<string, MessyRow>()
|
|
118
|
-
|
|
119
|
-
for await (const r of streamRows(CARE_COMPARE)) {
|
|
120
|
-
if (ccByID.size >= CAP) break
|
|
121
|
-
const ccn = norm(r["Facility ID"])
|
|
122
|
-
const name = norm(r["Facility Name"])
|
|
123
|
-
const address = addr(r["Address"]!, r["City/Town"]!, r["State"]!, r["ZIP Code"]!)
|
|
124
|
-
|
|
125
|
-
if (!ccn || !name || !address) continue
|
|
126
|
-
ccByID.set(ccn, { npi: ccn, name, org: name, address, source: "care-compare" })
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
report?.(` ${ccByID.size} Care Compare facilities`)
|
|
130
|
-
|
|
131
|
-
// --- Phase B: the SAME CCNs from the POS file + the corpus-wide address-frequency table. ---
|
|
132
|
-
report?.("[B] streaming POS + building the frequency table…")
|
|
133
|
-
const rows: MessyRow[] = []
|
|
134
|
-
const joined = new Set<string>()
|
|
135
|
-
const addrCounts = new Map<string, number>()
|
|
136
|
-
let addrTotal = 0
|
|
137
|
-
|
|
138
|
-
for await (const r of streamRows(POS)) {
|
|
139
|
-
const address = addr(r["ST_ADR"]!, r["CITY_NAME"]!, r["STATE_CD"]!, r["ZIP_CD"]!)
|
|
140
|
-
|
|
141
|
-
if (address) {
|
|
142
|
-
const k = addressFrequencyKey(address)
|
|
143
|
-
addrCounts.set(k, (addrCounts.get(k) ?? 0) + 1)
|
|
144
|
-
|
|
145
|
-
addrTotal++
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const ccn = norm(r["PRVDR_NUM"])
|
|
149
|
-
|
|
150
|
-
if (!ccn || !ccByID.has(ccn) || joined.has(ccn) || !address) continue
|
|
151
|
-
const name = norm(r["FAC_NAME"])
|
|
152
|
-
|
|
153
|
-
if (!name) continue
|
|
154
|
-
joined.add(ccn)
|
|
155
|
-
rows.push({ npi: ccn, name, org: name, address, source: "cms-pos" })
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
for (const ccn of joined) {
|
|
159
|
-
rows.push(ccByID.get(ccn)!)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const addressFrequency = {
|
|
163
|
-
total: addrTotal,
|
|
164
|
-
distinct: addrCounts.size,
|
|
165
|
-
frequency: (v: string) => (v ? (addrCounts.get(addressFrequencyKey(v)) ?? 0) / addrTotal : 0),
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
report?.(` ${joined.size} CCN-joined facilities → ${rows.length} records`)
|
|
169
|
-
|
|
170
|
-
// --- Phase C: geocode + ingest (record.id = the NPI label; `source` rides the record). The heavy
|
|
171
|
-
// geocoder is injected (see ./eval-geocoder.ts). ---
|
|
172
|
-
report?.("[C] geocoding…")
|
|
173
|
-
const geocoder = await options.createGeocoder()
|
|
174
|
-
|
|
175
|
-
// `ColumnMapping.source` is a LITERAL provenance label — ingest each source separately so every
|
|
176
|
-
// record carries its registry of origin (the cross-source filter + the sweep harness key on it).
|
|
177
|
-
const mappingFor = (source: string): ColumnMapping => ({
|
|
178
|
-
id: "npi",
|
|
179
|
-
name: "name",
|
|
180
|
-
organization: "org",
|
|
181
|
-
address: "address",
|
|
182
|
-
source,
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
const posRows = rows.filter((r) => r.source === "cms-pos")
|
|
186
|
-
const ccRows = rows.filter((r) => r.source === "care-compare")
|
|
187
|
-
|
|
188
|
-
const records: SourceRecord[] = [
|
|
189
|
-
...(await ingestRows(posRows as unknown as Record<string, string>[], mappingFor("cms-pos"), {
|
|
190
|
-
geocodeAddress: geocoder.seam,
|
|
191
|
-
})),
|
|
192
|
-
...(await ingestRows(ccRows as unknown as Record<string, string>[], mappingFor("care-compare"), {
|
|
193
|
-
geocodeAddress: geocoder.seam,
|
|
194
|
-
})),
|
|
195
|
-
]
|
|
196
|
-
|
|
197
|
-
geocoder.close()
|
|
198
|
-
report?.(` ${records.length} records, ${records.filter((r) => r.address?.geocode).length} geocoded`)
|
|
199
|
-
|
|
200
|
-
// --- Phase D: block over the UNION; keep only CROSS-source pairs; featurize; label by NPI. ---
|
|
201
|
-
report?.("[D] blocking + featurizing (cross-source pairs only)…")
|
|
202
|
-
const comparisons = buildDefaultModel({ collapseSpatial: true, addressFrequency }).comparisons
|
|
203
|
-
const featurize = createMatchFeaturizer({ comparisons, addressFrequency })
|
|
204
|
-
const { pairs: allPairs } = block(records, defaultBlockingKeys())
|
|
205
|
-
const pairs = allPairs.filter(([a, b]) => a.source !== b.source)
|
|
206
|
-
const X = pairs.map(([a, b]) => featurize(a, b))
|
|
207
|
-
const Y: number[] = pairs.map(([a, b]) => (a.id === b.id ? 1 : 0))
|
|
208
|
-
const posRate = Y.reduce((s, y) => s + y, 0) / Math.max(1, Y.length)
|
|
209
|
-
const W = Y.map((y) => (y === 1 ? 1 - posRate : posRate))
|
|
210
|
-
|
|
211
|
-
report?.(
|
|
212
|
-
` ${allPairs.length} blocked pairs → ${pairs.length} cross-source (${(100 * posRate).toFixed(1)}% positive)`
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
// --- Phase E: held-out-NPI calibration — the #655 threshold rule. ---
|
|
216
|
-
report?.("[E] held-out calibration…")
|
|
217
|
-
const hyperparams = { rounds: 120, depth: 3, lr: 0.3, minLeaf: 20 }
|
|
218
|
-
const rnd = makeLcg(655)
|
|
219
|
-
const split = new Map<string, "fit" | "holdout">()
|
|
220
|
-
|
|
221
|
-
for (const ccn of joined) {
|
|
222
|
-
split.set(ccn, rnd() < FIT_SPLIT_FRACTION ? "fit" : "holdout")
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const fitIdx = pairs
|
|
226
|
-
.map((_, i) => i)
|
|
227
|
-
.filter((i) => split.get(pairs[i]![0].id) === "fit" && split.get(pairs[i]![1].id) === "fit")
|
|
228
|
-
|
|
229
|
-
const holdIdx = pairs
|
|
230
|
-
.map((_, i) => i)
|
|
231
|
-
.filter((i) => split.get(pairs[i]![0].id) === "holdout" && split.get(pairs[i]![1].id) === "holdout")
|
|
232
|
-
|
|
233
|
-
const calib = trainGBT(
|
|
234
|
-
fitIdx.map((i) => X[i]!),
|
|
235
|
-
fitIdx.map((i) => Y[i]!),
|
|
236
|
-
fitIdx.map((i) => W[i]!),
|
|
237
|
-
hyperparams
|
|
238
|
-
)
|
|
239
|
-
|
|
240
|
-
const holdScores = holdIdx.map((i) => ({ s: gbtScore(calib, X[i]!), y: Y[i]! }))
|
|
241
|
-
const sorted = holdScores.map((h) => h.s).toSorted((a, b) => a - b)
|
|
242
|
-
const totalPos = holdScores.reduce((s, h) => s + h.y, 0)
|
|
243
|
-
let recommendedThreshold = Number.POSITIVE_INFINITY
|
|
244
|
-
let barRecall = 0
|
|
245
|
-
let f1MaxThreshold = 0
|
|
246
|
-
let bestF1 = -1
|
|
247
|
-
|
|
248
|
-
for (const t of uniqueQuantiles(sorted, 60)) {
|
|
249
|
-
let tp = 0
|
|
250
|
-
let fp = 0
|
|
251
|
-
|
|
252
|
-
for (const h of holdScores) {
|
|
253
|
-
if (h.s < t) continue
|
|
254
|
-
|
|
255
|
-
if (h.y) {
|
|
256
|
-
tp++
|
|
257
|
-
} else {
|
|
258
|
-
fp++
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const precision = tp + fp > 0 ? tp / (tp + fp) : 1
|
|
263
|
-
const recall = totalPos > 0 ? tp / totalPos : 0
|
|
264
|
-
const f1 = precision + recall > 0 ? (2 * precision * recall) / (precision + recall) : 0
|
|
265
|
-
|
|
266
|
-
// The #655 rule: the LOWEST threshold whose precision clears the bar (maximizes recall under it).
|
|
267
|
-
if (precision >= PRECISION_BAR && recall > barRecall) {
|
|
268
|
-
barRecall = recall
|
|
269
|
-
recommendedThreshold = t
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (f1 > bestF1) {
|
|
273
|
-
bestF1 = f1
|
|
274
|
-
f1MaxThreshold = t
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (!Number.isFinite(recommendedThreshold)) {
|
|
279
|
-
recommendedThreshold = f1MaxThreshold
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
report?.(
|
|
283
|
-
` held-out (${holdIdx.length} pairs, ${totalPos} pos): precision-bar ${PRECISION_BAR} → threshold ${recommendedThreshold.toFixed(3)} (recall ${(100 * barRecall).toFixed(1)}%); F1-max ${(100 * bestF1).toFixed(1)}% @ ${f1MaxThreshold.toFixed(3)}`
|
|
284
|
-
)
|
|
285
|
-
|
|
286
|
-
// --- Phase F: train the SHIPPED model on ALL cross-source pairs; emit the committed module. ---
|
|
287
|
-
report?.("[F] training the shipped model on all pairs…")
|
|
288
|
-
const model = trainGBT(X, Y, W, hyperparams)
|
|
289
|
-
|
|
290
|
-
const meta = {
|
|
291
|
-
version: "1.0.0",
|
|
292
|
-
objective: "org-cross-source-link" as const,
|
|
293
|
-
locale: LOCALE,
|
|
294
|
-
trainedOn: TRAIN_DATE,
|
|
295
|
-
facilities: joined.size,
|
|
296
|
-
records: records.length,
|
|
297
|
-
pairs: pairs.length,
|
|
298
|
-
posRate: Number(posRate.toFixed(4)),
|
|
299
|
-
precisionBar: PRECISION_BAR,
|
|
300
|
-
holdoutBarRecall: Number(barRecall.toFixed(4)),
|
|
301
|
-
holdoutF1Max: Number(bestF1.toFixed(4)),
|
|
302
|
-
hyperparams,
|
|
303
|
-
recommendedThreshold: Number(recommendedThreshold.toFixed(4)),
|
|
304
|
-
features: X[0]?.length ?? 0,
|
|
305
|
-
sources: ["cms-pos", "care-compare"],
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
const moduleSource =
|
|
309
|
-
`/**\n` +
|
|
310
|
-
` * @copyright Sister Software\n` +
|
|
311
|
-
` * @license AGPL-3.0\n` +
|
|
312
|
-
` * @author Teffen Ellis, et al.\n` +
|
|
313
|
-
` *\n` +
|
|
314
|
-
` * The ORG-LEVEL cross-source link scorer (#655 follow-on) — trained on CCN-joined CMS POS ↔\n` +
|
|
315
|
-
` * Care Compare facility pairs (both public domain). Scores "same facility, different registry\n` +
|
|
316
|
-
` * text" links for org-level cross-dataset flows. Generated by\n` +
|
|
317
|
-
` * \`mailwoman registry train-scorer org-cross-gbt\` (registry/tools/train-org-cross-gbt.ts) — retrain rather than editing.\n` +
|
|
318
|
-
` */\n\n` +
|
|
319
|
-
`import type { GBT } from "@mailwoman/match"\n\n` +
|
|
320
|
-
`export const ORG_CROSS_SOURCE_GBT_META = ${JSON.stringify(meta)} as const\n\n` +
|
|
321
|
-
`// prettier-ignore\n` +
|
|
322
|
-
`export const ORG_CROSS_SOURCE_GBT_MODEL: GBT = ${JSON.stringify(model)}\n`
|
|
323
|
-
|
|
324
|
-
mkdirSync(dirname(OUT), { recursive: true })
|
|
325
|
-
writeFileSync(OUT, moduleSource)
|
|
326
|
-
report?.(` ${model.trees.length} trees, ${meta.features} features -> ${OUT}`)
|
|
327
|
-
|
|
328
|
-
return { out: OUT, pairs: pairs.length, recommendedThreshold }
|
|
329
|
-
}
|
package/tools/viz/render.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright Sister Software
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Render a self-contained Plotly HTML to a PNG via headless Chromium — the shared renderer behind
|
|
7
|
-
* the `registry viz` figures (an internal helper, not a command).
|
|
8
|
-
*
|
|
9
|
-
* Plotly 3D (`surface`/`scatter3d`) needs a real WebGL context; headless Chromium has none by
|
|
10
|
-
* default, so we force ANGLE's SwiftShader software rasterizer (`--use-gl=angle
|
|
11
|
-
* --use-angle=swiftshader --enable-unsafe-swiftshader`). 2D traces (`contour`/`heatmap`) render
|
|
12
|
-
* on the 2D canvas regardless. We wait for Plotly's `plotly_afterplot` to fire on every graph div
|
|
13
|
-
* rather than a fixed sleep, so the screenshot can't race the (async) WebGL paint.
|
|
14
|
-
*
|
|
15
|
-
* Playwright (headless Chromium) is a heavy dev-only dependency — lazy-imported inside the entry
|
|
16
|
-
* fn (the corpus-tools lazy-import convention), so importing `@mailwoman/registry/tools` never
|
|
17
|
-
* pays for it.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import { resolve } from "node:path"
|
|
21
|
-
import { pathToFileURL } from "node:url"
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Options for {@linkcode renderPlotlyHTMLToPNG}.
|
|
25
|
-
*/
|
|
26
|
-
export interface RenderPlotlyOptions {
|
|
27
|
-
/**
|
|
28
|
-
* The self-contained Plotly HTML file.
|
|
29
|
-
*/
|
|
30
|
-
inHTML: string
|
|
31
|
-
/**
|
|
32
|
-
* Output PNG path.
|
|
33
|
-
*/
|
|
34
|
-
outPNG: string
|
|
35
|
-
/**
|
|
36
|
-
* Viewport width. Default 1160.
|
|
37
|
-
*/
|
|
38
|
-
width?: number
|
|
39
|
-
/**
|
|
40
|
-
* Viewport height. Default 1000.
|
|
41
|
-
*/
|
|
42
|
-
height?: number
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Screenshot a Plotly HTML page after every graph div's `plotly_afterplot` fires.
|
|
47
|
-
*/
|
|
48
|
-
export async function renderPlotlyHTMLToPNG(
|
|
49
|
-
options: RenderPlotlyOptions,
|
|
50
|
-
report?: (line: string) => void
|
|
51
|
-
): Promise<{ outPNG: string; consoleErrors: string[] }> {
|
|
52
|
-
// playwright + Chromium are heavy — lazy import (the pipeline convention).
|
|
53
|
-
const { chromium } = await import("playwright")
|
|
54
|
-
|
|
55
|
-
const browser = await chromium.launch({
|
|
56
|
-
args: ["--use-gl=angle", "--use-angle=swiftshader", "--enable-unsafe-swiftshader", "--ignore-gpu-blocklist"],
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
const page = await browser.newPage({
|
|
60
|
-
viewport: { width: options.width ?? 1160, height: options.height ?? 1000 },
|
|
61
|
-
deviceScaleFactor: 2,
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
const errors: string[] = []
|
|
65
|
-
page.on("console", (m) => m.type() === "error" && errors.push(m.text()))
|
|
66
|
-
page.on("pageerror", (e) => errors.push(String(e)))
|
|
67
|
-
|
|
68
|
-
await page.goto(pathToFileURL(resolve(options.inHTML)).href, { waitUntil: "networkidle" })
|
|
69
|
-
|
|
70
|
-
// Resolve once every Plotly graph div has fired plotly_afterplot (3D paints land async, after
|
|
71
|
-
// newPlot's promise resolves), with a per-div fallback so an already-painted div can't hang us.
|
|
72
|
-
await page.evaluate(async () => {
|
|
73
|
-
// Runs in the BROWSER — reach DOM/Plotly globals via globalThis so the script needs no DOM lib.
|
|
74
|
-
interface PlotlyDiv {
|
|
75
|
-
_fullLayout?: unknown
|
|
76
|
-
on?: (event: string, cb: () => void) => void
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const doc = (globalThis as unknown as { document: { querySelectorAll(s: string): Iterable<unknown> } }).document
|
|
80
|
-
|
|
81
|
-
const divs = [...doc.querySelectorAll("div")]
|
|
82
|
-
.map((d) => d as PlotlyDiv)
|
|
83
|
-
.filter((d) => d._fullLayout && typeof d.on === "function")
|
|
84
|
-
|
|
85
|
-
await Promise.all(
|
|
86
|
-
divs.map(
|
|
87
|
-
(d) =>
|
|
88
|
-
new Promise<void>((res) => {
|
|
89
|
-
d.on!("plotly_afterplot", () => res())
|
|
90
|
-
setTimeout(() => res(), 2000)
|
|
91
|
-
})
|
|
92
|
-
)
|
|
93
|
-
)
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
// Final settle for the software WebGL rasterizer.
|
|
97
|
-
await page.waitForTimeout(800)
|
|
98
|
-
|
|
99
|
-
await page.screenshot({ path: resolve(options.outPNG), fullPage: true })
|
|
100
|
-
await browser.close()
|
|
101
|
-
|
|
102
|
-
if (errors.length) {
|
|
103
|
-
report?.(`[render] ${errors.length} console error(s):`)
|
|
104
|
-
|
|
105
|
-
for (const e of errors.slice(0, 8)) {
|
|
106
|
-
report?.(" " + e)
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
report?.(`[render] ${options.outPNG}`)
|
|
111
|
-
|
|
112
|
-
return { outPNG: options.outPNG, consoleErrors: errors }
|
|
113
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|