def __init__(self, simulation_directory, iterate_number): self.number = iterate_number self.min_max_concns = {} agent_path = os.path.join(simulation_directory.agent_State, 'agent_State(%d).xml'%(iterate_number)) agent_path = toolbox_basic.check_path(agent_path) self.agent_output = toolbox_results.AgentOutput(path=agent_path) self.time = self.agent_output.time env_path = os.path.join(simulation_directory.env_State, 'env_State(%d).xml'%(iterate_number)) env_path = toolbox_basic.check_path(env_path) self.env_output = toolbox_results.EnvOutput(path=env_path)
def __init__(self, simulation_directory, iterate_number): self.number = iterate_number self.min_max_concns = {} agent_path = os.path.join(simulation_directory.agent_State, 'agent_State(%d).xml' % (iterate_number)) agent_path = toolbox_basic.check_path(agent_path) self.agent_output = toolbox_results.AgentOutput(path=agent_path) self.time = self.agent_output.time env_path = os.path.join(simulation_directory.env_State, 'env_State(%d).xml' % (iterate_number)) env_path = toolbox_basic.check_path(env_path) self.env_output = toolbox_results.EnvOutput(path=env_path)
def setup_heterogeneous_progenitors(sim_dir_path): sim_dir_path = basic.check_path(sim_dir_path) last_path = os.path.join(sim_dir_path, 'lastIter', 'agent_State(last).xml') output, species = results.get_single_species(last_path) timestep = float(output.time) / float(output.iterate) agent_State_dir = basic.unzip_files( os.path.join(sim_dir_path, 'agent_State.zip')) save_cells = [] for cell in species.members: genealogy = int(cell.vars['genealogy']) if genealogy == 1: iter = int(float(cell.vars['birthday']) / timestep) iter_path = os.path.join(agent_State_dir, 'agent_State(' + str(iter) + ').xml') iter_output, iter_species = results.get_single_species(iter_path) requirements = {'family': cell.vars['family'], 'genealogy': 0} oldie = species.find_cells(requirements)[0] oldie.vars['generation'] = 0 save_cells.append(oldie) requirements['genealogy'] = 1 newbie = species.find_cells(requirements)[0] newbie.vars['family'] = int(newbie.vars['family']) + 15 newbie.vars['generation'] = 0 newbie.vars['genealogy'] = 0 newbie.vars['birthday'] = 0.0 save_cells.append(newbie) species.members = save_cells species.update_agent_output() save_path = os.path.join(sim_dir_path, 'agentS.xml') output.write(output_path=save_path)
def __init__(self, root_dir, find_protocol=True): self.root_dir = toolbox_basic.check_path(root_dir) if not find_protocol: return # Set up the space from the protocol file. protocol_path = toolbox_basic.find_protocol_file_path(self.root_dir) self.protocol_tree = toolbox_basic.get_xml_tree(protocol_path) space_params = self.protocol_tree.findall('./space/param') for param in space_params: name, text = param.attrib['name'], param.text if name == 'wrapping': self.wrapping = (text == 'true') elif name == 'length': self.side_length = int(float(text)) elif name == 'nDims': self.is_3d = (int(float(text)) == 3) marks = self.protocol_tree.findall('./mark') for mark in marks: for param in mark: if param.attrib['name'] == 'value': value = int(param.text) if param.attrib['name'] == 'number': number = int(param.text) if value == 2: self.consumers = number else: self.producers = number rs = self.protocol_tree.find("./process/param[@name='randomSeed']") self.random_seed = int(rs.text)
def build_life_history(sim_dir_path, attribute, cell_id=(1,0), biomass_names=['activeBiomass', 'inactiveBiomass']): sim_dir_path = basic.check_path(sim_dir_path) attributes = {'name':attribute, 'header':'time,value'} results_file_path = os.path.join(sim_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.members requirements = {'family':str(cell_id[0]), 'genealogy':str(cell_id[1])} file_dir = os.path.join(sim_dir_path, 'agent_State') basic.unzip_files(file_dir+'.zip') file_list = basic.file_list(file_dir) for filename in file_list: output = results.AgentOutput(path=filename) species = results.SpeciesOutput(output) cells = species.find_cells(requirements) cell = cells[0] single_result = results.SingleResult() single_result.vars['time'] = output.time if attribute == 'specific growth rate': single_result.vars['value'] = \ cell.get_specific_growth_rate(biomass_names) else: single_result.vars['value'] = cell.vars[attribute] result_set.members.append(single_result) result_set.update_results_output() results_output.write(results_file_path) basic.rm_dir(file_dir) return result_set
def get_all_cells_location(sim_dir_path, iternum=480): sim_dir_path = basic.check_path(sim_dir_path) attributes = {'name':'locations', 'header':'X,Y'} results_file_path = os.path.join(sim_dir_path, 'cell_locations.xml') results_output = results.ResultsOutput(path=results_file_path) result_set = results.ResultSet(results_output, attributes) #requirements = {'family':'', 'genealogy':''} file_dir = os.path.join(sim_dir_path, 'agent_State') basic.unzip_files(file_dir+'.zip') file_list = basic.file_list(file_dir) for filename in file_list: output = results.AgentOutput(path=filename) if output.iterate == iternum: output = results.AgentOutput(path=filename) cells = output.get_all_cells() #species = results.SpeciesOutput(output) #cells = species.find_cells(requirements) for cell in cells: single_result = results.SingleResult() single_result.vars['X'] = cell.vars['locationX'] single_result.vars['Y'] = cell.vars['locationY'] result_set.members.append(single_result) result_set.update_results_output() results_output.write(results_file_path) basic.rm_dir(file_dir) return result_set #sim_dir_path = sys.argv[1] #get_grid_size(sim_dir_path) #get_all_cells_location(sim_dir_path)
def __init__(self, agent_output, save_path): self.agent_output = agent_output self.save_path = os.path.join( \ toolbox_basic.check_path(os.path.dirname(save_path)), os.path.basename(save_path)) nI, nJ = agent_output.grid_nI, agent_output.grid_nJ nK, res = agent_output.grid_nK, agent_output.grid_res max_dimension = max(nI, nJ, nK) x_scale = nI / max_dimension y_scale = nJ / max_dimension self.scaling = res * max_dimension ### Set the header self.script = '#declare white = color rgb < 1.0, 1.0, 1.0 >;\n\n' self.script += 'background { white }\n' # The camera self.script += 'camera {\n' self.script += '\t location < 0.5 %f 1 >\n'%(y_scale/2) self.script += '\t look_at < 0.5, %f -1 >\n'%(y_scale/2) self.script += '\t up < 0.0, %f, 0.0 >\n'%(1.2*y_scale) self.script += '\t right < %f, 0.0, 0.0 >\n}\n\n'%(1.2*x_scale) # The lights for i in [0, 1]: self.script += 'light_source {\n\t < %d, 1, 1 >\n'%(i) self.script += '\t white\n\t shadowless}\n\n' # The substratum self.script += 'box {\n\t < 0.0, 0.0, 0.0 >\n' self.script += '\t< 1.0, -0.01, -0.001 >\n' self.script += '\t pigment { color rgb < 0.25, 0.25, 0.25 > }\n}\n\n'
def use_image(self, axis, image_path): image_path = toolbox_basic.check_path(image_path) datafile = cbook.get_sample_data(image_path) image = Image.open(datafile) axis.set_ylim(0,image.size[0]) axis.set_ylim(image.size[1],0) return imshow(image)
def use_image(self, axis, image_path): image_path = toolbox_basic.check_path(image_path) datafile = cbook.get_sample_data(image_path) image = Image.open(datafile) axis.set_ylim(0, image.size[0]) axis.set_ylim(image.size[1], 0) return imshow(image)
def __init__(self, agent_output, save_path): self.agent_output = agent_output self.save_path = os.path.join( \ toolbox_basic.check_path(os.path.dirname(save_path)), os.path.basename(save_path)) nI, nJ = agent_output.grid_nI, agent_output.grid_nJ nK, res = agent_output.grid_nK, agent_output.grid_res max_dimension = max(nI, nJ, nK) x_scale = nI / max_dimension y_scale = nJ / max_dimension self.scaling = res * max_dimension ### Set the header self.script = '#declare white = color rgb < 1.0, 1.0, 1.0 >;\n\n' self.script += 'background { white }\n' # The camera self.script += 'camera {\n' self.script += '\t location < 0.5 %f 1 >\n' % (y_scale / 2) self.script += '\t look_at < 0.5, %f -1 >\n' % (y_scale / 2) self.script += '\t up < 0.0, %f, 0.0 >\n' % (1.2 * y_scale) self.script += '\t right < %f, 0.0, 0.0 >\n}\n\n' % (1.2 * x_scale) # The lights for i in [0, 1]: self.script += 'light_source {\n\t < %d, 1, 1 >\n' % (i) self.script += '\t white\n\t shadowless}\n\n' # The substratum self.script += 'box {\n\t < 0.0, 0.0, 0.0 >\n' self.script += '\t< 1.0, -0.01, -0.001 >\n' self.script += '\t pigment { color rgb < 0.25, 0.25, 0.25 > }\n}\n\n'
def setup_heterogeneous_progenitors(sim_dir_path): sim_dir_path = basic.check_path(sim_dir_path) last_path = os.path.join(sim_dir_path, 'lastIter', 'agent_State(last).xml') output, species = results.get_single_species(last_path) timestep = float(output.time) / float(output.iterate) agent_State_dir = basic.unzip_files(os.path.join(sim_dir_path, 'agent_State.zip')) save_cells = [] for cell in species.members: genealogy = int(cell.vars['genealogy']) if genealogy == 1: iter = int( float(cell.vars['birthday']) / timestep ) iter_path = os.path.join(agent_State_dir, 'agent_State('+str(iter)+').xml') iter_output, iter_species = results.get_single_species(iter_path) requirements = {'family': cell.vars['family'], 'genealogy':0} oldie = species.find_cells(requirements)[0] oldie.vars['generation'] = 0 save_cells.append(oldie) requirements['genealogy'] = 1 newbie = species.find_cells(requirements)[0] newbie.vars['family'] = int(newbie.vars['family']) + 15 newbie.vars['generation'] = 0 newbie.vars['genealogy'] = 0 newbie.vars['birthday'] = 0.0 save_cells.append(newbie) species.members = save_cells species.update_agent_output() save_path = os.path.join(sim_dir_path, 'agentS.xml') output.write(output_path=save_path)
def build_population_structure(sim_dir_path, attribute, bins=numpy.linspace(0, 0.6, 121), starting_time=2400, biomass_names=['activeBiomassGrowth', 'activeBiomassRepair', 'inactiveBiomassGrowth', 'activeBiomassRepair']): attributes = {'name':attribute+' population structure', 'starting_time':str(starting_time), 'header':'bin,frequency'} sim_dir_path = basic.check_path(sim_dir_path) results_file_path = os.path.join(sim_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.members attr_values = [] total_pop = 0.0 file_dir = os.path.join(sim_dir_path, 'agent_State') basic.unzip_files(file_dir+'.zip') file_list = basic.file_list(file_dir) for filename in file_list: output = results.AgentOutput(path=filename) if output.time >= starting_time: species = results.SpeciesOutput(output) species.set_biomass_names(biomass_names) attr_values.extend(species.get_attribute_values(attribute)) total_pop += species.population() hist, bin_edges = numpy.histogram(attr_values, bins) for i in range(len(hist)): single_result = results.SingleResult() single_result.vars['bin'] = str(bins[i+1])+'-'+str(bins[i]) single_result.vars['frequency'] = str(float(hist[i])/total_pop) result_set.members.append(single_result) result_set.update_results_output() results_output.write(results_file_path) basic.rm_dir(file_dir) return result_set
def calc_mean_attribute(sim_dir_path, attribute, output_type, file_process, starting_time=2400): attributes = {'name':attribute, 'starting_time':str(starting_time), 'header':'mean,std'} sim_dir_path = basic.check_path(sim_dir_path) results_file_path = os.path.join(sim_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: result = result_set.members[0] return float(result.vars['mean']), float(result.vars['std']) values = [] file_dir = os.path.join(sim_dir_path, output_type) basic.unzip_files(file_dir+'.zip') file_list = basic.file_list(file_dir) for filename in file_list: args = [attribute, filename] val = file_process(*args) # If output.time < starting_time, None will be returned if not val == None: values.append(val) single_result = results.SingleResult() mean_value = numpy.mean(values) std_value = numpy.std(values) single_result.vars['mean'] = mean_value single_result.vars['std'] = std_value result_set.members.append(single_result) result_set.update_results_output() results_output.write(results_file_path) basic.rm_dir(file_dir) return mean_value, std_value
def read_color_dict(file_path): out = {} file_path = toolbox_basic.check_path(file_path) with open(file_path, 'Ur') as f: for line in f.readlines()[1:]: line = line.replace('\n', '') vals = line.split('\t\t') out[vals[1]] = vals[0] return out
def __init__(self, path): self.path = toolbox_basic.check_path(path) self.iterate_numbers = [] self.iterate_information = [] self.min_max_concns = {} # agent_Sum try: self.agent_Sum = os.path.join(self.path, 'agent_Sum') if not os.path.isdir( self.agent_Sum ): toolbox_basic.unzip_files(self.agent_Sum + '.zip') self.agent_Sum = toolbox_basic.check_path(self.agent_Sum) except TypeError: print('Could not find agent_Sum info! '+self.path) # agent_State try: self.agent_State = os.path.join(self.path, 'agent_State') if not os.path.isdir( self.agent_State ): toolbox_basic.unzip_files(self.agent_State + '.zip') self.agent_State = toolbox_basic.check_path(self.agent_State) except TypeError: print('Could not find agent_State info! '+self.path) # env_Sum try: self.env_Sum = os.path.join(self.path, 'env_Sum') if not os.path.isdir( self.env_Sum ): toolbox_basic.unzip_files(self.env_Sum + '.zip') self.env_Sum = toolbox_basic.check_path(self.env_Sum) except TypeError: print('Could not find env_Sum info! '+self.path) # env_State try: self.env_State = os.path.join(self.path, 'env_State') if not os.path.isdir( self.env_State ): toolbox_basic.unzip_files(self.env_State + '.zip') self.env_State = toolbox_basic.check_path(self.env_State) except TypeError: print('Could not find env_State info! '+self.path) # Figures directory self.figures_dir = os.path.join(self.path, 'figures') if not os.path.isdir(self.figures_dir): toolbox_basic.make_dir(self.figures_dir) self.movies_dir = os.path.join(self.path, 'movies') if not os.path.isdir(self.movies_dir): toolbox_basic.make_dir(self.movies_dir)
def plot_lineages(axis, results_path, max_generation, line_width=1.0, error_bars=True): results_path = basic.check_path(results_path) results_output = results.AgentOutput(path=results_path) results_species = results.SpeciesOutput(results_output) if error_bars and results_species.attributes.count('std_growth_rate') == 0: print('Could not find data for error bars') error_bars = False for cell in results_species.members: generation = int(cell.vars['generation']) if generation <= max_generation-1: family = int(cell.vars['family']) genealogy = int(cell.vars['genealogy']) growth_rate = float(cell.vars['growth_rate']) if error_bars: eb_x = [generation+1] eb_y = [growth_rate] eb_e = [float(cell.vars['std_growth_rate'])] requirements = {'family':family, 'genealogy':genealogy, 'generation':generation+1} temp = results_species.find_cells(requirements) if not temp == []: oldie = temp[0] oldie_grow = float(oldie.vars['growth_rate']) axis.plot([generation+1, generation+2], [growth_rate, oldie_grow], 'r', linewidth=line_width, solid_capstyle='round', zorder=5+numpy.random.randint(5)) if error_bars: eb_x.append(generation+2) eb_y.append(oldie_grow) eb_e.append(float(oldie.vars['std_growth_rate'])) requirements['genealogy'] = genealogy + 2**generation temp = results_species.find_cells(requirements) if not temp == []: newbie = temp[0] newbie_grow = float(newbie.vars['growth_rate']) axis.plot([generation+1, generation+2], [growth_rate, newbie_grow], 'b', linewidth=line_width, solid_capstyle='round', zorder=5+numpy.random.randint(5)) if error_bars: eb_x.append(generation+2) eb_y.append(newbie_grow) eb_e.append(float(newbie.vars['std_growth_rate'])) if error_bars: axis.errorbar(eb_x, eb_y, yerr=eb_e, ecolor='grey', fmt=None, elinewidth=line_width/2, capsize=1, zorder=1) axis.set_xlabel('Generations') axis.set_ylabel('Normalised growth rate') axis.set_xlim([1, max_generation+1])
def get_first_fifteen(sim_dir_path): sim_dir_path = basic.check_path(sim_dir_path) last_path = os.path.join(sim_dir_path, 'lastIter', 'agent_State(last).xml') output, species = results.get_single_species(last_path) species.members = species.members[:15] for i in range(15): cell = species.members[i] cell.vars['family'] = i + 1 cell.vars['genealogy'] = 0 cell.vars['generation'] = 0 cell.vars['birthday'] = 0.0 species.update_agent_output() save_path = os.path.join(sim_dir_path, 'agentS.xml') output.write(output_path=save_path)
def normalise_by_generation(sim_dir_path, max_generation): results_path = basic.check_path(os.path.join(sim_dir_path, 'lineage_results.xml')) results_output = results.AgentOutput(path=results_path) results_species = results.SpeciesOutput(results_output) for generation in range(max_generation+1): cohort = [c for c in results_species.members if int(c.vars['generation']) == generation] growth_rates = [float(c.vars['growth_rate']) for c in cohort] mean = numpy.mean(growth_rates) for cell in cohort: cell.vars['growth_rate'] = str(float(cell.vars['growth_rate']) / mean) results_species.update_agent_output() save_path = os.path.join(sim_dir_path, 'norm_gen_lineage_results.xml') results_output.write(output_path=save_path)
def __init__(self, path): self.path = toolbox_basic.check_path(path) self.iterate_numbers = [] self.iterate_information = [] self.min_max_concns = {} # agent_Sum """ try: self.agent_Sum = os.path.join(self.path, 'agent_Sum') if not os.path.isdir( self.agent_Sum ): toolbox_basic.unzip_files(self.agent_Sum + '.zip') self.agent_Sum = toolbox_basic.check_path(self.agent_Sum) except TypeError: print('Could not find agent_Sum info! '+self.path) """ # agent_State try: self.agent_State = os.path.join(self.path, 'agent_State') if not os.path.isdir( self.agent_State ): toolbox_basic.unzip_files(self.agent_State + '.zip') self.agent_State = toolbox_basic.check_path(self.agent_State) except TypeError: print('Could not find agent_State info! '+self.path) """
def __init__(self, path=None, root_name='idynomics'): if path == None or not os.path.isfile(path): self.path = path self.root = xmlTree.Element(root_name) self.tree = xmlTree.ElementTree(self.root) simulation = xmlTree.SubElement(self.root, 'simulation') simulation.set('iterate', '0') simulation.set('time', '0.0') simulation.set('unit', 'h') else: self.path = toolbox_basic.check_path(path) self.tree = toolbox_basic.get_xml_tree(self.path) self.root = self.tree.getroot() self.simulation = self.find('./simulation') self.iterate = int(self.simulation.attrib['iterate']) self.time = float(self.simulation.attrib['time']) self.time_unit = self.simulation.attrib['unit']
def get_concn_array(self, detail_level, sif_name): self.get_run_dir(detail_level, sif_name) grid_length = detail_level * self.side_length array_path = os.path.join(self.run_dir, 'concn_array') if os.path.isfile(array_path): concn_array = toolbox_basic.load_array(array_path) else: result_file_path = os.path.join(self.run_dir, 'case.result') result_file_path = toolbox_basic.check_path(result_file_path) # This isn't quite correct! Without wrapping, Elmer skips some nodes num_nodes = (grid_length + 1)**2 array_shape = (grid_length + 1,)*2 with open(result_file_path, 'Ur') as f: last_lines = f.readlines()[-num_nodes:] concn_array = numpy.array([float(line) for line in last_lines]) concn_array = numpy.reshape(concn_array, array_shape) toolbox_basic.save_array(concn_array, array_path) return concn_array
def normalise_by_generation(sim_dir_path, max_generation): results_path = basic.check_path( os.path.join(sim_dir_path, 'lineage_results.xml')) results_output = results.AgentOutput(path=results_path) results_species = results.SpeciesOutput(results_output) for generation in range(max_generation + 1): cohort = [ c for c in results_species.members if int(c.vars['generation']) == generation ] growth_rates = [float(c.vars['growth_rate']) for c in cohort] mean = numpy.mean(growth_rates) for cell in cohort: cell.vars['growth_rate'] = str( float(cell.vars['growth_rate']) / mean) results_species.update_agent_output() save_path = os.path.join(sim_dir_path, 'norm_gen_lineage_results.xml') results_output.write(output_path=save_path)
def normalise_by_progenitor(sim_dir_path): results_path = basic.check_path(os.path.join(sim_dir_path, 'lineage_results.xml')) results_output = results.AgentOutput(path=results_path) results_species = results.SpeciesOutput(results_output) prog_growth_rates = {} for cell in results_species.members: generation = cell.vars['generation'] if generation == '0': family = cell.vars['family'] growth_rate = float(cell.vars['growth_rate']) prog_growth_rates[family] = growth_rate for cell in results_species.members: family = cell.vars['family'] top = float(cell.vars['growth_rate']) bottom = prog_growth_rates[family] cell.vars['growth_rate'] = str( top / bottom ) results_species.update_agent_output() save_path = os.path.join(sim_dir_path, 'norm_prog_lineage_results.xml') results_output.write(output_path=save_path)
def build_life_history( sim_dir_path, attribute1, attribute2, cell_id=(1, 0), biomass_names=["activeBiomassGrowth", "activeBiomassRepair", "inactiveBiomassGrowth", "inactiveBiomassRepair"], ): sim_dir_path = basic.check_path(sim_dir_path) # this must be value and not value1 for the first value so that the plotting # script can also plot older results at the same time attributes = {"name": attribute1, "name2": attribute2, "name3": "generation", "header": "time,value,value2,value3"} results_file_path = os.path.join(sim_dir_path, "life_history_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.members requirements = {"family": str(cell_id[0]), "genealogy": str(cell_id[1])} file_dir = os.path.join(sim_dir_path, "agent_State") basic.unzip_files(file_dir + ".zip") file_list = basic.file_list(file_dir) for filename in file_list: output = results.AgentOutput(path=filename) species = results.SpeciesOutput(output) cells = species.find_cells(requirements) single_result = results.SingleResult() single_result.vars["time"] = output.time if len(cells) == 0: continue cell = cells[0] single_result.vars["value3"] = cell.vars["generation"] if attribute1 == "specific growth rate": single_result.vars["value"] = cell.get_specific_growth_rate(biomass_names) else: single_result.vars["value"] = cell.vars[attribute1] if attribute2 == "specific growth rate": single_result.vars["value2"] = cell.get_specific_growth_rate(biomass_names) else: single_result.vars["value2"] = cell.vars[attribute2] result_set.members.append(single_result) result_set.update_results_output() results_output.write(results_file_path) basic.rm_dir(file_dir) return result_set
def normalise_by_progenitor(sim_dir_path): results_path = basic.check_path( os.path.join(sim_dir_path, 'lineage_results.xml')) results_output = results.AgentOutput(path=results_path) results_species = results.SpeciesOutput(results_output) prog_growth_rates = {} for cell in results_species.members: generation = cell.vars['generation'] if generation == '0': family = cell.vars['family'] growth_rate = float(cell.vars['growth_rate']) prog_growth_rates[family] = growth_rate for cell in results_species.members: family = cell.vars['family'] top = float(cell.vars['growth_rate']) bottom = prog_growth_rates[family] cell.vars['growth_rate'] = str(top / bottom) results_species.update_agent_output() save_path = os.path.join(sim_dir_path, 'norm_prog_lineage_results.xml') results_output.write(output_path=save_path)
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 build_population_structure( sim_dir_path, attribute, bins=numpy.linspace(0, 0.6, 90), starting_time=2400, biomass_names=["activeBiomassGrowth", "activeBiomassRepair", "inactiveBiomassGrowth", "inactiveBiomassRepair"], ): attributes = { "name": attribute + " population structure", "starting_time": str(starting_time), "header": "bin,frequency", } print sim_dir_path sim_dir_path = basic.check_path(sim_dir_path) results_file_path = os.path.join(sim_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.members attr_values = [] total_pop = 0.0 file_dir = os.path.join(sim_dir_path, "agent_State") basic.unzip_files(file_dir + ".zip") file_list = basic.file_list(file_dir) for filename in file_list: output = results.AgentOutput(path=filename) if output.time >= starting_time: species = results.SpeciesOutput(output) species.set_biomass_names(biomass_names) attr_values.extend(species.get_attribute_values(attribute)) total_pop += species.population() hist, bin_edges = numpy.histogram(attr_values, bins) for i in range(len(hist)): single_result = results.SingleResult() single_result.vars["bin"] = str(bins[i + 1]) + "-" + str(bins[i]) single_result.vars["frequency"] = str(float(hist[i]) / total_pop) result_set.members.append(single_result) result_set.update_results_output() results_output.write(results_file_path) basic.rm_dir(file_dir) return result_set
def calc_averages(results_path, max_generation): results_path = basic.check_path(results_path) results_output = results.AgentOutput(path=results_path) results_species = results.SpeciesOutput(results_output) results_species.change_header( 'family,genealogy,generation,growth_rate,std_growth_rate') save_cells = [] min_num_cells = 10000000000 max_num_cells = 0 for generation in range(max_generation + 1): cohort = [ c for c in results_species.members if int(c.vars['generation']) == generation ] genealogies = list(set([int(c.vars['genealogy']) for c in cohort])) for genealogy in genealogies: growth_rates = [ float(c.vars['growth_rate']) for c in cohort if int(c.vars['genealogy']) == genealogy ] num_cells = len(growth_rates) min_num_cells = min(min_num_cells, num_cells) max_num_cells = max(max_num_cells, num_cells) if num_cells < 5: print('Only ' + str(num_cells) + ' in point ' + str(genealogy) + ', ' + str(generation)) cell = LineageCell(results_species) cell.vars['genealogy'] = genealogy cell.vars['generation'] = generation cell.vars['growth_rate'] = numpy.mean(growth_rates) cell.vars['std_growth_rate'] = numpy.std(growth_rates) save_cells.append(cell) print('All lineages had between ' + str(min_num_cells) + ' and ' + str(max_num_cells) + ' cells') results_species.members = save_cells results_species.update_agent_output() dir_name = os.path.dirname(results_path) base_name = 'mean_' + os.path.basename(results_path) save_path = os.path.join(dir_name, base_name) results_output.write(output_path=save_path)
def collate_mean_attributes(set_dir_path, attribute, output_type, file_process, starting_time=2400): set_dir_path = basic.check_path(set_dir_path) attributes = {'name':attribute, 'starting_time':str(starting_time), 'header':'directory,mean,std'} 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) for dir_name in dir_list: dir_result = results.SingleResult() dir_result.vars['directory'] = os.path.basename(dir_name) mean, std = calc_mean_attribute(dir_name, attribute, output_type, file_process, starting_time=starting_time) dir_result.vars['mean'] = mean dir_result.vars['std'] = std result_set.members.append(dir_result) result_set.update_results_output() results_output.write(results_file_path) return result_set
def calc_averages(results_path, max_generation): results_path = basic.check_path(results_path) results_output = results.AgentOutput(path=results_path) results_species = results.SpeciesOutput(results_output) results_species.change_header( 'family,genealogy,generation,growth_rate,std_growth_rate') save_cells = [] min_num_cells = 10000000000 max_num_cells = 0 for generation in range(max_generation+1): cohort = [c for c in results_species.members if int(c.vars['generation']) == generation] genealogies = list(set([int(c.vars['genealogy']) for c in cohort])) for genealogy in genealogies: growth_rates = [float(c.vars['growth_rate']) for c in cohort if int(c.vars['genealogy']) == genealogy] num_cells = len(growth_rates) min_num_cells = min(min_num_cells, num_cells) max_num_cells = max(max_num_cells, num_cells) if num_cells < 5: print('Only '+str(num_cells)+' in point '+str(genealogy)+', '+ str(generation)) cell = LineageCell(results_species) cell.vars['genealogy'] = genealogy cell.vars['generation'] = generation cell.vars['growth_rate'] = numpy.mean(growth_rates) cell.vars['std_growth_rate'] = numpy.std(growth_rates) save_cells.append(cell) print('All lineages had between '+str(min_num_cells)+ ' and '+str(max_num_cells)+' cells') results_species.members = save_cells results_species.update_agent_output() dir_name = os.path.dirname(results_path) base_name = 'mean_'+os.path.basename(results_path) save_path = os.path.join(dir_name, base_name) results_output.write(output_path=save_path)
def plot_lineages(axis, results_path, max_generation, line_width=1.0, error_bars=True): results_path = basic.check_path(results_path) results_output = results.AgentOutput(path=results_path) results_species = results.SpeciesOutput(results_output) if error_bars and results_species.attributes.count('std_growth_rate') == 0: print('Could not find data for error bars') error_bars = False for cell in results_species.members: generation = int(cell.vars['generation']) if generation <= max_generation - 1: family = int(cell.vars['family']) genealogy = int(cell.vars['genealogy']) growth_rate = float(cell.vars['growth_rate']) if error_bars: eb_x = [generation + 1] eb_y = [growth_rate] eb_e = [float(cell.vars['std_growth_rate'])] requirements = { 'family': family, 'genealogy': genealogy, 'generation': generation + 1 } temp = results_species.find_cells(requirements) if not temp == []: oldie = temp[0] oldie_grow = float(oldie.vars['growth_rate']) axis.plot([generation + 1, generation + 2], [growth_rate, oldie_grow], 'r', linewidth=line_width, solid_capstyle='round', zorder=5 + numpy.random.randint(5)) if error_bars: eb_x.append(generation + 2) eb_y.append(oldie_grow) eb_e.append(float(oldie.vars['std_growth_rate'])) requirements['genealogy'] = genealogy + 2**generation temp = results_species.find_cells(requirements) if not temp == []: newbie = temp[0] newbie_grow = float(newbie.vars['growth_rate']) axis.plot([generation + 1, generation + 2], [growth_rate, newbie_grow], 'b', linewidth=line_width, solid_capstyle='round', zorder=5 + numpy.random.randint(5)) if error_bars: eb_x.append(generation + 2) eb_y.append(newbie_grow) eb_e.append(float(newbie.vars['std_growth_rate'])) if error_bars: axis.errorbar(eb_x, eb_y, yerr=eb_e, ecolor='grey', fmt=None, elinewidth=line_width / 2, capsize=1, zorder=1) axis.set_xlabel('Generations') axis.set_ylabel('Normalised growth rate') axis.set_xlim([1, max_generation + 1])
#!/usr/bin/python from __future__ import division from __future__ import with_statement import os import toolbox_basic input_file = \ os.path.join("~", "Dropbox", "KreftGroup", "NRM", "bac_000_070000.ml") input_file = toolbox_basic.check_path(input_file) with open(input_file, 'Ur') as f: input_script = f.readlines() species_names = {1: 'Eco', 2: 'Ego'} species_lists = [[], []] input_column_heads = [ 'species', 'locationX', 'locationY', 'locationZ', 'radius', 'unknown1', 'genealogy', 'growthRate', 'unknown2' ] for line in input_script: if line is '': continue columns = line.replace('\n', '').split('\t') ''' for i in range(len(columns)): if '.' in columns[i]: columns[i] = float(columns[i])
#!/usr/bin/python from __future__ import division from __future__ import with_statement import os import toolbox_basic input_file = os.path.join("~", "Dropbox", "KreftGroup", "NRM", "bac_000_070000.ml") input_file = toolbox_basic.check_path(input_file) with open(input_file, "Ur") as f: input_script = f.readlines() species_names = {1: "Eco", 2: "Ego"} species_lists = [[], []] input_column_heads = [ "species", "locationX", "locationY", "locationZ", "radius", "unknown1", "genealogy", "growthRate", "unknown2", ] for line in input_script: if line is "":
def process_last_iter(sim_dir_path, max_generation, use_masses=True): sim_dir_path = basic.check_path(sim_dir_path) last_path = os.path.join(sim_dir_path, 'lastIter', 'agent_State(last).xml') last_path = basic.check_path(last_path) last_output, last_species = results.get_single_species(last_path) if use_masses: agent_State_dir = basic.unzip_files( os.path.join(sim_dir_path, 'agent_State.zip')) biomass_names = ['activeBiomass', 'inactiveBiomass'] timestep = float(last_output.time) / float(last_output.iterate) # do a first run-through temp_cells = [] families = [] for cell in last_species.members: family = int(cell.vars['family']) if families.count(family) == 0: families.append(family) genealogy = int(cell.vars['genealogy']) birthday = float(cell.vars['birthday']) birth_generation = calc_birth_generation(genealogy) for generation in range(birth_generation, max_generation + 2): temp = LineageCell(last_species) temp.vars['family'] = family temp.vars['genealogy'] = genealogy temp.vars['generation'] = generation temp.vars['initial_time'] = birthday if use_masses: iter = str(int(birthday / timestep)) iter_path = os.path.join(agent_State_dir, 'agent_State(' + iter + ').xml') iter_output, iter_species = results.get_single_species( iter_path) requirements = {'family': family, 'genealogy': genealogy} iter_cell = iter_species.find_cells(requirements)[0] temp.vars['initial_mass'] = iter_cell.get_total_biomass( biomass_names) requirements['genealogy'] = genealogy - 2**(generation - 1) sibling = iter_species.find_cells(requirements) if len(sibling) == 1: temp.sibling_mass = sibling[0].get_total_biomass( biomass_names) temp_cells.append(temp) print( str(len(temp_cells)) + ' cells in ' + str(len(families)) + ' families') # then filter last_species.members = [] for family in families: members = [c for c in temp_cells if c.vars['family'] == family] next_gen = [c for c in members if c.vars['generation'] == 0] for generation in range(max_generation + 1): current_gen = next_gen next_gen = [ c for c in members if c.vars['generation'] == generation + 1 ] for cell in current_gen: genealogy = cell.vars['genealogy'] # find my baby baby_genealogy = genealogy + 2**generation possibles = [ c for c in next_gen if c.vars['genealogy'] == baby_genealogy ] if len(possibles) == 1: baby = possibles[0] baby_bday = baby.vars['initial_time'] cell.vars['division_time'] = baby_bday if use_masses: total_mass = baby.vars[ 'initial_mass'] + baby.sibling_mass cell.vars['division_mass'] = total_mass possibles = [ c for c in next_gen if c.vars['genealogy'] == genealogy ] if len(possibles) == 1: me_next = possibles[0] me_next.vars['initial_time'] = baby_bday if use_masses: me_next.vars['initial_mass'] = baby.sibling_mass cell.set_growth_rate() last_species.members.append(cell) print(str(len(last_species.members)) + ' cells remain') #for cell in temp_cells.members: header = 'family,genealogy,generation,initial_time,division_time,' header += 'initial_mass,division_mass,growth_rate' last_species.change_header(header) last_species.update_agent_output() save_path = os.path.join(sim_dir_path, 'lineage_results.xml') last_output.write(output_path=save_path)
def process_last_iter(sim_dir_path, max_generation, use_masses=True): sim_dir_path = basic.check_path(sim_dir_path) last_path = os.path.join(sim_dir_path, 'lastIter', 'agent_State(last).xml') last_path = basic.check_path(last_path) last_output, last_species = results.get_single_species(last_path) if use_masses: agent_State_dir = basic.unzip_files(os.path.join(sim_dir_path, 'agent_State.zip')) biomass_names = ['activeBiomass','inactiveBiomass'] timestep = float(last_output.time) / float(last_output.iterate) # do a first run-through temp_cells = [] families = [] for cell in last_species.members: family = int(cell.vars['family']) if families.count(family) == 0: families.append(family) genealogy = int(cell.vars['genealogy']) birthday = float(cell.vars['birthday']) birth_generation = calc_birth_generation(genealogy) for generation in range(birth_generation, max_generation+2): temp = LineageCell(last_species) temp.vars['family'] = family temp.vars['genealogy'] = genealogy temp.vars['generation'] = generation temp.vars['initial_time'] = birthday if use_masses: iter = str(int( birthday / timestep )) iter_path = os.path.join(agent_State_dir, 'agent_State('+iter+').xml') iter_output, iter_species = results.get_single_species(iter_path) requirements = {'family':family, 'genealogy':genealogy} iter_cell = iter_species.find_cells(requirements)[0] temp.vars['initial_mass'] = iter_cell.get_total_biomass(biomass_names) requirements['genealogy'] = genealogy - 2**(generation-1) sibling = iter_species.find_cells(requirements) if len(sibling) == 1: temp.sibling_mass = sibling[0].get_total_biomass(biomass_names) temp_cells.append(temp) print(str(len(temp_cells))+' cells in '+str(len(families))+' families') # then filter last_species.members = [] for family in families: members = [c for c in temp_cells if c.vars['family'] == family] next_gen = [c for c in members if c.vars['generation'] == 0] for generation in range(max_generation + 1): current_gen = next_gen next_gen = [c for c in members if c.vars['generation'] == generation+1] for cell in current_gen: genealogy = cell.vars['genealogy'] # find my baby baby_genealogy = genealogy + 2**generation possibles = [c for c in next_gen if c.vars['genealogy'] == baby_genealogy] if len(possibles) == 1: baby = possibles[0] baby_bday = baby.vars['initial_time'] cell.vars['division_time'] = baby_bday if use_masses: total_mass = baby.vars['initial_mass'] + baby.sibling_mass cell.vars['division_mass'] = total_mass possibles = [c for c in next_gen if c.vars['genealogy'] == genealogy] if len(possibles) == 1: me_next = possibles[0] me_next.vars['initial_time'] = baby_bday if use_masses: me_next.vars['initial_mass'] = baby.sibling_mass cell.set_growth_rate() last_species.members.append(cell) print(str(len(last_species.members))+' cells remain') #for cell in temp_cells.members: header = 'family,genealogy,generation,initial_time,division_time,' header += 'initial_mass,division_mass,growth_rate' last_species.change_header(header) last_species.update_agent_output() save_path = os.path.join(sim_dir_path, 'lineage_results.xml') last_output.write(output_path=save_path)
#!/usr/bin/python from __future__ import division from __future__ import with_statement import os import toolbox_basic input_dir = os.path.join("~", "Dropbox", "KreftGroup", "NRM") input_dir = toolbox_basic.check_path(input_dir) input_file = os.path.join(input_dir, "bac_000_070000.ml") output_file = os.path.join(input_dir, "bac_000_070000.pov") with open(input_file, "Ur") as f: input_script = f.readlines() species_names = {"1": "Eco", "2": "Ego"} # TODO .incl files? output_script = "" for line in input_script: if line is "": continue columns = line.replace("\n", "").split("\t") output_script += "sphere { <%s, %s, %s> %s texture { %s } }\n" % ( columns[2], columns[1], columns[3], columns[4], species_names[columns[0]], )
#!/usr/bin/python from __future__ import division import aging_extras import os import math from pylab import * import toolbox_basic import toolbox_plotting import toolbox_results from mpl_toolkits.axes_grid1 import make_axes_locatable # base_path = os.path.join('~', 'Dropbox', 'EclipseWorkspace', 'iDynoAge') # base_path = os.path.join('C:\\', 'Users', 'RJW598', 'git', 'iDynoMiCS') base_path = os.path.join("~", "git", "iDynoMiCS") print base_path base_path = toolbox_basic.check_path(base_path) print base_path input_path = os.path.join(base_path, "results", "Older", "Figure_4") output_path = os.path.join(input_path, "figure_4_life_history_ABC.pdf") fig = toolbox_plotting.BmcFigure(double_column=True, height="double") axisA = fig.add_subplot("A", 221) paths = [ "T010S12OBConstEnvCleggComp_life_history_results.xml", "T010S12NRConstEnvCleggComp_life_history_results.xml", "T010S12007ConstEnvCleggComp_life_history_results.xml", "T010AS12NRConstEnvCleggComp_life_history_results.xml", "T010AS12007ConstEnvCleggComp_life_history_results.xml", "T010AS12OBConstEnv1_0D_Ind_life_history_results.xml", ]
#!/usr/bin/python from __future__ import division from __future__ import with_statement import os import toolbox_basic input_dir = os.path.join("~", "Dropbox", "KreftGroup", "NRM") input_dir = toolbox_basic.check_path(input_dir) input_file = os.path.join(input_dir, "bac_000_070000.ml") output_file = os.path.join(input_dir, "bac_000_070000.pov") with open(input_file, 'Ur') as f: input_script = f.readlines() species_names = {'1': 'Eco', '2': 'Ego'} # TODO .incl files? output_script = "" for line in input_script: if line is '': continue columns = line.replace('\n', '').split('\t') output_script += 'sphere { <%s, %s, %s> %s texture { %s } }\n' \ %(columns[2], columns[1], columns[3], columns[4], species_names[columns[0]]) with open(output_file, 'w') as f: f.write(output_script)
def get_all(folder, f): sim_dir_path = basic.check_path(folder + f + '/') file_dir = os.path.join(sim_dir_path, 'agent_Sum') basic.unzip_files(file_dir + '.zip') file_list = basic.file_list(file_dir) t_a, population_a, biomass_a, growthrate_a = [], [], [], [] t_b, population_b, biomass_b, growthrate_b = [], [], [], [] last_pop_a, last_biomass_a, last_growthrate_a = [], [], [] last_pop_b, last_biomass_b, last_growthrate_b = [], [], [] for filename in file_list: output = results.AgentOutput(path=filename) species = results.SpeciesOutput(output, 'OldieA') requirements = {} cells = species.find_cells(requirements) #single_result = results.SingleResult() t_a.append(float(output.time)) if len(cells) == 0: continue cell = cells[0] population_a.append(float(cell.vars['population'])) biomass_a.append(float(cell.vars['mass'])) growthrate_a.append(float(cell.vars['growthRate'])) species = results.SpeciesOutput(output, 'OldieB') requirements = {} cells = species.find_cells(requirements) #single_result = results.SingleResult() t_b.append(float(output.time)) if len(cells) == 0: continue cell = cells[0] population_b.append(float(cell.vars['population'])) biomass_b.append(float(cell.vars['mass'])) growthrate_b.append(float(cell.vars['growthRate'])) basic.rm_dir(file_dir) lists_a, lists_b = [population_a, biomass_a, growthrate_a], [population_b, biomass_b, growthrate_b] t_a1, t_b1 = t_a, t_b for i in range(3): list1, list2, list3, list4 = t_a1, lists_a[i], t_b1, lists_b[i] t_a, lists_a[i] = (list(x) for x in zip( *sorted(zip(list1, list2), key=itemgetter(0)))) t_b, lists_b[i] = (list(x) for x in zip( *sorted(zip(list3, list4), key=itemgetter(0)))) biomass_ratio, population_ratio, growthrate_ratio = [], [], [] for j in range(len(lists_a[1])): biomass_ratio.append(numpy.log(lists_a[1][j] / lists_b[1][j])) population_ratio.append(numpy.log(lists_a[0][j] / lists_b[0][j])) if lists_a[2][j] == 0 and lists_b[2][j] == 0: growthrate_ratio.append(0) elif lists_a[2][j] == 0: lists_b[2][j] = abs(1 / (lists_b[2][j])) growthrate_ratio.append(numpy.log(lists_b[2][j])) elif lists_b[2][j] == 0: lists_a[2][j] = abs((lists_a[2][j]) / 1) growthrate_ratio.append(numpy.log(lists_a[2][j])) else: growthrate_ratio.append( abs(numpy.log(lists_a[2][j] / lists_b[2][j]))) if j == len(lists_a[1]) - 1: last_pop_a.append(lists_a[0][j]) last_pop_b.append(lists_b[0][j]) last_biomass_a.append(lists_a[1][j]) last_biomass_b.append(lists_b[1][j]) last_growthrate_a.append(lists_a[2][j]) last_growthrate_b.append(lists_b[2][j]) return t_a, [biomass_ratio, population_ratio, growthrate_ratio]
#!/usr/bin/python #import numpy import os import sys import toolbox_basic as basic #import toolbox_results as results #import toolbox_fitness as fitness #import toolbox_idynomics import random import math #this is for one species only dir_path = basic.check_path( '/Users/u1560915/git/iDynoMiCS/results_analysis_python') file_path = os.path.join(dir_path, 'even_spacing_to_start.xml') gridres = 4 density = 201 genealogy = 0.0 generation = 0.0 birthday = 0.0 activeBiomassGrowth = 300 activeBiomassRepair = 0.0 inactiveBiomassGrowth = 0.0 inactiveBiomassRepair = 0.0 growthRate = 0.0 volumeRate = 0.0 locationX = 0.0 #- or the same as the radius? locationY = 0.000000 #+i = +4 = +4*(i-1) locationZ = 0.000000 radius = 0.0
def get_replicate_results(replicates_dir): replicates_dir = toolbox_basic.check_path(replicates_dir) results_path = os.path.join(replicates_dir, 'results.xml') results_file = ReplicatesFile(path=results_path) return results_file