@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
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Train the CROSS-SOURCE link scorer (#655 option 2 — unblocked 2026-07-06). The dedup GBT (#603)
|
|
7
|
+
* is trained on within-NPPES labels, so its strongest feature (`spatial-exact × name-disagree`)
|
|
8
|
+
* REJECTS the prototypical cross-source pair ("same provider, different operational text across
|
|
9
|
+
* registries") — the reason the cross-dataset flows pin the FS baseline. The 2026-06-16
|
|
10
|
+
* feasibility doc blocked a cross-source retrain on "no non-circular anchor"; the anchor exists:
|
|
11
|
+
* **CMS Open Payments joins NPPES by NPI** — the same practitioner in two INDEPENDENT registries,
|
|
12
|
+
* each with independently human-entered name + address. Same-NPI cross-source pairs are
|
|
13
|
+
* ground-truth positives labeled by a key the matcher's features never see.
|
|
14
|
+
*
|
|
15
|
+
* Pipeline: assemble NPPES + Open Payments TX records for the same NPI population (Phases A/B
|
|
16
|
+
* here), then the SHARED `trainCrossSourceModel` runs Phases C–F — geocode through the standard
|
|
17
|
+
* ingest → block the UNION, keep only CROSS-source candidate pairs → the SHARED
|
|
18
|
+
* `createMatchFeaturizer` (train ≡ inference) → label by NPI → held-out-NPI calibration (the #655
|
|
19
|
+
* threshold rule: max recall subject to a pairwise-precision bar, reported alongside F1-max) →
|
|
20
|
+
* train the shipped model on all pairs → emit `registry/models/crosssource-gbt-en-us.ts`.
|
|
21
|
+
*
|
|
22
|
+
* Sources (both public domain, `.notes/data-sources.md`):
|
|
23
|
+
*
|
|
24
|
+
* - `nppes_npi-registry_*.tsv` — the practice-location + primary-name records.
|
|
25
|
+
* - `openpayments_covered-recipient-profile_*.csv` — the OP profile supplement (NPI, profile
|
|
26
|
+
* first/last, profile practice address).
|
|
27
|
+
*
|
|
28
|
+
* Run: `mailwoman registry train-scorer cross-gbt [--state TX] [--npis 2000]
|
|
29
|
+
* [--precision-bar 0.95] [--wof <admin.db>] [--data-root <dir>]
|
|
30
|
+
* [--out registry/models/crosssource-gbt-en-us.ts]`
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { dataRootPath } from "@mailwoman/core/data-root"
|
|
34
|
+
import { isoDate } from "@mailwoman/core/utils"
|
|
35
|
+
|
|
36
|
+
import { addressFrequencyKey, streamRows } from "#index"
|
|
37
|
+
import type { EvalGeocoderFactory } from "#tools/eval-geocoder"
|
|
38
|
+
import { addr, norm, NPPES_COLUMNS as N, stateOption, trainCrossSourceModel, type CrossSourceRow } from "#tools/shared"
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Options for {@linkcode trainCrossSourceGBT}.
|
|
42
|
+
*/
|
|
43
|
+
export interface TrainCrossSourceGBTOptions {
|
|
44
|
+
/**
|
|
45
|
+
* The injected geocoder factory (the command wires `mailwoman/geocode-core`; see `./eval-geocoder.ts`).
|
|
46
|
+
*/
|
|
47
|
+
createGeocoder: EvalGeocoderFactory
|
|
48
|
+
/**
|
|
49
|
+
* Record-matcher sources directory. Default `$MAILWOMAN_DATA_ROOT/record-matcher/sources`.
|
|
50
|
+
*/
|
|
51
|
+
sources?: string
|
|
52
|
+
/**
|
|
53
|
+
* State filter. Default TX.
|
|
54
|
+
*/
|
|
55
|
+
state?: string
|
|
56
|
+
/**
|
|
57
|
+
* NPIs sampled. Default 2000.
|
|
58
|
+
*/
|
|
59
|
+
npis?: number
|
|
60
|
+
/**
|
|
61
|
+
* Output TS module path. Default `registry/models/crosssource-gbt-en-us.ts`.
|
|
62
|
+
*/
|
|
63
|
+
out?: string
|
|
64
|
+
/**
|
|
65
|
+
* Locale recorded in the model meta. Default en-US.
|
|
66
|
+
*/
|
|
67
|
+
locale?: string
|
|
68
|
+
/**
|
|
69
|
+
* #655 threshold rule: max cross-source recall subject to this held-out pairwise precision. Default 0.95.
|
|
70
|
+
*/
|
|
71
|
+
precisionBar?: number
|
|
72
|
+
/**
|
|
73
|
+
* Training date stamped into the meta. Default today.
|
|
74
|
+
*/
|
|
75
|
+
date?: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Train + emit the cross-source link GBT — see the module doc.
|
|
80
|
+
*/
|
|
81
|
+
export async function trainCrossSourceGBT(
|
|
82
|
+
options: TrainCrossSourceGBTOptions,
|
|
83
|
+
report?: (line: string) => void
|
|
84
|
+
): Promise<{ out: string; pairs: number; recommendedThreshold: number }> {
|
|
85
|
+
const SOURCES = options.sources || String(dataRootPath("record-matcher", "sources"))
|
|
86
|
+
const STATE = stateOption(options)
|
|
87
|
+
const NPIS = options.npis ?? 2000
|
|
88
|
+
const OUT = options.out || "packages/registry/lib/models/crosssource-gbt-en-us.ts"
|
|
89
|
+
const LOCALE = options.locale || "en-US"
|
|
90
|
+
// #655 threshold rule: max cross-source recall subject to this held-out pairwise precision.
|
|
91
|
+
const PRECISION_BAR = options.precisionBar ?? 0.95
|
|
92
|
+
const TRAIN_DATE = options.date || isoDate()
|
|
93
|
+
|
|
94
|
+
const REGISTRY = `${SOURCES}/nppes_npi-registry_20260607.tsv`
|
|
95
|
+
const OP_PROFILE = `${SOURCES}/openpayments_covered-recipient-profile_20260603.csv`
|
|
96
|
+
|
|
97
|
+
// --- Phase A: Open Payments TX practitioners (NPI + profile name + profile address). ---
|
|
98
|
+
report?.(`[A] streaming the OP profile supplement (${STATE})…`)
|
|
99
|
+
const opByNPI = new Map<string, CrossSourceRow>()
|
|
100
|
+
|
|
101
|
+
for await (const r of streamRows(OP_PROFILE)) {
|
|
102
|
+
if (opByNPI.size >= NPIS) break
|
|
103
|
+
const npi = norm(r["Covered_Recipient_NPI"])
|
|
104
|
+
const st = norm(r["Covered_Recipient_Profile_State"]).toUpperCase()
|
|
105
|
+
|
|
106
|
+
if (!npi || st !== STATE || opByNPI.has(npi)) continue
|
|
107
|
+
|
|
108
|
+
const name =
|
|
109
|
+
`${norm(r["Covered_Recipient_Profile_First_Name"])} ${norm(r["Covered_Recipient_Profile_Last_Name"])}`.trim()
|
|
110
|
+
|
|
111
|
+
const address = addr(
|
|
112
|
+
r["Covered_Recipient_Profile_Address_Line_1"]!,
|
|
113
|
+
r["Covered_Recipient_Profile_City"]!,
|
|
114
|
+
st,
|
|
115
|
+
r["Covered_Recipient_Profile_Zipcode"]!
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
if (!name || !address) continue
|
|
119
|
+
opByNPI.set(npi, { npi, name, org: "", address, source: "openpayments" })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
report?.(` ${opByNPI.size} OP ${STATE} practitioners`)
|
|
123
|
+
|
|
124
|
+
// --- Phase B: the SAME NPIs from NPPES (practice address + legal name) + the corpus-wide
|
|
125
|
+
// address-frequency table (one full registry pass, identical to train-gbt). ---
|
|
126
|
+
report?.("[B] full registry pass: address-frequency table + the NPI-joined NPPES rows…")
|
|
127
|
+
const rows: CrossSourceRow[] = []
|
|
128
|
+
const joined = new Set<string>()
|
|
129
|
+
const addrCounts = new Map<string, number>()
|
|
130
|
+
let addrTotal = 0
|
|
131
|
+
let scanned = 0
|
|
132
|
+
|
|
133
|
+
for await (const r of streamRows(REGISTRY)) {
|
|
134
|
+
if (++scanned % 1_000_000 === 0) {
|
|
135
|
+
report?.(` scanned ${scanned / 1e6}M, joined ${joined.size}`)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const practice = addr(r[N.pAddr]!, r[N.pCity]!, r[N.pState]!, r[N.pZip]!)
|
|
139
|
+
|
|
140
|
+
if (practice) {
|
|
141
|
+
const k = addressFrequencyKey(practice)
|
|
142
|
+
addrCounts.set(k, (addrCounts.get(k) ?? 0) + 1)
|
|
143
|
+
|
|
144
|
+
addrTotal++
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const npi = norm(r[N.npi])
|
|
148
|
+
|
|
149
|
+
if (!npi || !opByNPI.has(npi) || joined.has(npi) || !practice) continue
|
|
150
|
+
|
|
151
|
+
// Practitioner-level matching: OP covered recipients are individuals (entity type 1).
|
|
152
|
+
if (norm(r[N.entityType]) !== "1") continue
|
|
153
|
+
const name = `${norm(r[N.first])} ${norm(r[N.last])}`.trim()
|
|
154
|
+
|
|
155
|
+
if (!name) continue
|
|
156
|
+
joined.add(npi)
|
|
157
|
+
rows.push({ npi, name, org: "", address: practice, source: "nppes" })
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Keep only NPIs present in BOTH sources — every record has a cross-source counterpart.
|
|
161
|
+
for (const npi of joined) {
|
|
162
|
+
rows.push(opByNPI.get(npi)!)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const addressFrequency = {
|
|
166
|
+
total: addrTotal,
|
|
167
|
+
distinct: addrCounts.size,
|
|
168
|
+
frequency: (v: string) => (v ? (addrCounts.get(addressFrequencyKey(v)) ?? 0) / addrTotal : 0),
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
report?.(` ${joined.size} NPI-joined pairs → ${rows.length} records`)
|
|
172
|
+
|
|
173
|
+
// --- Phases C–F: the SHARED cross-source trainer (geocode → cross-source pairs → #655 calibration
|
|
174
|
+
// → shipped model → committed module). ---
|
|
175
|
+
return trainCrossSourceModel({
|
|
176
|
+
createGeocoder: options.createGeocoder,
|
|
177
|
+
rows,
|
|
178
|
+
joined,
|
|
179
|
+
addressFrequency,
|
|
180
|
+
sources: ["nppes", "openpayments"],
|
|
181
|
+
precisionBar: PRECISION_BAR,
|
|
182
|
+
out: OUT,
|
|
183
|
+
exportPrefix: "CROSS_SOURCE_GBT",
|
|
184
|
+
moduleDoc:
|
|
185
|
+
` * The CROSS-SOURCE link scorer (#655 option 2) — trained on NPI-joined NPPES ↔ Open Payments\n` +
|
|
186
|
+
` * pairs (the non-circular cross-registry anchor; both public domain). Scores "same provider,\n` +
|
|
187
|
+
` * different registry text" links the dedup GBT rejects by construction. Generated by\n` +
|
|
188
|
+
` * \`mailwoman registry train-scorer cross-gbt\` (registry/tools/train-cross-gbt.ts) — retrain + re-run rather than editing.\n`,
|
|
189
|
+
meta: (figures) => ({
|
|
190
|
+
version: "1.0.0",
|
|
191
|
+
objective: "cross-source-link",
|
|
192
|
+
locale: LOCALE,
|
|
193
|
+
trainedOn: TRAIN_DATE,
|
|
194
|
+
state: STATE,
|
|
195
|
+
npis: joined.size,
|
|
196
|
+
records: figures.records,
|
|
197
|
+
pairs: figures.pairs,
|
|
198
|
+
posRate: figures.posRate,
|
|
199
|
+
precisionBar: PRECISION_BAR,
|
|
200
|
+
holdoutBarRecall: figures.barRecall,
|
|
201
|
+
holdoutF1Max: figures.f1Max,
|
|
202
|
+
hyperparams: figures.hyperparams,
|
|
203
|
+
recommendedThreshold: figures.recommendedThreshold,
|
|
204
|
+
features: figures.features,
|
|
205
|
+
sources: ["nppes", "openpayments"],
|
|
206
|
+
}),
|
|
207
|
+
report,
|
|
208
|
+
})
|
|
209
|
+
}
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
6
|
* Train the production learned-scorer model (#603). Builds the SAME NPI-keyed record set the dedup
|
|
7
|
-
* benchmark + the clustering A/B use (real registry + name-drift +
|
|
8
|
-
* it, blocks → candidate pairs, featurizes each pair with the SHARED
|
|
9
|
-
* train ≡ inference), labels by held-out NPI, and fits the
|
|
10
|
-
* the model as a committed TS module
|
|
11
|
-
* package.
|
|
7
|
+
* benchmark + the clustering A/B use (the SHARED `buildNPPESSample`: real registry + name-drift +
|
|
8
|
+
* address-variation), geocodes it, blocks → candidate pairs, featurizes each pair with the SHARED
|
|
9
|
+
* `createMatchFeaturizer` (so train ≡ inference), labels by held-out NPI, and fits the
|
|
10
|
+
* gradient-boosted-tree model. Writes the model as a committed TS module
|
|
11
|
+
* (`registry/models/dedup-gbt-en-us.ts`) that ships in the package.
|
|
12
12
|
*
|
|
13
13
|
* Unlike the eval, this trains on ALL sampled NPIs (no held-out split) — the held-out F1 is the
|
|
14
14
|
* eval's job; this produces the shipped artifact. The eval (`learned-scorer-clustering-eval.ts`)
|
|
@@ -18,25 +18,26 @@
|
|
|
18
18
|
* [--data-root <dir>] [--out registry/models/dedup-gbt-en-us.ts]`
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
import {
|
|
21
|
+
import { dataRootPath } from "@mailwoman/core/data-root"
|
|
22
|
+
import { makeDirectories, writeLocalFile } from "@mailwoman/core/fs/writers"
|
|
23
|
+
import { makeLcg } from "@mailwoman/core/random"
|
|
24
|
+
import { isoDate } from "@mailwoman/core/utils"
|
|
25
25
|
import { block, gbtScore, trainGBT } from "@mailwoman/match"
|
|
26
|
+
import { dirname } from "path-ts"
|
|
27
|
+
|
|
26
28
|
import {
|
|
27
|
-
addressFrequencyKey,
|
|
28
29
|
buildDefaultModel,
|
|
29
30
|
createMatchFeaturizer,
|
|
30
31
|
defaultBlockingKeys,
|
|
31
32
|
ingestRows,
|
|
32
33
|
resolveEntities,
|
|
33
|
-
streamRows,
|
|
34
34
|
type ColumnMapping,
|
|
35
35
|
type SourceRecord,
|
|
36
|
-
} from "
|
|
37
|
-
|
|
38
|
-
import
|
|
39
|
-
import {
|
|
36
|
+
} from "#index"
|
|
37
|
+
import type { EvalGeocoderFactory } from "#tools/eval-geocoder"
|
|
38
|
+
import { buildNPPESSample } from "#tools/nppes/sample"
|
|
39
|
+
import { scoreEntities } from "#tools/nppes/scoring"
|
|
40
|
+
import { stateOption, uniqueQuantiles } from "#tools/shared"
|
|
40
41
|
|
|
41
42
|
/**
|
|
42
43
|
* Share of entities assigned to fit; the rest are held out.
|
|
@@ -73,7 +74,7 @@ export interface TrainDedupGBTOptions {
|
|
|
73
74
|
locale?: string
|
|
74
75
|
/**
|
|
75
76
|
* Cost-sensitive training (#625): up-weight the NEGATIVE (distinct-pair) class by this factor so the model is more
|
|
76
|
-
* conservative about merging — directly trades recall for precision to
|
|
77
|
+
* conservative about merging — directly trades recall for precision to reduce over-merge. 1 = the symmetric
|
|
77
78
|
* class-balanced default; >1 penalizes a false merge more than a missed one.
|
|
78
79
|
*/
|
|
79
80
|
cost?: number
|
|
@@ -83,53 +84,6 @@ export interface TrainDedupGBTOptions {
|
|
|
83
84
|
date?: string
|
|
84
85
|
}
|
|
85
86
|
|
|
86
|
-
interface MessyRow {
|
|
87
|
-
npi: string
|
|
88
|
-
name: string
|
|
89
|
-
org: string
|
|
90
|
-
address: string
|
|
91
|
-
/**
|
|
92
|
-
* Authorized official — feeds the #625 roll-up-signature features (officialAgree × orgDisagree).
|
|
93
|
-
*/
|
|
94
|
-
auth: string
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Pairwise clustering F1 of resolved entities vs the NPI grouping (record.id = the NPI).
|
|
99
|
-
*/
|
|
100
|
-
function clusterF1(entities: { records: readonly SourceRecord[] }[]): number {
|
|
101
|
-
const choose2 = (k: number) => (k * (k - 1)) / 2
|
|
102
|
-
const npiTotals = new Map<string, number>()
|
|
103
|
-
let tp = 0
|
|
104
|
-
let sumCluster = 0
|
|
105
|
-
|
|
106
|
-
for (const e of entities) {
|
|
107
|
-
const byNPI = new Map<string, number>()
|
|
108
|
-
|
|
109
|
-
for (const rec of e.records) {
|
|
110
|
-
byNPI.set(rec.id, (byNPI.get(rec.id) ?? 0) + 1)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
sumCluster += choose2(e.records.length)
|
|
114
|
-
|
|
115
|
-
for (const [npi, c] of byNPI) {
|
|
116
|
-
tp += choose2(c)
|
|
117
|
-
npiTotals.set(npi, (npiTotals.get(npi) ?? 0) + c)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
let sumClass = 0
|
|
122
|
-
|
|
123
|
-
for (const total of npiTotals.values()) {
|
|
124
|
-
sumClass += choose2(total)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const precision = sumCluster > 0 ? tp / sumCluster : 0
|
|
128
|
-
const recall = sumClass > 0 ? tp / sumClass : 0
|
|
129
|
-
|
|
130
|
-
return precision + recall > 0 ? (2 * precision * recall) / (precision + recall) : 0
|
|
131
|
-
}
|
|
132
|
-
|
|
133
87
|
/**
|
|
134
88
|
* Train + emit the production dedup GBT — see the module doc.
|
|
135
89
|
*/
|
|
@@ -138,95 +92,22 @@ export async function trainDedupGBT(
|
|
|
138
92
|
report?: (line: string) => void
|
|
139
93
|
): Promise<{ out: string; pairs: number; recommendedThreshold: number; heldOutF1: number }> {
|
|
140
94
|
const SOURCES = options.sources || dataRootPath("record-matcher", "sources")
|
|
141
|
-
const STATE = (options
|
|
95
|
+
const STATE = stateOption(options)
|
|
142
96
|
const NPIS = options.npis ?? 3000
|
|
143
|
-
const OUT = options.out || "registry/models/dedup-gbt-en-us.ts"
|
|
97
|
+
const OUT = options.out || "packages/registry/lib/models/dedup-gbt-en-us.ts"
|
|
144
98
|
const LOCALE = options.locale || "en-US"
|
|
145
99
|
const COST = options.cost ?? 1
|
|
146
|
-
const TRAIN_DATE = options.date ||
|
|
100
|
+
const TRAIN_DATE = options.date || isoDate() // overridable for reproducible commits
|
|
147
101
|
|
|
148
102
|
const REGISTRY = `${SOURCES}/nppes_npi-registry_20260607.tsv`
|
|
149
103
|
const OTHER_NAMES = `${SOURCES}/nppes_other-names_20260607.tsv`
|
|
150
104
|
|
|
151
|
-
// ---
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const alt = norm(r[C.otherOrg])
|
|
158
|
-
|
|
159
|
-
if (!npi || !alt) continue
|
|
160
|
-
const list = altNames.get(npi) ?? []
|
|
161
|
-
|
|
162
|
-
if (list.length < MIN_GROUP_SIZE) {
|
|
163
|
-
list.push(alt)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
altNames.set(npi, list)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// --- Phase B: one full registry pass — corpus-wide address-frequency table + the sample. ---
|
|
170
|
-
report?.(`[B] full registry pass: address-frequency table + ${NPIS} ${STATE} sample…`)
|
|
171
|
-
const rows: MessyRow[] = []
|
|
172
|
-
const kept = new Set<string>()
|
|
173
|
-
const addrCounts = new Map<string, number>()
|
|
174
|
-
let addrTotal = 0
|
|
175
|
-
let scanned = 0
|
|
176
|
-
|
|
177
|
-
for await (const r of streamRows(REGISTRY)) {
|
|
178
|
-
if (++scanned % 1_000_000 === 0) {
|
|
179
|
-
report?.(` scanned ${scanned / 1e6}M, kept ${kept.size}`)
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const practice = addr(r[C.pAddr]!, r[C.pCity]!, r[C.pState]!, r[C.pZip]!)
|
|
183
|
-
|
|
184
|
-
if (practice) {
|
|
185
|
-
const k = addressFrequencyKey(practice)
|
|
186
|
-
addrCounts.set(k, (addrCounts.get(k) ?? 0) + 1)
|
|
187
|
-
|
|
188
|
-
addrTotal++
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const npi = norm(r[C.npi])
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
kept.size < NPIS &&
|
|
195
|
-
npi &&
|
|
196
|
-
!kept.has(npi) &&
|
|
197
|
-
altNames.has(npi) &&
|
|
198
|
-
practice &&
|
|
199
|
-
norm(r[C.pState]).toUpperCase() === STATE
|
|
200
|
-
) {
|
|
201
|
-
const isOrg = norm(r[C.entityType]) === "2"
|
|
202
|
-
const primaryName = isOrg ? norm(r[C.orgLegal]) : `${norm(r[C.first])} ${norm(r[C.last])}`.trim()
|
|
203
|
-
|
|
204
|
-
if (primaryName) {
|
|
205
|
-
const org = isOrg ? norm(r[C.orgLegal]) : ""
|
|
206
|
-
const auth = `${norm(r[C.authFirst])} ${norm(r[C.authLast])}`.trim()
|
|
207
|
-
kept.add(npi)
|
|
208
|
-
rows.push({ npi, name: primaryName, org, address: practice, auth })
|
|
209
|
-
|
|
210
|
-
for (const alt of altNames.get(npi)!) {
|
|
211
|
-
rows.push({ npi, name: alt, org: alt, address: practice, auth })
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const mailing = addr(r[C.mAddr]!, r[C.mCity]!, r[C.mState]!, r[C.mZip]!)
|
|
215
|
-
|
|
216
|
-
if (mailing && mailing !== practice) {
|
|
217
|
-
rows.push({ npi, name: primaryName, org, address: mailing, auth })
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const addressFrequency = {
|
|
224
|
-
total: addrTotal,
|
|
225
|
-
distinct: addrCounts.size,
|
|
226
|
-
frequency: (v: string) => (v ? (addrCounts.get(addressFrequencyKey(v)) ?? 0) / addrTotal : 0),
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
report?.(` ${kept.size} NPIs → ${rows.length} records; freq table ${addrCounts.size} distinct over ${addrTotal}`)
|
|
105
|
+
// --- Phases A + B: the variation-rich sample + the corpus-wide address-frequency table (the SHARED
|
|
106
|
+
// sample builder — the same records the dedup benchmark and the learned-scorer evals see). ---
|
|
107
|
+
const { rows, keptNpis, addressFrequency } = await buildNPPESSample(
|
|
108
|
+
{ registryPath: REGISTRY, otherNamesPath: OTHER_NAMES, state: STATE, maxNpis: NPIS },
|
|
109
|
+
report
|
|
110
|
+
)
|
|
230
111
|
|
|
231
112
|
// --- Phase C: geocode + ingest (NPI rides on record.id as the label). The heavy geocoder is
|
|
232
113
|
// injected (see ./eval-geocoder.ts) — the registry package never imports the runtime. ---
|
|
@@ -242,11 +123,11 @@ export async function trainDedupGBT(
|
|
|
242
123
|
attributes: { authorizedOfficial: "auth" },
|
|
243
124
|
}
|
|
244
125
|
|
|
245
|
-
const records: SourceRecord[] = await ingestRows(rows
|
|
246
|
-
geocodeAddress: geocoder.
|
|
126
|
+
const records: SourceRecord[] = await ingestRows(rows, mapping, {
|
|
127
|
+
geocodeAddress: geocoder.geocodeAddress,
|
|
247
128
|
})
|
|
248
129
|
|
|
249
|
-
geocoder.
|
|
130
|
+
geocoder[Symbol.dispose]()
|
|
250
131
|
const geocoded = records.filter((r) => r.address?.geocode).length
|
|
251
132
|
report?.(` ${records.length} records, ${geocoded} geocoded`)
|
|
252
133
|
|
|
@@ -274,7 +155,7 @@ export async function trainDedupGBT(
|
|
|
274
155
|
const rnd = makeLcg(20_260_615)
|
|
275
156
|
const split = new Map<string, "fit" | "holdout">()
|
|
276
157
|
|
|
277
|
-
for (const npi of
|
|
158
|
+
for (const npi of keptNpis) {
|
|
278
159
|
split.set(npi, rnd() < FIT_SPLIT_FRACTION ? "fit" : "holdout")
|
|
279
160
|
}
|
|
280
161
|
|
|
@@ -291,6 +172,7 @@ export async function trainDedupGBT(
|
|
|
291
172
|
const holdoutRecords = records.filter((r) => split.get(r.id) === "holdout")
|
|
292
173
|
const { pairs: holdoutPairs } = block(holdoutRecords, defaultBlockingKeys())
|
|
293
174
|
const holdoutScores = holdoutPairs.map(([a, b]) => calibScorer(a, b)).toSorted((p, q) => p - q)
|
|
175
|
+
const npiLabel = (rec: SourceRecord) => rec.id
|
|
294
176
|
let recommendedThreshold = 0
|
|
295
177
|
let bestF1 = -1
|
|
296
178
|
|
|
@@ -302,7 +184,7 @@ export async function trainDedupGBT(
|
|
|
302
184
|
threshold: t,
|
|
303
185
|
})
|
|
304
186
|
|
|
305
|
-
const f1 =
|
|
187
|
+
const f1 = scoreEntities(entities, npiLabel, holdoutRecords.length).f1
|
|
306
188
|
|
|
307
189
|
if (f1 > bestF1) {
|
|
308
190
|
bestF1 = f1
|
|
@@ -326,7 +208,7 @@ export async function trainDedupGBT(
|
|
|
326
208
|
locale: LOCALE,
|
|
327
209
|
trainedOn: TRAIN_DATE,
|
|
328
210
|
state: STATE,
|
|
329
|
-
npis:
|
|
211
|
+
npis: keptNpis.size,
|
|
330
212
|
records: records.length,
|
|
331
213
|
pairs: pairs.length,
|
|
332
214
|
posRate: Number(posRate.toFixed(4)),
|
|
@@ -334,8 +216,8 @@ export async function trainDedupGBT(
|
|
|
334
216
|
hyperparams,
|
|
335
217
|
recommendedThreshold: Number(recommendedThreshold.toFixed(4)), // F1-max link threshold (held-out); resolveEntities' default when learnedScorer is active
|
|
336
218
|
features: X[0]?.length ?? 0,
|
|
337
|
-
addressFrequencyDistinct:
|
|
338
|
-
addressFrequencyTotal:
|
|
219
|
+
addressFrequencyDistinct: addressFrequency.distinct,
|
|
220
|
+
addressFrequencyTotal: addressFrequency.total,
|
|
339
221
|
}
|
|
340
222
|
|
|
341
223
|
const moduleSource =
|
|
@@ -347,7 +229,7 @@ export async function trainDedupGBT(
|
|
|
347
229
|
` * GENERATED by \`mailwoman registry train-scorer gbt\` (registry/tools/train-gbt.ts) — DO NOT edit by hand; retrain to update.\n` +
|
|
348
230
|
` *\n` +
|
|
349
231
|
` * The default learned-scorer model (#603): a gradient-boosted-tree dedup scorer trained on the\n` +
|
|
350
|
-
` * NPPES NPI-truth set (${STATE}, ${
|
|
232
|
+
` * NPPES NPI-truth set (${STATE}, ${keptNpis.size} NPIs → ${pairs.length} candidate pairs). Validated to\n` +
|
|
351
233
|
` * generalize across states by learned-scorer-crossstate-eval.ts. Used by resolveEntities'\n` +
|
|
352
234
|
` * opt-in learnedScorer hook via createGBTScorer. The trained {@link GBT} is plain data.\n` +
|
|
353
235
|
` */\n\n` +
|
|
@@ -357,8 +239,8 @@ export async function trainDedupGBT(
|
|
|
357
239
|
`// prettier-ignore\n` +
|
|
358
240
|
`export const DEDUP_GBT_MODEL: GBT = ${JSON.stringify(model)}\n`
|
|
359
241
|
|
|
360
|
-
|
|
361
|
-
|
|
242
|
+
await makeDirectories(dirname(OUT))
|
|
243
|
+
await writeLocalFile(moduleSource, OUT)
|
|
362
244
|
report?.(`[written] ${OUT} (${(moduleSource.length / 1024).toFixed(0)} KB)`)
|
|
363
245
|
|
|
364
246
|
return { out: OUT, pairs: pairs.length, recommendedThreshold, heldOutF1: bestF1 }
|
|
@@ -0,0 +1,179 @@
|
|
|
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 (Phases A/B here), then the
|
|
15
|
+
* SHARED `trainCrossSourceModel` runs Phases C–F — geocode → block the UNION, keep only
|
|
16
|
+
* CROSS-source pairs → the SHARED featurizer → label by CCN → held-out-CCN calibration (max
|
|
17
|
+
* recall s.t. precision ≥ bar) → train on all pairs → emit
|
|
18
|
+
* `registry/models/org-crosssource-gbt-en-us.ts`.
|
|
19
|
+
*
|
|
20
|
+
* Sources (both public domain, direct CSVs):
|
|
21
|
+
*
|
|
22
|
+
* - `cms-pos_hospital-other_*.csv` — Provider of Services (PRVDR_NUM, FAC_NAME, ST_ADR…).
|
|
23
|
+
* - `cms-carecompare_hospital-general_*.csv` — Care Compare (Facility ID, Facility Name, Address…).
|
|
24
|
+
*
|
|
25
|
+
* Run: `mailwoman registry train-scorer org-cross-gbt [--cap 6000] [--precision-bar 0.95]
|
|
26
|
+
* [--wof <admin.db>] [--data-root <dir>] [--out registry/models/org-crosssource-gbt-en-us.ts]`
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { dataRootPath } from "@mailwoman/core/data-root"
|
|
30
|
+
import { isoDate } from "@mailwoman/core/utils"
|
|
31
|
+
|
|
32
|
+
import { addressFrequencyKey, streamRows } from "#index"
|
|
33
|
+
import type { EvalGeocoderFactory } from "#tools/eval-geocoder"
|
|
34
|
+
import { addr, norm, trainCrossSourceModel, type CrossSourceRow } from "#tools/shared"
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Options for {@linkcode trainOrgCrossSourceGBT}.
|
|
38
|
+
*/
|
|
39
|
+
export interface TrainOrgCrossSourceGBTOptions {
|
|
40
|
+
/**
|
|
41
|
+
* The injected geocoder factory (the command wires `mailwoman/geocode-core`; see `./eval-geocoder.ts`).
|
|
42
|
+
*/
|
|
43
|
+
createGeocoder: EvalGeocoderFactory
|
|
44
|
+
/**
|
|
45
|
+
* Record-matcher sources directory. Default `$MAILWOMAN_DATA_ROOT/record-matcher/sources`.
|
|
46
|
+
*/
|
|
47
|
+
sources?: string
|
|
48
|
+
/**
|
|
49
|
+
* Care Compare facilities sampled. Default 6000.
|
|
50
|
+
*/
|
|
51
|
+
cap?: number
|
|
52
|
+
/**
|
|
53
|
+
* Output TS module path. Default `registry/models/org-crosssource-gbt-en-us.ts`.
|
|
54
|
+
*/
|
|
55
|
+
out?: string
|
|
56
|
+
/**
|
|
57
|
+
* Locale recorded in the model meta. Default en-US.
|
|
58
|
+
*/
|
|
59
|
+
locale?: string
|
|
60
|
+
/**
|
|
61
|
+
* #655 threshold rule: max cross-source recall subject to this held-out pairwise precision. Default 0.95.
|
|
62
|
+
*/
|
|
63
|
+
precisionBar?: number
|
|
64
|
+
/**
|
|
65
|
+
* Training date stamped into the meta. Default today.
|
|
66
|
+
*/
|
|
67
|
+
date?: string
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Train + emit the org-level cross-source link GBT — see the module doc. The CCN is the cross-system facility key; it
|
|
72
|
+
* rides {@link CrossSourceRow.npi} → `record.id` as the held-out label.
|
|
73
|
+
*/
|
|
74
|
+
export async function trainOrgCrossSourceGBT(
|
|
75
|
+
options: TrainOrgCrossSourceGBTOptions,
|
|
76
|
+
report?: (line: string) => void
|
|
77
|
+
): Promise<{ out: string; pairs: number; recommendedThreshold: number }> {
|
|
78
|
+
const SOURCES = options.sources || String(dataRootPath("record-matcher", "sources"))
|
|
79
|
+
const CAP = options.cap ?? 6000
|
|
80
|
+
const OUT = options.out || "packages/registry/lib/models/org-crosssource-gbt-en-us.ts"
|
|
81
|
+
const LOCALE = options.locale || "en-US"
|
|
82
|
+
// #655 threshold rule: max cross-source recall subject to this held-out pairwise precision.
|
|
83
|
+
const PRECISION_BAR = options.precisionBar ?? 0.95
|
|
84
|
+
const TRAIN_DATE = options.date || isoDate()
|
|
85
|
+
|
|
86
|
+
const POS = `${SOURCES}/cms-pos_hospital-other_2026q1.csv`
|
|
87
|
+
const CARE_COMPARE = `${SOURCES}/cms-carecompare_hospital-general_20260706.csv`
|
|
88
|
+
|
|
89
|
+
// --- Phase A: Care Compare (Facility ID + name + address). ---
|
|
90
|
+
report?.("[A] streaming Care Compare…")
|
|
91
|
+
const ccByID = new Map<string, CrossSourceRow>()
|
|
92
|
+
|
|
93
|
+
for await (const r of streamRows(CARE_COMPARE)) {
|
|
94
|
+
if (ccByID.size >= CAP) break
|
|
95
|
+
const ccn = norm(r["Facility ID"])
|
|
96
|
+
const name = norm(r["Facility Name"])
|
|
97
|
+
const address = addr(r["Address"]!, r["City/Town"]!, r["State"]!, r["ZIP Code"]!)
|
|
98
|
+
|
|
99
|
+
if (!ccn || !name || !address) continue
|
|
100
|
+
ccByID.set(ccn, { npi: ccn, name, org: name, address, source: "care-compare" })
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
report?.(` ${ccByID.size} Care Compare facilities`)
|
|
104
|
+
|
|
105
|
+
// --- Phase B: the SAME CCNs from the POS file + the corpus-wide address-frequency table. ---
|
|
106
|
+
report?.("[B] streaming POS + building the frequency table…")
|
|
107
|
+
const rows: CrossSourceRow[] = []
|
|
108
|
+
const joined = new Set<string>()
|
|
109
|
+
const addrCounts = new Map<string, number>()
|
|
110
|
+
let addrTotal = 0
|
|
111
|
+
|
|
112
|
+
for await (const r of streamRows(POS)) {
|
|
113
|
+
const address = addr(r["ST_ADR"]!, r["CITY_NAME"]!, r["STATE_CD"]!, r["ZIP_CD"]!)
|
|
114
|
+
|
|
115
|
+
if (address) {
|
|
116
|
+
const k = addressFrequencyKey(address)
|
|
117
|
+
addrCounts.set(k, (addrCounts.get(k) ?? 0) + 1)
|
|
118
|
+
|
|
119
|
+
addrTotal++
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const ccn = norm(r["PRVDR_NUM"])
|
|
123
|
+
|
|
124
|
+
if (!ccn || !ccByID.has(ccn) || joined.has(ccn) || !address) continue
|
|
125
|
+
const name = norm(r["FAC_NAME"])
|
|
126
|
+
|
|
127
|
+
if (!name) continue
|
|
128
|
+
joined.add(ccn)
|
|
129
|
+
rows.push({ npi: ccn, name, org: name, address, source: "cms-pos" })
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
for (const ccn of joined) {
|
|
133
|
+
rows.push(ccByID.get(ccn)!)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const addressFrequency = {
|
|
137
|
+
total: addrTotal,
|
|
138
|
+
distinct: addrCounts.size,
|
|
139
|
+
frequency: (v: string) => (v ? (addrCounts.get(addressFrequencyKey(v)) ?? 0) / addrTotal : 0),
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
report?.(` ${joined.size} CCN-joined facilities → ${rows.length} records`)
|
|
143
|
+
|
|
144
|
+
// --- Phases C–F: the SHARED cross-source trainer (geocode → cross-source pairs → #655 calibration
|
|
145
|
+
// → shipped model → committed module). ---
|
|
146
|
+
return trainCrossSourceModel({
|
|
147
|
+
createGeocoder: options.createGeocoder,
|
|
148
|
+
rows,
|
|
149
|
+
joined,
|
|
150
|
+
addressFrequency,
|
|
151
|
+
sources: ["cms-pos", "care-compare"],
|
|
152
|
+
precisionBar: PRECISION_BAR,
|
|
153
|
+
out: OUT,
|
|
154
|
+
exportPrefix: "ORG_CROSS_SOURCE_GBT",
|
|
155
|
+
moduleDoc:
|
|
156
|
+
` * The ORG-LEVEL cross-source link scorer (#655 follow-on) — trained on CCN-joined CMS POS ↔\n` +
|
|
157
|
+
` * Care Compare facility pairs (both public domain). Scores "same facility, different registry\n` +
|
|
158
|
+
` * text" links for org-level cross-dataset flows. Generated by\n` +
|
|
159
|
+
` * \`mailwoman registry train-scorer org-cross-gbt\` (registry/tools/train-org-cross-gbt.ts) — retrain rather than editing.\n`,
|
|
160
|
+
meta: (figures) => ({
|
|
161
|
+
version: "1.0.0",
|
|
162
|
+
objective: "org-cross-source-link",
|
|
163
|
+
locale: LOCALE,
|
|
164
|
+
trainedOn: TRAIN_DATE,
|
|
165
|
+
facilities: joined.size,
|
|
166
|
+
records: figures.records,
|
|
167
|
+
pairs: figures.pairs,
|
|
168
|
+
posRate: figures.posRate,
|
|
169
|
+
precisionBar: PRECISION_BAR,
|
|
170
|
+
holdoutBarRecall: figures.barRecall,
|
|
171
|
+
holdoutF1Max: figures.f1Max,
|
|
172
|
+
hyperparams: figures.hyperparams,
|
|
173
|
+
recommendedThreshold: figures.recommendedThreshold,
|
|
174
|
+
features: figures.features,
|
|
175
|
+
sources: ["cms-pos", "care-compare"],
|
|
176
|
+
}),
|
|
177
|
+
report,
|
|
178
|
+
})
|
|
179
|
+
}
|