def test_script_reads_parameter_ranges_file_and_writes_files_correctly_in_different_folders(self, createParameterRangesFile): createParameterRangesFile(multi=True) self.l = Launcher('simulations', 'parameter_ranges.txt') self.l.writeParameterFilesInFolders() self.subfoldernames = ['pgg_first1.1secnd2.3third3.4','pgg_first1.1secnd2.3third3.5','pgg_first1.2secnd2.3third3.4','pgg_first1.2secnd2.3third3.5'] self.parvalues = [[1.1,2.3,3.4],[1.1,2.3,3.5],[1.2,2.3,3.4],[1.2,2.3,3.5]] self.inputfiles = [INITIALISATION_FILE, INITIAL_PHENOTYPES_FILE, INITIAL_TECHNOLOGY_FILE, PARAMETER_FILE, FITNESS_PARAMETERS_FILE] for comb in range(len(self.parvalues)): folder = self.subfoldernames[comb] assert os.path.exists('simulations/'+folder), "did not create specific simulation file: {0}".format(os.listdir('simulations')) fileslist = os.listdir('simulations/'+folder) try: for file in self.inputfiles: assert file in fileslist pars = fman.extractColumnFromFile('simulations/'+folder+'/'+FITNESS_PARAMETERS_FILE, 0, str) vals = fman.extractColumnFromFile('simulations/'+folder+'/'+FITNESS_PARAMETERS_FILE, 1, float) assert pars == ['first','secnd','third'], "wrong parameter name" assert pytest.approx(vals) == self.parvalues[comb], "wrong parameter value" except AssertionError as e: shutil.rmtree('simulations') os.remove('parameter_ranges.txt') assert False, "one or more parameter value(s) missing. File contents: {0},{1}".format(pars,vals) shutil.rmtree('simulations') os.remove('parameter_ranges.txt')
def test_demography_file_has_correct_output(self, pseudorandom, runSim, clearOutputFiles): pseudorandom(69) self.pop = Pop(inst='test/test') self.pop.mutationRate = 0.1 self.pop.numberOfDemes = 5 self.pop.initialDemeSize = 8 self.pop.fitnessParameters.clear() self.pop.fitnessParameters.update({ "fb": 10, "b": 0.5, "c": 0.05, "gamma": 0.01 }) self.pop.createAndPopulateDemes() expDemog = [] for gen in range(5): self.pop.lifecycle() assert self.pop.numberOfDemes == 5 expDemog.append(self.pop.demography / self.pop.numberOfDemes) pseudorandom(69) runSim() obsDemog = fman.extractColumnFromFile('test/test/out_demography.txt', 0, float) assert obsDemog == expDemog, "unexpected value of group size has been printed" clearOutputFiles('test/test/')
def test_initialisation_values_provided(self): self.pathToFile = fman.getPathToFile(filename=INITIALISATION_FILE, dirname='test/test') self.pars = fman.extractColumnFromFile(self.pathToFile, 1, int) for par in self.pars: assert type( par ) is int, "Did you insert a non integer value for deme number, deme size or generation number?" assert par > 0, "Did you insert a negative value for deme number, deme size or generation number?"
def test_population_is_initialised_with_right_values( self, objectAttributesExist, objectAttributesValues): # First, the population is initialised according to the initialisation settings self.pathToFile = fman.getPathToFile(filename=INITIALISATION_FILE, dirname='test/test') self.attributeNames = fman.extractColumnFromFile( self.pathToFile, 0, str) self.attributeValues = fman.extractColumnFromFile( self.pathToFile, 1, int) self.population = Pop(inst='test/test') testAttr, whichAttr = objectAttributesExist(self.population, self.attributeNames) assert testAttr, "Population does not have attribute(s) {0}".format( whichAttr) testVal, attributes, expected, observed = objectAttributesValues( self.population, self.attributeNames, self.attributeValues) assert testVal, "Population has {1}={2} instead of {3}".format( attributes, expected, observed)
def test_single_par_combination_gets_a_full_folder(self): self.l = Launcher('simulations', 'parameter_ranges.txt') self.l.createFolder(self.l.metafolder) self.l.writeParameterFilesInFolder(fitfun='pgg', pname=('small','big'), pval=(0.3,0.5)) self.subfoldername = 'pgg_small0.3big0.5' assert os.path.exists('simulations/'+self.subfoldername), "create subfolder" self.fileslist = os.listdir('simulations/'+self.subfoldername) self.inputfiles = [INITIALISATION_FILE, INITIAL_PHENOTYPES_FILE, INITIAL_TECHNOLOGY_FILE, PARAMETER_FILE, FITNESS_PARAMETERS_FILE] try: for file in self.inputfiles: assert file in self.fileslist pars = fman.extractColumnFromFile('simulations/'+self.subfoldername+'/'+FITNESS_PARAMETERS_FILE, 0, str) vals = fman.extractColumnFromFile('simulations/'+self.subfoldername+'/'+FITNESS_PARAMETERS_FILE, 1, float) assert pars == ['small','big'], "wrong parameter name" assert vals == [0.3,0.5], "wrong parameter value" shutil.rmtree('simulations') except AssertionError as e: shutil.rmtree('simulations') assert False, "one or more parameter value(s) missing. File contents: {0},{1}".format(pars,vals)
def __init__(self, fit_fun='pgg', inst=None, mutationBoundaries=True): logging.basicConfig(level=logging.INFO, format='[%(asctime)s]::%(levelname)s %(message)s', datefmt='%Y.%m.%d - %H:%M:%S') logging.info('Creating population') if inst is None: self.pathToInputFiles = PARAMETER_FOLDER self.pathToOutputFiles = OUTPUT_FOLDER else: self.pathToInputFiles = inst self.pathToOutputFiles = inst self.pathToInitFile = fman.getPathToFile(filename=INITIALISATION_FILE, dirname=self.pathToInputFiles) self.attrs = fman.extractColumnFromFile(self.pathToInitFile, 0, str) self.vals = fman.extractColumnFromFile(self.pathToInitFile, 1, int) for attr, val in zip(self.attrs, self.vals): setattr(self, attr, val) self.pathToPhenFile = fman.getPathToFile( filename=INITIAL_PHENOTYPES_FILE, dirname=self.pathToInputFiles) with open(self.pathToPhenFile) as f: self.initialPhenotypes = [float(line) for line in f.readlines()] self.pathToTechFile = fman.getPathToFile( filename=INITIAL_TECHNOLOGY_FILE, dirname=self.pathToInputFiles) self.initialTechnologyLevel = fman.extractColumnFromFile( self.pathToTechFile, 0, float)[0] self.pathToParFile = fman.getPathToFile(filename=PARAMETER_FILE, dirname=self.pathToInputFiles) self.parattrs = fman.extractColumnFromFile(self.pathToParFile, 0, str) self.parvals = fman.extractColumnFromFile(self.pathToParFile, 1, float) for parattr, parval in zip(self.parattrs, self.parvals): setattr(self, parattr, parval) self.pathToFitFile = fman.getPathToFile( filename=FITNESS_PARAMETERS_FILE, dirname=self.pathToInputFiles) self.fitattrs = fman.extractColumnFromFile(self.pathToFitFile, 0, str) self.fitvals = fman.extractColumnFromFile(self.pathToFitFile, 1, float) self.fitnessParameters = {} for fitattr, fitval in zip(self.fitattrs, self.fitvals): self.fitnessParameters[fitattr] = fitval if hasattr(self, "individualBaseResources") == False: setattr(self, "individualBaseResources", 1) self.fit_fun = fit_fun self.mutationBoundaries = mutationBoundaries
def test_resources_file_has_correct_output(self, pseudorandom, runSim, clearOutputFiles): parameters = { "gamma": 0.01, "p": 0.6, "q": 0.9, "d": 0.2, "productionTime": 1, "alphaResources": 0.6, "rb": 10, "atech": 2, "btech": 0.2, "betaTech": 0.6 } pseudorandom(54) self.pop = Pop(fit_fun='technology', inst='test/test') self.pop.mutationRate = 0.1 self.pop.numberOfDemes = 5 self.pop.initialDemeSize = 8 self.pop.fitnessParameters.clear() self.pop.fitnessParameters.update(parameters) self.pop.createAndPopulateDemes() expTotRes = [] for gen in range(5): self.pop.lifecycle() assert self.pop.numberOfDemes == 5 collectDemeTotRes = [] for deme in self.pop.demes: collectDemeTotRes.append(deme.totalResources) expTotRes.append(sum(collectDemeTotRes) / len(collectDemeTotRes)) pseudorandom(54) runSim(fun='technology', pars=parameters) obsTotRes = fman.extractColumnFromFile('test/test/out_resources.txt', 0, float) assert obsTotRes == expTotRes, "unexpected value of total resources has been printed" clearOutputFiles('test/test/')
def test_consensus_file_has_correct_output(self, pseudorandom, runSim, clearOutputFiles): parameters = { "gamma": 0.01, "aconsensus": 3, "bconsensus": 2, "epsilon": 0.01, "aquality": 4, "alphaquality": 0.9, "alphaResources": 0.8, "techcapital": 50, "rb": 10 } pseudorandom(22) self.pop = Pop(fit_fun='debate', inst='test/test') self.pop.mutationRate = 0.1 self.pop.numberOfDemes = 5 self.pop.initialDemeSize = 8 self.pop.fitnessParameters.clear() self.pop.fitnessParameters.update(parameters) self.pop.createAndPopulateDemes() expCons = [] for gen in range(5): self.pop.lifecycle() assert self.pop.numberOfDemes == 5 collectDemeCons = [] for deme in self.pop.demes: collectDemeCons.append(deme.politicsValues["consensusTime"]) expCons.append(sum(collectDemeCons) / len(collectDemeCons)) pseudorandom(22) runSim(fun='debate', pars=parameters) obsCons = fman.extractColumnFromFile('test/test/out_consensus.txt', 0, float) assert obsCons == expCons, "unexpected value of total resources has been printed" clearOutputFiles('test/test/')