def cloud_two(c, n_points, n_steps, state): f = plt.figure() starting_points = [] walks = {} for i in range(n_points): try: pos = imutils.spawn_random_point(state[:, :, 0]) starting_points.append(pos) walks[i] = imutils.spawn_random_walk(pos, n_steps) state[pos[0], pos[1], :] = colors[c] except IndexError: pass k0 = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] k1 = [[1, 1, 1, 1, 1], [1, 2, 2, 2, 1], [1, 2, 3, 2, 1], [1, 2, 2, 2, 1], [1, 1, 1, 1, 1]] walk = [] walk.append([plt.imshow(state)]) for step in range(n_steps): ind = 0 new_positions = [] rmean = state[:, :, 0].mean() gmean = state[:, :, 1].mean() bmean = state[:, :, 2].mean() imean = [rmean, gmean, bmean] for pt in starting_points: px = walks[ind][step] try: state[pt[0], pt[1], :] = [0, 0, 0] state[px[0], px[1], :] = colors[c] except IndexError: pass new_positions.append(px) ind += 1 rch = ndi.convolve(state[:, :, 0], k1) gch = ndi.convolve(state[:, :, 0], k1) bch = ndi.convolve(state[:, :, 2], k1) # for ii in range(len(new_positions)): state, starting_points = rule_two(ind - 1, state, rch, gch, bch, starting_points, imean) # state, starting_points = apply_rule(ind - 1, state, rch, gch, bch, starting_points, imean) starting_points = new_positions state[:, :, 2] += 2 * bch / (n_steps) state[:, :, 1] += gch / (2 * n_steps) walk.append([plt.imshow(state)]) print '\033[1m\033[3mSIMULATION FINISHED \033[0m\033[1m[%ss Elapsed]\033[0m' % str( time.time() - tic) a = animation.ArtistAnimation(f, walk, interval=100, blit=True, repeat_delay=900) w = FFMpegWriter(fps=20, metadata=dict(artist='Me'), bitrate=1800) a.save('pattern_generator_2.mp4', writer=w) plt.show() return state
def initialize_cloud(self, N, verbose): pt_cloud = {} for pt in range(N): [x, y] = imutils.spawn_random_point(self.state) pt_cloud[pt] = [x, y] if verbose: print str(len(pt_cloud.keys())) + " Particle Point Cloud Created" return pt_cloud
def add_custom_particle_mix(state, mixture): px_added = {'r': [], 'g': [], 'b': [], 'c': [], 'm': [], 'y': []} for c in mixture.keys(): for pt in range(mixture[c]): color = known_colors[c] added = False while not added: try: [x, y] = imutils.spawn_random_point(state) state[x, y, :] = color added = True px_added[c].append([x, y]) except IndexError: pass return state, px_added
def add_particles(world, particle, n): points = [] for pt in range(n): try: [x, y] = imutils.spawn_random_point(world[:, :, 0]) world[x, y, 0] = 1 p = Particle(1, 1, [x, y]) nx = particle.position[0] ny = particle.position[1] px = p.position[0] py = p.position[1] p.rvec = np.sqrt((px - nx)**2 + (py - ny)**2) p.calculate_gravitational_attraction(particle) points.append(p) except IndexError: pass return points
def add_even_particle_mix(state, n_total, colors): extra = 0 pix_per_col = int(np.floor(n_total / float(len(colors)))) if pix_per_col != n_total / len(colors): extra = len(colors) - pix_per_col print '[*] Overflow: %d' % extra print 'Total: %d, Pix-Per-Color: %d N-Colors: %d' %\ (n_total, pix_per_col, len(colors)) px_added = {'r': [], 'g': [], 'b': [], 'c': [], 'm': [], 'y': []} while len(colors) >= 1: if extra and len(colors) == 1: pix_per_col += extra color = colors.pop() if color not in known_colors.keys() and px_added.keys(): print '[!!] Unknown Color: %s' % color c = known_colors[color] while len(px_added[color]) < pix_per_col: try: [x, y] = imutils.spawn_random_point(state) state[x, y, :] = c px_added[color].append([x, y]) except IndexError: pass return state, px_added
import PIL.Image as Image import numpy as np import imutils import time root = Tk.Tk() canvas = Tk.Canvas(root, height = 500, width = 500) tic = time.time() cell_file = PIL.Image.open("box.png") cell_mat = cell_file.resize((100, 100)) cell_im = PIL.ImageTk.PhotoImage(cell_mat) cell = canvas.create_image(100, 100, image=cell_im, tags='cell') # Create ANT Object(s) ant_start = imutils.spawn_random_point(np.zeros((500, 500))) ant_file = PIL.Image.open("ant.png") ant_mat = ant_file.resize((20, 35)) ant_im = PIL.ImageTk.PhotoImage(ant_mat) ant = canvas.create_image(ant_start[0], ant_start[1],image=ant_im,tags='ant') images = [cell] def getImage(imagePI, x, y): for image in images: curr_x, curr_y = canvas.coords(image) x1 = curr_x - imagePI.width()/2 x2 = curr_x + imagePI.width()/2 y1 = curr_y - imagePI.height()/2 y2 = curr_y + imagePI.height()/2 if (x1 <= x <= x2) and (y1 <= y <= y2):
move = directions[n] opt = self.state[move[0], move[1]] if (opt[0] and opt[1]) == 0 and opt[2] == 1 and world[self.x, self.y] == 0: self.state[self.x, self.y, :] = 0 moves.append(move) self.set_pos(move) break return moves if __name__ == '__main__': ''' Create World ''' W = 250 H = 250 world = np.zeros((W, H, 3)) world[:, :, 2] += np.random.random_integers(0, 255, W * H).reshape( (W, H)) > 128 ''' Define Simulation Parameters''' depth = 1000 n_cells = 100 organisms = [] for i in range(n_cells): [x, y] = imutils.spawn_random_point(world[:, :, 2]) fly = Cell([x, y], 'red') organisms.append(fly) plt.imshow(fly.state) plt.show()
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure import matplotlib.pyplot as plt import PIL.Image as Image import Tkinter as Tk import numpy as np import imutils import engine import time import sys tic = time.time() width = 250 height = 250 world = np.zeros((width, height, 3)) start = imutils.spawn_random_point(world[:, :, 0]) Point = engine.particle(start) world[Point.x - 3:Point.x, Point.y - 3:Point.y, 2] = 1 root = Tk.Tk() root.wm_title("Embedding in TK") # ##### ###################### [| * ~ Figure Methods ~ * |] ###################### ##### # def on_click(event): [x, y] = [int(event.xdata), int(event.ydata)] print '[%s, %s]' % (str(x), str(y)) Point.update(x, y) world[Point.x - 3:Point.x, Point.y - 3:Point.y, 2] = 1 a.imshow(world)