Skip to content

Commit dcd4a3a

Browse files
committed
cleanup plotting, numpy, quake solution
1 parent e306dff commit dcd4a3a

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed
File renamed without changes.

ch02data/068QuakesSolution.ipynb.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
# As a first approach, we will set the first eartquake to be the largest and as we iterate over the others we will replace the first one with whatever is larger.
171171

172172
# %%
173-
quakes = requests_json['features']
173+
quakes = quakes_json['features']
174174

175175
largest_so_far = quakes[0]
176176
for quake in quakes:
@@ -212,26 +212,38 @@
212212
# We saw something similar in the [Greengraph example](../ch01python/010exemplar.html#More-complex-functions) [(notebook version)](../ch01python/010exemplar.ipynb#More-complex-functions) of the previous chapter.
213213

214214
# %%
215-
def request_map_at(lat, long, satellite=True,
216-
zoom=10, size=(400, 400)):
217-
base = "https://static-maps.yandex.ru/1.x/?"
215+
def deg2num(lat_deg, lon_deg, zoom):
216+
"""Convert latitude and longitude to XY tiles coordinates."""
217+
218+
lat_rad = math.radians(lat_deg)
219+
n = 2.0 ** zoom
220+
x_tiles_coord = int((lon_deg + 180.0) / 360.0 * n)
221+
y_tiles_coord = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
222+
223+
return (x_tiles_coord, y_tiles_coord)
224+
225+
def request_map_at(latitude, longitude, zoom=10, satellite=True):
226+
"""Retrieve a map from Google at a given location."""
227+
228+
base_url = "https://mt0.google.com/vt?"
229+
x_coord, y_coord = deg2num(latitude, longitude, zoom)
218230

219231
params = dict(
232+
x=x_coord,
233+
y=y_coord,
220234
z=zoom,
221-
size="{},{}".format(size[0], size[1]),
222-
ll="{},{}".format(long, lat),
223-
l="sat" if satellite else "map",
224-
lang="en_US"
225235
)
226-
227-
return requests.get(base, params=params)
236+
if satellite:
237+
params['lyrs'] = 's'
238+
239+
return requests.get(base_url, params=params)
228240

229241
# %% [markdown]
230242
# As a test we can check the map displayed for the coordinates of the [Prime meridian at the Royal Observatory Greenwich](https://geohack.toolforge.org/geohack.php?pagename=Prime_meridian_(Greenwich)&params=51_28_40.1_N_0_0_5.3_W_type:landmark_globe:earth_region:GB_scale:1000)
231243

232244
# %%
233-
map = request_map_at(lat=51.477806, long=-0.001472, zoom=14)
234-
Image(map.content)
245+
map_response = request_map_at(51.477806, -0.001472, zoom=14)
246+
Image(map_response.content)
235247

236248
# %% [markdown]
237249
# ## Display the maps
@@ -244,4 +256,3 @@ def request_map_at(lat, long, satellite=True,
244256
latitude = quake['geometry']['coordinates'][1]
245257
print(quake['properties']['title'])
246258
display(Image(request_map_at(latitude, longitude, 12).content))
247-

ch02data/082NumPy.ipynb.py

+47
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
# * [NumPy](https://numpy.org/), a fast numeric computing library offering a flexible *n*-dimensional array type.
3131
# * [IPython](https://ipython.readthedocs.io/en/stable/overview.html), an interactive Python interpreter that later led to the [Jupyter notebook](https://jupyter.org/) interface.
3232
#
33+
# <details><summary>Who created those?</summary>
34+
#
35+
# * [John D. Hunter](https://en.wikipedia.org/wiki/John_D._Hunter) created Matplotlib
36+
# * [Travis Oliphant](https://en.wikipedia.org/wiki/Travis_Oliphant) is the primary creator of NumPy, founding contributor of SciPy, and he's also the founder of Anaconda
37+
# * [Fernando Perez](https://en.wikipedia.org/wiki/Fernando_P%C3%A9rez_\(software_developer\)) created IPython
38+
#
39+
# </details>
40+
#
41+
#
3342
# By combining a plotting library, a fast numeric library, and an easy-to-use interface allowing live plotting commands in a persistent environment, the powerful capabilities of MATLAB were matched by a free and open toolchain.
3443
#
3544
# We've learned about Matplotlib and IPython in this course already. NumPy is the last part of the trilogy.
@@ -533,6 +542,44 @@
533542
# %%
534543
v @ v
535544

545+
# %% [markdown]
546+
# We can alternatively use a built in function:
547+
548+
# %%
549+
np.dot(a, b)
550+
551+
# %% [markdown]
552+
# Though it is possible to represent this in the algebra of broadcasting and newaxis:
553+
554+
# %%
555+
a[:, :, np.newaxis].shape
556+
557+
# %%
558+
b[np.newaxis, :, :].shape
559+
560+
# %%
561+
a[:, :, np.newaxis] * b[np.newaxis, :, :]
562+
563+
# %%
564+
(a[:, :, np.newaxis] * b[np.newaxis, :, :]).sum(1)
565+
566+
# %% [markdown]
567+
# Or if you prefer:
568+
569+
# %%
570+
(a.reshape(3, 3, 1) * b.reshape(1, 3, 3)).sum(1)
571+
572+
# %% [markdown]
573+
# We use broadcasting to generate $A_{ij}B_{jk}$ as a 3-d matrix:
574+
575+
# %%
576+
a.reshape(3, 3, 1) * b.reshape(1, 3, 3)
577+
578+
# %% [markdown]
579+
# Then we sum over the middle, $j$ axis, [which is the 1-axis of three axes numbered (0,1,2)] of this 3-d matrix. Thus we generate $\Sigma_j A_{ij}B_{jk}$.
580+
#
581+
# We can see that the broadcasting concept gives us a powerful and efficient way to express many linear algebra operations computationally.
582+
536583
# %% [markdown]
537584
# ## Structured arrays
538585
#

0 commit comments

Comments
 (0)