Skip to content

Commit

Permalink
adding updated changes... but this doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
tripplerizz committed Apr 14, 2020
1 parent f68b784 commit 8792556
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 84 deletions.
97 changes: 32 additions & 65 deletions Cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def isOld(self):
return int(self.age >= 74)

def hasVirus(self):
return self.infectionStatus == 2
return int(self.infectionStatus == 2 )

# -----------------------------------------------------------------------------------------
# set functions
Expand All @@ -75,84 +75,51 @@ def setInfectionStatus(self, stat):
self.infectionStatus = stat

# move functions---------------------------------------------------------------------------------
def setPos(self, x, y): # setting x,y coords
# this function checks the bounds of the board
if (x >= self.gameBoard.size - 1):
def setPos(self,x,y): # setting x,y coords
# this function checks the bounds of the board
if(x > self.gameBoard.size -1):
return
if (y >= self.gameBoard.size - 1):
if (y > self.gameBoard.size - 1):
return
if (x <= 1):
if ( x <= 1):
return
if (y <= 1):
return

self.Column = y
self.Row = x

def returnKernel(self):
miniboard = numpy.ones((3, 3), dtype=int) # this function creates a sub grid of neighbors around cell
gameGrid = self.gameBoard.grid
gameSize = self.gameBoard.size
#------------------------------------
rowDown = self.Row - 1 # these variables represent pos on grid next to cell
rowUp = self.Row + 1
columnDown = self.Column -1
columnUp = self.Column + 1

miniboard[0][0] = gameGrid[(rowDown) % gameSize][columnDown % gameSize]
miniboard[0][1] = gameGrid[rowDown % gameSize][(self.Column)]
miniboard[0][2] = gameGrid[rowDown % gameSize][columnUp % gameSize]
miniboard[1][0] = gameGrid[(self.Row)][columnDown % gameSize]
miniboard[1][1] = self.infectionStatus
miniboard[1][2] = gameGrid[(self.Row)][columnUp % gameSize]
miniboard[2][0] = gameGrid[rowUp % gameSize][columnDown % gameSize]
miniboard[2][1] = gameGrid[rowUp % gameSize][(self.Column)]
miniboard[2][2] = gameGrid[rowUp % gameSize][columnUp % gameSize]

return miniboard


def getPosInGrid(self, x, y):
return self.Row + x, self.Column + y

def move(self):
newrow = self.Row
newcol = self.Column
if (self.infectionStatus == 1): # move for healthy cells, finding the least crowded space
kernel = numpy.ones((3, 3), dtype=numpy.int8)
kernel[1, 1] = 0
miniboard = self.returnKernel()
neighbour = signal.convolve(miniboard, kernel, mode='same') # count neighbors
neighbour[1][1] = 999
j = numpy.argwhere(neighbour == numpy.min(neighbour))
x = random.randrange(0, len(j))
minindex = j[x]
newrow, newcol = self.getPosInGrid(minindex[0] - 1, minindex[1]- 1)

elif(self.infectionStatus==2):
#SET RANDOM POSITION FOR INFECTED CELLS
newrow = self.Row + (random.randint(-1, 1))
newcol = self.Column + (random.randint(-1, 1))

# IF POSITION OCCUPIED, perform collision
if(self.gameBoard.grid[newrow][newcol]!=0):
self.collision()
self.time +=1
self.setPos(newrow, newcol)

def collision(self): # Collision function with other cells
if (self.infectionStatus == 1):
x = random.randint(0, 2)
def move(self, cellDict):
while True:
x = self.Row + (random.randint(-1,1))
y = self.Column + (random.randint(-1,1))
if x > 0 and x < self.gameBoard.size -1 and y > 0 and y < self.gameBoard.size -1:
break

while(self.gameBoard.grid[x,y] != 0):
if cellDict[x,y].infectionStatus == 2:
self.collision()

x = self.Row + (random.randint(-1,1))
y = self.Column + (random.randint(-1,1))

self.setPos(x, y)
self.time += 1


def collision(self):
if(self.infectionStatus == 1):
x = random.randint(0,2)
if (x == 2):
self.infectionStatus = 2 # set to infected
self.gameBoard.infectedCount += 1 # updating infected count of population
self.infectionStatus = 2
self.gameBoard.infectedCount += 1 # updating infected count of population

def deathRate(self): # All encompassing death rate function for cells.
virus = self.hasVirus()
vulnerable = self.isVulnerable()
daily = 0.00005
total = (daily + vulnerable + virus)
if random.random() <= total:
total = (vulnerable + virus)
print(total)
if random.randint(0, 10) <= total:
self.gameBoard.infectedCount -= 1
return 1
return 0
29 changes: 18 additions & 11 deletions GameBoard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import matplotlib.pyplot as plt
import random as rd
from Cell import Cell
from info import getTotalCases, getTotalDeaths, getDeathRate, getPopulation
import info
# from info import getTotalCases, getTotalDeaths, getDeathRate, getPopulation

class Board:

Expand All @@ -19,18 +20,24 @@ def __init__(self, inSize): # creating parameter for user
self.grid = np.zeros((inSize, inSize))
cell_dict = {}

def createPopulation (self, population, infected): # creates a population based on parameters and board
def relativePopulation(self, state): # this function grabs population from census information
return int(info.getPopulation(state)/ self.size **2)

def getPercentInfected(self, state):
return float(info.getTotalCases(state)/info.getPopulation(state))

def createPopulation (self, state): # creates a population based on parameters and board

self.population = population + 1
self.infectedCount = infected
self.population = self.relativePopulation(state)
self.infectedCount = int(self.population * self.getPercentInfected(state))
randomCoord = [0,0]
randomCoord[0] = (rd.randint(1, self.size - 1)) # location x
randomCoord[1] = (rd.randint(1, self.size - 1)) # location y

for x in range(0, self.population -1):
for x in range(0, self.population):

infectionStatus = 1
if x < infected: # this condition creates number of infected sells specified by parameters
if x < self.infectedCount: # this condition creates number of infected sells specified by parameters
infectionStatus = 2

# get random coordinates for cell
Expand All @@ -50,24 +57,24 @@ def createPopulation (self, population, infected): # creates a population based
self.grid[new_cell.Row, new_cell.Column] = new_cell.infectionStatus # add cell status to grid

def update_grid(self):
for x in range(0, self.size-1):
for y in range(0, self.size-1):
for x in range(0, self.size):
for y in range(0, self.size):
if (x, y) in self.cell_dict:
popped_cell = self.cell_dict.pop((x, y)) # get cell from 'grid' (accessed via dictionary)
self.grid[x][y] = 0 # set grid cell to 0
popped_cell.move() # call move on cell
popped_cell.move(self.cell_dict) # call move on cell

if(popped_cell.time % 10 != 0): # Every 10 moves, no deaths
self.cell_dict[popped_cell.Row, popped_cell.Column] = popped_cell
self.grid[popped_cell.Row][popped_cell.Column] = popped_cell.infectionStatus
else:
#if(popped_cell.deathRate() == 0): # Otherwise, call for death
if (popped_cell.deathRate() == 0): # Otherwise, call for death
self.cell_dict[popped_cell.Row, popped_cell.Column] = popped_cell
self.grid[popped_cell.Row][popped_cell.Column] = popped_cell.infectionStatus


def show(self):
while len(self.cell_dict) != 20: # Visualize the grid
while len(self.cell_dict) > 150: # Visualize the grid
plt.imshow(self.grid)
plt.title("Population: " + str(len(self.cell_dict)))
plt.xlabel("starting infection count = " + str(self.infectedCount))
Expand Down
4 changes: 2 additions & 2 deletions GermTheory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def main():


b1 = Board(100) # Create board
b1.createPopulation( 10 , int(input("how many infected members: "))) # Create population
b1.update_grid() # Update grid
b1.createPopulation( input("What state are you in? ")) # Create population
b1.update_grid() # Update gridFlo
b1.show() # Show

main()
60 changes: 54 additions & 6 deletions info.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import requests
import pandas as pd

url = 'https://covidtracking.com/api/states'

response = requests.request("GET", url)
data = response.json()


def prepareData():
Expand All @@ -29,10 +26,62 @@ def getPopulation(input):
return int(data.at[state, 'population'])


url = 'https://covidtracking.com/api/states'

response = requests.request("GET", url)
data = response.json()

stateConversion = { "Alabma" :"AL",
"Alaska": "AK",
"Arizona": "AZ",
"Arkansas" : "AR",
"California" : "CA",
"Connecticut" : "CO",
"Delaware": "DE",
"Florida" : "FL",
"Georgia" : "GA",
"Hawaii" : "HI",
"Idaho" : "ID",
"Illinois" : "IL",
"Indiana" : "IN",
"Iowa" : "IA",
"Kansas" : "KS",
"Kentucky" : "KY",
"Louisiana" : "LA",
"Maine": "ME",
"Maryland" : "MD",
"Massachusetts" : "MA",
"Michigan" : "MI",
"Minnesota" : "MN",
"Mississipi" : "MS",
"Missouri" : "MO",
"Montana" : "MT",
"Nebraska" : "Nevada",
"New Hampshire" : "NH",
"New Jersey" : "NJ",
"New Mexico" : "NM",
"New York" : "NY",
"North Carolina" : "NC",
"North Dakota" : "ND",
"Ohio" : "OH",
"Oklahoma" : "OK",
"Oregon" : "OR",
"Pennsylvania" :"PA",
"Rhode Island" : "RI",
"South Carolina" : "SC",
"Tennessee" : "TN",
"Texas" : "TX",
"Utah" : "UT",
"Vermont" : "VT",
"Virginia" : "VA",
"Washington" : "WA",
"West Virginia" : "WV",
"Wisconsin" : "WI",
"Wyoming" : "WY"}

def getState(state):
for index,list in enumerate(data):
if list['state'] == state:
for list in data:
if list['state'] == stateConversion.get(state):
return list

def getTotalCases(state):
Expand All @@ -46,4 +95,3 @@ def getTotalDeaths(state):
def getDeathRate(state):
return (getTotalDeaths(state)/getTotalCases(state)) * 100

print(getPopulation(input("what state?")))
Binary file added populationExcel.xlsx
Binary file not shown.

0 comments on commit 8792556

Please sign in to comment.