In [1]:
import pandas as pd
import colgen.display
import numpy as np
import os
from collections import defaultdict
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
%load_ext autoreload
%autoreload 2
In [2]:
from utils import DATASETS, TREATMENTS, COLORS, rack_layout, colorize_pondscum, format_tree_labels
export_path = "02_tree_display"
if not os.path.exists(export_path):
os.makedirs(export_path)
In [3]:
SMALL_SIZE = 12
MEDIUM_SIZE = 14
BIGGER_SIZE = 16
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
In [4]:
import sqlite3
with sqlite3.connect("lce_data.sqlite") as database:
genealogies = pd.read_sql("SELECT * FROM genealogies", database)
In [5]:
def plot_trajectories(data):
fig, ax = plt.subplots(1,1, figsize=(6,6))
for ex,df in data.groupby("experiment"):
x = df[df.phase==1].groupby(["cycle"]).extinct
y = df[df.phase==2].groupby(["cycle"]).extinct
color = COLORS[ex]
l = ax.scatter(1-x.mean(),1-y.mean(), label=ex, s=250, color=color)
ax.plot(1-x.mean(),1-y.mean(),color=color, lw=0.5, ls='--')
for c, (xx,yy) in enumerate(zip(1-x.mean(), 1-y.mean())):
ax.text(xx, yy, c+1, color='white',
verticalalignment='center',
horizontalalignment='center')
dd = df[np.logical_or(df.cycle==1,df.cycle==10)]
x = dd[dd.phase==1].groupby(["cycle"]).extinct
y = dd[dd.phase==2].groupby(["cycle"]).extinct
ax.plot(1-x.mean(),1-y.mean(),color=color)
ax.legend()
ax.set(xlabel='Proportion of surviving collectives, Phase I',
ylabel='Proportion of surviving collectives, Phase II',
xlim=(0.0,1.05),
ylim=(0.0,1.05))
return fig, ax
fig, ax = plot_trajectories(genealogies)
fig.savefig(os.path.join(export_path, f"survival_phase12.svg"), bbox_inches='tight')
fig.savefig(os.path.join(export_path, f"survival_phase12.pdf"), bbox_inches='tight')
In [6]:
def plot_tree(tree, df, title, ax_tree):
name = df.experiment.unique()[0]
coal = colgen.display.coalescent(list(df.query('time==20 & extinct==0').name), tree['branches'])
ypos = rack_layout(df)
_, scales = colgen.display.draw_tree(tree['branches'],
tree['xinfo'],
ypos,
oinfo={d['name']:1 if d['name'] in coal else 0.3 for _,d in df.iterrows()},
color={d['name']:colorize_pondscum(d) for _,d in df.iterrows()},
child_color_branch=True,
ax=ax_tree)
format_tree_labels(tree['xinfo'].keys(), ax_tree, scales)
ax_tree.set_title(title, font={'size' : 20, 'weight':'bold'})
In [7]:
def plot_bar(df, ax_phase1, ax_phase2, set_yticks=True):
df['color'] = [colorize_pondscum(d) for _,d in df.iterrows()]
count = df.groupby(['cycle','phase','color']).count().name
bottom = defaultdict(lambda:0)
scale = df.groupby(['cycle','phase']).count().name.to_dict()
for (cycle, phase, color), number in count.items():
ax = ax_phase1 if phase==1 else ax_phase2
number = number/scale[cycle,phase]
ax.bar(cycle,number, bottom=bottom[cycle,phase], color=color)
ax.set(ylim=(0,1))
if set_yticks:
ax.set_yticks([0,0.25,0.5,0.75,1])
ax.set_yticklabels(['0%',"25%","50%","75%","100%"], rotation=90, ha='center', va= 'center',)
bottom[cycle,phase] += number
ax_phase1.set(xticklabels='', xticks=df.cycle.unique(), title='Phase I')
ax_phase2.set(xlabel='Cycle', xticks=df.cycle.unique(), title='Phase II')
In [8]:
def plot_tree_and_bar(tree, df, title, ax_tree, ax_phase1, ax_phase2):
plot_tree(tree,df,title,ax_tree)
plot_bar(df, ax_phase1, ax_phase2)
In [9]:
data = {}
for treat,dt, in zip(TREATMENTS, DATASETS):
data[treat] = colgen.display.load_df(dt)
In [10]:
ax = plt.figure(layout="constrained",
figsize=(12*1.5+4,9*1.5*4)).subplot_mosaic(
"""
AAAB
AAAI
CCCD
CCCJ
EEEF
EEEK
GGGH
GGGL
"""
)
letters = ["ABI","CDJ","EFK","GHL"]
for treat,dt,axid, in zip(TREATMENTS, DATASETS, letters):
plot_tree_and_bar(*data[treat], treat, *[ax[x] for x in axid])
plt.savefig(os.path.join(export_path, f"all_genealogies_and_bar.pdf"), bbox_inches='tight')
plt.savefig(os.path.join(export_path, f"all_genealogies_and_bar.svg"), bbox_inches='tight')
In [11]:
s = 3.2
ax = plt.figure(layout="constrained",
figsize=(7*s,6*s)).subplot_mosaic("AB\nCD")
fig = plt.gcf()
for treat,axid in zip(TREATMENTS, "ABCD"):
plot_tree(*data[treat], axid.lower()+". "+treat, ax[axid])
fig.subplots_adjust(bottom=0.3, wspace=0.33)
labels=['Survival', 'Failure to produce germ cells', 'Failure of soma', "Failure to produce soma cells"]
colors = ['C0','C3','C1','C2']
patches = [mpatches.Patch(color=n, label=s) for s,n in zip(labels, colors)]
leg = ax['C'].legend(handles=patches,loc='upper center',
bbox_to_anchor=(1, -0.05),fancybox=False, shadow=False, ncol=4,
prop={'size': 16})
leg.set_in_layout(False)
# trigger a draw so that constrained layout is executed once
# before we turn it off when printing....
fig.canvas.draw()
# we want the legend included in the bbox_inches='tight' calcs.
leg.set_in_layout(True)
# we don't want the layout to change at this point.
fig.set_layout_engine('none')
fig.savefig(os.path.join(export_path,'all_genealogies.png'), bbox_inches='tight')
fig.savefig(os.path.join(export_path,'all_genealogies.pdf'), bbox_inches='tight')
/tmp/ipykernel_347981/3163840379.py:8: UserWarning: This figure was using a layout engine that is incompatible with subplots_adjust and/or tight_layout; not calling subplots_adjust. fig.subplots_adjust(bottom=0.3, wspace=0.33)
In [12]:
for k,v in data.items():
x = v[1].query('phase==1').query('cycle==1')
print(k, x.mat.sum(), x.shape[0], x.shape[0]-x.mat.sum())
S-LCS+ 47.0 48 1.0 L-LCS+ 35.0 48 13.0 S-LCS- 46.0 48 2.0 L-LCS- 21.0 48 27.0
In [13]:
s = 3.8
ax = plt.figure(layout="constrained",
figsize=(3.5*s,1.75*s)).subplot_mosaic("ABCD\nEFGH")
fig = plt.gcf()
for treat,ax1,ax2 in zip(TREATMENTS, "ABCD", "EFGH"):
plot_bar(data[treat][1], ax[ax1], ax[ax2], False)
ax[ax1].set_title(ax1.lower()+'. '+treat, font={'size' : 16, 'weight':'bold'})
ax[ax2].set_title('')
for a in (ax[ax1],ax[ax2]):
a.grid(color=[.9,.9,.9], axis='y')
a.set_yticks([0,0.25,0.5,0.75,1])
if ax1=='A':
a.set_yticklabels(['0%',"25%","50%","75%","100%"], rotation=0)
else:
a.set_yticklabels(['',"","","",""])
ax['A'].set_ylabel("Ph. I", font={'size' : 16, 'weight':'bold'})
ax['E'].set_ylabel("Ph. II", font={'size' : 16, 'weight':'bold'})
fig.subplots_adjust(bottom=0.3, wspace=0.33)
labels=['Survival', 'Failure to produce germ cells', 'Failure of soma', "Failure to produce soma cells"]
colors = ['C0','C3','C1','C2']
patches = [mpatches.Patch(color=n, label=s) for s,n in zip(labels, colors)]
leg = ax['F'].legend(handles=patches,loc='upper center',
bbox_to_anchor=(1, -0.2),fancybox=False, shadow=False, ncol=4)
leg.set_in_layout(False)
# trigger a draw so that constrained layout is executed once
# before we turn it off when printing....
fig.canvas.draw()
# we want the legend included in the bbox_inches='tight' calcs.
leg.set_in_layout(True)
# we don't want the layout to change at this point.
fig.set_layout_engine('none')
fig.savefig(os.path.join(export_path,'survival_phase12_bar.png'), bbox_inches='tight')
fig.savefig(os.path.join(export_path,'survival_phase12_bar.pdf'), bbox_inches='tight')
/tmp/ipykernel_347981/2789303761.py:20: UserWarning: This figure was using a layout engine that is incompatible with subplots_adjust and/or tight_layout; not calling subplots_adjust. fig.subplots_adjust(bottom=0.3, wspace=0.33)
In [ ]:
In [ ]: