In [191]:
    
import pandas as pd
import matplotlib.pyplot as plt
import scipy as sp
from uncertainties import ufloat
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
exec(open('settings.py').read(), globals())
    
In [192]:
    
outgrowth = pd.read_csv('../data/outgrowth.csv')
    
Outgrowth in mm
In [193]:
    
mean_outgrowth = sp.around(outgrowth[['time', 'length']].groupby('time').agg(['mean', 'sem']) / 1000.0, 2)
mean_outgrowth
    
    Out[193]:
800 micron zone vs 500 micron zone
In [194]:
    
levan_zone = ufloat(500.0, 0.0)
source_zone = ufloat(800.0, 100.0)
fraction_day0 = levan_zone / source_zone
print fraction_day0
    
    
In [195]:
    
outgrowth_day8 = float(outgrowth[['time', 'length']].groupby('time').agg('mean').loc[8])
outgrowth_day8_delta = float(outgrowth[['time', 'length']].groupby('time').agg('sem').loc[8])
outgrowth_day8 = ufloat(outgrowth_day8, outgrowth_day8_delta)
    
In [196]:
    
fraction_day8 = outgrowth_day8 / (outgrowth_day8 + source_zone)
print fraction_day8
    
    
Finding the avarage trajectory to use for Fig. 1
In [197]:
    
for i, row in outgrowth.iterrows():
    if row['time'] == 0:
        outgrowth.loc[i, 'chi2'] = 0.0
    else:
        outgrowth.loc[i, 'chi2'] = ((1000 * mean_outgrowth.loc[row['time']]['length', 'mean'] - row['length']) / (1000 * mean_outgrowth.loc[row['time']]['length', 'sem']))**2
    
In [198]:
    
chi2 = outgrowth[['ID', 'chi2']].groupby('ID').sum()
chi2
    
    Out[198]:
In [199]:
    
averageID = chi2.idxmin().iloc[0]
averageID
    
    Out[199]:
Animal t3 has the most average behaviour
In [200]:
    
outgrowth.query('ID == @averageID')
    
    Out[200]:
Setting the colors: Iterate the colors, but then make sure the average ID shown in Fig. 1A gets green, which is the 3rd color in the color cycle.
In [ ]:
    
    
In [ ]:
    
    
In [201]:
    
colors = {}
markers = {}
lss = {}
markerlist = ['o', 'v', '^', '<', '>', 's', 'D', '*']
lslist = ['-', '--', '-.', ':', '-', '--', '-.', ':']
colorlist = [1, 2, 3, 4, 5, 6, 7]
for i, ID in enumerate(sp.sort(list(set(outgrowth['ID'].unique()) - set([averageID])))):
    colors[ID] = colorcycle[colorlist[i]]
    markers[ID] = markerlist[i]
    lss[ID] = lslist[i]
colors['t3'] = colorcycle[0]
markers['t3'] = 's'
colors['t4'], colors['t5'] = colors['t5'], colors['t4'] 
colors['t6'], colors['t8'] = colors['t6'], colors['t8']
    
Plotting the outgrowth:
In [202]:
    
fig, ax = plt.subplots(figsize=(40/25.4,70.0/25.4))
fig.patch.set_alpha(1.0)
for ID, IDdata in outgrowth.groupby('ID'):
    pitem = ax.plot(IDdata['time'], IDdata['length'] / 1000.0, '-', markeredgewidth = 0, color = colors[ID], marker = markers[ID], label = ID)[0]
# plot the average guy last such that you can see him
ax.plot(outgrowth.query('ID == @averageID')['time'], outgrowth.query('ID == @averageID')['length'] / 1000.0, '-', markeredgewidth = 0, color = colors[averageID], marker = 's')
    
ax.set_xlabel('Time (days)')
ax.set_ylabel('Outgrowth (mm)'.decode('utf-8'), labelpad=8)
ax.set_xlim(-0.5, 8.5)
ax.set_xticks([1, 3, 5, 7], minor=True)
plt.legend(loc = 'best')
plt.savefig('../figure_plots/Fig1_outgrowth_trajectories.svg', transparent = True)
plt.show()
    
    
Semilog plot of outgrowth + L0
In [218]:
    
L0 = 0.8
fig, ax = plt.subplots(figsize=(40/25.4,70.0/25.4))
fig.patch.set_alpha(1.0)
for ID, IDdata in outgrowth.groupby('ID'):
    pitem = ax.plot(IDdata['time'], IDdata['length'] / 1000.0 + L0, '-', markeredgewidth = 0, color = colors[ID], marker = markers[ID], label = ID)[0]
# plot the average guy last such that you can see him
ax.plot(outgrowth.query('ID == @averageID')['time'], outgrowth.query('ID == @averageID')['length'] / 1000.0 + L0, '-', markeredgewidth = 0, color = colors[averageID], marker = 's')
    
ax.set_xlabel('Time (days)')
ax.set_ylabel('Outgrowth + 0.8 (mm)'.decode('utf-8'), labelpad=8)
ax.set_xlim(-0.5, 8.5)
ax.set_xticks([1, 3, 5, 7], minor=True)
ax.set_yscale('log')
# plt.legend(loc = 'upper left')
plt.savefig('../figure_plots/Fig3_outgrowth_trajectories_semilog.svg', transparent = True)
plt.show()
    
    
In [347]:
    
import probfit
import iminuit
    
In [348]:
    
def growth_model(time, L0, r):
    return L0 * (sp.exp(r * time) - 1.0)
    
In [349]:
    
outgrowth.head()
    
    Out[349]:
In [373]:
    
chi2 = probfit.Chi2Regression(growth_model,sp.array(mean_outgrowth.index)[1:],
                                sp.array(mean_outgrowth['length', 'mean'])[1:],
                                sp.array(mean_outgrowth['length', 'sem'])[1:])
minuit = iminuit.Minuit(chi2, L0 = 0.8, r = 1.0, error_L0 = 0.1, error_r = 0.1)
minuit.migrad();
    
    
    
    
    
In [367]:
    
L0 = minuit.values['L0']
r = minuit.values['r']
fig, ax = plt.subplots(figsize=(40/25.4,70.0/25.4))
fig.patch.set_alpha(1.0)
# for ID, IDdata in outgrowth.groupby('ID'):
#     pitem = ax.plot(IDdata['time'], IDdata['length'] / 1000.0 + L0, '-', markeredgewidth = 0, color = colors[ID], marker = markers[ID], label = ID)[0]
ax.plot(time, growth_model(time, L0, r) + L0)
ax.errorbar(sp.array(mean_outgrowth.index),
                                sp.array(mean_outgrowth['length', 'mean'] + L0),
                                sp.array(mean_outgrowth['length', 'sem']))
ax.set_xlabel('Time (days)')
ax.set_ylabel('Outgrowth + L0 (mm)'.decode('utf-8'), labelpad=8)
ax.set_xlim(-0.5, 8.5)
ax.set_xticks([1, 3, 5, 7], minor=True)
ax.set_yscale('log')
plt.show()
    
    
but this time time-depndent rate switch
In [353]:
    
def growth_model2(time, L0, r1, r2, tswitch):
    if tswitch <= 0:
        return L0 * (sp.exp(r2 * time) - 1.0) 
    else:
        if time <= tswitch:
            return L0 * (sp.exp(r1 * time) - 1.0) 
        else:
            L1 = L0 * (sp.exp(r1 * tswitch) - 1.0) + L0
            return L1 * (sp.exp(r2 * (time - tswitch)) - 1.0)  + L1 - L0
    
In [354]:
    
chi2_2 = probfit.Chi2Regression(growth_model2,
                                sp.array(mean_outgrowth.index)[1:],
                                sp.array(mean_outgrowth['length', 'mean'])[1:],
                                sp.array(mean_outgrowth['length', 'sem'])[1:])
    
In [355]:
    
iminuit.describe(chi2_2)
    
    Out[355]:
In [487]:
    
minuit2 = iminuit.Minuit(chi2_2, L0 = 0.8, r1 = 0.10, r2 = 0.1, tswitch = 3.0,\
                         error_L0 = 0.1, error_r1 = 0.01, error_r2 = 0.01, error_tswitch = 0.1,
                         limit_r1 = (0, None), limit_r2 = (0, None))
    
In [488]:
    
minuit2.migrad();
    
    
    
    
    
In [489]:
    
minuit2.values
    
    Out[489]:
In [490]:
    
L0 = minuit2.values['L0'] 
r1 = minuit2.values['r1'] 
r2 = minuit2.values['r2'] 
tswitch = minuit2.values['tswitch']
fig, ax = plt.subplots(figsize=(80/25.4,100.0/25.4))
fig.patch.set_alpha(1.0)
# for ID, IDdata in outgrowth.groupby('ID'):
#     pitem = ax.plot(IDdata['time'], IDdata['length'] / 1000.0 + L0, '-', markeredgewidth = 0, color = colors[ID], marker = markers[ID], label = ID)[0]
# plot the average guy last such that you can see him
# ax.plot(outgrowth.query('ID == @averageID')['time'], outgrowth.query('ID == @averageID')['length'] / 1000.0 + L0, '-', markeredgewidth = 0, color = colors[averageID], marker = 's')
ax.plot(time, sp.vectorize(growth_model2)(time, L0, r1, r2, tswitch) + L0, lw = 2)
ax.errorbar(sp.array(mean_outgrowth.index),
                                sp.array(mean_outgrowth['length', 'mean'] + L0),
                                sp.array(mean_outgrowth['length', 'sem']))
    
ax.set_xlabel('Time (days)')
ax.set_ylabel('Outgrowth + L0 (mm)'.decode('utf-8'), labelpad=8)
ax.set_xlim(-0.5, 8.5)
ax.set_xticks([1, 3, 5, 7], minor=True)
ax.set_yscale('log')
plt.show()
    
    
In [493]:
    
minuit.values
    
    Out[493]:
In [494]:
    
minuit2.minos('L0', sigma = 1);
    
    
In [496]:
    
minuit2.minos('L0', sigma = 2);
    
    
In [504]:
    
minuit2.minos('L0', sigma = 3);
    
    
In [471]:
    
minuit2 = iminuit.Minuit(chi2_2, L0 = 3.0, r1 = 0.10, r2 = 0.1, tswitch = 2.5,
                         error_L0 = 0.1, error_r1 = 0.01, error_r2 = 0.01, error_tswitch = 0.1,
                         limit_r1 = (0, None), limit_r2 = (0, None), limit_tswitch = (0.5, None),
                         fix_L0 = True)
    
In [472]:
    
minuit2.migrad();
    
    
    
    
    
In [433]:
    
time = sp.linspace(1, 8)
L01 = minuit.values['L0'] 
r = minuit.values['r'] 
L02 = minuit2.values['L0'] 
r1 = minuit2.values['r1'] 
r2 = minuit2.values['r2'] 
tswitch = minuit2.values['tswitch']
fig, axs = plt.subplots(1, 3, figsize=(3*80/25.4,100.0/25.4), sharex = True)
fig.patch.set_alpha(1.0)
# for ID, IDdata in outgrowth.groupby('ID'):
#     pitem = ax.plot(IDdata['time'], IDdata['length'] / 1000.0 + L0, '-', markeredgewidth = 0, color = colors[ID], marker = markers[ID], label = ID)[0]
# plot the average guy last such that you can see him
# ax.plot(outgrowth.query('ID == @averageID')['time'], outgrowth.query('ID == @averageID')['length'] / 1000.0 + L0, '-', markeredgewidth = 0, color = colors[averageID], marker = 's')
for L0plot, ax, title in zip([0, L01, L02], axs, ['', ' + L01', ' + L02']):
    ax.errorbar(sp.array(mean_outgrowth.index)[1:],
                sp.array(mean_outgrowth['length', 'mean'])[1:]+ L0plot,
                sp.array(mean_outgrowth['length', 'sem'])[1:]
               )
    ax.plot(time, sp.vectorize(growth_model)(time, L01, r) + L0plot, lw = 2)
    ax.plot(time, sp.vectorize(growth_model2)(time, L02, r1, r2, tswitch)+ L0plot, lw = 2)
    ax.set_xlabel('Time (days)')
    ax.set_ylabel('Outgrowth{} (mm)'.format(title), labelpad=8)
    ax.set_xlim(-0.5, 8.5)
    ax.set_xticks([1, 3, 5, 7], minor=True)
    ax.set_yscale('log')
plt.show()
    
    
In [418]:
    
def AIC(minuit):
    SSE = minuit.fval
    # only works if no fixed paramters are used
    k = len(minuit.values)
    
    
    AIC = 2 * k + SSE
    
    return AIC
def AICc(minuit):
    AICv = AIC(minuit)
    k = len(minuit.values)
    n = minuit.fcn.data_len
    AICc = AICv + 2 * k * (k + 1) / (n - k - 1)
    
    return AICc
    
compute AIC
In [420]:
    
print AIC(minuit)
print AIC(minuit2)
    
    
we cannot compute the corrected AIC because of the low number od data points
In [419]:
    
# print AICc(minuit)
# print AICc(minuit2)