6.00.2x-pset3.py
def simulationWithDrug(numViruses, maxPop, maxBirthProb, clearProb,
                       resistances, mutProb, numTrials):
    """
    For each of numTrials trials, instantiates a patient, runs a simulation for
    150 timesteps, adds guttagonol, and runs the simulation for an additional
    150 timesteps.  At the end plots the average virus population size
    (for both the total virus population and the guttagonol-resistant virus
    population) as a function of time.

    numViruses: number of ResistantVirus to create for patient (an integer)
    maxPop: maximum virus population for patient (an integer)
    maxBirthProb: Maximum reproduction probability (a float between 0-1)
    clearProb: maximum clearance probability (a float between 0-1)
    resistances: a dictionary of drugs that each ResistantVirus is resistant to
                 (e.g., {'guttagonol': False})
    mutProb: mutation probability for each ResistantVirus particle
             (a float between 0-1).
    numTrials: number of simulation runs to execute (an integer)

    """
    numTimesteps = 300
    virusPopulationPerTrial = {x: () for x in range(numTimesteps)}
    resistantVirusPopulationPerTrial = dict(virusPopulationPerTrial)
    for trial in range(numTrials):
        # Line bellow broken to comply with pep8
        viruses = [ResistantVirus(maxBirthProb, clearProb, resistances,
                                  mutProb) for x in range(numViruses)]
        patient = TreatedPatient(viruses, maxPop)
        # Runs the first half of the simulation without drugs
        for timestep in range(numTimesteps//2):
            patient.update()
            virusPopulationPerTrial[timestep] += (patient.getTotalPop(),)
            # Line bellow broken to comply with pep8
            resistantVirusPopulationPerTrial[timestep] += (
                patient.getResistPop(['guttagonol']),
            )
        patient.addPrescription('guttagonol')
        # Runs the second half of the simulation after adding the drug
        for timestep in range(numTimesteps//2, numTimesteps):
            patient.update()
            virusPopulationPerTrial[timestep] += (patient.getTotalPop(),)
            # Line bellow broken to comply with pep8
            resistantVirusPopulationPerTrial[timestep] += (
                patient.getResistPop(['guttagonol']),
            )
    avgVirusPopulation = {}
    avgResistantVirusPopulation = {}
    # Runs the trials
    for timestep in virusPopulationPerTrial:
        # Line bellow broken to comply with pep8
        avgVirusPopulation[timestep] = (
            sum(virusPopulationPerTrial[timestep]) /
            len(virusPopulationPerTrial[timestep])
        )
        # Line bellow broken to comply with pep8
        avgResistantVirusPopulation[timestep] = (
            sum(resistantVirusPopulationPerTrial[timestep]) /
            len(resistantVirusPopulationPerTrial[timestep])
        )
    # Plots the results
    # Line bellow broken to comply with pep8
    pylab.plot(list(avgVirusPopulation.values()),
               label="Average total virus population")
    # Line bellow broken to comply with pep8
    pylab.plot(list(avgResistantVirusPopulation.values()),
               label="Average population of guttagonol-resistant virus")
    pylab.title("ResistantVirus simulation")
    pylab.xlabel("time step")
    pylab.ylabel("# viruses")
    pylab.legend(loc="best")
    pylab.show()

    For each of numTrials trials, instantiates a patient, runs a simulation for
    150 timesteps, adds guttagonol, and runs the simulation for an additional
    150 timesteps.  At the end plots the average virus population size
    (for both the total virus population and the guttagonol-resistant virus
    population) as a function of time.

    numViruses: number of ResistantVirus to create for patient (an integer)
    maxPop: maximum virus population for patient (an integer)
    maxBirthProb: Maximum reproduction probability (a float between 0-1)
    clearProb: maximum clearance probability (a float between 0-1)
    resistances: a dictionary of drugs that each ResistantVirus is resistant to
                 (e.g., {'guttagonol': False})
    mutProb: mutation probability for each ResistantVirus particle
             (a float between 0-1).
    numTrials: number of simulation runs to execute (an integer)

    
For each of numTrials trials, instantiates a patient, runs a simulation for
150 timesteps, adds guttagonol, and runs the simulation for an additional
150 timesteps.  At the end plots the average virus population size
(for both the total virus population and the guttagonol-resistant virus
population) as a function of time.


For each of numTrials trials instantiates a patient runs a simulation for 150 timesteps adds guttagonol and runs the simulation for an additional 150 timesteps . At the end plots the average virus population size ( for both the total virus population and the guttagonol - resistant virus population ) as a function of time .
def simulationWithDrug(numViruses, maxPop, maxBirthProb, clearProb,
                       resistances, mutProb, numTrials):
    numTimesteps = 300
    virusPopulationPerTrial = {x: () for x in range(numTimesteps)}
    resistantVirusPopulationPerTrial = dict(virusPopulationPerTrial)
    for trial in range(numTrials):
        viruses = [ResistantVirus(maxBirthProb, clearProb, resistances,
                                  mutProb) for x in range(numViruses)]
        patient = TreatedPatient(viruses, maxPop)
        for timestep in range(numTimesteps//2):
            patient.update()
            virusPopulationPerTrial[timestep] += (patient.getTotalPop(),)
            resistantVirusPopulationPerTrial[timestep] += (
                patient.getResistPop(['guttagonol']),
            )
        patient.addPrescription('guttagonol')
        for timestep in range(numTimesteps//2, numTimesteps):
            patient.update()
            virusPopulationPerTrial[timestep] += (patient.getTotalPop(),)
            resistantVirusPopulationPerTrial[timestep] += (
                patient.getResistPop(['guttagonol']),
            )
    avgVirusPopulation = {}
    avgResistantVirusPopulation = {}
    for timestep in virusPopulationPerTrial:
        avgVirusPopulation[timestep] = (
            sum(virusPopulationPerTrial[timestep]) /
            len(virusPopulationPerTrial[timestep])
        )
        avgResistantVirusPopulation[timestep] = (
            sum(resistantVirusPopulationPerTrial[timestep]) /
            len(resistantVirusPopulationPerTrial[timestep])
        )
    pylab.plot(list(avgVirusPopulation.values()),
               label="Average total virus population")
    pylab.plot(list(avgResistantVirusPopulation.values()),
               label="Average population of guttagonol-resistant virus")
    pylab.title("ResistantVirus simulation")
    pylab.xlabel("time step")
    pylab.ylabel("# viruses")
    pylab.legend(loc="best")
    pylab.show()
def simulationWithDrug ( numViruses , maxPop , maxBirthProb , clearProb , resistances , mutProb , numTrials ) : numTimesteps = 300 virusPopulationPerTrial = { x : ( ) for x in range ( numTimesteps ) } resistantVirusPopulationPerTrial = dict ( virusPopulationPerTrial ) for trial in range ( numTrials ) : viruses = [ ResistantVirus ( maxBirthProb , clearProb , resistances , mutProb ) for x in range ( numViruses ) ] patient = TreatedPatient ( viruses , maxPop ) for timestep in range ( numTimesteps // 2 ) : patient . update ( ) virusPopulationPerTrial [ timestep ] += ( patient . getTotalPop ( ) , ) resistantVirusPopulationPerTrial [ timestep ] += ( patient . getResistPop ( [ 'guttagonol' ] ) , ) patient . addPrescription ( 'guttagonol' ) for timestep in range ( numTimesteps // 2 , numTimesteps ) : patient . update ( ) virusPopulationPerTrial [ timestep ] += ( patient . getTotalPop ( ) , ) resistantVirusPopulationPerTrial [ timestep ] += ( patient . getResistPop ( [ 'guttagonol' ] ) , ) avgVirusPopulation = { } avgResistantVirusPopulation = { } for timestep in virusPopulationPerTrial : avgVirusPopulation [ timestep ] = ( sum ( virusPopulationPerTrial [ timestep ] ) / len ( virusPopulationPerTrial [ timestep ] ) ) avgResistantVirusPopulation [ timestep ] = ( sum ( resistantVirusPopulationPerTrial [ timestep ] ) / len ( resistantVirusPopulationPerTrial [ timestep ] ) ) pylab . plot ( list ( avgVirusPopulation . values ( ) ) , label = "Average total virus population" ) pylab . plot ( list ( avgResistantVirusPopulation . values ( ) ) , label = "Average population of guttagonol-resistant virus" ) pylab . title ( "ResistantVirus simulation" ) pylab . xlabel ( "time step" ) pylab . ylabel ( "# viruses" ) pylab . legend ( loc = "best" ) pylab . show ( )
For each of numTrials trials, instantiates a patient, runs a simulation for
150 timesteps, adds guttagonol, and runs the simulation for an additional
150 timesteps.
For each of numTrials trials instantiates a patient runs a simulation for 150 timesteps adds guttagonol and runs the simulation for an additional 150 timesteps .
"""
    For each of numTrials trials, instantiates a patient, runs a simulation for
    150 timesteps, adds guttagonol, and runs the simulation for an additional
    150 timesteps.  At the end plots the average virus population size
    (for both the total virus population and the guttagonol-resistant virus
    population) as a function of time.

    numViruses: number of ResistantVirus to create for patient (an integer)
    maxPop: maximum virus population for patient (an integer)
    maxBirthProb: Maximum reproduction probability (a float between 0-1)
    clearProb: maximum clearance probability (a float between 0-1)
    resistances: a dictionary of drugs that each ResistantVirus is resistant to
                 (e.g., {'guttagonol': False})
    mutProb: mutation probability for each ResistantVirus particle
             (a float between 0-1).
    numTrials: number of simulation runs to execute (an integer)

    """ # Line bellow broken to comply with pep8 # Runs the first half of the simulation without drugs # Line bellow broken to comply with pep8 # Runs the second half of the simulation after adding the drug # Line bellow broken to comply with pep8 # Runs the trials # Line bellow broken to comply with pep8 # Line bellow broken to comply with pep8 # Plots the results # Line bellow broken to comply with pep8 # Line bellow broken to comply with pep8
numViruses
maxPop
maxBirthProb
clearProb
resistances
mutProb
numTrials