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 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 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 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 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)