Skip to content

Commit 2a85fbf

Browse files
authored
Merge pull request #16 from alife-data-standards/support-forest
2 parents 8a5dd1d + 9c6ec83 commit 2a85fbf

2 files changed

Lines changed: 38 additions & 26 deletions

File tree

ALifeStdDev/phylogeny/loader.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
def load_phylogeny_to_pandas_df(filename):
77
data = pd.read_csv(filename)
8+
data.columns = data.columns.str.replace(' ', '')
9+
810
# The ancestor_list column contains a string of lists of strings
911
# Let's turn it into an actual list of strings
12+
data.loc[:, "ancestor_list"] = \
13+
data.loc[:, "ancestor_list"].apply(lambda x: x.strip())
1014
data.loc[:, "ancestor_list"] = \
1115
data.loc[:, "ancestor_list"].apply(ast.literal_eval)
1216
data.set_index("id", inplace=True)
@@ -69,7 +73,11 @@ def networkx_to_pandas_df(g, bonus_cols=None):
6973
bonus_cols = {}
7074
df = pd.DataFrame()
7175
df["id"] = g.nodes.keys()
72-
df["ancestor_list"] = [list(g.predecessors(i)) for i in df["id"]]
76+
df["ancestor_list"] = [list(g.predecessors(i)) if len(list(g.predecessors(i))) > 0 else [None] for i in df["id"]]
77+
78+
# roots = df[df["ancestor_list"].apply(lambda x: len(x) == 0)].index
79+
# df.loc[roots, "ancestor_list"] = "[None]"
80+
7381
for name1, name2 in bonus_cols.items():
7482
df[name1] = [g.nodes[i][name2] for i in df["id"]]
7583

ALifeStdDev/phylogeny/utils.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -517,32 +517,36 @@ def abstract_asexual_phylogeny(phylogeny, attribute_list,
517517

518518
# Start with the root nodes
519519
# TODO: Make work for forests (multiple roots)
520-
root_id = get_root_ids(phylogeny)[0]
520+
to_process = []
521+
root_ids = get_root_ids(phylogeny)
521522
state_id = 0
523+
next_id = 1
522524
# Add the first phylogeny state to the abstract phylogeny
523-
abstract_phylogeny.add_node(state_id)
524-
abstract_phylogeny.nodes[state_id]["state_id"] = state_id
525-
abstract_phylogeny.nodes[state_id]["node_state"] = \
526-
[phylogeny.nodes[root_id][attr] for attr in attribute_list]
527-
# Add attributes to state
528-
for attr in attribute_list:
529-
abstract_phylogeny.nodes[state_id][attr] = phylogeny.nodes[root_id][attr]
530-
if track_origin:
531-
abstract_phylogeny.nodes[state_id]["origin_time"] = \
532-
phylogeny.nodes[root_id][origin_time_attr]
533-
if track_destruction: # (this might get updated as we go)
534-
dest_time = phylogeny.nodes[root_id][destruction_time_attr]
535-
if dest_time == "none" or float(dest_time) == float("inf"):
536-
dest_time = -1
537-
dest_time = float(dest_time)
538-
abstract_phylogeny.nodes[state_id]["destruction_time"] = dest_time
539-
540-
# Add first member
541-
abstract_phylogeny.nodes[state_id]["members"] = {root_id:
542-
phylogeny.nodes[root_id]}
543-
544-
next_id = state_id + 1
545-
to_process = [(root_id, state_id)]
525+
for root_id in root_ids:
526+
abstract_phylogeny.add_node(state_id)
527+
abstract_phylogeny.nodes[state_id]["state_id"] = state_id
528+
abstract_phylogeny.nodes[state_id]["node_state"] = \
529+
[phylogeny.nodes[root_id][attr] for attr in attribute_list]
530+
# Add attributes to state
531+
for attr in attribute_list:
532+
abstract_phylogeny.nodes[state_id][attr] = phylogeny.nodes[root_id][attr]
533+
if track_origin:
534+
abstract_phylogeny.nodes[state_id]["origin_time"] = \
535+
phylogeny.nodes[root_id][origin_time_attr]
536+
if track_destruction: # (this might get updated as we go)
537+
dest_time = phylogeny.nodes[root_id][destruction_time_attr]
538+
if dest_time == "none" or float(dest_time) == float("inf"):
539+
dest_time = -1
540+
dest_time = float(dest_time)
541+
abstract_phylogeny.nodes[state_id]["destruction_time"] = dest_time
542+
543+
# Add first member
544+
abstract_phylogeny.nodes[state_id]["members"] = {root_id:
545+
phylogeny.nodes[root_id]}
546+
547+
to_process.append((root_id, state_id))
548+
state_id = next_id
549+
next_id += 1
546550

547551
while to_process:
548552
phylogeny_id, state_id = to_process.pop()
@@ -592,7 +596,7 @@ def abstract_asexual_phylogeny(phylogeny, attribute_list,
592596
phylogeny.nodes[id][attr]
593597
# Add first member
594598
abstract_phylogeny.nodes[next_state_id]["members"] = \
595-
{phylogeny_id: phylogeny.nodes[id]}
599+
{id: phylogeny.nodes[id]}
596600
to_process.append((id, next_state_id))
597601

598602
return abstract_phylogeny

0 commit comments

Comments
 (0)