#!/usr/bin/env node // scripts/texas-report.js - Brief 63b, R1 and R2: the Texas Hospital Leadership Report, computed from public CMS data. // Every number on /reports/texas-2026/ comes from this file. Two steps: // node scripts/texas-report.js extract reads the five CMS files (downloaded from the URLs in SOURCES) and writes // data/texas-2026/hospitals.csv: one row per US hospital, only the // facility-level fields the analysis uses. No patient data exists in any file. // node scripts/texas-report.js computes data/texas-2026/report.json and data/texas-2026/tables.csv // from hospitals.csv (the build runs this step; it needs no network). // Regions are sets of Texas counties (REGIONS). "Rural" is where the hospital is (scripts/rural.js, the Lab's rule): its county // lies outside every Metropolitan Statistical Area in the OMB delineation of July 2023, or it is a Critical Access or Rural // Emergency Hospital. (Until 25 November 2026 it was the cost report's payment class, which called Parkland, Dallas, rural; from // 25 to 28 November the Provider of Services CBSA indicator, which carries the payment CBSA for some hospitals.) 'use strict'; const fs = require('fs'); const path = require('path'); const ROOT = path.join(__dirname, '..'); const OUT = path.join(ROOT, 'data/texas-2026'); const SOURCES = [ { id: 'xubh-q36u', title: 'Hospital General Information', file: 'Hospital_General_Information_2.csv', modified: '2026-10-26', released: '2026-11-12', uses: 'overall star rating, hospital type, ownership, emergency services, mortality and readmission group counts' }, { id: 'dgck-syfz', title: 'Patient survey (HCAHPS) - Hospital', file: 'HCAHPS-Hospital.csv', modified: '2026-10-19', released: '2026-11-12', uses: 'HCAHPS summary star rating (H_STAR_RATING)' }, { id: '9n3s-kdb3', title: 'Hospital Readmissions Reduction Program', file: 'FY_2026_Hospital_Readmissions_Reduction_Program_Hospital.csv', modified: '2026-01-26', released: '2026-11-12', uses: 'Excess Readmission Ratio for the six HRRP conditions (FY 2026)' }, { id: 'yq43-i98g', title: 'Hospital-Acquired Condition (HAC) Reduction Program', file: 'FY_2026_HAC_Reduction_Program_Hospital.csv', modified: '2026-01-26', released: '2026-11-12', uses: 'Total HAC Score and Payment Reduction (FY 2026)' }, { id: 'ynj2-r877', title: 'Complications and Deaths - Hospital', file: 'Complications_and_Deaths-Hospital.csv', modified: '2026-10-20', released: '2026-11-12', uses: 'checked: the six 30-day death rates read "Not Available" for every hospital in this release, so the mortality figures use the mortality group counts in Hospital General Information' } ].map(function (s) { s.url = 'https://data.cms.gov/provider-data/dataset/' + s.id; return s; }); const MORT = ['MORT_30_AMI', 'MORT_30_HF', 'MORT_30_PN', 'MORT_30_STK', 'MORT_30_COPD', 'MORT_30_CABG']; const MORT_NAMES = { MORT_30_AMI: 'Death rate for heart attack patients', MORT_30_HF: 'Death rate for heart failure patients', MORT_30_PN: 'Death rate for pneumonia patients', MORT_30_STK: 'Death rate for stroke patients', MORT_30_COPD: 'Death rate for COPD patients', MORT_30_CABG: 'Death rate for CABG surgery patients' }; const REGIONS = [ ['houston', 'Houston and Fort Bend', ['Harris', 'Fort Bend', 'Montgomery', 'Brazoria', 'Galveston', 'Liberty', 'Waller', 'Chambers', 'Austin']], ['dfw', 'Dallas-Fort Worth', ['Dallas', 'Tarrant', 'Collin', 'Denton', 'Ellis', 'Johnson', 'Kaufman', 'Parker', 'Rockwall', 'Hunt', 'Wise']], ['austin', 'Austin', ['Travis', 'Williamson', 'Hays', 'Bastrop', 'Caldwell']], ['sanantonio', 'San Antonio', ['Bexar', 'Comal', 'Guadalupe', 'Medina', 'Wilson', 'Atascosa', 'Kendall', 'Bandera']], ['rgv', 'Rio Grande Valley', ['Hidalgo', 'Cameron', 'Starr', 'Willacy']], ['west', 'West Texas', ['El Paso', 'Lubbock', 'Midland', 'Ector', 'Potter', 'Randall', 'Taylor', 'Tom Green']] ]; const COLS = ['ccn', 'name', 'city', 'state', 'county', 'type', 'ownership', 'emergency', 'stars', 'hcahps', 'hrrp_reported', 'hrrp_above', 'hac_scored', 'hac_reduction', 'mort_compared', 'mort_better', 'mort_worse', 'rural']; // A compact CSV reader (CMS files quote fields that hold commas). function parseCSV(text) { const rows = []; let row = [], f = '', q = false; for (let i = 0; i < text.length; i++) { const c = text[i]; if (q) { if (c === '"') { if (text[i + 1] === '"') { f += '"'; i++; } else q = false; } else f += c; continue; } if (c === '"') q = true; else if (c === ',') { row.push(f); f = ''; } else if (c === '\n') { row.push(f.replace(/\r$/, '')); rows.push(row); row = []; f = ''; } else f += c; } if (f || row.length) { row.push(f); rows.push(row); } const head = rows.shift().map(function (h) { return h.replace(/^/, ''); }); return rows.filter(function (r) { return r.length > 1; }).map(function (r) { const o = {}; head.forEach(function (h, j) { o[h] = r[j]; }); return o; }); } function csvLine(v) { return v.map(function (x) { x = x === undefined || x === null ? '' : String(x); return /[",\n]/.test(x) ? '"' + x.replace(/"/g, '""') + '"' : x; }).join(','); } function num(x) { const n = parseFloat(x); return isFinite(n) ? n : null; } function ccn6(x) { return String(x || '').trim().padStart(6, '0'); } function extract(dir) { const read = function (f) { return parseCSV(fs.readFileSync(path.join(dir, f), 'utf8')); }; const gen = read(SOURCES[0].file), H = {}; gen.forEach(function (r) { H[ccn6(r['Facility ID'])] = { ccn: ccn6(r['Facility ID']), name: r['Facility Name'], city: r['City/Town'], state: r.State, county: r['County/Parish'], type: r['Hospital Type'], ownership: r['Hospital Ownership'], emergency: r['Emergency Services'], stars: num(r['Hospital overall rating']), hcahps: null, hrrp_reported: 0, hrrp_above: 0, hac_scored: 0, hac_reduction: 0, mort_compared: num(r['Count of Facility MORT Measures']) || 0, mort_better: num(r['Count of MORT Measures Better']) || 0, mort_worse: num(r['Count of MORT Measures Worse']) || 0, rural: '' }; }); read(SOURCES[1].file).forEach(function (r) { const h = H[ccn6(r['Facility ID'])]; if (h && r['HCAHPS Measure ID'] === 'H_STAR_RATING') h.hcahps = num(r['Patient Survey Star Rating']); }); read(SOURCES[2].file).forEach(function (r) { const h = H[ccn6(r['Facility ID'])], e = num(r['Excess Readmission Ratio']); if (h && e !== null) { h.hrrp_reported++; if (e > 1) h.hrrp_above++; } }); read(SOURCES[3].file).forEach(function (r) { const h = H[ccn6(r['Facility ID'])]; if (h && (r['Payment Reduction'] === 'Yes' || r['Payment Reduction'] === 'No')) { h.hac_scored = 1; h.hac_reduction = r['Payment Reduction'] === 'Yes' ? 1 : 0; } }); // Complications and Deaths: count the six 30-day death rates that carry a comparison (none do in the 12 November 2026 release). let mortAvailable = 0; read(SOURCES[4].file).forEach(function (r) { if (MORT.indexOf(r['Measure ID']) >= 0 && /Better|No Different|Worse/.test(r['Compared to National'])) mortAvailable++; }); SOURCES[4].mortComparedRows = mortAvailable; // Rural or urban, from the hospital's own /reports/ data, by location (scripts/rural.js). Object.keys(H).forEach(function (k) { const h = H[k], f = path.join(ROOT, 'data/reports', String(h.state || '').toLowerCase(), k + '.json'); if (fs.existsSync(f)) { try { h.rural = require('./rural').correct(JSON.parse(fs.readFileSync(f, 'utf8'))).ruralUrban || ''; } catch (e) { /* the field stays empty */ } } }); fs.mkdirSync(OUT, { recursive: true }); fs.writeFileSync(path.join(OUT, 'hospitals.csv'), [csvLine(COLS)].concat(Object.keys(H).sort().map(function (k) { return csvLine(COLS.map(function (c) { return H[k][c]; })); })).join('\n') + '\n'); fs.writeFileSync(path.join(OUT, 'sources.json'), JSON.stringify(SOURCES, null, 2) + '\n'); return Object.keys(H).length; } function load() { return parseCSV(fs.readFileSync(path.join(OUT, 'hospitals.csv'), 'utf8')).map(function (r) { ['stars', 'hcahps'].forEach(function (k) { r[k] = num(r[k]); }); ['hrrp_reported', 'hrrp_above', 'hac_scored', 'hac_reduction', 'mort_compared', 'mort_better', 'mort_worse'].forEach(function (k) { r[k] = parseInt(r[k], 10) || 0; }); return r; }); } function regionOf(h) { if (h.state !== 'TX') return null; for (let i = 0; i < REGIONS.length; i++) if (REGIONS[i][2].some(function (c) { return c.toLowerCase() === String(h.county).toLowerCase(); })) return REGIONS[i][0]; return (h.rural === 'Rural' || (!h.rural && h.type === 'Critical Access Hospitals')) ? 'rural' : 'other'; } function pct(a, b) { return b ? Math.round(1000 * a / b) / 10 : null; } // The measures for one group of hospitals. function stats(list) { const rated = list.filter(function (h) { return h.stars !== null; }), hc = list.filter(function (h) { return h.hcahps !== null; }); const hr = list.filter(function (h) { return h.hrrp_reported > 0; }), hac = list.filter(function (h) { return h.hac_scored; }), mo = list.filter(function (h) { return h.mort_compared > 0; }); const sum = function (l, k) { return l.reduce(function (a, h) { return a + h[k]; }, 0); }; return { hospitals: list.length, rated: rated.length, stars45: rated.filter(function (h) { return h.stars >= 4; }).length, stars5: rated.filter(function (h) { return h.stars === 5; }).length, stars45Pct: pct(rated.filter(function (h) { return h.stars >= 4; }).length, rated.length), meanStars: rated.length ? Math.round(100 * sum(rated, 'stars') / rated.length) / 100 : null, hcahpsRated: hc.length, hcahps45: hc.filter(function (h) { return h.hcahps >= 4; }).length, hcahps45Pct: pct(hc.filter(function (h) { return h.hcahps >= 4; }).length, hc.length), hrrpHospitals: hr.length, hrrpResults: sum(hr, 'hrrp_reported'), hrrpAbove: sum(hr, 'hrrp_above'), hrrpAbovePct: pct(sum(hr, 'hrrp_above'), sum(hr, 'hrrp_reported')), hrrpNoneAbove: hr.filter(function (h) { return h.hrrp_above === 0; }).length, hrrpNoneAbovePct: pct(hr.filter(function (h) { return h.hrrp_above === 0; }).length, hr.length), hacScored: hac.length, hacReduction: sum(hac, 'hac_reduction'), hacClearPct: pct(hac.length - sum(hac, 'hac_reduction'), hac.length), mortHospitals: mo.length, mortResults: sum(mo, 'mort_compared'), mortBetter: sum(mo, 'mort_better'), mortWorse: sum(mo, 'mort_worse'), mortBetterHosp: mo.filter(function (h) { return h.mort_better > 0; }).length, mortBetterHospPct: pct(mo.filter(function (h) { return h.mort_better > 0; }).length, mo.length) }; } function compute() { const all = load(), tx = all.filter(function (h) { return h.state === 'TX'; }); const groups = { national: stats(all), texas: stats(tx), regions: {}, types: {}, ownership: {} }; REGIONS.map(function (r) { return [r[0], r[1]]; }).concat([['rural', 'Rural Texas'], ['other', 'Other urban Texas']]).forEach(function (r) { groups.regions[r[0]] = Object.assign({ label: r[1] }, stats(tx.filter(function (h) { return regionOf(h) === r[0]; }))); }); [['acute', 'Acute care hospitals', 'Acute Care Hospitals'], ['cah', 'Critical access hospitals', 'Critical Access Hospitals']].forEach(function (t) { groups.types[t[0]] = { label: t[1], texas: stats(tx.filter(function (h) { return h.type === t[2]; })), national: stats(all.filter(function (h) { return h.type === t[2]; })) }; }); const own = function (h) { return /^Voluntary/.test(h.ownership) ? 'nonprofit' : /^Government/.test(h.ownership) ? 'government' : h.ownership === 'Proprietary' ? 'proprietary' : h.ownership === 'Physician' ? 'physician' : 'other'; }; [['nonprofit', 'Non-profit'], ['proprietary', 'For-profit (proprietary)'], ['government', 'Government'], ['physician', 'Physician-owned']].forEach(function (o) { groups.ownership[o[0]] = { label: o[1], texas: stats(tx.filter(function (h) { return own(h) === o[0]; })), national: stats(all.filter(function (h) { return own(h) === o[0]; })) }; }); // The top performers: Texas hospitals with a five-star CMS overall rating (named only in this positive table). const top = tx.filter(function (h) { return h.stars === 5; }).map(function (h) { return { ccn: h.ccn, name: h.name, city: h.city, type: h.type }; }) .sort(function (a, b) { return a.city.localeCompare(b.city) || a.name.localeCompare(b.name); }); return { title: 'Texas Hospital Leadership Report', edition: 'November 2026', datePublished: '2026-11-23', sources: SOURCES, regions: REGIONS.map(function (r) { return { id: r[0], label: r[1], counties: r[2] }; }), mort: MORT_NAMES, groups: groups, top: top }; } function tables(r) { const lines = [csvLine(['group', 'hospitals', 'rated_overall', 'overall_4_or_5_stars_pct', 'mean_overall_stars', 'hcahps_rated', 'hcahps_4_or_5_stars_pct', 'hrrp_results', 'hrrp_above_expected_pct', 'hac_scored', 'hac_no_payment_reduction_pct', 'mortality_results', 'mortality_better_than_national', 'mortality_worse_than_national'])]; const row = function (label, s) { lines.push(csvLine([label, s.hospitals, s.rated, s.stars45Pct, s.meanStars, s.hcahpsRated, s.hcahps45Pct, s.hrrpResults, s.hrrpAbovePct, s.hacScored, s.hacClearPct, s.mortResults, s.mortBetter, s.mortWorse])); }; row('United States', r.groups.national); row('Texas', r.groups.texas); Object.keys(r.groups.regions).forEach(function (k) { row('Texas region: ' + r.groups.regions[k].label, r.groups.regions[k]); }); Object.keys(r.groups.types).forEach(function (k) { row('Texas: ' + r.groups.types[k].label, r.groups.types[k].texas); row('United States: ' + r.groups.types[k].label, r.groups.types[k].national); }); Object.keys(r.groups.ownership).forEach(function (k) { row('Texas: ' + r.groups.ownership[k].label, r.groups.ownership[k].texas); row('United States: ' + r.groups.ownership[k].label, r.groups.ownership[k].national); }); return lines.join('\n') + '\n'; } // R2: the findings, one sentence each, written from the numbers (the Canon keys tx26.f1 to tx26.f7 hold the same sentences; a test // fails if the two ever differ, so every number on the page traces to this code). function P(x) { return (Math.round(x * 10) / 10).toFixed(1) + '%'; } function findings(r) { const T = r.groups.texas, N = r.groups.national, R = r.groups.regions; const all = Object.keys(R).map(function (k) { return R[k]; }), best = all.slice().sort(function (a, b) { return b.stars45Pct - a.stars45Pct; })[0], hou = R.houston; return [ ['tx26.f1', T.stars45 + ' of Texas\'s ' + T.rated + ' rated hospitals, ' + P(T.stars45Pct) + ', hold a four- or five-star CMS overall rating, against ' + P(N.stars45Pct) + ' of rated hospitals nationally.'], ['tx26.f2', best.label + ' leads the Texas regions on star ratings: ' + best.stars45 + ' of its ' + best.rated + ' rated hospitals, ' + P(best.stars45Pct) + ', hold four or five stars.'], ['tx26.f3', 'In the Houston and Fort Bend region, CMS reports ' + hou.mortBetter + ' mortality results better than the national rate and ' + hou.mortWorse + ' worse, across ' + hou.mortHospitals + ' hospitals.'], ['tx26.f4', P(T.hacClearPct) + ' of the Texas hospitals scored in the fiscal 2026 Hospital-Acquired Condition Reduction Program, ' + (T.hacScored - T.hacReduction) + ' of ' + T.hacScored + ', received no payment reduction, against ' + P(N.hacClearPct) + ' nationally.'], ['tx26.f5', P(T.hcahps45Pct) + ' of Texas hospitals with a patient survey star rating, ' + T.hcahps45 + ' of ' + T.hcahpsRated + ', hold four or five stars, against ' + P(N.hcahps45Pct) + ' nationally.'], ['tx26.f6', P(T.hrrpAbovePct) + ' of Texas hospitals\' fiscal 2026 readmission results, ' + T.hrrpAbove + ' of ' + T.hrrpResults + ', came in above the expected rate, adjusted for patient mix, against ' + P(N.hrrpAbovePct) + ' nationally.'], ['tx26.f7', 'CMS\'s proposed Medicare Advantage rule of 20 November 2026 (CMS-4214-P2) concerns health plans\' Star Ratings, not hospital star ratings: it narrows plan ratings to 15 measures that emphasize clinical outcomes and patient experience, which gives plans that stand to lose quality bonuses a reason to press their hospital networks on those same two areas.'] ].map(function (x) { return { id: x[0], text: x[1] }; }); } function esc(s) { return String(s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } function cell(v) { return v === null || v === undefined ? 'n/a' : esc(v); } // R3: the tables and charts (inline SVG with a text alternative), from report.json. function bars(id, title, rows, nat) { // rows: [label, value, 'us'?]; nat: the national figure drawn as a line, or null when each row carries its own comparison const W = 680, rowH = 30, top = 10, left = 330, H = top + rows.length * rowH + (nat === null ? 10 : 30), scale = function (v) { return Math.round((W - left - 70) * v / 100); }; const alt = title + '. ' + rows.map(function (r) { return r[0] + ': ' + P(r[1]); }).join('; ') + (nat === null ? '.' : '. United States: ' + P(nat) + '.'); let svg = '' + esc(title) + '' + esc(alt) + ''; rows.forEach(function (r, i) { const y = top + i * rowH; svg += '' + esc(r[0]) + '' + '' + '' + P(r[1]) + ''; }); if (nat !== null) { const x = left + scale(nat), yb = top + rows.length * rowH; svg += 'United States ' + P(nat) + ''; } return '
' + svg + '
' + esc(title) + '
'; } function table(caption, head, rows) { return '
' + head.map(function (h, i) { return ''; }).join('') + '' + rows.map(function (r) { return '' + r.slice(1).map(function (v) { return ''; }).join('') + ''; }).join('') + '
' + esc(caption) + '
' + esc(h) + '
' + esc(r[0]) + '' + cell(v) + '
'; } const HEAD = ['Group', 'Hospitals', 'Rated, overall', '4 or 5 stars', 'Patient survey 4 or 5 stars', 'Readmission results above expected', 'No HAC payment reduction', 'Mortality results better / worse']; function line(label, s) { return [label, s.hospitals, s.rated, s.stars45Pct === null ? null : P(s.stars45Pct), s.hcahps45Pct === null ? null : P(s.hcahps45Pct), s.hrrpAbovePct === null ? null : P(s.hrrpAbovePct), s.hacClearPct === null ? null : P(s.hacClearPct), s.mortBetter + ' / ' + s.mortWorse]; } function block(which) { const r = JSON.parse(fs.readFileSync(path.join(OUT, 'report.json'), 'utf8')), G = r.groups; if (which === 'charts') { const T = G.texas, N = G.national, pair = function (label, k) { return [[label + ', Texas', T[k]], [label + ', United States', N[k], 'us']]; }; return bars('tx-c1', 'Share of rated hospitals with a four- or five-star CMS overall rating, by Texas region', Object.keys(G.regions).map(function (k) { return [G.regions[k].label, G.regions[k].stars45Pct]; }), N.stars45Pct) + bars('tx-c2', 'Texas and the United States on four measures', [].concat(pair('4 or 5 stars overall', 'stars45Pct'), pair('Patient survey 4 or 5 stars', 'hcahps45Pct'), pair('No HAC payment reduction', 'hacClearPct'), pair('Readmissions above expected', 'hrrpAbovePct')), null); } if (which === 'regions') return table('Texas by region, with Texas and the United States', HEAD, [line('United States', G.national), line('Texas', G.texas)].concat(Object.keys(G.regions).map(function (k) { return line(G.regions[k].label, G.regions[k]); }))); if (which === 'types') return table('By hospital type, Texas and the United States', HEAD, [].concat.apply([], Object.keys(G.types).map(function (k) { return [line('Texas: ' + G.types[k].label, G.types[k].texas), line('United States: ' + G.types[k].label, G.types[k].national)]; }))); if (which === 'ownership') return table('By ownership, Texas and the United States', HEAD, [].concat.apply([], Object.keys(G.ownership).map(function (k) { return [line('Texas: ' + G.ownership[k].label, G.ownership[k].texas), line('United States: ' + G.ownership[k].label, G.ownership[k].national)]; }))); if (which === 'top') return '
' + r.top.map(function (h) { return ''; }).join('') + '
Texas hospitals with a five-star CMS overall rating (' + r.top.length + '), by city
HospitalCityType
' + esc(title(h.name)) + '' + esc(title(h.city)) + '' + esc(h.type) + '
'; if (which === 'sources') return table('The CMS data sets', ['Data set', 'CMS id', 'Released', 'Last modified', 'Used for'], r.sources.map(function (x) { return [x.title, x.id, x.released, x.modified, x.uses]; })) .replace(/([a-z0-9]{4}-[a-z0-9]{4})<\/td>/g, '$1'); if (which === 'mortnames') return ''; if (which === 'regiondefs') return ''; throw new Error('texas-report block: unknown "' + which + '"'); } // Hospital names in CMS files are in capitals; shown in title case, keeping short words and initials as CMS writes them. function title(s) { return String(s).toLowerCase().replace(/\s+/g, ' ').replace(/\b([a-z])([a-z']*)/g, function (_, a, b) { return a.toUpperCase() + b; }) .replace(/\b(Va|Ut|Hca|Copd|Cabg)\b/g, function (w) { return w.toUpperCase(); }).replace(/\b(Of|And|At|The|De)\b(?!$)/g, function (w, _, i) { return i === 0 ? w : w.toLowerCase(); }); } function run() { const r = compute(); fs.writeFileSync(path.join(OUT, 'report.json'), JSON.stringify(r, null, 2) + '\n'); fs.writeFileSync(path.join(OUT, 'tables.csv'), tables(r)); return r; } if (require.main === module) { if (process.argv[2] === 'extract') console.log('hospitals:', extract(process.argv[3])); const r = run(); console.log(JSON.stringify({ texas: r.groups.texas, national: r.groups.national, top: r.top.length }, null, 1)); } module.exports = { findings: findings, block: block, title: title, P: P, compute: compute, run: run, tables: tables, parseCSV: parseCSV, SOURCES: SOURCES, REGIONS: REGIONS, regionOf: regionOf, stats: stats, load: load };