def initialise(self): """Initialisation method for EpithelialSystem, run only once when first created. Sets up the world's epithelial """ initialInfected = int( (Worldspace.GRID_WIDTH * Worldspace.GRID_HEIGHT) * self.INFECT_INIT) if int( (Worldspace.GRID_WIDTH * Worldspace.GRID_HEIGHT) * self.INFECT_INIT) > 1 else 1 self.initialInfected = initialInfected self.containingCount = initialInfected self.healthyCount = (Worldspace.GRID_WIDTH * Worldspace.GRID_HEIGHT) - initialInfected # Create epithelial cell at every site, add to system's cell list for i in xrange(Worldspace.GRID_WIDTH): for j in xrange(Worldspace.GRID_HEIGHT): tempECell = EpithelialCell(Vector2d(i, j)) if EpithelialSystem.RANDOM_AGE: tempECell.age = RNG.randint(0, EpithelialCell.CELL_LIFESPAN) self.world[i][j].eCell = tempECell self.cells.append(tempECell) # Set random epithelial cells to containing for initial infected count while initialInfected > 0: randomx = RNG.randint(0, Worldspace.GRID_WIDTH - 1) randomy = RNG.randint(0, Worldspace.GRID_HEIGHT - 1) tempECell = self.world[randomx][randomy].getECell() if tempECell.State == EpithelialStates.HEALTHY: tempECell.State = EpithelialStates.CONTAINING if FocusSystem.ENABLED: self.fSys.addNewFocus(tempECell) initialInfected -= 1
def initialise(self): """Create the initial density of virgin cells in the worldspace, and add them to the system's cell list.""" for i in range(ImmuneSystem.INIT_CELLS): x = RNG.randint(0, Worldspace.GRID_WIDTH - 1) y = RNG.randint(0, Worldspace.GRID_HEIGHT - 1) cell = ImmuneCell(Vector2d(x, y)) cell.age = RNG.randint(0, ImmuneCell.IMM_LIFESPAN) self.cells.append(cell) self.world[x][y].getImmCells().append(cell) self.virginCount += 1
def __updateMaintenance(self): """Private method, should only be called from public update() method. Creates new virgin immune cells to maintain minimum density as required.""" cell = None while self.virginCount < self.INIT_CELLS: x = RNG.randint(0, Worldspace.GRID_WIDTH - 1) y = RNG.randint(0, Worldspace.GRID_HEIGHT - 1) cell = ImmuneCell(Vector2d(x, y)) self.world[x][y].getImmCells().append(cell) self.cells.append(cell) self.virginCount += 1
def setUp(self): Systems.ImmuneSystem.BASE_IMM_CELL = 0.00015 Systems.EpithelialSystem.INFECT_INIT = 0.01 Worldspace.GRID_WIDTH = 3 Worldspace.GRID_HEIGHT = 3 Cells.ImmuneCell.IMM_LIFESPAN = 1008 self.world = [] for x in xrange(Worldspace.GRID_WIDTH): self.world.append([]) for y in xrange(Worldspace.GRID_HEIGHT): self.world[x].append(Worldsite(Vector2d(x, y))) self.eSys = Systems.EpithelialSystem(self.world) self.iSys = Systems.ImmuneSystem(self.world)
def __updateRecruitment(self): """Private method, should only be called from public update() method. Creates new immune cells randomly about the Worldspace as required.""" cell = None for i in xrange(len(self.recruitmentTimes) - 1, -1, -1): self.recruitmentTimes[i] += 1 if self.recruitmentTimes[i] >= ImmuneSystem.RECRUITMENT_DELAY: del self.recruitmentTimes[i] self.currentRecruitment += ImmuneSystem.RECRUITMENT if self.currentRecruitment >= 1: self.currentRecruitment -= 1 x = RNG.randint(0, Worldspace.GRID_WIDTH - 1) y = RNG.randint(0, Worldspace.GRID_HEIGHT - 1) cell = ImmuneCell(Vector2d(x, y)) ImmuneSystem.setNextState(cell, ImmuneStates.MATURE) self.matureCount += 1 self.world[x][y].getImmCells().append(cell) self.cells.append(cell)
def run(self, settings): """Main loop.""" self.avgFociAreaMM2 = None if Graph.SHOW: data = SimulationData() graph = OverallSimulationDataGraph() graph.setXMeasurement('hours') graph.setTimestepsInXMeasurement(6) fociAreaGraph = FociAreaGraph() #fociAreaGraph = FociAreaGraph(True, Worldspace.GRID_WIDTH, Worldspace.GRID_HEIGHT) fociAreaGraph.setXMeasurement('hours') fociAreaGraph.setTimestepsInXMeasurement(6) if SimVis.ENABLED: q.put((simVis.display, (), {})) self.numberOfRuns = settings["iNumberOfRuns"] self.runTime = settings["iRunTime"] self.debugTextEnabled = settings["bDebugTextEnabled"] #initialise runs run = 0 while run < self.numberOfRuns: if self.debugTextEnabled: startTime = time.clock() Log.out("Start time: %s" % startTime) #re-initialize world and systems if not on the initial run world = [] for x in xrange(Worldspace.GRID_WIDTH): world.append([]) for y in xrange(Worldspace.GRID_HEIGHT): world[x].append(Worldsite(Vector2d(x, y))) eSys = Systems.EpithelialSystem(world) immSys = Systems.ImmuneSystem(world) eSys.initialise() if (Systems.ImmuneSystem.ISENABLED): immSys.initialise() if Graph.SHOW: graph.setTotalEpithelialCells(Worldspace.GRID_WIDTH * Worldspace.GRID_HEIGHT) graph.setBaseImmuneCells(immSys.INIT_CELLS) graph.initRun() fociAreaGraph.initRun() if SimVis.ENABLED: simVis.init(world, run + 1) # Run simulation for a given number of timesteps # 10 days = 1440 timesteps timesteps = 0 while timesteps <= self.runTime: eSys.update() if (Systems.ImmuneSystem.ISENABLED): immSys.update() eSys.synchronise() if (Systems.ImmuneSystem.ISENABLED): immSys.synchronise() if Graph.SHOW: data.time = timesteps data.eCellsHealthy = eSys.healthyCount data.eCellsContaining = eSys.containingCount data.eCellsExpressing = eSys.expressingCount data.eCellsInfectious = eSys.infectiousCount data.eCellsDead = eSys.naturalDeathCount + eSys.infectionDeathCount data.immCellsTotal = immSys.virginCount + immSys.matureCount graph.addSimulationData(data) if (Systems.FocusSystem.ENABLED): area = 0.0 c = 0 for foci in eSys.fSys.foci.values(): if foci.isEnabled: if foci.cellCount != 0: area += foci.cellCount c += 1 if c == 0: area = 0 else: area = area / c self.avgFociAreaMM2 = fociAreaGraph.addAverageFociAreaData( area, timesteps) if self.debugTextEnabled: Log.out('%d: %d' % (run + 1, timesteps)) Log.out("Infected cells: %s" % (eSys.containingCount + eSys.expressingCount + eSys.infectiousCount)) Log.out("Healthy: %s" % (eSys.healthyCount)) Log.out("Containing: %s" % (eSys.containingCount)) Log.out("Expressing: %s" % (eSys.expressingCount)) Log.out("Infectious: %s" % (eSys.infectiousCount)) Log.out( "Dead: %s" % (eSys.naturalDeathCount + eSys.infectionDeathCount)) Log.out("Virgin: %s" % (immSys.virginCount)) Log.out("Mature: %s" % (immSys.matureCount)) if Systems.FocusSystem.ENABLED and self.avgFociAreaMM2 != None: #Log.out("Average focus area: %s" % (eSys.avgFociArea)) Log.out("Average focus area (mm2): %s" % (self.avgFociAreaMM2)) Log.out("\n") if SimVis.ENABLED: if timesteps == 0 or timesteps % 72 == 0: simVis.drawSimWorld(True, timesteps) else: simVis.drawSimWorld(False, timesteps) if Systems.FocusSystem.ENABLED: if len(eSys.fSys.mergeDetected) > 0: for i in xrange( len(eSys.fSys.mergeDetected) - 1, -1, -1): if SimVis.HIGHLIGHT_COLLISIONS: focus = eSys.fSys.mergeDetected[i] for perimeterCell in focus.perimeter: simVis.drawCollision(perimeterCell) del eSys.fSys.mergeDetected[i] # HACK: For debugging/testing purposes. Will be removed/refactored soon. if SimVis.HIGHLIGHT_COLLISIONS and SimVis.ENABLED: simVis._SimVis__savePILImageToFile(False) simVis._SimVis__updateCanvas(False) #if Systems.FocusSystem.DEBUG_TEXT_ENABLED: #raw_input() timesteps += 1 if (Systems.FocusSystem.ENABLED): if self.debugTextEnabled: out = "remaining usable foci: " c = 0 for focus in eSys.fSys.foci.values(): if focus.isEnabled and focus.cellCount > 0: c += 1 out += str(focus.id) + ", " Log.out(out + " count = " + str(c) + "\n") if self.debugTextEnabled: endTime = time.clock() Log.out("End time: %s" % endTime) Log.out("Elapsed time: %s" % (endTime - startTime)) #increment run run += 1 # All runs finished: display results graph if Graph.SHOW: if (Systems.FocusSystem.ENABLED): q.put((fociAreaGraph.showGraph, ([True]), {})) q.put((graph.showGraph, ([True]), {})) running = False
def setUp(self): self.testsite = Worldsite(Vector2d(2,2)) self.testsite.eCell = EpithelialCell(Vector2d(2,2)) self.testsite.immCells = [ImmuneCell(Vector2d(2,2))]