Thanks Alan Appreciate your help
I’ll try that The code is listed here as well I am currently working with the lattice.py code attached. The code is a simulation that models the interactions between bacteria by producing a lattice output. Once you run the code, there should be both blue and red bacteria that show on a lattice. I can manipulate the size of the lattice to be an integer. In this case, I have set it to 250 However, there are different bacteria cells occupying each "pixel" on the lattice. I could count the number of red or blue bacteria on the output, but It would take an extremely long time I need to keep track of the number of red or blue cells in lists, and while I know how to instantiate a list, I don't know how to store the information I need in the list with the code given. The majority of the code has mostly commented out explanations but not entirely. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Dec 3 10:51:05 2016 """ #%% imports and prep import numpy as np from numpy.random import rand as r from matplotlib import pyplot as plt from collections import defaultdict as d #%% class Lattice(object): def __init__(self,size=100,slider=0,rb=False,redAdvantage=1,blueAdvantage=1,defKillers=False,density=1,numRatio=1,redGrowth=1,blueGrowth=1,deathRate=100000000,antibioticDeath=1): self.rb=rb #rb=True means the lattice contains only red and blue bacteria self.slider=slider #if slider is 0 then only killing happens, if slider is 1 then only "random death" and for a range between it's a mixture #set up growth and killing disparity between red and blue bacteria self.redGrowth=redGrowth #1 means equal growth self.blueGrowth=blueGrowth self.redAdvantage=redAdvantage #killing disparity, 1 means equal killers self.blueAdvantage=blueAdvantage #establish the size of the lattice. For a non-square lattice use size=[x,y] try: x,y=size[1],size[0] except TypeError: x,y=size,size; self.x=x self.y=y self.size=size #if defective killers then red and blue can't kill each other self.defKillers=defKillers #overall cell density at initialization of the lattice self.density=density #overall number ratio (number of blue/ total number of cells) self.numRatio=numRatio #if defective killers set to true then there's no random death either (no killing, no random death) if defKillers==True: self.slider=0; if rb: #initialize the lattice to contain only red and blue cells and empty sites, chosen randomly according to numRatio and density self.lattice=np.random.choice([.2295,.00254],size=(x,y)) try: if density!=1: self.lattice=np.random.choice([0,.2295,.00254],p=[1-density,(density)*(1-numRatio),density*(numRatio)],size=(x,y)) except ValueError: print("Density should be an integer or float") else: #initialize the lattice with a bunch of different types of cells (represented as different colors) self.lattice=r(x,y) if density!=1: for bug in np.ravel(self.lattice): if r()>density: self.lattice[self.lattice==bug]=0 #killdict is a hashtable containing the killing effectiveness for each color killdict=d(list) killdict[0]=0 for color in np.ravel(self.lattice): killdict[color]=r() killdict[0]=0 self.killdict=killdict def evolve(self,n_steps): #main function, moves the lattice forward n steps in time for t in range(n_steps): #pick lattice site try: j=np.random.randint(1,self.y-2) i=np.random.randint(1,self.x-2) except ValueError: #this will happen if you've chosen your lattice to be one dimensional i=0 j=np.random.randint(0,self.y-1) #random death happens if slider>random float in [0,1] if self.slider>r(): self.lattice[i,j]=np.random.choice(np.ravel(self.lattice[i-1:i+2,j-1:j+2])) #else killing/filling a la IBM happens else: #get the neighborhood of the ith,jth 'pixel' neighborhood=self.lattice[i-1:i+2,j-1:j+2]; # find number of species one (red, .2295), species two (blue, .00254) n_blue=np.size(neighborhood[neighborhood==0.00254]); n_red=np.size(neighborhood[neighborhood==.2295]); n_enemy=np.size(neighborhood[neighborhood!=self.lattice[i,j]]); #total number of differently colored cells in neighborhood # KILLING..........########## if (self.rb==True and self.lattice[i,j]==.2295): #site is filled with red bact if self.x==1: #this will happen if your lattice is one dimensional thresh=.5 else: thresh=2 if n_blue*r()*self.blueAdvantage>thresh: #if number of blue cells * their killing advantage * random number > 2, kill this red bacteria (replace with empty site) if self.defKillers==False: self.lattice[i,j]=0; #kill this bacteria elif (self.rb==True and self.lattice[i,j]==.00254): #site is filled with a blue bacteria if self.x==1: thresh=.5 else: thresh=2 if n_red*r()*self.redAdvantage>thresh: if self.defKillers==False: self.lattice[i,j]=0; #kill this bacteria elif (n_enemy>0 and self.lattice[i,j]!=0): #site is not empty and has neighbors (non-specific neighbors, different color) enemy_weight=0; for enemy in np.ravel(neighborhood): if (enemy!=0 and enemy!=self.lattice[i,j]): try: enemy_weight+=self.killdict[enemy]; except TypeError: print("ERROR") pass #enemy_weight=enemy_weight+self.killdict[enemy][0]; if enemy_weight*r()>2: #if enough enemies, kill this bacterium self.lattice[i,j]=0; # FILLING ....... ######### elif (self.lattice[i,j]==0): #site is empty if (self.rb==True and n_red+n_blue>0): #going to fill with either red or blue if (((n_red*self.redGrowth+n_blue*self.blueGrowth)*r())>2): if (n_red*self.redGrowth*r()>n_blue*self.blueGrowth*r()): self.lattice[i,j]=.2295 else: self.lattice[i,j]=0.00254; else: self.lattice[i,j]=0; elif(n_enemy>0): choices=np.ravel(neighborhood[neighborhood!=0]) #find all the other colors in neighborhood if choices.size==0: #if no other cells in neighborhood then stay empty self.lattice[i,j]=0 continue #fill with one of the other colors in neighborhood (according to number of cells) choices=list(choices) choices2=[choice*(1-self.killdict[choice]) for choice in choices] choices2=[choice/len(choices2) for choice in choices2] zeroprob=1-sum(choices2) choices2.append(zeroprob) choices2=np.array(choices2) choices.append(0) choices=np.array(choices) self.lattice[i,j]=np.random.choice(choices,p=choices2) #self.lattice[i,j]=np.random.choice(np.ravel(neighborhood[neighborhood!=0])) return def view(self): #use this to view your lattice def int2color(x): #converts lattice integer to RGB tuple red_val=int(1000*x%255) green_val=int(10000*x%255) blue_val=int(100000*x%255) return (red_val,green_val,blue_val) from PIL import Image lu=list(map(int2color,np.ravel(self.lattice[:,:]))) #now a list of RGB tuples imu = Image.new('RGB', [self.lattice.shape[1],self.lattice.shape[0]]) imrb = Image.new('RGB', [self.lattice.shape[1],self.lattice.shape[0]]) imu.putdata(lu); imu.show if self.rb!=True: return imu return imu my_lattice = Lattice(size=50, slider=0, rb=True, numRatio=20, deathRate=100000) my_lattice.view() im = my_lattice.view(); im.show() On Mon, Aug 13, 2018 at 04:21 Alan Gauld via Tutor <tutor@python.org> wrote: > On 13/08/18 06:15, Gautam Desai wrote: > > > I am currently working with the lattice.py code attached. > > The server doesn't like attachments so strips them off > for security. As a result we can't see the code. > > Please resend, but paste the code into the body of your > message (in plain text to preserve formatting). > It would be helpful to also include your OS and > Python version. > > > lattice. I could count the number of red or blue bacteria on the output, > > but with a size of 250 that would take an extremely long time. > > 250 of anything is not very much in computing terms > (even if your lattice is 250 cubed its still not > unusually big). But I guess it depends on what the data > elements look like. > > > I need to keep track of the number of red or blue cells in lists, and > while > > I know how to instantiate a list, I don't know how to store the > information > > I need in the list with the code given. > > We can't see the code but the most common operation for > adding data to a list is the append() method > > mylist.append(anObject) > > But we can say more when we see the code. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor