Esempio n. 1
0
    def __updateCollisions(self):
        """This is a private method, and should only be called from the public update() method. Discovers collisions and determines merges."""

        for focus in self.foci.values():
            if focus.isEnabled:
                collisions = {}
                for i in xrange(len(focus.perimeter)):
                    perimeterCell = focus.perimeter[i]
                    perimeterNeighbours = Worldspace.getMooreNeighbours(
                        self.world, perimeterCell.location,
                        EpithelialStates.INFECTION_DEATH)
                    for neighbour in perimeterNeighbours:
                        if neighbour.focusId != perimeterCell.focusId:
                            if collisions.has_key(neighbour.focusId):
                                if not (neighbour.location
                                        in collisions[neighbour.focusId]):
                                    collisions[neighbour.focusId].append(
                                        neighbour.location)
                                else:
                                    continue
                            else:
                                collisions[neighbour.focusId] = [
                                    neighbour.location
                                ]

                for collisionList in collisions.values():
                    collisionCount = len(collisionList)

                    if collisionCount > (
                            len(focus.perimeter) *
                        (FocusSystem.COLLISION_MERGE_PERCENTAGE / 100.0)
                            if len(focus.perimeter) *
                        (FocusSystem.COLLISION_MERGE_PERCENTAGE / 100.0) > 1
                            else 1):

                        focus.isEnabled = False
                        if FocusSystem.DEBUG_TEXT_ENABLED:
                            Log.out("Merge detected on Focus #%s" % focus.id)
                        self.mergeDetected.append(focus)
    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
Esempio n. 3
0
    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
Esempio n. 4
0
 def __debugPrint(self):
     """Debug method, to be removed. Prints the focus by id and it's area."""
     for focus in self.foci.values():
         if focus.isEnabled:
             Log.out("Focus #%s: %s" % (focus.id, focus.cellCount))