Metadata-Version: 2.4
Name: aadtkit
Version: 0.2.0
Summary: Traffic count processing and AADT estimation per the FHWA Traffic Monitoring Guide: AASHTO and hour-based AADT, seasonal factors, short-count expansion, K/D factors and count quality checks.
Author-email: Yusuf Eminoğlu <geospacephilo@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://gitlab.com/geophilo1/aadtkit_sdk
Project-URL: Repository, https://gitlab.com/geophilo1/aadtkit_sdk
Project-URL: Documentation, https://geophilo.com/sdks/aadtkit/
Project-URL: Issues, https://gitlab.com/geophilo1/aadtkit_sdk/-/work_items
Project-URL: Changelog, https://gitlab.com/geophilo1/aadtkit_sdk/-/blob/main/CHANGELOG.md
Project-URL: SDK dashboard, https://geophilo.com/sdk-dashboard/
Project-URL: DOI, https://doi.org/10.5281/zenodo.24235596
Keywords: traffic,aadt,annual-average-daily-traffic,traffic-counts,traffic-monitoring,traffic-monitoring-guide,fhwa,expansion-factors,seasonal-factors,k-factor,peak-hour-factor,vehicle-classification,transportation-planning,traffic-engineering,data-quality
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Classifier: Environment :: Console
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: GIS
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Natural Language :: English
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Requires-Dist: mypy>=1.0.0; extra == "dev"
Dynamic: license-file

<p align="center">
  <a href="https://geophilo.com/sdks/aadtkit/"><img src="https://geophilo.com/assets/sdk_icons/aadtkit_sdk.svg" width="120" height="120" alt="aadtkit icon"></a>
</p>

# aadtkit

[![PyPI version](https://img.shields.io/pypi/v/aadtkit.svg)](https://pypi.org/project/aadtkit/)
[![Python versions](https://img.shields.io/pypi/pyversions/aadtkit.svg)](https://pypi.org/project/aadtkit/)
[![pipeline status](https://gitlab.com/geophilo1/aadtkit_sdk/badges/main/pipeline.svg)](https://gitlab.com/geophilo1/aadtkit_sdk/-/pipelines)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://gitlab.com/geophilo1/aadtkit_sdk/-/blob/main/LICENSE)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.24235596.svg)](https://doi.org/10.5281/zenodo.24235596)
[![Downloads](https://static.pepy.tech/badge/aadtkit/month)](https://pepy.tech/project/aadtkit)

**Traffic count processing for monitoring programs and traffic engineers: AADT, expansion factors, factor groups, short-count expansion and K-, D- and peak-hour factors, implemented step by step from the FHWA Traffic Monitoring Guide in pure Python.**

<p align="center"><img src="https://gitlab.com/geophilo1/aadtkit_sdk/-/raw/main/docs/assets/hero.svg" width="820" alt="Three charts for a synthetic continuous count station in 2025: monthly average daily traffic from about 9,700 in December to 14,800 in July with the AADT of 11,943 as a dashed line; day-of-week factors from 0.86 on Friday to 1.24 on Sunday; and the 300 highest hourly volumes of the year with the 30th highest hour of 1,312 vehicles marked, K30 = 0.110."></p>

## Why aadtkit?

- **Both AADT procedures of the Traffic Monitoring Guide.** The AASHTO method (monthly
  average days of week) and the hour-based, calendar-weighted procedure FHWA has
  recommended since 2016 (TMG 2022, Section 3.1.4.7), with the TMG sections cited in the
  code.
- **The whole factoring workflow.** Monthly, day-of-week and month by day-of-week factors,
  factor groups with standard deviation, coefficient of variation and 95 % precision,
  k-means grouping of stations, and short counts expanded with axle-correction and growth
  factors. The TMG worked example (518 vehicles to an AADT of 610) is reproduced in the
  tests.
- **Design-hour figures.** K30 from the 30th highest hour, the D-factor at that hour,
  design hour volumes and the HCM peak-hour factor.
- **Quality first.** Missing hours, zero runs, stuck counters, ceilings and robust
  outliers are flagged, never edited; incomplete days are never imputed.
- **No dependencies.** Standard library only, CSV in and out, and a command line for
  every step.
- **Tested.** 62 tests run in CI on Python 3.9, 3.11 and 3.13, with 97 % statement and
  branch coverage; ruff and mypy are clean.

## Installation

```bash
pip install aadtkit
```

Python 3.9 or newer; no other packages.

## Quickstart

A synthetic station with a year of hourly counts in two directions, then quality checks,
AADT, factors, a 48-hour count expanded to AADT and the design-hour factors:

```python
import datetime as dt
import random

import aadtkit as ak

SEASON = [0.82, 0.86, 0.94, 1.00, 1.06, 1.14, 1.24, 1.20, 1.06, 1.00, 0.90, 0.82]  # Jan..Dec
WEEK = [1.00, 1.00, 1.02, 1.05, 1.15, 0.92, 0.80]  # Mon..Sun
SHAPE = [p / 108 for p in [1, 1, 1, 1, 2, 4, 7, 9, 8, 5, 4, 4, 5, 5, 5, 6, 8, 9, 7, 5, 4, 3, 2, 2]]

rng = random.Random(1)
records = []  # one year of hourly counts, two directions, at a synthetic station
day = dt.date(2025, 1, 1)
while day.year == 2025:
    total = 12000 * SEASON[day.month - 1] * WEEK[day.weekday()]
    for h in range(24):
        ts = dt.datetime.combine(day, dt.time(h))
        vol = total * SHAPE[h] * rng.uniform(0.92, 1.08)
        nb = 0.58 if h < 12 else 0.42
        records.append(ak.HourlyRecord("CCS-1", ts, round(vol * nb), "NB"))
        records.append(ak.HourlyRecord("CCS-1", ts, round(vol * (1 - nb)), "SB"))
    day += dt.timedelta(days=1)
ak.write_hourly_csv("ccs-1.csv", records)

series = ak.to_series(records, station="CCS-1")
report = ak.check_hourly(series)  # flags, never edits
res = ak.aadt_hourly_weighted(series)  # TMG 2022 hour-based procedure
factors = ak.factors_from_result(res, "CCS-1")
start = dt.datetime(2025, 4, 15, 9)  # a 48-hour short count
short = {start + dt.timedelta(hours=i): series[start + dt.timedelta(hours=i)] for i in range(48)}
est = ak.short_count_aadt(short, factors, basis="blocks")
d = ak.d_factor(ak.by_direction(records, "CCS-1"))
print(f"{report.completeness:.0%} of hours present, {len(report.issues)} issues")
print(f"AADT {res.aadt:,.0f} (AASHTO method: {ak.aadt_aashto(ak.daily_totals(series)).aadt:,.0f})")
print(f"July factor {factors.monthly[7]:.3f}, Friday factor {factors.dow[4]:.3f}")
print(f"48-hour count: raw ADT {est.raw_adt:,.0f}, expanded to AADT {est.aadt:,.0f}")
print(f"K30 {ak.k_factor(series, res.aadt):.3f}, D {d.d:.2f} ({d.peak_direction})")
```

```text
100% of hours present, 0 issues
AADT 11,943 (AASHTO method: 11,932)
July factor 0.808, Friday factor 0.863
48-hour count: raw ADT 12,105, expanded to AADT 11,807
K30 0.110, D 0.58 (SB)
```

The 48-hour count in April overstates the year by 1.4 %; the station's own factors bring
the estimate to 1.1 % below the true AADT.

## Features

| Area | Functions and classes |
| --- | --- |
| AADT | `aadt_hourly_weighted`, `aadt_aashto`, `aadt_simple`, `daily_totals`, `madt`, `dow_occurrences`, `AADTResult` |
| Factors | `factors_from_result`, `FactorSet.factor`, `group_factors`, `FactorGroup`, `FactorStats`, `cluster_stations`, `t_critical_95` |
| Short counts | `short_count_aadt`, `expand_volume`, `growth_factor`, `split_24h_blocks`, `ShortCountEstimate` |
| Axle correction | `axle_correction_factor`, `axle_correction_from_classes`, `DEFAULT_AXLES_PER_CLASS` |
| Design hour | `highest_hours`, `nth_highest_hour`, `k_factor`, `d_factor`, `design_hour_volume`, `peak_hour_factor` |
| Classification | `FHWAClass`, `CLASS_DESCRIPTIONS`, `class_shares`, `group_shares`, `truck_share` |
| Quality | `check_hourly`, `QualityReport`, `QualityIssue`, `drop_flagged` |
| I/O | `read_hourly_csv`, `write_hourly_csv`, `to_series`, `by_station`, `by_direction`, `class_totals`, `duplicate_keys`, `read_factors_csv`, `write_factors_csv` |

Hourly CSV files have one row per hour, `station,datetime,volume[,direction][,vehicle_class]`
(`date` and `hour` may replace `datetime`; empty or `NA` volumes are missing hours). Factor
tables use `name,type,month,dow,factor`.

## Command line

```text
$ aadtkit --help
usage: aadtkit [-h] [--version]
               {aadt,factors,cluster,expand,peak,qc,classes} ...

Traffic count processing and AADT estimation (FHWA Traffic Monitoring Guide).

positional arguments:
  {aadt,factors,cluster,expand,peak,qc,classes}
    aadt                AADT of continuous count stations
    factors             monthly and day-of-week factors per station
    cluster             k-means factor groups from a factor CSV
    expand              expand short counts to AADT
    peak                K-factor (n-th highest hour) and D-factor
    qc                  data-quality checks
    classes             FHWA 13-class vehicle shares

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
```

On the CSV that the quickstart writes:

```bash
aadtkit qc ccs-1.csv -o issues.csv
aadtkit aadt ccs-1.csv
aadtkit factors ccs-1.csv -o factors.csv
aadtkit peak ccs-1.csv
```

```text
CCS-1: 8760/8760 hours (100.0%), 0 issue(s)
station,method,year,aadt,complete_days,missing_cells
CCS-1,hourly_weighted,2025,11943.2,365,0
station,aadt,nth,hour,volume,k_factor,d_factor,peak_direction
CCS-1,11943.2,30,2025-07-23 17:00,1312,0.1099,0.5800,SB
```

`aadtkit expand short.csv --factors factors.csv --group NAME` expands short counts and
`aadtkit cluster factors.csv -k 3` forms factor groups. Add `--hour-ending` when the
timestamps (or hours 1 to 24) mark the end of the hour. `python -m aadtkit` works too.

## Documentation

- [Usage guide](https://gitlab.com/geophilo1/aadtkit_sdk/-/blob/main/docs/guide.md):
  eight tasks with real output, every formula (AASHTO and hour-based AADT, factors, group
  precision, short-count expansion, axle correction, K, D, PHF, robust outlier score) with
  its TMG section, and troubleshooting.
- [Package page on geophilo.com](https://geophilo.com/sdks/aadtkit/)
- [Changelog](https://gitlab.com/geophilo1/aadtkit_sdk/-/blob/main/CHANGELOG.md)

| API | What it does |
|---|---|
| `aadt_hourly_weighted(hourly)` | AADT by the TMG 2022 hour-based procedure |
| `aadt_aashto(daily)` | AADT by the AASHTO method from complete days |
| `factors_from_result(result)` | Monthly, day-of-week and month by day-of-week factors |
| `group_factors(sets)`, `cluster_stations(sets, k)` | Factor groups and their precision |
| `short_count_aadt(hourly, factors)` | Short count to AADT |
| `k_factor`, `d_factor`, `peak_hour_factor` | Design-hour characteristics |
| `check_hourly(hourly)` | Data-quality flags |

**Limitations.** The quality thresholds (zero run 6 h, constant run 4 h, robust z above 5)
are engineering defaults, not FHWA values. `DEFAULT_AXLES_PER_CLASS` holds nominal axle
numbers; use measured ones where available. k-means groups need review against road
function and geography. Incomplete days are never imputed, and daylight-saving shifts are
not handled: supply local standard time or UTC. Only 95 % confidence intervals are
tabulated.

Development: `pip install -e ".[dev]"`, then `python -m pytest`, `python -m ruff check src
tests` and `python -m mypy`.

## Related packages

- [crashmap](https://pypi.org/project/crashmap/): network screening with crash rates,
  EPDO and Empirical Bayes; its segments take AADT as exposure.
- [roadnoise](https://pypi.org/project/roadnoise/): road traffic noise screening whose road
  input starts from an `aadt` column.
- [streetlos](https://pypi.org/project/streetlos/): multimodal street level of service;
  takes the directional hourly volume and the peak-hour factor.
- [bikelts](https://pypi.org/project/bikelts/): bicycle Level of Traffic Stress; replaces
  its planning defaults with a measured `adt` or `aadt` per segment.

All packages: [Geophilo SDK dashboard](https://geophilo.com/sdk-dashboard/).

## Citation

```text
@software{eminoglu_aadtkit,
  author  = {Eminoğlu, Yusuf},
  title   = {aadtkit},
  year    = {2026},
  version = {0.2.0},
  doi     = {10.5281/zenodo.24235596},
  url     = {https://gitlab.com/geophilo1/aadtkit_sdk}
}
```

Machine-readable metadata: [CITATION.cff](https://gitlab.com/geophilo1/aadtkit_sdk/-/blob/main/CITATION.cff).

## License

MIT, see [LICENSE](https://gitlab.com/geophilo1/aadtkit_sdk/-/blob/main/LICENSE). Contact: geospacephilo@gmail.com
