Skip to content

Instantly share code, notes, and snippets.

@binary10
Last active October 19, 2016 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binary10/2b994492283b8b0f9950af19b8f322d0 to your computer and use it in GitHub Desktop.
Save binary10/2b994492283b8b0f9950af19b8f322d0 to your computer and use it in GitHub Desktop.
"""
Test how to composite matrices with 'where'
Test generator builds random incrementing matrices
The compositor takes output from this generator and performs the compositing.
Display an image map of the matrices
"""
import numpy as np
from matplotlib import pyplot as pl
def screen_generator(n, size):
"""
Generates a matrix with 1's in randomly selected positions.
At each iteration of the generator, the matrix is scalar
multiplied by the current step sequence number. This results
in a matrix with the current step number in random positions.
"""
for i in range(n):
yield np.random.binomial(1, .5, (size,size)) * (i+1)
def compositor(gen):
"""
Produces a generator that overlays each screen
"""
composite = 0 # Keeps aggregation of composites
for screen in gen:
# Take the values that are nonzero from screen and
# keep them in position. For all other values, take
# What is stored in composite.
composite = np.where(screen > 0, screen, composite)
yield composite
# Test and display
g = screen_generator(10, 20)
c = compositor(g)
pl.set_cmap('viridis')
for image in c:
pl.imshow(next(c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment