Skip to content

Commit 6e22892

Browse files
authored
Create pollution data example & run black, fixes #14 (#34)
1 parent c2712f4 commit 6e22892

File tree

11 files changed

+1252
-299
lines changed

11 files changed

+1252
-299
lines changed

2-analysis-examples/bird-migration.ipynb

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"import geopandas as gpd\n",
3939
"import movingpandas as mpd\n",
4040
"import shapely as shp\n",
41-
"import hvplot.pandas \n",
41+
"import hvplot.pandas\n",
4242
"import matplotlib.pyplot as plt\n",
4343
"\n",
4444
"from geopandas import GeoDataFrame, read_file\n",
@@ -49,11 +49,14 @@
4949
"from urllib.request import urlretrieve\n",
5050
"\n",
5151
"import warnings\n",
52-
"warnings.filterwarnings('ignore')\n",
5352
"\n",
54-
"plot_defaults = {'linewidth':5, 'capstyle':'round', 'figsize':(9,3), 'legend':True}\n",
55-
"opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=300, frame_height=500))\n",
56-
"hvplot_defaults = {'tiles':None, 'cmap':'Viridis', 'colorbar':True}\n",
53+
"warnings.filterwarnings(\"ignore\")\n",
54+
"\n",
55+
"plot_defaults = {\"linewidth\": 5, \"capstyle\": \"round\", \"figsize\": (9, 3), \"legend\": True}\n",
56+
"opts.defaults(\n",
57+
" opts.Overlay(active_tools=[\"wheel_zoom\"], frame_width=300, frame_height=500)\n",
58+
")\n",
59+
"hvplot_defaults = {\"tiles\": None, \"cmap\": \"Viridis\", \"colorbar\": True}\n",
5760
"\n",
5861
"mpd.show_versions()"
5962
]
@@ -72,7 +75,7 @@
7275
"outputs": [],
7376
"source": [
7477
"%%time\n",
75-
"df = read_file('../data/gulls.gpkg')\n",
78+
"df = read_file(\"../data/gulls.gpkg\")\n",
7679
"print(f\"Finished reading {len(df)}\")"
7780
]
7881
},
@@ -114,7 +117,7 @@
114117
"metadata": {},
115118
"outputs": [],
116119
"source": [
117-
"df['individual-local-identifier'].unique()"
120+
"df[\"individual-local-identifier\"].unique()"
118121
]
119122
},
120123
{
@@ -130,7 +133,7 @@
130133
"metadata": {},
131134
"outputs": [],
132135
"source": [
133-
"df['individual-local-identifier'].value_counts().plot(kind='bar', figsize=(17,3))"
136+
"df[\"individual-local-identifier\"].value_counts().plot(kind=\"bar\", figsize=(17, 3))"
134137
]
135138
},
136139
{
@@ -146,7 +149,9 @@
146149
"metadata": {},
147150
"outputs": [],
148151
"source": [
149-
"tc = mpd.TrajectoryCollection(df, 'individual-local-identifier', t='timestamp', min_length=100) \n",
152+
"tc = mpd.TrajectoryCollection(\n",
153+
" df, \"individual-local-identifier\", t=\"timestamp\", min_length=100\n",
154+
")\n",
150155
"tc"
151156
]
152157
},
@@ -186,7 +191,7 @@
186191
"metadata": {},
187192
"outputs": [],
188193
"source": [
189-
"filtered = tc.filter('individual-local-identifier', '91916A')\n",
194+
"filtered = tc.filter(\"individual-local-identifier\", \"91916A\")\n",
190195
"my_traj = filtered.trajectories[0].copy()\n",
191196
"my_traj.df.head()"
192197
]
@@ -197,7 +202,7 @@
197202
"metadata": {},
198203
"outputs": [],
199204
"source": [
200-
"my_traj.hvplot(title=f'Movement of {my_traj.id}', line_width=2) "
205+
"my_traj.hvplot(title=f\"Movement of {my_traj.id}\", line_width=2)"
201206
]
202207
},
203208
{
@@ -215,7 +220,7 @@
215220
"metadata": {},
216221
"outputs": [],
217222
"source": [
218-
"trips_by_year = mpd.TemporalSplitter(filtered).split(mode='year')\n",
223+
"trips_by_year = mpd.TemporalSplitter(filtered).split(mode=\"year\")\n",
219224
"trips_by_year.to_traj_gdf()"
220225
]
221226
},
@@ -232,7 +237,7 @@
232237
"metadata": {},
233238
"outputs": [],
234239
"source": [
235-
"one_year = trips_by_year.get_trajectory('91916A_2010-12-31 00:00:00')\n",
240+
"one_year = trips_by_year.get_trajectory(\"91916A_2010-12-31 00:00:00\")\n",
236241
"one_year"
237242
]
238243
},
@@ -251,8 +256,14 @@
251256
"metadata": {},
252257
"outputs": [],
253258
"source": [
254-
"one_year.hvplot(title=f'Movement speed of {one_year.id}', \n",
255-
" line_width=5.0, c='speed', cmap='RdYlGn', colorbar=True, clim=(0,20)) "
259+
"one_year.hvplot(\n",
260+
" title=f\"Movement speed of {one_year.id}\",\n",
261+
" line_width=5.0,\n",
262+
" c=\"speed\",\n",
263+
" cmap=\"RdYlGn\",\n",
264+
" colorbar=True,\n",
265+
" clim=(0, 20),\n",
266+
")"
256267
]
257268
},
258269
{
@@ -270,8 +281,11 @@
270281
"source": [
271282
"def plot_location_at_timestamp(traj, t, fig_size=250):\n",
272283
" loc = GeoDataFrame([traj.get_row_at(t)])\n",
273-
" return (loc.hvplot(title=str(t), geo=True, tiles='OSM', size=200, color='red') * \n",
274-
" traj.hvplot(line_width=1.0, color='black', tiles=False, width=fig_size, height=fig_size))"
284+
" return loc.hvplot(\n",
285+
" title=str(t), geo=True, tiles=\"OSM\", size=200, color=\"red\"\n",
286+
" ) * traj.hvplot(\n",
287+
" line_width=1.0, color=\"black\", tiles=False, width=fig_size, height=fig_size\n",
288+
" )"
275289
]
276290
},
277291
{
@@ -280,9 +294,11 @@
280294
"metadata": {},
281295
"outputs": [],
282296
"source": [
283-
"( plot_location_at_timestamp(one_year, datetime(2010,9,1)) + \n",
284-
" plot_location_at_timestamp(one_year, datetime(2010,10,1)) +\n",
285-
" plot_location_at_timestamp(one_year, datetime(2010,11,1)) )"
297+
"(\n",
298+
" plot_location_at_timestamp(one_year, datetime(2010, 9, 1))\n",
299+
" + plot_location_at_timestamp(one_year, datetime(2010, 10, 1))\n",
300+
" + plot_location_at_timestamp(one_year, datetime(2010, 11, 1))\n",
301+
")"
286302
]
287303
},
288304
{
@@ -302,11 +318,13 @@
302318
" ts = [datetime(year, month, day) for year in traj.df.index.year.unique()]\n",
303319
" return plot_locations_at_timestamps(traj, ts, ax=ax)\n",
304320
"\n",
305-
"def plot_locations_at_timestamps(traj, ts, ax=None): \n",
321+
"\n",
322+
"def plot_locations_at_timestamps(traj, ts, ax=None):\n",
306323
" loc = GeoDataFrame([traj.get_row_at(t) for t in ts])\n",
307-
" loc['date_label'] = loc.index.strftime('%Y-%m-%d')\n",
308-
" return (loc.hvplot(title=f'Movement of {traj.id}', c='date_label', size=200, geo=True, tiles='OSM') *\n",
309-
" traj.hvplot(line_width=1.0, color='black', geo=True, tiles=False) )"
324+
" loc[\"date_label\"] = loc.index.strftime(\"%Y-%m-%d\")\n",
325+
" return loc.hvplot(\n",
326+
" title=f\"Movement of {traj.id}\", c=\"date_label\", size=200, geo=True, tiles=\"OSM\"\n",
327+
" ) * traj.hvplot(line_width=1.0, color=\"black\", geo=True, tiles=False)"
310328
]
311329
},
312330
{
@@ -334,7 +352,9 @@
334352
"outputs": [],
335353
"source": [
336354
"area_of_interest = Polygon([(30, 25), (50, 25), (50, 15), (30, 15), (30, 25)])\n",
337-
"plotted_area_of_interest = GeoDataFrame(pd.DataFrame([{'geometry': area_of_interest, 'id': 1}]), crs=4326).hvplot(geo=True, color='yellow', alpha=0.5)"
355+
"plotted_area_of_interest = GeoDataFrame(\n",
356+
" pd.DataFrame([{\"geometry\": area_of_interest, \"id\": 1}]), crs=4326\n",
357+
").hvplot(geo=True, color=\"yellow\", alpha=0.5)"
338358
]
339359
},
340360
{
@@ -347,7 +367,9 @@
347367
"print(f\"Found {len(arrivals)} arrivals\")\n",
348368
"\n",
349369
"for traj in arrivals:\n",
350-
" print(f\"Individual '{traj.df['individual-local-identifier'].iloc[0]}' arrived at {traj.get_start_time()}\")"
370+
" print(\n",
371+
" f\"Individual '{traj.df['individual-local-identifier'].iloc[0]}' arrived at {traj.get_start_time()}\"\n",
372+
" )"
351373
]
352374
},
353375
{
@@ -356,7 +378,10 @@
356378
"metadata": {},
357379
"outputs": [],
358380
"source": [
359-
"( plot_locations_at_timestamps(my_traj, [traj.get_start_time() for traj in arrivals]) * plotted_area_of_interest )"
381+
"(\n",
382+
" plot_locations_at_timestamps(my_traj, [traj.get_start_time() for traj in arrivals])\n",
383+
" * plotted_area_of_interest\n",
384+
")"
360385
]
361386
},
362387
{
@@ -376,7 +401,12 @@
376401
"source": [
377402
"year_of_interest = 2010\n",
378403
"trajs_in_aoi = tc.clip(area_of_interest)\n",
379-
"relevant = [ traj for traj in trajs_in_aoi if traj.get_start_time().year <= year_of_interest and traj.get_end_time().year >= year_of_interest]\n",
404+
"relevant = [\n",
405+
" traj\n",
406+
" for traj in trajs_in_aoi\n",
407+
" if traj.get_start_time().year <= year_of_interest\n",
408+
" and traj.get_end_time().year >= year_of_interest\n",
409+
"]\n",
380410
"print(\"Found {} arrivals\".format(len(relevant)))"
381411
]
382412
},
@@ -387,9 +417,13 @@
387417
"outputs": [],
388418
"source": [
389419
"for traj in relevant:\n",
390-
" print(\"Individual '{}' arrived at {} (duration: {})\".format(\n",
391-
" traj.df['individual-local-identifier'].iloc[0], traj.get_start_time().date(), \n",
392-
" traj.get_end_time()-traj.get_start_time()))"
420+
" print(\n",
421+
" \"Individual '{}' arrived at {} (duration: {})\".format(\n",
422+
" traj.df[\"individual-local-identifier\"].iloc[0],\n",
423+
" traj.get_start_time().date(),\n",
424+
" traj.get_end_time() - traj.get_start_time(),\n",
425+
" )\n",
426+
" )"
393427
]
394428
},
395429
{
@@ -407,10 +441,12 @@
407441
"metadata": {},
408442
"outputs": [],
409443
"source": [
410-
"my_traj = tc.get_trajectory('91761A')\n",
411-
"segment = my_traj.get_segment_between(datetime(year_of_interest,1,1), datetime(year_of_interest,12,31))\n",
444+
"my_traj = tc.get_trajectory(\"91761A\")\n",
445+
"segment = my_traj.get_segment_between(\n",
446+
" datetime(year_of_interest, 1, 1), datetime(year_of_interest, 12, 31)\n",
447+
")\n",
412448
"\n",
413-
"segment.hvplot(color='black', line_width=1.0) * plotted_area_of_interest "
449+
"segment.hvplot(color=\"black\", line_width=1.0) * plotted_area_of_interest"
414450
]
415451
},
416452
{
@@ -435,7 +471,8 @@
435471
"1. [Soccer game](soccer-game.ipynb)\n",
436472
"1. [Mars rover & heli](mars-rover.ipynb)\n",
437473
"1. [Ever Given](ever-given.ipynb)\n",
438-
"1. [Iceberg](iceberg.ipynb) "
474+
"1. [Iceberg](iceberg.ipynb)\n",
475+
"1. [Pollution data](pollution-data.ipynb)"
439476
]
440477
}
441478
],

0 commit comments

Comments
 (0)