def get_last_only(folder, f):
    os.chdir(folder + f + '/lastIter')
    output = results.AgentOutput(path='agent_Sum(last).xml')
    species = results.SpeciesOutput(output, 'OldieA')
    requirements = {}
    cells = species.find_cells(requirements)
    t_a = (float(output.time))
    cell = cells[0]
    population_a = (float(cell.vars['population']))
    biomass_a = (float(cell.vars['mass']))
    growthrate_a = (float(cell.vars['growthRate']))
    species = results.SpeciesOutput(output, 'OldieB')
    requirements = {}
    cells = species.find_cells(requirements)
    cell = cells[0]
    population_b = (float(cell.vars['population']))
    biomass_b = (float(cell.vars['mass']))
    growthrate_b = (float(cell.vars['growthRate']))
    if growthrate_a == 0:
        growthrate_b = abs(1 / growthrate_b)
        gr = numpy.log(growthrate_b)
    elif growthrate_b == 0:
        growthrate_a = abs(growthrate_a / 1)
        gr = numpy.log(growthrate_a)
    elif growthrate_a == 0 and growthrate_b == 0:
        gr = 0
    else:
        gr = abs(growthrate_a / growthrate_b)
        gr = numpy.log(gr)
    br = numpy.log(biomass_a / biomass_b)
    pr = numpy.log(population_a / population_b)
    ratio = [br, pr, gr]
    time = t_a
    return time, ratio
예제 #2
0
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 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
예제 #4
0
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 get_population(sim_dir_path):
    sim_dir_path = os.path.join(
        '/Volumes/Robyn_W_2/july_2018/paper/comparison_clegg/' + sim_dir_path +
        '/lastIter/')
    output = results.AgentOutput(path=sim_dir_path + 'agent_State(last).xml')
    species = results.SpeciesOutput(output)
    cells = species.findcells()
    age, total_biomass = [], []
    for cell in cells:
        if len(cells) == 0:
            continue
        ar = cell.vars['activeBiomassRepair']
        ir = cell.vars['inactiveBiomassRepair']
        ag = cell.vars['activeBiomassGrowth']
        ig = cell.vars['inactiveBiomassGrowth']
        total_biomass.append(ar + ir + ag + ig)
        age.append(cell.vars['age'])
    return age, biomass
예제 #6
0
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)
예제 #7
0
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 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
예제 #9
0
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_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]
 axisA, axisB, axisC = fig.add_subplot(311), fig.add_subplot(
     312), fig.add_subplot(313)
 axisA.set_title('A', loc='left'), axisB.set_title(
     'B', loc='left'), axisC.set_title('C', loc='left')
 ax = [axisC, axisA, axisB, axisC, axisA, axisB]
 color = [colors[4], colors[0], colors[2], colors[5], colors[1], colors[3]]
 #axisA, axisA, axisB, axisB, axisC, axisC]
 #color = [colors[0], colors[1], colors[2], colors[3], colors[4], colors[5]]
 for b in range(6):
     c = (b * 3) + a
     input_path = os.path.join(
         '/Volumes/Robyn_W_2/july_2018/paper/comparison_clegg/steady_state/'
         + names3[c] + '/lastIter/')
     results_path = os.path.join(input_path, 'agent_State(last).xml')
     agent_output = toolbox_results.AgentOutput(path=results_path)
     species = toolbox_results.SpeciesOutput(agent_output)
     aging_extras.scatter_population(ax[b],
                                     species,
                                     'totalBiomass',
                                     'age',
                                     color=color[b],
                                     markersize=ms)
 axisA.set_xlim([285, 630])
 axisA.set_ylim([0.0, 1.0])
 axisA.set_title('Age & Size Distributions', fontsize=10)
 plt.setp(axisA.get_xticklabels(), visible=False)
 axisB.set_xlim([285, 630])
 axisB.set_ylim([0.0, 1.0])
 axisB.set_ylabel(r'Cellular age $P_{dam}/(P_{act}+P_{dam})$', fontsize=10)
 plt.setp(axisB.get_xticklabels(), visible=False)
 axisC.set_xlim([285, 630])