#-*- coding: iso-latin-1 -*- """ LDynamicArray es un archivo con una clase DynamicArray útil para obtener patrones bidimensionales de Sistemas Dinámicos marzo, 2009 """ import math import Tkinter import Image, ImageTk, ImageDraw import scipy from pickle import load,dump from tkFileDialog import * from tkMessageBox import * DefaultImageSize = (500,500) root = Tkinter.Tk() root.withdraw() class DynamicArray (Tkinter.Label): def __init__(self, shape, size=DefaultImageSize, RGB=(1,1,1), mode='RGB', zmin=0.0, zmax=1.0): self.top = Tkinter.Toplevel() self.top.title('DynamicArray') Tkinter.Label.__init__(self, self.top) self.shape = shape self.size = size self.mode = mode self.zmin = zmin self.zmax = zmax self.RGB = RGB self.zrange = zmax-zmin self.NormalContrast = True self.HighContrast = False self.ExtremeContrast = False self.InverseColor = False self.canvas = Tkinter.Canvas(self.top, width=self.size[0], height=self.size[1]) Tkinter.Widget.bind(self.canvas, "", self.mouseDown) Tkinter.Widget.bind(self.canvas, "", self.mouseUp) self.mousecoords = [] self.im = Image.new(self.mode, self.shape) self.displayIm = self.im.resize(self.size) if self.mode == '1': self.tkimage = \ ImageTk.BitmapImage(self.displayIm,foreground="white") else: self.tkimage = \ ImageTk.PhotoImage(self.displayIm) self.canvas.create_image(0, 0, anchor=Tkinter.NW, image=self.tkimage) self.canvas.pack() def setTitle(self, title): self.top.title(title) def mouseDown(self, event): x0 = self.canvas.canvasx(event.x) y0 = self.canvas.canvasx(event.y) sx0 = int(self.shape[0] * float(x0)/self.size[0]) sy0 = int(self.shape[1] * float(y0)/self.size[1]) self.mousecoords = [sx0, sy0] def mouseUp(self, event): sx0, sy0 = self.mousecoords x1 = self.canvas.canvasx(event.x) y1 = self.canvas.canvasx(event.y) sx1 = int(self.shape[0] * float(x1)/self.size[0]) sy1 = int(self.shape[1] * float(y1)/self.size[1]) X0, X1 = min(sx0, sx1), max(sx0, sx1) Y0, Y1 = min(sy0, sy1), max(sy0, sy1) self.mousecoords = [X0, Y0, X1, Y1] def IsBoxSelected(self): return len(self.mousecoords)==4 def GetMouseBox(self): mc = self.mousecoords[:] self.mousecoords = [] return mc def display(self, a): print "display ", a.shape for i in range(a.shape[0]): for j in range(a.shape[1]): self.im.putpixel((i,j), self.colorscale(a[i,j])) self.displayIm = self.im.resize(self.size) self.tkimage.paste(self.displayIm) self.canvas.update() def line(self, x, color='black'): draw = ImageDraw.Draw(self.im) draw.line(x,fill=color) del draw self.displayIm = self.im.resize(self.size) self.tkimage.paste(self.displayIm) self.canvas.update() def point(self, x, radius=1, color='black'): draw = ImageDraw.Draw(self.im) draw.ellipse((x[0]-radius,x[1]-radius,x[0]+radius,x[1]+radius),\ fill=color) del draw self.displayIm = self.im.resize(self.size) self.tkimage.paste(self.displayIm) self.canvas.update() def colorscale(self, value): sval = 1.-(value-self.zmin)/(self.zrange) if self.InverseColor: sval = 1.-sval if sval < 0.: sval = 0. if sval > 1.: sval = 1. if self.ExtremeContrast: if sval: sval -= 1.e-10 sval = int(2*sval)/2. elif self.HighContrast: if sval: sval -= 1.e-10 sval = int(3*sval)/3. return (int(255.*(sval))*self.RGB[0],\ int(255.*(sval))*self.RGB[1],\ int(255.*(sval))*self.RGB[2]) def setNormalContrast(self): self.NormalContrast = True self.HighContrast = False self.ExtremeContrast = False def setHighContrast(self): self.NormalContrast = False self.HighContrast = True self.ExtremeContrast = False def setExtremeContrast(self): self.NormalContrast = False self.HighContrast = False self.ExtremeContrast = True def setInverseColor(self, v = True): self.InverseColor = v def set_zmin(self, zmin): self.zmin = zmin self.zrange = self.zmax - self.zmin def set_zmax(self, zmax): self.zmax = zmax self.zrange = self.zmax - self.zmin ### numéricos ############# def DiscreteLap(self, A): nx, ny = A.shape del2 = scipy.zeros((nx, ny), float) del2[1:-1, 1:-1] = A[1:-1,2:] + A[1:-1,:-2] + \ A[2:,1:-1] + A[:-2,1:-1] return del2 def Lap_5(self, A, dx=1.0): nx, ny = A.shape del2 = scipy.zeros((nx, ny), float) del2[1:-1, 1:-1] = (A[1:-1,2:] + A[1:-1,:-2] + \ A[2:,1:-1] + A[:-2,1:-1] - 4.*A[1:-1,1:-1])/(dx*dx) return del2 def Lap_9(self, A, dx=1.0): ## OJO! este es para simetría circular!! nx, ny = A.shape del2 = scipy.zeros((nx, ny), float) del2[1:-1, 1:-1] = ((2./3.)*A[1:-1,2:] + (2./3.)*A[1:-1,:-2] + \ (2./3.)*A[2:,1:-1] + (2./3.)*A[:-2,1:-1] - \ (10./3.)*A[1:-1,1:-1] + \ (1./6.)*A[2:,2:] + (1./6.)*A[2:,:-2] + \ (1./6.)*A[:-2,2:] + (1./6.)*A[:-2,:-2])/(dx*dx) return del2 def Frontier(self, A, mode=2): if mode == 1: A[0,:] = A[1,:] # fila cero / uno A[:,0] = A[:,1] # columna cero / uno A[-1,:] = A[-2,:] # última fila / penúltima A[:,-1] = A[:,-2] # última columna / penúltima elif mode == 2: A[0,:], A[-1,:] = A[-2,:].copy(), A[1,:].copy() A[:,0], A[:,-1] = A[:,-2].copy(), A[:,1].copy() elif mode == 3: A[0,:] = 0 A[:,0] = 0 A[-1,:] = 0 A[:,-1] = 0 def saveim(self, A, name='DynamicArray', ext='.jpeg'): ff = open(name+ext,"w") self.im.save(ff,"JPEG") ff.close() def savearray(self, A, name='DynamicArray', ext='.pkl'): ff = open(name+ext,"w") dump(A,ff) ff.close() def savedata(self, A, name='DynamicArray', ext='.dat'): ff = open(name+ext,"w") for i in range(A.shape[0]): for j in range(A.shape[1]): ff.write("%f " % (A[i][j])) ff.write("\n") ff.close() def loadarray(self, name='*pkl'): self.name=askopenfilename(title="Selección de Mapa",\ filetypes=[('MAP Files',name)]) ## lectura de paisaje self.top.title(self.name) self.ff = open(self.name,"r") A = load(self.ff) self.ff.close() print self.n return A