def find_single_result(self, attribute_name, attribute_value): for result in self.members: if str(result.vars[attribute_name]) == str(attribute_value): return result toolbox_basic.error_message('Could not find result with '+str(attribute_name), 'of '+str(attribute_value)) return None
def find_axis_from_label(self, label): for ax, lab in self.subplots.iteritems(): if lab == label: return ax toolbox_basic.error_message('Could not find subplot with', 'label ' + str(label)) return None
def __init__(self, env_output, name=''): search_pattern = './simulation/' if not env_output.find('./simulation/bulk') == None: search_pattern += 'bulk/' if not (name == ''): search_pattern += 'solute[@name="'+name+'"]' else: toolbox_basic.error_message('Please define which solute to use in', env_output.path) self.env_output = env_output solute = env_output.find(search_pattern) if solute == None: toolbox_basic.error_message('Trouble finding solute from name:', search_pattern) self.name = solute.attrib['name'] self.unit = solute.attrib['unit'] self.grid_res = float(solute.attrib['resolution']) self.grid_nI = int(solute.attrib['nI']) self.grid_nJ = int(solute.attrib['nJ']) self.grid_nK = int(solute.attrib['nK']) self.three_dim = (self.grid_nK > 1) temp = solute.text.translate(None,' ').split(';\n') self.values = [] for value in temp: if value == '' or value == '\n': continue self.values.append(float(value))
def find_axis_from_label(self, label): for ax, lab in self.subplots.iteritems(): if lab == label: return ax toolbox_basic.error_message('Could not find subplot with', 'label '+str(label)) return None
def calc_total_attibute(self, attribute): if attribute == 'specific growth rate': return self.calc_total_specific_growth_rate() if self.attributes.count(attribute) < 1: toolbox_basic.error_message('Species '+self.name, 'does not have attribute '+attribute) exit() return sum(self.get_attribute_values(attribute))
def get_mean_NN_dist(self, detail_level, mean='amean', dist='oNN_dist'): rf = self.calc_nearest_neighbor_distances(detail_level) dists = [e.vars[dist] for e in rf.events] if not mean in ['amean', 'gmean', 'hmean']: toolbox_basic.error_message('toolbix_elmer.Simulation.get_mean_NN_dist()', 'mean not recognised: %s'%(mean)) exec 'def mean(x): return %s(x)'%(mean) return mean(dists)
def check_single_species(self): if len(self.species) > 1: toolbox_basic.error_message('More than one species in:',self.path) return False elif len(self.species) < 1: toolbox_basic.error_message('No species present in:',self.path) return False else: return True
def find_attribute_position(self, atttribute): position = -1 for i, x in enumerate(self.header.split(',')): if str(x) == str(attribute): position = i break if position < 0: msg = 'Could not find attribute "'+attribute msg += '" for species "'+self.name+'" in ' toolbox_basic.error_message(msg, path) return position
def calc_mean_attribute(self, attribute): if attribute == 'specific growth rate': return self.calc_mean_specific_growth_rate() if self.attributes.count(attribute) < 1: toolbox_basic.error_message('Species '+self.name, 'does not have attribute '+attribute) exit() values = self.get_attribute_values(attribute) mean = numpy.mean(values) std = numpy.std(values) return mean, std
def get_mean_surf_concn(self, cell_type='all', mean='amean'): if cell_type == 'all': events = self.events elif cell_type == 'consumers': events = self.consumers() elif cell_type == 'producers': events = self.producers() if not mean in ['amean', 'gmean', 'hmean']: toolbox_basic.error_message('toolbix_elmer.Simulation.get_mean_NN_dist()', 'mean not recognised: %s'%(mean)) exec 'def mean(x): return %s(x)'%(mean) return mean([e.vars['amean_surf_concn'] for e in events])
def setup_replicate_results(replicates_dir): results_file = get_replicate_results(replicates_dir) replicate_simulations = get_replicate_simulations(replicates_dir) wrapping = replicate_simulations[0].wrapping is_3d=replicate_simulations[0].is_3d side_length=replicate_simulations[0].side_length results_file.set_space_parameters(wrapping=wrapping, is_3d=is_3d, side_length=side_length) for sim in replicate_simulations: if not ((sim.wrapping == wrapping) and (sim.is_3d == is_3d) and (sim.side_length == side_length)): toolbox_basic.error_message('toolbox_elmer.get_replicates_results():'+ 'Replicates have different space parameters', replicates_dir) exit() results_file.write() return results_file
def settle_competition(set_dir_path): set_dir_path = basic.check_path(set_dir_path) attributes = {'name' : 'competition', 'header' : 'speciesAwashout,speciesBwashout,numSims,pValue'} results_file_path = os.path.join(set_dir_path, 'results.xml') results_output = results.ResultsOutput(path=results_file_path) result_set = results.ResultSet(results_output, attributes) if len(result_set.members) > 0: return result_set dir_list = basic.subdir_list(set_dir_path) dir_result = results.SingleResult() washoutA = 0 washoutB = 0 for dir_name in dir_list: last_path = os.path.join(dir_name, 'lastIter', 'agent_Sum(last).xml') last_output = results.AgentOutput(path=last_path) species_names = last_output.get_species_names() if not len(species_names) == 2: basic.error_message('There should be 2 species for a competition', last_path) last_speciesA = results.SpeciesOutput(last_output, name=species_names[0]) populationA = float(last_speciesA.members[0].vars['population']) last_speciesB = results.SpeciesOutput(last_output, name=species_names[1]) populationB = float(last_speciesB.members[0].vars['population']) if populationA < 1 and populationB > 0: washoutA += 1 if populationB < 1 and populationA > 0: washoutB += 1 if populationA < 1 and populationB < 1: basic.error_message('Both species washed out in', last_path) dir_result.vars['speciesAwashout'] = washoutA dir_result.vars['speciesBwashout'] = washoutB dir_result.vars['numSims'] = len(dir_list) dir_result.vars['pValue'] = \ stats.binom.cdf(min(washoutA,washoutB),(washoutA+washoutB),0.5) result_set.members.append(dir_result) result_set.update_results_output() results_output.write(results_file_path) return result_set
def get_optima(set_dir_path, attribute, dir_group_process, higher_is_fitter=True, starting_time=2400): results_file_path = os.path.join(set_dir_path, 'results.xml') results_output = results.ResultsOutput(path=results_file_path) attributes = {'name':'optimum '+attribute, 'starting_time':str(starting_time), 'header':'directory,mean,std'} optimum_set = results.ResultSet(results_output, attributes) if len(optimum_set.members) > 0: return optimum_set attributes['name'] = attribute result_set = results.ResultSet(results_output, attributes) if result_set.members == []: basic.error_message('Collate mean attributes before assembling optima', set_dir_path) exit() args = [set_dir_path] groups = dir_group_process(*args) for group in groups: single_result = results.SingleResult() means = {} for dir_name in group: dir_name = os.path.basename(dir_name) result = result_set.find_single_result('directory', dir_name) means[dir_name] = float(result.vars['mean']) if means == {}: continue if higher_is_fitter: optimum = max(means.values()) else: optimum = min(means.values()) fittest_dir = basic.get_key_from_dict_value(means, optimum) fittest = result_set.find_single_result('directory', fittest_dir) single_result.vars['directory'] = fittest.vars['directory'] single_result.vars['mean'] = fittest.vars['mean'] single_result.vars['std'] = fittest.vars['std'] optimum_set.members.append(single_result) optimum_set.update_results_output() results_output.write(results_file_path) return optimum_set
def __init__(self, agent_output, name=''): if not (name == ''): search_pattern = './simulation/species[@name="'+name+'"]' elif agent_output.check_single_species(): search_pattern = './simulation/species' else: toolbox_basic.error_message('Please define which species to use in', agent_output.path) self.agent_output = agent_output species = self.agent_output.find(search_pattern) self.name = species.attrib['name'] self.attributes = species.attrib['header'].split(',') self.biomass_names = [] self.members = [] for line in species.text.translate(None,'\n').split(';'): if line == '': break variables = line.split(',') cell = CellOutput(self) for i, attribute in enumerate(self.attributes): cell.vars[attribute] = variables[i] self.members.append(cell)
def get_species_by_name(self, name): for species in self.get_species_outputs(): if name == species.name: return species toolbox_basic.error_message('Species %s cannot be found in'%(name), self.path)
def get_solute(self, solute_name): for solute in self.solute_outputs: if solute.name == solute_name: return solute toolbox_basic.error_message('Could not find solute '+solute_name, 'in '+self.path)