transitmatters.models
¶
Data models returned by the client, e.g. Headway or RouteStops.
Every model is a frozen dataclass. Times are timezone-aware datetimes in
Eastern time (America/New_York), dates are datetime.dates, and missing
values are None.
transitmatters.to_dataframe turns a list of models into a pandas
DataFrame; Model.to_dict gives one model as a dict.
Model
dataclass
¶
Base class for every model.
Headway
dataclass
¶
Headway(
*,
route_id: str,
direction: int,
current_dep_dt: datetime,
headway_time_sec: float,
benchmark_headway_time_sec: float | None,
vehicle_label: str,
vehicle_consist: tuple[str, ...],
)
Bases: Model
The time between two consecutive departures from a stop.
route_id
instance-attribute
¶
Route of the departing trip, e.g. "Red", "28", "CR-Fairmount".
direction
instance-attribute
¶
Direction ID, 0 or 1. transitmatters.TransitMatters.stops names them.
benchmark_headway_time_sec
instance-attribute
¶
Scheduled headway in seconds, if known.
vehicle_consist
instance-attribute
¶
Car numbers, e.g. ("1850", "1851"); empty when unknown.
Dwell
dataclass
¶
Dwell(
*,
route_id: str,
direction: int,
arr_dt: datetime,
dep_dt: datetime,
dwell_time_sec: float,
vehicle_label: str,
vehicle_consist: tuple[str, ...],
)
TravelTime
dataclass
¶
TravelTime(
*,
route_id: str,
direction: int,
dep_dt: datetime,
arr_dt: datetime,
travel_time_sec: float,
benchmark_travel_time_sec: float | None,
tm_benchmark_travel_time_sec: float | None,
vehicle_label: str,
vehicle_consist: tuple[str, ...],
)
Bases: Model
One trip's travel time between an origin and a destination stop.
benchmark_travel_time_sec
instance-attribute
¶
Scheduled travel time in seconds, if known.
tm_benchmark_travel_time_sec
instance-attribute
¶
TransitMatters' benchmark travel time in seconds for the stop pair
(the same on every trip), or None for bus, commuter rail, and ferry.
vehicle_consist
instance-attribute
¶
Car numbers; empty when unknown.
Alert
dataclass
¶
LiveAlert
dataclass
¶
LiveAlert(
*,
id: str,
type: str,
header: str,
active_period: tuple[ActivePeriod, ...],
stops: tuple[str, ...],
routes: tuple[str, ...],
description: str | None,
)
Bases: Model
A current MBTA service alert.
Which fields are set depends on type: routes for DELAY and
DETOUR, and description for elevator and escalator closures.
type
instance-attribute
¶
Effect, e.g. DELAY, DETOUR, SHUTTLE, SUSPENSION,
STOP_CLOSURE, ELEVATOR_CLOSURE, or ESCALATOR_CLOSURE.
active_period
instance-attribute
¶
active_period: tuple[ActivePeriod, ...]
When the alert is active.
stops
instance-attribute
¶
Affected stop IDs, unordered: platform IDs like "70061" for
DELAY and DETOUR, station IDs like "place-dwnxg" otherwise.
routes
instance-attribute
¶
Affected route IDs, possibly repeated; empty unless DELAY or DETOUR.
description
instance-attribute
¶
Long-form description, for elevator and escalator closures.
ActivePeriod
dataclass
¶
SummaryStats
dataclass
¶
SummaryStats(
*,
count: int,
mean: float,
min: float,
max: float,
std: float,
sum: float,
p25: float,
p50: float,
p75: float,
)
DailyStats
dataclass
¶
DailyStats(
*,
count: int,
mean: float,
min: float,
max: float,
std: float,
sum: float,
p25: float,
p50: float,
p75: float,
service_date: date,
peak: str,
holiday: bool,
weekend: bool,
)
Bases: SummaryStats
Statistics for one service date and peak period.
DailyHeadwayStats
dataclass
¶
DailyHeadwayStats(
*,
count: int,
mean: float,
min: float,
max: float,
std: float,
sum: float,
p25: float,
p50: float,
p75: float,
service_date: date,
peak: str,
holiday: bool,
weekend: bool,
bunched: int,
on_time: int,
)
Bases: DailyStats
Headway statistics for one service date, with bunching and on-time counts.
TimeOfDayStats
dataclass
¶
TimeOfDayStats(
*,
count: int,
mean: float,
min: float,
max: float,
std: float,
sum: float,
p25: float,
p50: float,
p75: float,
dep_time: timedelta,
is_peak_day: bool,
)
Bases: SummaryStats
Statistics for one 30-minute departure-time slot across a date range.
TravelTimeStats
dataclass
¶
TravelTimeStats(
*,
by_date: tuple[DailyStats, ...],
by_time: tuple[TimeOfDayStats, ...],
)
Bases: Model
Travel-time statistics by date and by time of day.
by_date
instance-attribute
¶
by_date: tuple[DailyStats, ...]
One entry per service date and peak period.
by_time
instance-attribute
¶
by_time: tuple[TimeOfDayStats, ...]
One entry per 30-minute departure-time slot.
TripMetrics
dataclass
¶
TripMetrics(
*,
date: date,
line: str,
count: float,
total_time: float,
miles_covered: float,
avg_car_age: float | None,
pct_new_trips: float | None,
fleet_mix: dict[str, float],
)
Bases: Model
Delivered trips on a line for one day, week, or month.
count
instance-attribute
¶
Trips delivered; the median daily count for weekly and monthly data.
pct_new_trips
instance-attribute
¶
Percentage of trips run with new cars, if known.
fleet_mix
instance-attribute
¶
Percentage of trips by fleet type, e.g. {"red1": 25.5, "red4": 30.1};
empty when unknown.
BusTripMetrics
dataclass
¶
BusTripMetrics(
*,
date: date,
route: str,
count: float,
total_time: float,
miles_covered: float,
n_traversals: float,
median_speed_mph: float | None,
mean_speed_mph: float | None,
fleet_trips: float | None,
avg_bus_age: float | None,
pct_battery_trips: float | None,
fleet_mix: dict[str, float],
)
Bases: Model
Delivered trips on a bus route for one day, week, or month.
Average speed in miles per hour is miles_covered / (total_time / 3600).
route
instance-attribute
¶
Route ID, e.g. "1", or the comma-separated routes requested together.
median_speed_mph
instance-attribute
¶
Median speed in miles per hour, for daily entries covering the whole day.
mean_speed_mph
instance-attribute
¶
Mean speed in miles per hour, for daily entries covering the whole day.
pct_battery_trips
instance-attribute
¶
Percentage of trips run with battery-electric buses, if known.
fleet_mix
instance-attribute
¶
Percentage of trips by propulsion, e.g. {"hybrid": 81.3, "cng": 18.7};
empty when unknown.
BusRouteSpeed
dataclass
¶
BusRouteSpeed(
*,
route: str,
count: float,
total_time: float,
miles_covered: float,
n_traversals: float,
)
Bases: Model
A bus route's delivered trips over a date range.
Average speed in miles per hour is miles_covered / (total_time / 3600).
CommuterRailTripMetrics
dataclass
¶
CommuterRailTripMetrics(
*,
date: date,
route: str,
fleet_trips: float | None,
avg_cab_car_age: float | None,
fleet_mix: dict[str, float],
)
LineDelays
dataclass
¶
LineDelays(
*,
date: date,
line: str,
total_delay_time: int,
brake_problem: int,
car_traffic: int,
disabled_vehicle: int,
door_problem: int,
fire: int,
flooding: int,
mechanical_problem: int,
medical_emergency: int,
other: int,
police_activity: int,
power_problem: int,
signal_problem: int,
switch_problem: int,
track_issue: int,
track_work: int,
)
ServiceHours
dataclass
¶
Bases: Model
Scheduled and delivered service hours for one day, week, or month.
Ridership
dataclass
¶
ScheduledService
dataclass
¶
ScheduledService(
*,
start_date: date,
end_date: date,
start_date_service_levels: ServiceLevels,
end_date_service_levels: ServiceLevels,
counts: tuple[ScheduledServiceCount, ...],
)
Bases: Model
Scheduled trips on a route over a date range.
start_date_service_levels
instance-attribute
¶
start_date_service_levels: ServiceLevels
Service on the first weekday, Saturday, and Sunday in the range.
end_date_service_levels
instance-attribute
¶
end_date_service_levels: ServiceLevels
Service on the last weekday, Saturday, and Sunday in the range.
counts
instance-attribute
¶
counts: tuple[ScheduledServiceCount, ...]
Scheduled trips at the requested aggregation level.
ScheduledServiceCount
dataclass
¶
ServiceLevels
dataclass
¶
ServiceLevels(
*,
weekday: DayServiceLevels | None,
saturday: DayServiceLevels | None,
sunday: DayServiceLevels | None,
)
Bases: Model
Scheduled service on a weekday, Saturday, and Sunday.
weekday
instance-attribute
¶
weekday: DayServiceLevels | None
None when the date range has no weekday.
saturday
instance-attribute
¶
saturday: DayServiceLevels | None
None when the date range has no Saturday.
DayServiceLevels
dataclass
¶
TimePredictionWeek
dataclass
¶
TimePredictionWeek(
*,
route_id: str,
week: date,
prediction: tuple[TimePredictionBucket, ...],
)
Bases: Model
Prediction accuracy for a route over one week.
prediction
instance-attribute
¶
prediction: tuple[TimePredictionBucket, ...]
Accuracy per prediction horizon.
TimePredictionBucket
dataclass
¶
TimePredictionBucket(
*,
weekly: date,
mode: str,
route_id: str,
bin: str,
arrival_departure: str,
num_predictions: int,
num_accurate_predictions: int,
)
SpeedRestrictions
dataclass
¶
SpeedRestrictions(
*,
available: bool,
date: date | None,
zones: tuple[SpeedRestriction, ...],
)
Bases: Model
Speed restrictions on a line on one date.
available
instance-attribute
¶
Whether there is speed restriction data for the date.
date
instance-attribute
¶
Date of the data, which may be earlier than the requested date.
zones
instance-attribute
¶
zones: tuple[SpeedRestriction, ...]
Restrictions in effect; empty when unavailable.
SpeedRestriction
dataclass
¶
SpeedRestriction(
*,
id: str,
date: date,
line_id: str,
description: str,
direction: str,
reason: str,
from_stop_id: str | None,
to_stop_id: str | None,
reported: date,
speed_mph: int,
track_feet: int,
)
Reliability
dataclass
¶
Reliability(
*,
date: date,
days: int,
completed: int | None,
on_time: int | None,
requests: int | None,
no_shows: int | None,
missed: int | None,
otp_numerator: int | None,
otp_denominator: int | None,
cancelled: int | None,
peak: ReliabilityCounts | None,
off_peak: ReliabilityCounts | None,
)
Bases: Model
On-time performance of The RIDE or commuter rail for one day, week, or month.
The RIDE entries set completed through missed, and commuter rail
entries set otp_numerator through off_peak. The others are None.
requests
instance-attribute
¶
The RIDE trip requests, if recorded on every day of the period.
no_shows
instance-attribute
¶
The RIDE rider no-shows, if recorded on every day of the period.
missed
instance-attribute
¶
The RIDE missed trips, if recorded on every day of the period.
otp_denominator
instance-attribute
¶
Commuter rail trips measured for on-time performance.
off_peak
instance-attribute
¶
off_peak: ReliabilityCounts | None
Commuter rail counts outside peak periods.
ReliabilityCounts
dataclass
¶
Bases: Model
On-time and cancelled commuter rail trips in one part of the day.
CommuterRailScorecard
dataclass
¶
CommuterRailScorecard(
*,
date: date | None,
route_id: str | None,
days: int,
outbound: ScorecardDirection,
inbound: ScorecardDirection,
peak_trips: float | None,
reverse_peak_trips: float | None,
)
Bases: Model
A commuter rail schedule measured against Regional Rail standards.
ScorecardDirection
dataclass
¶
ScorecardDirection(
*,
trips: float | None,
first_trip: timedelta | None,
last_trip: timedelta | None,
max_gap_midday: float | None,
max_gap_evening: float | None,
hours_at_target: float | None,
clock_face_share: float | None,
by_hour: tuple[float | None, ...],
)
Bases: Model
Scheduled commuter rail service in one direction, at the Boston terminal. Values are averages over the days measured.
last_trip
instance-attribute
¶
Last train, as time since midnight. Trains after midnight are past 24 hours.
max_gap_evening
instance-attribute
¶
Longest evening wait, in minutes.
hours_at_target
instance-attribute
¶
Hours from 5 AM to 1 AM with a train at least every 30 minutes.
clock_face_share
instance-attribute
¶
Share of trains on a repeating hourly pattern, from 0 to 1.
by_hour
instance-attribute
¶
Trains by hour, index 0 = midnight; empty if unavailable.
Weather
dataclass
¶
Weather(
*,
timestamp: datetime,
temperature_f: float | None,
weather_code: int | None,
condition: str | None,
precipitation_in: float | None,
humidity_pct: int | None,
wind_mph: float | None,
)
Routes
dataclass
¶
Routes(
*,
rapid_transit: tuple[str, ...],
bus: tuple[str, ...],
commuter_rail: tuple[str, ...],
ferry: tuple[str, ...],
)
RouteStops
dataclass
¶
RouteStops(
*,
type: str,
directions: dict[int, str],
stations: tuple[Station, ...],
service_start: date | None,
service_end: date | None,
)
Bases: Model
The stations along a route, with stop IDs by direction.
Find a station with station, or the stop IDs for a trip with trip.
directions
instance-attribute
¶
Direction names by direction ID, e.g. {0: "northbound", 1: "southbound"}.
service_start
instance-attribute
¶
When the route started running, for routes that changed.
service_end
instance-attribute
¶
When the route stopped running, for routes that changed.
station
¶
station(name: str) -> Station
The station named name, e.g. "Davis", or with that station ID,
e.g. "place-davis". Case-insensitive.
Raises:
| Type | Description |
|---|---|
KeyError
|
No station matches; the message suggests close names. |
Source code in src/transitmatters/models.py
trip
¶
trip(
origin: str,
destination: str,
*,
direction: int | None = None,
) -> StopPair
The platforms for travel from origin to destination.
Stations are looked up as in station. The direction is worked out
from the stations' order along the route, unless given.
Raises:
| Type | Description |
|---|---|
KeyError
|
A station isn't on this route. |
ValueError
|
The stations are the same, on different branches, or
have no platforms in |
Source code in src/transitmatters/models.py
Station
dataclass
¶
Station(
*,
stop_name: str,
station: str,
order: float,
stops: dict[int, tuple[str, ...]],
branches: tuple[str, ...],
accessible: bool,
pedal_park: bool,
enclosed_bike_parking: bool,
terminus: bool,
disabled: bool,
short: str | None,
)
Bases: Model
A station or stop along a route.
StopPair
dataclass
¶
Bases: Model
The platforms for a trip between two stations, from RouteStops.trip.
Pass them to transitmatters.TransitMatters.travel_times or
transitmatters.TransitMatters.aggregate_travel_times:
Facility
dataclass
¶
Facility(
*,
id: str,
type: str,
long_name: str | None,
short_name: str | None,
latitude: float | None,
longitude: float | None,
stop_id: str | None,
properties: tuple[FacilityProperty, ...],
)
Bases: Model
An MBTA facility, such as an elevator, escalator, or bike rack.
stop_id
instance-attribute
¶
Station the facility belongs to, e.g. "place-andrw".
properties
instance-attribute
¶
properties: tuple[FacilityProperty, ...]
Other properties. A name can appear more than once.
FacilityProperty
dataclass
¶
Healthcheck
dataclass
¶
ServiceRidershipDashboard
dataclass
¶
ServiceRidershipDashboard(
*,
summary_data: DashboardSummary,
mode_data: dict[str, DashboardSummary],
line_data: dict[str, DashboardLine],
)
Bases: Model
The data behind the dashboard's service and ridership overview.
mode_data
instance-attribute
¶
mode_data: dict[str, DashboardSummary]
Summaries by mode: "rapid-transit", "regional-rail", "bus", "boat".
line_data
instance-attribute
¶
line_data: dict[str, DashboardLine]
Lines by line ID, e.g. "line-Red".
DashboardSummary
dataclass
¶
DashboardSummary(
*,
total_ridership_history: dict[date, float],
total_service_history: dict[date, float],
total_ridership_percentage: float,
total_service_percentage: float,
total_passengers: float,
total_trips: float,
total_routes_cancelled: int,
total_reduced_service: int,
total_increased_service: int,
start_date: date,
end_date: date,
)
Bases: Model
Service and ridership across the system or one mode.
total_ridership_history
instance-attribute
¶
Weekly median ridership by week start date.
total_service_history
instance-attribute
¶
Weekly median scheduled trips by week start date.
total_ridership_percentage
instance-attribute
¶
Currently always 0.
total_passengers
instance-attribute
¶
Weekly median ridership in the latest week.
total_trips
instance-attribute
¶
Weekly median scheduled trips in the latest week.
total_routes_cancelled
instance-attribute
¶
Routes with cancelled service.
total_increased_service
instance-attribute
¶
Routes with increased service.
DashboardLine
dataclass
¶
DashboardLine(
*,
id: str,
short_name: str,
long_name: str,
route_ids: tuple[str, ...],
start_date: date,
line_kind: str,
ridership_history: dict[date, float],
service_history: dict[date, float],
service_regimes: ServiceRegimes,
)
Bases: Model
Service and ridership for one line.
line_kind
instance-attribute
¶
"red", "orange", "blue", "green", "silver",
"regional-rail", "bus", or "boat".
ridership_history
instance-attribute
¶
Weekly median ridership by week start date; empty if unavailable.
service_history
instance-attribute
¶
Weekly median scheduled trips by week start date.
service_regimes
instance-attribute
¶
service_regimes: ServiceRegimes
Scheduled service now, a year ago, and before COVID.
ServiceRegimes
dataclass
¶
ServiceRegimes(
*,
current: ServiceRegime,
one_year_ago: ServiceRegime,
baseline: ServiceRegime,
)
ServiceRegime
dataclass
¶
ServiceRegime(
*,
weekday: ServiceDay,
saturday: ServiceDay,
sunday: ServiceDay,
)