Skip to content

Commit

Permalink
cleanup plotting, numpy, quake solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp committed Aug 20, 2024
1 parent e306dff commit dcd4a3a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
File renamed without changes.
37 changes: 24 additions & 13 deletions ch02data/068QuakesSolution.ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
# 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.

# %%
quakes = requests_json['features']
quakes = quakes_json['features']

largest_so_far = quakes[0]
for quake in quakes:
Expand Down Expand Up @@ -212,26 +212,38 @@
# 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.

# %%
def request_map_at(lat, long, satellite=True,
zoom=10, size=(400, 400)):
base = "https://static-maps.yandex.ru/1.x/?"
def deg2num(lat_deg, lon_deg, zoom):
"""Convert latitude and longitude to XY tiles coordinates."""

lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
x_tiles_coord = int((lon_deg + 180.0) / 360.0 * n)
y_tiles_coord = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)

return (x_tiles_coord, y_tiles_coord)

def request_map_at(latitude, longitude, zoom=10, satellite=True):
"""Retrieve a map from Google at a given location."""

base_url = "https://mt0.google.com/vt?"
x_coord, y_coord = deg2num(latitude, longitude, zoom)

params = dict(
x=x_coord,
y=y_coord,
z=zoom,
size="{},{}".format(size[0], size[1]),
ll="{},{}".format(long, lat),
l="sat" if satellite else "map",
lang="en_US"
)

return requests.get(base, params=params)
if satellite:
params['lyrs'] = 's'

return requests.get(base_url, params=params)

# %% [markdown]
# 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)

# %%
map = request_map_at(lat=51.477806, long=-0.001472, zoom=14)
Image(map.content)
map_response = request_map_at(51.477806, -0.001472, zoom=14)
Image(map_response.content)

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

47 changes: 47 additions & 0 deletions ch02data/082NumPy.ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
# * [NumPy](https://numpy.org/), a fast numeric computing library offering a flexible *n*-dimensional array type.
# * [IPython](https://ipython.readthedocs.io/en/stable/overview.html), an interactive Python interpreter that later led to the [Jupyter notebook](https://jupyter.org/) interface.
#
# <details><summary>Who created those?</summary>
#
# * [John D. Hunter](https://en.wikipedia.org/wiki/John_D._Hunter) created Matplotlib
# * [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
# * [Fernando Perez](https://en.wikipedia.org/wiki/Fernando_P%C3%A9rez_\(software_developer\)) created IPython
#
# </details>
#
#
# 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.
#
# We've learned about Matplotlib and IPython in this course already. NumPy is the last part of the trilogy.
Expand Down Expand Up @@ -533,6 +542,44 @@
# %%
v @ v

# %% [markdown]
# We can alternatively use a built in function:

# %%
np.dot(a, b)

# %% [markdown]
# Though it is possible to represent this in the algebra of broadcasting and newaxis:

# %%
a[:, :, np.newaxis].shape

# %%
b[np.newaxis, :, :].shape

# %%
a[:, :, np.newaxis] * b[np.newaxis, :, :]

# %%
(a[:, :, np.newaxis] * b[np.newaxis, :, :]).sum(1)

# %% [markdown]
# Or if you prefer:

# %%
(a.reshape(3, 3, 1) * b.reshape(1, 3, 3)).sum(1)

# %% [markdown]
# We use broadcasting to generate $A_{ij}B_{jk}$ as a 3-d matrix:

# %%
a.reshape(3, 3, 1) * b.reshape(1, 3, 3)

# %% [markdown]
# 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}$.
#
# We can see that the broadcasting concept gives us a powerful and efficient way to express many linear algebra operations computationally.

# %% [markdown]
# ## Structured arrays
#
Expand Down

0 comments on commit dcd4a3a

Please sign in to comment.