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 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 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 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 build_population_structure(sim_dir_path, attribute='specific growth rate', bins=numpy.linspace(0, 0.6, 90), starting_time=2400, biomass_names=[ 'activeBiomassGrowth', 'activeBiomassRepair', 'inactiveBiomassGrowth', 'inactiveBiomassRepair' ]): sim_dir_path = os.path.join( '/Volumes/Robyn_W_2/july_2018/paper/comparison_clegg/' + sim_dir_path + '/') 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) all_growth = [] 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)) all_growth.append(species.get_attribute_values(attribute)) total_pop += species.population() hist, bin_edges = numpy.histogram(attr_values, bins) bin_using, frequencies = [], [] for i in range(len(hist)): bin_using.append((bins[i + 1] + bins[i]) / 2) frequencies.append(float(hist[i] / total_pop)) basic.rm_dir(file_dir) return bin_using, frequencies, all_growth
def get_biomass(sim_dir_path, a): times1, biomass1, population1, growthrate1 = [], [], [], [] times2, biomass2, population2, growthrate2 = [], [], [], [] file_path = '/Volumes/Robyn_W_2/july_2018/paper/' + sim_dir_path file_dir = file_path + '/agent_sum' basic.unzip_files(file_dir + '.zip') file_list = basic.file_list(file_dir) for filename in file_list: output = results.AgentOutput(path=filename) species1 = results.SpeciesOutput(output, 'OldieA') requirements = {} cells = species1.find_cells(requirements) times1.append(float(output.time)) if len(cells) == 0: continue cell = cells[0] population1.append(float(cell.vars['population'])) biomass1.append(float(cell.vars['mass'])) growthrate1.append(float(cell.vars['growthRate'])) species2 = results.SpeciesOutput(output, 'OldieB') cells = species2.find_cells(requirements) times2.append(float(output.time)) if len(cells) == 0: continue cell = cells[0] population2.append(float(cell.vars['population'])) biomass2.append(float(cell.vars['mass'])) growthrate2.append(float(cell.vars['growthRate'])) basic.rm_dir(file_dir) lists_a, lists_b = [population1, biomass1, growthrate1], [population2, biomass2, growthrate2] t_a1, t_b1 = times1, times2 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])): if lists_a[1][j] == 0 and lists_b[1][j] == 0 or lists_a[1][ j] == 0 and lists_b[1][j] != 0 or lists_a[1][ j] != 0 and lists_b[1][j] == 0: biomass_ratio.append(0) else: biomass_ratio.append(numpy.log(lists_a[1][j] / lists_b[1][j])) if lists_a[0][j] == 0 and lists_b[0][j] == 0 or lists_a[0][ j] == 0 and lists_b[0][j] != 0 or lists_a[0][ j] != 0 and lists_b[0][j] == 0: population_ratio.append(0) else: population_ratio.append(lists_a[0][j] / lists_b[0][j]) if lists_a[2][j] == 0 and lists_b[2][j] == 0 or lists_a[2][ j] == 0 and lists_b[2][j] != 0 or lists_a[2][ j] != 0 and lists_b[2][j] == 0: growthrate_ratio.append(0) else: growthrate_ratio.append(lists_a[2][j] / lists_b[2][j]) return t_a, biomass_ratio, population_ratio, growthrate_ratio
def clean_up(self): """ if os.path.isdir( self.env_Sum ): toolbox_basic.rm_dir(self.env_Sum) if os.path.isdir( self.env_State ): toolbox_basic.rm_dir(self.env_State) if os.path.isdir( self.agent_Sum ): toolbox_basic.rm_dir(self.agent_Sum) """ if os.path.isdir( self.agent_State ): toolbox_basic.rm_dir(self.agent_State) """
def clean_up(self): if os.path.isdir( self.env_Sum ): toolbox_basic.rm_dir(self.env_Sum) if os.path.isdir( self.env_State ): toolbox_basic.rm_dir(self.env_State) if os.path.isdir( self.agent_Sum ): toolbox_basic.rm_dir(self.agent_Sum) if os.path.isdir( self.agent_State ): toolbox_basic.rm_dir(self.agent_State) """
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 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 build_life_history(sim_dir_path, attribute1='specific growth rate', attribute2='age', cell_id=(1, 0), biomass_names=[ 'activeBiomassGrowth', 'activeBiomassRepair', 'inactiveBiomassGrowth', 'inactiveBiomassRepair' ]): sim_dir_path = os.path.join( '//Volumes/Robyn_W_2/july_2018/paper/comparison_clegg/' + sim_dir_path + '/') time, generation, spec_growth, age, inact_rep, act_rep, inact_gro, act_gro, total_biomass = [], [], [], [], [], [], [], [], [] 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: #print filename output = results.AgentOutput(path=filename) species = results.SpeciesOutput(output) cells = species.find_cells(requirements) time.append(output.time) if len(cells) == 0: continue cell = cells[0] generation.append(cell.vars['generation']) ar = cell.vars['activeBiomassRepair'] act_rep.append(ar) ir = cell.vars['inactiveBiomassRepair'] inact_rep.append(ir) ag = cell.vars['activeBiomassGrowth'] act_gro.append(ag) ig = cell.vars['inactiveBiomassGrowth'] inact_gro.append(ig) total_biomass.append(ar + ir + ag + ig) spec_growth.append(cell.get_specific_growth_rate(biomass_names)) age.append(cell.vars[attribute2]) basic.rm_dir(file_dir) return time, generation, spec_growth, age, inact_rep, act_rep, inact_gro, act_gro
type="int", help="number of images per second") parser.add_option("-r", "--ResultsDir", dest="results_dir", default=os.getcwd(), help="path to results directory") parser.add_option("-s", "--SoluteName", dest="solute_name", default="none", help="name of the solute to be plotted behind cells") (options, args) = parser.parse_args() sim = toolbox_idynomics.SimulationDirectory(options.results_dir) save_name = 'biofilm_'+options.solute_name file_list = toolbox_basic.file_list(sim.figures_dir, save_name+'*.png') num_digits = len(str(len(file_list))) temp_dir = os.path.join(os.path.abspath(sim.movies_dir), 'temp') toolbox_basic.make_dir(temp_dir) for i, filename in enumerate(file_list): save_num = str(i) link_name = 'img' + (num_digits-len(save_num))*'0' + save_num + '.png' link_name = os.path.join(temp_dir, link_name) toolbox_basic.make_symbolic_link(filename, link_name) cmd = "ffmpeg -framerate "+str(options.frame_rate)+" -i '" cmd += os.path.join(temp_dir, "img%"+str(num_digits)+"d.png'") cmd += " -pix_fmt yuv420p -r 24 '" cmd += os.path.join(os.path.abspath(sim.movies_dir), save_name+".mp4'") os.system(cmd) toolbox_basic.rm_dir(temp_dir)
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]
help="path to results directory") parser.add_option("-s", "--SoluteName", dest="solute_name", default="none", help="name of the solute to be plotted behind cells") (options, args) = parser.parse_args() sim = toolbox_idynomics.SimulationDirectory(options.results_dir) save_name = 'biofilm_' + options.solute_name file_list = toolbox_basic.file_list(sim.figures_dir, save_name + '*.png') num_digits = len(str(len(file_list))) temp_dir = os.path.join(os.path.abspath(sim.movies_dir), 'temp') toolbox_basic.make_dir(temp_dir) for i, filename in enumerate(file_list): save_num = str(i) link_name = 'img' + (num_digits - len(save_num)) * '0' + save_num + '.png' link_name = os.path.join(temp_dir, link_name) toolbox_basic.make_symbolic_link(filename, link_name) cmd = "ffmpeg -framerate " + str(options.frame_rate) + " -i '" cmd += os.path.join(temp_dir, "img%" + str(num_digits) + "d.png'") cmd += " -pix_fmt yuv420p -r 24 '" cmd += os.path.join(os.path.abspath(sim.movies_dir), save_name + ".mp4'") os.system(cmd) toolbox_basic.rm_dir(temp_dir)