Created
January 16, 2023 08:11
-
-
Save hodzanassredin/50071755f5e63b0d8d257b8bd7d30762 to your computer and use it in GitHub Desktop.
game of life and other scipy stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
from skimage import io | |
import numpy as np | |
import matplotlib.pyplot as plt | |
url_astronaut = ('https://raw.githubusercontent.com/scikit-image/scikit-image/' | |
'master/skimage/data/astronaut.png') | |
astro = io.imread(url_astronaut) | |
print("Type:", type(astro), "Shape:", astro.shape, "Data type:", astro.dtype) | |
plt.imshow(astro) | |
# %% | |
def overlay_grid(image, spacing=128): | |
"""Return an image with a grid overlay, using the provided spacing. | |
Parameters | |
---------- | |
image : array, shape (M, N, 3) | |
The input image. | |
spacing : int | |
The spacing between the grid lines. | |
Returns | |
------- | |
image_gridded : array, shape (M, N, 3) | |
The original image with a blue grid superimposed. | |
""" | |
image_gridded = image.copy() | |
sq_mask = np.zeros(astro.shape[:2], bool) | |
sq_mask[0:-1:spacing, 0:-1] = True | |
sq_mask[0:-1, 0:-1:spacing] = True | |
image_gridded[sq_mask] = [0, 255, 0] | |
return image_gridded | |
fig, ax = plt.subplots(figsize=(30, 30)) | |
ax.imshow(overlay_grid(astro, 128), interpolation='nearest') | |
# %% | |
from scipy import ndimage as ndi | |
from skimage import morphology | |
def tax(prices): | |
return 10000 + 0.05 * np.percentile(prices, 90) | |
house_price_map = (0.5 + np.random.rand(100, 100)) * 1e6 | |
footprint = morphology.disk(radius=10) | |
tax_rate_map = ndi.generic_filter(house_price_map, tax, footprint=footprint) | |
plt.imshow(tax_rate_map) | |
plt.colorbar() | |
# %% | |
#figure = np.zeros((20,20)) | |
#x = figure.shape[0] // 2 | |
#y = figure.shape[1] // 2 | |
#figure[x:x+3, y] = 1 | |
#figure[x, y:y+3] = 1 | |
figure = np.random.randint(0, 2, size=(100, 100)) | |
fig, ax = plt.subplots(figsize=(10, 10)) | |
ax.imshow(figure) | |
frame_count = 500 | |
frames = [] | |
# %% | |
def convey(тeighbours): | |
тeighbours = тeighbours.reshape(3,3) | |
i = тeighbours[1,1] | |
count = np.sum(тeighbours) - i | |
#return count | |
if i == 0 and count == 3: | |
return 1 | |
if i == 1 and count in [2,3]: | |
return 1 | |
return 0 | |
footprint = np.ones((3,3)) | |
frames.append(figure.copy()) | |
for i in range(0,frame_count): | |
figure = ndi.generic_filter(figure, convey, footprint=footprint) | |
frames.append(figure.copy()) | |
video = np.array(frames) | |
fig, ax = plt.subplots(figsize=(20, 20)) | |
ax.imshow(figure) | |
# %% | |
from matplotlib import pyplot as plt | |
from matplotlib import animation | |
from IPython.display import HTML | |
# np array with shape (frames, height, width, channels) | |
fig = plt.figure(figsize=(20, 20)) | |
im = plt.imshow(video[0,:,:]) | |
plt.close() # this is required to not display the generated image | |
def init(): | |
im.set_data(video[0,:,:]) | |
def animate(i): | |
im.set_data(video[i,:,:]) | |
return im | |
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=video.shape[0], | |
interval=200) | |
HTML(anim.to_html5_video()) | |
# %% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment