def setInfection(human, area, d): human.isInfected = True human.infectionNumber += 1 human.infectionDate = d human.isTested = False illness = rd.setIllnessDevelopment() human.incubationPeriod = illness illness += rd.setIllnessDevelopment() human.illnessPeriod = illness human.evolutionState = 0 #Defining if infected humans will be symptomatic or will need treatment auxRandom = rd.aRandom() symptomsThreshold = vr.getSymptomsThreshold( ) * human.deathRiskFactor * vr.getDeathRiskSymptomsW() treatmentThreshold = vr.getTreatmentThreshold( ) * human.deathRiskFactor * vr.getDeathRiskTreatmentW() reinfectionFactor = vr.getReinfectionFactor(human.infectionNumber) if auxRandom < symptomsThreshold * reinfectionFactor: human.willBeSymptomatic = True auxRandom = rd.aRandom() if auxRandom < treatmentThreshold * reinfectionFactor: human.willNeedTreatment = True Simulation.increaseV("totalInfected") Simulation.increaseV("actualInfected") if area == "A": Simulation.increaseV("actualAInfected") elif area == "B": Simulation.increaseV("actualBInfected")
def create_virus(ai_settings, screen, viruses, virus_number, row_number): virus = Virus(ai_settings, screen) virus_width = virus.rect.width virus.x = virus_width + 2 * virus_width * virus_number virus.rect.x = virus.x virus.rect.y = virus.rect.height + 2 * virus.rect.height * row_number viruses.add(virus)
def __init__(self, population_size, vacc_percentage, virus_name, mortality_rate, basic_repro_num, initial_infected=1): self.population_size = population_size self.population = [] self.total_infected = 0 self.current_infected = 0 self.next_person_id = 0 self.virus_name = virus_name self.mortality_rate = mortality_rate self.basic_repro_num = basic_repro_num self.file_name = "{}_simulation_pop_{}_vp_{}_infected_{}.txt".format( virus_name, population_size, vacc_percentage, initial_infected) self.virus = Virus(virus_name, mortality_rate, basic_repro_num) # Evenually chave the name change self.logger = Logger('simulation') # This attribute will be used to keep track of all the people that catch # the infection during a given time step. We'll store each newly infected # person's .ID attribute in here. At the end of each time step, we'll call # self._infect_newly_infected() and then reset .newly_infected back to an empty # list. # new infected person, by id// Used newly infected to add to infetced self.newly_infected = [] #Create a population self._create_population(initial_infected, vacc_percentage)
def run_game(): eg_settings = Settings() pygame.init() screen = pygame.display.set_mode( (eg_settings.screen_width, eg_settings.screen_height)) virus = Virus(eg_settings, screen) pygame.display.set_caption("virus human") humans = Group() bullets = Group() stats = Stats(eg_settings) buttons = Button(eg_settings, screen, stats, virus, bullets, gf) sb = Score_Board(eg_settings, screen, stats) while True: gf.check_event(eg_settings, screen, virus, bullets, humans, buttons, stats, sb) if stats.active_game: virus.update() gf.update_bullet(screen, bullets, humans, eg_settings, virus, stats, sb) gf.update_human(humans, virus, stats, bullets, eg_settings, screen, sb) if virus.flag: gf.fire_humans(eg_settings, screen, humans, virus) gf.update_screen(eg_settings, screen, virus, bullets, humans, buttons, stats, sb)
def _create_virus(self, virus_number, row_number): """create a virus and place it in the row""" virus = Virus(self) virus_width, virus_height = virus.rect.size virus.x = virus_width + 2 * virus_width * virus_number virus.rect.x = virus.x virus.rect.y = virus.rect.height + 2 * virus.rect.height * row_number self.viruses.add(virus)
def test_infect_person(): person = setup_for_test() person.infection = Virus("Ebola", -2, 0.25) assert person.infection.name == "Ebola" assert person.did_survive_infection() is True person.infection = Virus("Ebola", 2, 0.25) assert person.did_survive_infection() is False
def test_virus_instantiation(): virus = Virus("HIV", 0.8, 0.3) assert virus.name == "HIV" assert virus.repro_rate == 0.8 assert virus.mortality_rate == 0.3 virus2 = Virus('Ebola', 0.4, 0.3) assert virus2.name == 'Ebola' assert virus2.repro_rate == 0.4 assert virus2.mortality_rate == 0.3
def create_virus(settings, screen, viruses, virus_number, row_number): """ """ virus = Virus(settings, screen) virus_width = 100 virus_height = 50 virus.x = virus_width + 2 * virus_width * virus_number virus.rect.x = virus.x virus.rect.y = virus_height + 1.5 * virus_height * row_number viruses.add(virus)
def decideInfection(infectedHuman, human, area, d): Simulation.checkImmunity(human, d, vr.getImmunityPeriod()) if human.hasImmunity == False: r = rd.aRandom() c = rd.isCarefulAverage(infectedHuman, human) * gov.getInfoFactor() s = rd.isDistancedAverage(infectedHuman, human) * gov.getSocialDistanceFactor() infectionP = r * rd.getInfectionThresholdVar(c, s) if infectionP < vr.getInfectionThreshold( ) * infectedHuman.contagiousFactor: Simulation.setInfection(human, area, d) infectedHuman.transmission += 1
def test_person_did_survive_simulation(): test_person = Person(124, False, Virus("Ebola", 1.5, 0.8)) assert test_person._id == 124 assert test_person.is_vaccinated == False assert test_person.is_alive == True assert test_person.infection.name == "Ebola" assert test_person.infection.repro_rate == 0.8 test_person.did_survive_infection() assert test_person.is_alive == False test_person_two = Person(125, False, Virus("Ebola", -1.0, 0.5)) test_person_two.did_survive_infection() test_person_two.is_alive == True
def test_interaction(): virus = Virus("Ebola", 1, .8) sim = Simulation(10, .5, virus, 1) person = Person(1, False, virus) random_person = Person(2, True) assert sim.interaction(person, random_person) == "random is vaccinated" random_person.infection = virus random_person.is_vaccinated = False assert sim.interaction(person, random_person) == "random is already infected" random_person.infection = None assert sim.interaction(person, random_person) == "random got infected" virus.repro_rate = 0 assert sim.interaction(person, random_person) == "random got lucky"
def __init__(self, id, creation_date, num_segments=2, parent=None, \ generate_sequence=False): Virus.__init__(self, id=id, creation_date=creation_date, \ num_segments=num_segments, parent=parent, \ generate_sequence=generate_sequence) """ This function is a replacement function for initializing the virus. Specifically, a SmallFluVirus is initialized from a starting seed sequence, then it is mutated. """ # This is the seed sequence for a SmallFluVirus' Segment 0. sequence0 = 'ATTTCCCTTGCATATATATTGCGTTTCTTCGACCTTTTAACCGCTCTCTTAGAA' + \ 'GAGAGACAGATAGCTTCTTACCCGTGCCCCACCGTTGGCAGTACGATCGCACGCCCCACGTGAACG' + \ 'ATTGGTAAACCCTGTGGCCTGTGAGCGACAAAAGCTTTAATGGGAAATACGCGCCCATAACTTGGT' + \ 'GCGAATACGGGTCGTAGCAATGTTCGTCTGAGTATGATCTATATAATACGGGCGGTACGTCTGCTT' + \ 'TGGTCAGCCTCTAATGGCTCGTATGATAGTGCAGCCGCTGGTGATCAC' # This is the seed sequence for a SmallFluVirus' Segment 1. sequence1 = 'CACCGATCTAGAATCGAATGCAAAGATCACCCAGGTGCAAATCAAAAATTCTAG' + \ 'GTAACTAGAAGATTTGCGACGTTCTAAGTGTTGGACGATATGAATCGCGACCCAGGATGACGTCGC' + \ 'CCTGAAAAAAAGATTTCTGCAACTCTCCTCGTCAGCAGTCTGGTGTATCGAAAGTACAGGACTAGC' + \ 'CTTCCTAGCAACCGCGGGCTGGGAATCTGAGACATGAGTCAAGATATTTGCTCGGTAACGTATGCT' + \ 'CTAGGCATCTAACTATTCCCTGTGTCTTATAGGGGCCTGCGTTATCTG' self.segments = [] # Make segment 0 and append to list of segments self.segments.append(self.GenerateSegment(0, sequence=sequence0, \ length=len(sequence0))) # Make segment 1's 200 n.t. constant region and append to list # of segments self.segments.append(self.GenerateSegment(1, sequence=sequence1, \ length=len(sequence1))) # Initialize the virus with some mutations, so that it is # distinguishable from another virus. # Firstly, introduce a random mutation anywhere in genome. self.Mutate() # Mutate the variable region with default parameters self.MutateVariableRegion() # Mutate the constant region with default parameters self.MutateConstantRegion()
def test_simulation_should_continue(): v = Virus("Test", .25, .25) sim = Simulation(100, .25, v, initial_infected=4) sim1 = Simulation(100, .25, v, initial_infected=4) sim2 = Simulation(100, .25, v, initial_infected=4) #Check if all are dead for person in sim.population: person.is_alive = False assert sim._simulation_should_continue() == False #make everyone vaccinated for person in sim1.population: person.is_vaccinated = True assert sim1._simulation_should_continue() == False #check that all survivors are vaccinated for person in sim2.population: person.is_vaccinated = True #kill off 3 people sim2.population[0].is_alive = False sim2.population[90].is_alive = False sim2.population[86].is_alive = False sim2.total_dead = 3 assert sim2._simulation_should_continue() == False
def run(self): pygame.init() clock = pygame.time.Clock() pygame.display.set_caption('Game ') while True: clock.tick(self.speed) for event in pygame.event.get(): if event.type == QUIT: exit() elif event.type == pygame.MOUSEBUTTONDOWN: x, y = pygame.mouse.get_pos() if event.button == 1: self.s.add( Virus((x // self.cell) + 1, y // self.cell, 10)) elif event.button == 3: self.s.add( Bacterium((x // self.cell) + 1, y // self.cell)) elif event.button == 2: s = [(x // self.cell) + 1, y // self.cell] i = self.s.search(s, False) print(i) if i != None: self.s.remove(i) elif event.type == pygame.KEYDOWN: if event.key == K_p: self.pause() self.draw_lines() self.print() self.s.step(self.cell_width, self.cell_height) pygame.display.flip() pygame.quit()
def testNewSimProps(): simVirus = Virus('Ebola', 0.5, 0.3) sim = Simulation(100, 0.4, simVirus, 50) assert sim.currentlyInfected == 50 assert sim.currentlyAlive == 100 assert sim.currentlyVaccinated == 40 assert sim.virus.name == 'Ebola'
def test_who_dies(): virus = Virus("Rabies",0.1,1) subject_1 = Person(0,False,virus) test = Simulation(1,0,virus,1) test.population.append(subject_1) test._who_dies() assert subject_1.is_alive == False
def __init__(self, population_size, vacc_percentage, virus_name, mortality_rate, repro_rate, initial_infected=1, dead=0): self.population_size = population_size self.current_infected = 0 self.initial_infected = initial_infected self.total_infected = self.initial_infected self.dead = dead self.next_person_id = 0 self.file_name = "{}_simulation_pop_{}_vp_{}_infected_{}.txt".format( virus_name, population_size, vacc_percentage, initial_infected) self.newly_infected = [] # * Creating Virus object self.virus = Virus(virus_name, mortality_rate, repro_rate) # * Create a Logger object and bind it to self.logger. self.logger = Logger(self.file_name) self.basic_repro_num = repro_rate self.population = self._create_population(self.initial_infected, self.population_size)
def gerar_virus(ai_settings, screen, tipo_virus, estado_jogo): virus_tmp = Virus(ai_settings, screen, tipo_virus, estado_jogo) virus_tmp.rect.bottomright = (randint(virus_tmp.largura, ai_settings.screen_width), randint(virus_tmp.altura, ai_settings.screen_height)) return virus_tmp
def test__create_population(self): """create population length, and number of initial infected""" virus = Virus() virus.set_virus_cooties() simulation = Simulation(1000, .1, virus) simulation.virus = virus population = simulation._create_population(10, 1000, .1) infected = 0 vaccinated = 0 for person in population: if person.infection != None: infected += 1 if person.is_vaccinated != False: vaccinated += 1 assert (len(population) == 1000), "length of population is not 1000" assert infected == 10, "there is not only 10 people infected"
def test_sick_person_instantiation(): virus = Virus("Dysentery", 0.7, 0.2) person = Person(3, False, infection=virus) assert person._id == 3 assert person.is_alive is True assert person.is_vaccinated is False assert person.infection is virus
def test_infect_newly_infected(): virus = Virus("Ebola", .2, .8) sim = Simulation(10, .5, virus, 1) sim.newly_infected = [sim.population[1], sim.population[5]] sim._infect_newly_infected() assert sim.population[1].infection == virus assert sim.population[5].infection == virus
def __init__(self, population_size, vacc_percentage, virus_name, mortality_rate, basic_repro_num, initial_infected=1): # Attributes when called and initalized self.population_size = population_size self.vacc_percentage = vacc_percentage self.virus_name = virus_name self.mortality_rate = mortality_rate self.basic_repro_num = basic_repro_num self.virus = Virus(self.virus_name, self.mortality_rate, self.basic_repro_num) self.population = [] self.dead_people = 0 self.total_infected = 0 self.current_infected = 0 self.next_person_id = 0 self.newly_infected = [] self.population = self._create_population(initial_infected) self.file_name = "{}_simulation_pop_{}_vp_{}_infected_{}.txt".format( virus_name, population_size, vacc_percentage, initial_infected) self.logger = Logger(self.file_name) print("population size:", self.population_size)
def wrapper(self, *args, **kwargs): VirusArgs = namedtuple("VirusArgs", "name repro_rate mortality_rate") SimulationArgs = namedtuple( "SimulationArgs", "pop_size vacc_percentage virus logger initial_infected") virus_args_tuples = [ VirusArgs("earth", 1, 0), VirusArgs("wind", 0, 1), VirusArgs("fire", .5, .25), VirusArgs("big earth", .25, .75), ] with Logger("sim_test_log.txt") as logger: simulation_args_list = [[100, .1, None, logger, 50], [100000, 0, None, logger, 1000], [100000, .9, None, logger, 100], [100, .5, None, logger, 1], [100, .5, None, logger, 50], [100, .5, None, logger, 49], [2, .5, None, logger, 0]] for simulation_args in simulation_args_list: for virus_args_tuple in virus_args_tuples: virus = Virus(*virus_args_tuple._asdict().values()) simulation_args[2] = virus simulation_args_tuple = SimulationArgs(*simulation_args) simulation = Simulation( *simulation_args_tuple._asdict().values()) func( self, { "virus": virus, "virus_args_tuples": virus_args_tuple, "simulation": simulation, "simulation_args_tuple": simulation_args_tuple })
def test_create_population(): v = Virus("Test", .25, .25) sim = Simulation(100, .25, v, initial_infected=4) #vaccinated count v_count = 0 #infected count i_count = 0 #Check entire population for person in sim.population: #Make sure everyone is alive in population at start assert person.is_alive == True #count number of vaccinated people if person.is_vaccinated: v_count += 1 #person has infection if person.infection is not None: i_count += 1 #verify correct number of infected and vaccinated people assert v_count == 25 assert i_count == 4 assert sim.next_person_id == 100 assert len(sim.population) == 100
def test_did_survive_infection(): # TODO: Create a Virus object to give a Person object an infection virus = Virus("Dysentery", 0.7, 0.2) # TODO: Create a Person object and give them the virus infection person = Person(4, False, virus) # Resolve whether the Person survives the infection or not survived = person.did_survive_infection() # Check if the Person survived or not if survived: assert person.is_alive is True # TODO: Write your own assert statements that test # the values of each attribute for a Person who survived # assert ... assert person._id == 4 assert person.is_vaccinated is True assert person.infection is None else: assert person.is_alive is False # TODO: Write your own assert statements that test # the values of each attribute for a Person who did not survive # assert ... assert person._id == 4 assert person.is_vaccinated is False assert person.infection is virus
def test_simulation_init(): virus = Virus("Ebola", .3, .8) sim = Simulation(35000, .9, virus, 1) assert sim.pop_size == 35000 assert sim.vacc_percentage == .9 assert sim.virus.name == "Ebola" assert sim.initial_infected == 1
def __init__(self, population_size, vacc_percentage, virus_name, mortality_rate, basic_repro_num, initial_infected=1): self.population_size = (population_size) self.population = [] self.next_id = 0 self.vacc_percentage = (vacc_percentage) self.total_infected = (initial_infected) self.total_dead = 0 self.current_infected = (initial_infected) self.virus_name = virus_name self.mortality_rate = (mortality_rate) self.repro_num = basic_repro_num self.newly_infected = [] self.file_name = "{}_simulation_pop_{}_vp_{}_infected_{}.txt".format( virus_name, population_size, vacc_percentage, initial_infected) self.virus = Virus(self.virus_name, self.mortality_rate, self.repro_num) self.create_population(initial_infected, self.virus) self.logger = Logger(self.file_name) self.logger.write_metadata(self.population_size, self.vacc_percentage, self.virus_name, self.mortality_rate, self.repro_num)
def test_is_alive_and_is_vaccinated(): virus = Virus("HIV", 0.8, 0.3) person = Person(925, True, virus) assert person._id == 925 assert person.is_vaccinated == True assert person.infected == virus
class SimulationTest(unittest.TestCase): hiv = Virus("HIV", 0.5, 0.5) # bird_flu = Virus("Hard Virus", 0.99, 0.99) self.large_sim = Simulation(100, 0.5, hiv, 100) self.small_sim = Simulation(10, 0.25, hiv, 5) def test_create_population_method(self): infected_count = 0 vaccinated = 0 self.large_sim._create_population() assert len(self.large_sim.population) == 10000 for person in self.large_sim.population: if person.infection is not None: infected_count += 1 assert infected_count == 100 for person in self.large_sim.population: if person.is_vaccinated and person.infection is None: vaccinated += 1 assert vaccinated == 4950 def test_should_continue_method(self): self.large_sim._create_population() assert self.large_sim._simulation_should_continue() is True def test_should_continue_method_cure_found_case(self): self.large_sim._create_population() for person in self.large_sim.population: person.is_alive = True person.infection = None person.is_vaccinated = True assert self.large_sim._simulation_should_continue() is False def test_should_continue_method_deaths_case(self): self.large_sim._create_population() for person in self.large_sim.population: person.is_alive = False assert self.large_sim._simulation_should_continue() is False def test_interaction(self): self.small_sim._create_population() number_of_interaction = 1 for person in self.small_sim.population: while number_of_interaction <= 100: random_person = random.choice(self.small_sim.population) # Prevent interaction with dead people and with it self while random_person.is_alive is False or random_person._id == person._id: rando = random.choice(self.small_sim.population) assert person._id is not random_person._id assert random_person.is_alive is True self.small_sim.interaction(person, rando) number_of_interaction += 1 number_of_interaction = 1
def test_sick_person_instantiation(): # the values at each attributes virus = Virus("Dysentery", 0.7, 0.2) person = Person(1, False, virus) assert person._id == 1 assert person.is_vaccinated == False assert person.infection == virus
def test_create_population(): virus = Virus("Test", 0.8, 0.2) # name, repro, mortality rate sim = Simulation(100, 0.5, virus, 10) # popsize, vacc%, virus, initial infected infected_list = [] vacc_list = [] print("Total Population:", len(sim.population)) assert len(sim.population) == 100 for person in sim.population: if person.infection != None: infected_list.append(person) elif person.is_vaccinated: vacc_list.append(person) print("Infected", len(infected_list)) assert len(infected_list) == 10 print("Vaccinated", len(vacc_list)) assert len(vacc_list) == 50 assert sim.total_vaccinated == len(vacc_list)