예제 #1
0
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
예제 #6
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
예제 #7
0
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)
예제 #8
0
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_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
예제 #12
0
 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):
     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)
예제 #14
0
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)
예제 #15
0
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 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]