Metadata-Version: 2.4
Name: neissai
Version: 1.7.1
Summary: AI-assisted national injury estimates from NEISS emergency department narratives: consistent e-bike and e-scooter counts across product-code changes, with survey-correct confidence intervals.
Author: Subash Kumar
License-Expression: MIT
Project-URL: Homepage, https://github.com/subash28kumar/neiss-ai
Project-URL: Dashboard, https://subash28kumar.github.io/neiss-ai/
Project-URL: Documentation, https://github.com/subash28kumar/neiss-ai#readme
Project-URL: Changelog, https://github.com/subash28kumar/neiss-ai/blob/main/CHANGELOG.md
Keywords: NEISS,injury surveillance,e-bike,e-scooter,micromobility,text classification,survey statistics
Classifier: Programming Language :: Python :: 3
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pandas>=1.5
Requires-Dist: numpy>=1.22
Provides-Extra: train
Requires-Dist: scikit-learn>=1.1; extra == "train"
Provides-Extra: excel
Requires-Dist: openpyxl; extra == "excel"
Provides-Extra: briefs
Requires-Dist: matplotlib; extra == "briefs"
Requires-Dist: reportlab; extra == "briefs"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# NEISS-AI

[![Tests](https://github.com/subash28kumar/neiss-ai/actions/workflows/tests.yml/badge.svg)](https://github.com/subash28kumar/neiss-ai/actions/workflows/tests.yml) [![PyPI](https://img.shields.io/pypi/v/neissai)](https://pypi.org/project/neissai/) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.23306744.svg)](https://doi.org/10.5281/zenodo.23306744) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/subash28kumar/neiss-ai/blob/main/notebooks/NEISS-AI_Quickstart.ipynb)

**AI-assisted national injury estimates from US emergency department narratives.**

NEISS-AI turns the US Consumer Product Safety Commission's [NEISS](https://www.cpsc.gov/Research--Statistics/NEISS-Injury-Data) data into consistent national estimates for e-bikes, e-scooters, bicycles and other micromobility devices, with survey-correct 95% confidence intervals.

**Live web app:** https://subash28kumar.github.io/neiss-ai/

The web app has four parts, all free and running in the browser:

- **Overview:** headline numbers and trends for e-bikes, e-scooters and kick scooters.
- **Ask a question:** type a question in plain English (for example, *"How many teenagers broke a bone on an e-scooter in 2025?"*) and get a national estimate with a 95% confidence interval, a trend chart and an "Understood as" line showing how the question was read.
- **Explore the data:** filter by device, years, age, sex, injury, body part, outcome and circumstance; download the results as CSV; share a link to any view.
- **Search injury stories:** search the hospital narratives (for example *battery*, *hit by car*) to see national estimates by year and real example stories.

The site refreshes itself: a GitHub Actions workflow (`.github/workflows/refresh.yml`) checks monthly for a new year of NEISS data, reruns the tool and republishes the site. It never publishes fewer years than the site already shows.

## Why it exists

In January 2024, CPSC gave e-bikes their own product code (5045). Before then, e-bikes shared code 3215 ("mopeds or power-assisted cycles") with gas mopeds. Counts based on codes alone therefore jump at 2024 and cannot show a true trend.

NEISS-AI reads the short injury story in each record:

1. **Rules.** Product codes and whole-word keywords label each injury (e-bike, e-scooter, bicycle, kick scooter, hoverboard, skateboard or moped). Whole-word matching matters: "the bike" is not an "e bike".
2. **Text classifier.** For code-3215 records whose story is unclear, a classifier trained on 2024-2025 records (where 5045 and 5046 are separate) decides e-bike vs moped. It was tested on 833 unseen 2024-2025 records (98% right); it has not yet been tested on the harder unclear 2019-2023 notes where it is used (14-57% of each of those years' e-bike estimates). It is TF-IDF + logistic regression, stored as plain JSON and run with numpy only. Trained with a fixed 70/15/15 train/validation/test split and scored once on the locked test (833 stories): AUC **0.998**, accuracy 98.0% (see `validation/classifier_v2/RESULTS.md`).
3. **National estimates.** NEISS is a stratified sample of hospitals. Estimates use the survey weights and Taylor-linearized standard errors (hospitals as clusters within strata), and are flagged "unstable" by CPSC's rule (fewer than 20 records, an estimate below 1,200, or a coefficient of variation above 33%).

**Validation:** e-bike estimates for 2022-2024 are within 5% of CPSC's published figures (27,900 vs 26,800; 37,800 vs 37,600; 59,300 vs 59,200). **E-scooter totals do not match CPSC's published e-scooter figures** (2019: 15,600 vs 39,100; 2021-2024: 50,500 vs 61,200; 56,900 vs 74,800; 63,700 vs 58,400; 117,900 vs 79,300). CPSC reviews each case and removes non-electric, seated, moped, mobility and powered-skateboard scooters; that does not explain the whole gap, and the gap changes by year. Use CPSC's figures for e-scooter counts and trends.

## Install

```
pip install neissai
```

NEISS-AI is on PyPI: https://pypi.org/project/neissai/. Requires Python 3.9+, pandas and numpy. (scikit-learn is needed only to retrain the classifier: `pip install "neissai[train]"`. For the latest development version: `pip install git+https://github.com/subash28kumar/neiss-ai`.)

## Try it in Google Colab (no install)

Open the [quickstart notebook](https://colab.research.google.com/github/subash28kumar/neiss-ai/blob/main/notebooks/NEISS-AI_Quickstart.ipynb) and click **Runtime → Run all**. In a few minutes it downloads the public NEISS files, labels each injury, and reproduces the national estimates by year and device (with 95% confidence intervals), a trend chart and hospital-stay shares. The notebook is also in this repository at `notebooks/NEISS-AI_Quickstart.ipynb`.

## Use from the command line

Download the annual NEISS files (for example `neiss2019.tsv` ... `neiss2025.tsv`) from the [CPSC NEISS page](https://www.cpsc.gov/Research--Statistics/NEISS-Injury-Data) into a folder called `data`, or try `neissai download --years 2019-2025 --data-dir data`. Then:

```
neissai micromobility --years 2019-2025 --data-dir data --out-dir results
```

This prints a table of national estimates and saves:

| File | Contents |
|---|---|
| `estimates_by_year_device.csv` | national ED visits by year and device, 95% CI, CV, records, unstable flag |
| `estimates_by_year_device_age.csv` | the same by age group (0-17, 18-64, 65+) |
| `shares_by_device.csv` | weighted shares hospitalized, head/face injury, motor vehicle involved, no helmet stated, alcohol |
| `micromobility_records.csv` | every labelled record, with device, label source and classifier probability |

Check a single story:

```
neissai classify "14YOM FELL OFF HIS ELECTRIC BIKE GOING 20MPH"
```

## Run it on your own injury notes (any hospital data)

Have your own emergency department records, for example a state health department's triage notes?
NEISS-AI can label them too, on your own computer (nothing is uploaded):

```
pip install neissai
neissai label-notes my_ed_visits.csv --text-col triage_note --code-cols ecode1 --out labelled.csv
```

It reads the note text (and US ICD-10-CM external-cause codes, if you have them) and adds e-bike /
e-scooter / bicycle / kick scooter / hoverboard / skateboard / moped labels, the e-bike vs e-moto
group, helmet, motor-vehicle contact and the rider's stated speed (mph or km/h). Motorcycles are kept
apart ("Motorcycle or moped" from a code alone). Tested on 141,634 real NEISS stories with the product
codes hidden: same label as NEISS-AI's full method (not a human check) for 99% of bicycles, 95% of
e-bikes and 82% of e-scooters; "scooter" alone cannot be sorted from words (a code settles it). Built
and tested on US notes; the ICD-10-CM code rules have not yet been tested on a real coded dataset.
Plain guide and limits: [docs/own-notes.md](docs/own-notes.md). Results:
[validation/freetext/RESULTS.md](validation/freetext/RESULTS.md).

## Emerging-hazard scan (Hazard Watch)

New in v1.6.0. Checks **every NEISS product code** (about 800) and flags products whose injuries rose clearly faster than all emergency visits, after strict checks (design-based standard errors, Benjamini–Hochberg false discovery rate across all products, CPSC's reporting rule, and automatic handling of CPSC product-code changes). It also lists new words in the injury stories that started appearing more often.

```
neissai download --years 2019-2025 --data-dir data
neissai hazard-export --years 2019-2025 --data-dir data --out-dir neissai_hazard_export
neissai hazards --export-dir neissai_hazard_export --years 2020-2025 --out-dir hazards
```

| File | Contents |
|---|---|
| `hazard_scan_<year>.csv` | every product: estimates and rates per 100,000 residents for both years, excess change, 95% uncertainty, minimum detectable change, q-value, flag |
| `hazard_flags.csv` | flagged products, all years |
| `hazard_words_<year>.csv` | short reading list of product and device words in the stories that rose faster than all stories |
| `emerging_terms_<year>.csv` | every word that passed the screen, with its category (product, clinical, writing style) |
| `watch_list.csv` | products chosen in advance (e-bikes, e-scooters, mopeds), tested on their own |
| `code_changes.csv` | product codes that start or stop between years |

2025 results: e-bikes (+72%; 17.5 → 30.0 ED visits per 100,000 people), e-scooters (+40%; 34.9 → 48.5) and power workshop grinders (+85%; 3.0 → 5.5) are flagged; floors are flagged but most likely reflect a 2025 coding change. Looking back, e-bike wording in the stories rose every year from 2021, three years before CPSC's separate e-bike code. Method and limits: `validation/METHODS_hazard_scan.md`. Two-page summary: `docs/NEISS-AI_Hazard_Watch_2025.pdf`. The website's Hazard Watch tab has all result tables and updates itself when CPSC publishes a new year (the monthly refresh reruns the scan).

## Use from Python

```python
import neissai

df = neissai.read_neiss("data", years="2019-2025")
df = neissai.add_outcomes(neissai.label_devices(df))
mm = df["device"].isin(neissai.DEVICES)

# Pass the full table plus a filter, so the variance uses the whole sample design
by_year = neissai.national_estimates(df, by=["year", "device"], where=mm)
kids = neissai.national_estimates(df, by=["year"], where=(df["device"] == "E-bike") & (df["age_group"] == "0-17"))
hosp = neissai.shares(df, "hospitalized", by=["device", "age_group"], where=mm)

# Regular e-bike, likely e-moto (e.g. Sur-Ron, Talaria, rider speed over 28 mph) or unclear
df = neissai.add_ebike_type(df)
etype = neissai.national_estimates(df, by=["year", "ebike_type"], where=df["device"] == "E-bike")
```

The e-bike vs e-moto rule and its check are described in `validation/EBIKE_TYPE.md`.

## Rebuild the website data

```
python scripts/update_site.py --data-dir data --years 2019-2025
```

## Retrain the classifier

```
python scripts/train_classifier.py --data-dir data --years 2024-2025
```

## Limits

NEISS counts emergency department visits, not riders or miles, so results are counts, not risk rates. Injuries treated elsewhere and deaths at the scene are not included. Helmet use is only what the story mentions. Early e-bike estimates rest on few records (2019-2020 by CPSC's rule; 2021 is also small). The hoverboard group also includes electric skateboards, one-wheels and Segways. The powered-scooter code may include some seated, moped-style scooters.

## Cite

If you use NEISS-AI, please cite the software (see `CITATION.cff` or "Cite this repository" on GitHub):

> Kumar S. NEISS-AI: AI-assisted national injury estimates from NEISS emergency department narratives [software]. Zenodo; 2026. https://doi.org/10.5281/zenodo.23306744
>
> This DOI always resolves to the latest version, so all citations count together. To cite one specific version, use that version's DOI on Zenodo.

and the method paper:

> Kumar S. E-bike injuries among US children surged in 2025: national estimates from emergency department reports, 2019-2025, using machine learning to read injury narratives [preprint]. Zenodo; 2026. https://doi.org/10.5281/zenodo.23354066

and the labeled dataset:

> Kumar S. NEISS-AI device labels for US micromobility injuries, 2019–2025 [dataset]. Version 1.1. Zenodo; 2026. https://doi.org/10.5281/zenodo.23550382

Data come from the US Consumer Product Safety Commission. CPSC does not endorse this tool.

## Licence

MIT. Built by Subash Kumar (ORCID [0009-0003-6661-3424](https://orcid.org/0009-0003-6661-3424)).
