gtfs_kit 10.2.2__tar.gz → 10.3.0__tar.gz

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.
Files changed (64) hide show
  1. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/.gitignore +2 -1
  2. gtfs_kit-10.3.0/.pre-commit-config.yaml +7 -0
  3. gtfs_kit-10.3.0/.pre-commit-hooks.yaml +2 -0
  4. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/CHANGELOG.rst +10 -0
  5. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/PKG-INFO +2 -2
  6. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/README.rst +1 -1
  7. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/__init__.py +1 -1
  8. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/cleaners.py +31 -19
  9. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/routes.py +15 -5
  10. gtfs_kit-10.3.0/notebooks/examples.py +164 -0
  11. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/pyproject.toml +3 -5
  12. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_routes.py +16 -0
  13. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/uv.lock +191 -1284
  14. gtfs_kit-10.2.2/.pre-commit-config.yaml +0 -12
  15. gtfs_kit-10.2.2/.pre-commit-hooks.yaml +0 -4
  16. gtfs_kit-10.2.2/notebooks/examples.ipynb +0 -256
  17. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/.github/workflows/test.yml +0 -0
  18. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/LICENSE.txt +0 -0
  19. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/cairns_gtfs.zip +0 -0
  20. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/cairns_screen_line.geojson +0 -0
  21. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/cairns_screen_lines.geojson +0 -0
  22. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/cairns_square_stop_750070.geojson +0 -0
  23. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/cairns_trip_stats.csv +0 -0
  24. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/nyc_subway_gtfs.zip +0 -0
  25. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/agency.txt +0 -0
  26. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/attributions.txt +0 -0
  27. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/calendar.txt +0 -0
  28. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/calendar_dates.txt +0 -0
  29. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/fare_attributes.txt +0 -0
  30. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/fare_rules.txt +0 -0
  31. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/feed_info.txt +0 -0
  32. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/frequencies.txt +0 -0
  33. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/routes.txt +0 -0
  34. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/shapes.txt +0 -0
  35. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/stop_times.txt +0 -0
  36. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/stops.txt +0 -0
  37. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs/trips.txt +0 -0
  38. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs.zip +0 -0
  39. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/data/sample_gtfs_2.zip +0 -0
  40. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/docs/Makefile +0 -0
  41. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/docs/conf.py +0 -0
  42. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/docs/index.rst +0 -0
  43. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/docs/introduction.rst +0 -0
  44. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/docs/modules.rst +0 -0
  45. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/calendar.py +0 -0
  46. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/constants.py +0 -0
  47. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/feed.py +0 -0
  48. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/helpers.py +0 -0
  49. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/miscellany.py +0 -0
  50. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/shapes.py +0 -0
  51. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/stop_times.py +0 -0
  52. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/stops.py +0 -0
  53. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/gtfs_kit/trips.py +0 -0
  54. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/__init__.py +0 -0
  55. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/context.py +0 -0
  56. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_calendar.py +0 -0
  57. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_cleaners.py +0 -0
  58. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_feed.py +0 -0
  59. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_helpers.py +0 -0
  60. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_miscellany.py +0 -0
  61. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_shapes.py +0 -0
  62. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_stop_times.py +0 -0
  63. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_stops.py +0 -0
  64. {gtfs_kit-10.2.2 → gtfs_kit-10.3.0}/tests/test_trips.py +0 -0
@@ -1,6 +1,7 @@
1
1
  data/other_feeds
2
2
  monkeytype.sqlite
3
- notebooks/prototyping.ipynb
3
+ notebooks/prototyping.py
4
+ notebooks/__marimo__
4
5
 
5
6
  # Byte-compiled / optimized / DLL files
6
7
  .ruff_cache/
@@ -0,0 +1,7 @@
1
+ repos:
2
+ - repo: https://github.com/charliermarsh/ruff-pre-commit
3
+ # Ruff version.
4
+ rev: 'v0.11.7'
5
+ hooks:
6
+ - id: ruff
7
+ - id: ruff-format
@@ -0,0 +1,2 @@
1
+ - id: ruff
2
+ name: ruff
@@ -1,6 +1,16 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ 10.3.0, 2025-04-30
5
+ ------------------
6
+ - In ``cleaners.drop_zombies``, additionally dropped agencies without routes.
7
+ - Fixed ``routes.get_routes`` to handle the case when at least one of a route's shapes is a point.
8
+ - Turned the example notebook into a Marimo notebook.
9
+
10
+ 10.2.2, 2025-03-13
11
+ ------------------
12
+ - Bugfixed an edge case in the function ``shapes.split_simple``.
13
+
4
14
  10.2.1, 2025-03-12
5
15
  ------------------
6
16
  - Added a forgotten ``drop_duplicates('shape_id')`` line to the function ``miscellany.compute_screen_line_counts``, which speeds things up on large sets of screen lines.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gtfs-kit
3
- Version: 10.2.2
3
+ Version: 10.3.0
4
4
  Summary: A Python library for analyzing GTFS feeds.
5
5
  Project-URL: Repository, https://github.com/mrcagney/gtfs_kit
6
6
  Project-URL: Documentation, https://mrcagney.github.io/gtfs_kit_docs
@@ -32,7 +32,7 @@ Install it from PyPI with UV, say, via ``uv add gtfs_kit``.
32
32
 
33
33
  Examples
34
34
  ========
35
- In the Jupyter notebook ``notebooks/examples.ipynb``.
35
+ In the Marimo notebook ``notebooks/examples.py``.
36
36
 
37
37
 
38
38
  Authors
@@ -13,7 +13,7 @@ Install it from PyPI with UV, say, via ``uv add gtfs_kit``.
13
13
 
14
14
  Examples
15
15
  ========
16
- In the Jupyter notebook ``notebooks/examples.ipynb``.
16
+ In the Marimo notebook ``notebooks/examples.py``.
17
17
 
18
18
 
19
19
  Authors
@@ -11,4 +11,4 @@ from .cleaners import *
11
11
  from .feed import *
12
12
 
13
13
 
14
- __version__ = "10.2.2"
14
+ __version__ = "10.3.0"
@@ -113,54 +113,66 @@ def drop_zombies(feed: "Feed") -> "Feed":
113
113
  """
114
114
  In the given Feed, do the following in order and return the resulting Feed.
115
115
 
116
- 1. Drop stops of location type 0 or NaN with no stop times.
117
- 2. Remove undefined parent stations from the ``parent_station`` column.
118
- 3. Drop trips with no stop times.
119
- 4. Drop shapes with no trips.
120
- 5. Drop routes with no trips.
121
- 6. Drop services with no trips.
116
+ 1. Drop agencies with no routes.
117
+ 2. Drop stops of location type 0 or NaN with no stop times.
118
+ 3. Remove undefined parent stations from the ``parent_station`` column.
119
+ 4. Drop trips with no stop times.
120
+ 5. Drop shapes with no trips.
121
+ 6. Drop routes with no trips.
122
+ 7. Drop services with no trips.
122
123
 
123
124
  """
124
125
  feed = feed.copy()
125
126
 
126
- f = feed.stops.copy()
127
- ids = feed.stop_times.stop_id.unique()
127
+ # Drop agencies with no routes
128
+ f = feed.agency
129
+ feed.agency = f[f["agency_id"].isin(feed.routes["agency_id"].unique())].reset_index(
130
+ drop=True
131
+ )
132
+
133
+ # Drop stops of location type 0 or NaN with no stop times.
134
+ f = feed.stops
135
+ ids = feed.stop_times["stop_id"].unique()
128
136
  cond = f.stop_id.isin(ids)
129
137
  if "location_type" in f.columns:
130
138
  cond |= ~f.location_type.isin([0, np.nan])
131
- feed.stops = f[cond].copy()
139
+ feed.stops = f[cond].reset_index(drop=True)
132
140
 
133
141
  # Remove undefined parent stations from the ``parent_station`` column
134
142
  if "parent_station" in feed.stops.columns:
135
143
  f = feed.stops.copy()
136
- ids = f.stop_id.unique()
137
- f["parent_station"] = f.parent_station.map(lambda x: x if x in ids else np.nan)
144
+ ids = f["stop_id"].unique()
145
+ f["parent_station"] = f["parent_station"].map(
146
+ lambda x: x if x in ids else np.nan
147
+ )
138
148
  feed.stops = f
139
149
 
140
150
  # Drop trips with no stop times
141
- ids = feed.stop_times["trip_id"].unique()
142
151
  f = feed.trips
143
- feed.trips = f[f["trip_id"].isin(ids)]
152
+ ids = feed.stop_times["trip_id"].unique()
153
+ feed.trips = f[f["trip_id"].isin(ids)].reset_index(drop=True)
144
154
 
145
155
  # Drop shapes with no trips
146
- ids = feed.trips["shape_id"].unique()
147
156
  f = feed.shapes
148
157
  if f is not None:
149
- feed.shapes = f[f["shape_id"].isin(ids)]
158
+ feed.shapes = f[
159
+ f["shape_id"].isin(feed.trips["shape_id"].unique())
160
+ ].reset_index(drop=True)
150
161
 
151
162
  # Drop routes with no trips
152
- ids = feed.trips["route_id"].unique()
153
163
  f = feed.routes
154
- feed.routes = f[f["route_id"].isin(ids)]
164
+ feed.routes = f[f["route_id"].isin(feed.trips["route_id"].unique())].reset_index(
165
+ drop=True
166
+ )
155
167
 
156
168
  # Drop services with no trips
157
169
  ids = feed.trips["service_id"].unique()
158
170
  if feed.calendar is not None:
159
171
  f = feed.calendar
160
- feed.calendar = f[f["service_id"].isin(ids)]
172
+ feed.calendar = f[f["service_id"].isin(ids)].reset_index(drop=True)
161
173
  if feed.calendar_dates is not None:
162
174
  f = feed.calendar_dates
163
- feed.calendar_dates = f[f["service_id"].isin(ids)]
175
+ feed.calendar_dates = f[f["service_id"].isin(ids)].reset_index(drop=True)
164
176
 
165
177
  return feed
166
178
 
@@ -204,7 +204,7 @@ def compute_route_stats_0(
204
204
  )
205
205
  if f.empty:
206
206
  raise ValueError(
207
- "At least one trip stats direction ID value " "must be non-NaN."
207
+ "At least one trip stats direction ID value must be non-NaN."
208
208
  )
209
209
 
210
210
  g = (
@@ -335,7 +335,7 @@ def compute_route_time_series_0(
335
335
  )
336
336
  if tss.empty:
337
337
  raise ValueError(
338
- "At least one trip stats direction ID value " "must be non-NaN."
338
+ "At least one trip stats direction ID value must be non-NaN."
339
339
  )
340
340
 
341
341
  # Alter route IDs to encode direction:
@@ -726,10 +726,20 @@ def get_routes(
726
726
  groupby_cols = ["route_id"]
727
727
  final_cols = f.columns.tolist() + ["geometry"]
728
728
 
729
+ # def merge_lines(group):
730
+ # d = {}
731
+ # d["geometry"] = so.linemerge(group["geometry"].tolist())
732
+ # return pd.Series(d)
733
+
729
734
  def merge_lines(group):
730
- d = {}
731
- d["geometry"] = so.linemerge(group["geometry"].tolist())
732
- return pd.Series(d)
735
+ lines = [
736
+ g
737
+ for g in group["geometry"]
738
+ if g.geom_type in ["LineString", "MultiLineString"]
739
+ ]
740
+ if not lines:
741
+ return pd.Series({"geometry": None})
742
+ return pd.Series({"geometry": so.linemerge(lines)})
733
743
 
734
744
  f = (
735
745
  trips
@@ -0,0 +1,164 @@
1
+
2
+
3
+ import marimo
4
+
5
+ __generated_with = "0.13.2"
6
+ app = marimo.App(width="medium")
7
+
8
+
9
+ @app.cell
10
+ def _():
11
+ import pathlib as pl
12
+ import json
13
+
14
+ import pandas as pd
15
+ import numpy as np
16
+ import geopandas as gp
17
+ import matplotlib
18
+ import folium as fl
19
+
20
+ import gtfs_kit as gk
21
+
22
+ DATA = pl.Path("data")
23
+ return DATA, fl, gk, gp, pd
24
+
25
+
26
+ @app.cell
27
+ def _(DATA, gk):
28
+ # List feed
29
+
30
+ path = DATA / "cairns_gtfs.zip"
31
+ gk.list_feed(path)
32
+ return (path,)
33
+
34
+
35
+ @app.cell
36
+ def _(gk, path):
37
+ # Read feed and describe
38
+
39
+ feed = gk.read_feed(path, dist_units="m")
40
+ feed.describe()
41
+ return (feed,)
42
+
43
+
44
+ @app.cell
45
+ def _(feed):
46
+ print(feed.stop_times)
47
+ feed_1 = feed.append_dist_to_stop_times()
48
+ print(feed_1.stop_times)
49
+ return (feed_1,)
50
+
51
+
52
+ @app.cell
53
+ def _(feed_1):
54
+ week = feed_1.get_first_week()
55
+ dates = [week[4], week[6]]
56
+ dates
57
+ return dates, week
58
+
59
+
60
+ @app.cell
61
+ def _(dates, feed_1):
62
+ trip_stats = feed_1.compute_trip_stats()
63
+ trip_stats.head().T
64
+ fts = feed_1.compute_feed_time_series(trip_stats, dates, freq="6h")
65
+ fts
66
+ return fts, trip_stats
67
+
68
+
69
+ @app.cell
70
+ def _(fts, gk):
71
+ gk.downsample(fts, freq="12h")
72
+ return
73
+
74
+
75
+ @app.cell
76
+ def _(feed_1, trip_stats, week):
77
+ feed_stats = feed_1.compute_feed_stats(trip_stats, week)
78
+ feed_stats
79
+ return
80
+
81
+
82
+ @app.cell
83
+ def _(dates, feed_1, trip_stats):
84
+ rts = feed_1.compute_route_time_series(trip_stats, dates, freq="12h")
85
+ rts
86
+ return (rts,)
87
+
88
+
89
+ @app.cell
90
+ def _(rts):
91
+ # Slice time series
92
+
93
+ inds = ["service_distance", "service_duration", "service_speed"]
94
+ rids = ["110-423", "111-423"]
95
+
96
+ rts.loc[:, (inds, rids)]
97
+ return (rids,)
98
+
99
+
100
+ @app.cell
101
+ def _(rids, rts):
102
+ # Slice again by cross-section
103
+
104
+ rts.xs(rids[0], axis="columns", level=1)
105
+ return
106
+
107
+
108
+ @app.cell
109
+ def _(dates, feed_1, pd):
110
+ rng = pd.date_range("1/1/2000", periods=24, freq="h")
111
+ times = [t.strftime("%H:%M:%S") for t in rng]
112
+ loc = feed_1.locate_trips(dates[0], times)
113
+ loc.head()
114
+ return
115
+
116
+
117
+ @app.cell
118
+ def _(dates, feed_1):
119
+ route_id = feed_1.routes["route_id"].iat[0]
120
+ feed_1.build_route_timetable(route_id, dates).T
121
+ return
122
+
123
+
124
+ @app.cell
125
+ def _(DATA, feed_1, fl, gp):
126
+ trip_id = "CNS2014-CNS_MUL-Weekday-00-4166247"
127
+ m = feed_1.map_trips([trip_id], show_stops=True, show_direction=True)
128
+ screen_line = gp.read_file(DATA / "cairns_screen_line.geojson")
129
+ keys_to_remove = [
130
+ key
131
+ for key in m._children.keys()
132
+ if key.startswith("layer_control_") or key.startswith("fit_bounds_")
133
+ ]
134
+ for key in keys_to_remove:
135
+ m._children.pop(key)
136
+ fg = fl.FeatureGroup(name="Screen lines")
137
+ fl.GeoJson(
138
+ screen_line, style_function=lambda feature: {"color": "red", "weight": 2}
139
+ ).add_to(fg)
140
+ fg.add_to(m)
141
+ fl.LayerControl().add_to(m)
142
+ m.fit_bounds(fg.get_bounds())
143
+ m
144
+
145
+
146
+ return screen_line, trip_id
147
+
148
+
149
+ @app.cell
150
+ def _(dates, feed_1, screen_line, trip_id):
151
+ slc = feed_1.compute_screen_line_counts(screen_line, dates=dates)
152
+ slc.loc[lambda x: x["trip_id"] == trip_id]
153
+ return
154
+
155
+
156
+ @app.cell
157
+ def _(feed_1):
158
+ rsns = feed_1.routes["route_short_name"].iloc[2:4]
159
+ feed_1.map_routes(route_short_names=rsns, show_stops=True)
160
+ return
161
+
162
+
163
+ if __name__ == "__main__":
164
+ app.run()
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "gtfs-kit"
3
- version = "10.2.2"
3
+ version = "10.3.0"
4
4
  description = "A Python library for analyzing GTFS feeds."
5
5
  readme = "README.rst"
6
6
  requires-python = ">=3.10"
@@ -31,16 +31,14 @@ packages = ["gtfs_kit"]
31
31
 
32
32
  [tool.uv]
33
33
  dev-dependencies = [
34
- "jupyter>=1",
35
34
  "pytest>=6",
36
35
  "sphinx>=3",
37
36
  "pre-commit-uv>=0",
38
37
  "matplotlib>=1",
39
38
  "pytest-socket>=0",
40
- "black>22",
41
- "nbstripout>=0.5",
42
39
  "ruff>=0.0.257",
43
40
  "publish_sphinx_docs>=1.0",
41
+ "marimo>=0.13.2",
44
42
  ]
45
43
 
46
44
  [tool.pytest.ini_options]
@@ -54,4 +52,4 @@ markers = [
54
52
  # Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
55
53
  lint.select = ["E", "F"]
56
54
  lint.ignore = ["E501", "E731", "F401", "F403"]
57
- exclude = ["*.ipynb"]
55
+ exclude = ["notebooks/*.py"]
@@ -229,6 +229,22 @@ def test_get_routes():
229
229
  print(feed.get_trips(as_gdf=True))
230
230
  assert g.crs == cs.WGS84
231
231
 
232
+ # Turning a route's shapes into point geometries,
233
+ # should yield an empty route geometry and should not throw an error
234
+ rid = feed.routes["route_id"].iat[0]
235
+ shids = feed.trips.loc[lambda x: x["route_id"] == rid, "shape_id"]
236
+ f0 = feed.shapes.loc[lambda x: x["shape_id"].isin(shids)].drop_duplicates(
237
+ "shape_id"
238
+ )
239
+ f1 = feed.shapes.loc[lambda x: ~x["shape_id"].isin(shids)]
240
+ feed.shapes = pd.concat([f0, f1])
241
+ assert (
242
+ feed.get_routes(as_gdf=True)
243
+ .loc[lambda x: x["route_id"] == rid, "geometry"]
244
+ .iat[0]
245
+ is None
246
+ )
247
+
232
248
 
233
249
  def test_compute_route_stats():
234
250
  feed = cairns.copy()