<jupyter_start><jupyter_text>Single Cell Clone Velocities<jupyter_code>import pandas as pd
import scipy as sp
import scipy.stats
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
exec(open('../settings.py').read(), globals())<jupyter_output><empty_output><jupyter_text>Trajectories<jupyter_code>L0 = 800.0
clone_trajectories = pd.read_csv('../../data/clone_trajectories.csv')
clone_trajectories.head()<jupyter_output><empty_output><jupyter_text>The number of trajectories used for the analysis is:<jupyter_code>len(clone_trajectories['ID'].unique())<jupyter_output><empty_output><jupyter_text>Plot trajectories<jupyter_code>cmap = mpl.colors.LinearSegmentedColormap.from_list('from_list',\
                                                    [(0, colorcycle[2]), (0.2, colorcycle[2]), (1.0, colorcycle[6])])
cmap = plt.get_cmap('jet')
pos0 = clone_trajectories.query('time == 0')['pos']
norm = plt.Normalize(-L0 / 1000.0, 0.0)
# norm = plt.Normalize(pos0.min() / 1000.0, 0.0)
# cmap = plt.get_cmap('jet')
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
# fake up the array of the scalar mappable. Urgh...
sm._A = []

fig, ax = plt.subplots(1, figsize = (73.0 / 25.4, 47.0 / 25.4))
fig.patch.set_alpha(1.0)
for ID, IDdata in clone_trajectories.groupby('ID'):
    color = cmap(norm(float(IDdata.query('time == 0')['pos']) /1000.0))
    ax.plot(IDdata['time'], IDdata['pos'] / 1000.0, color = color, marker = '.', ms = 5)
    
ax.set_xlim(-0.5, 8.5)
ax.set_ylim(-1.0, 2.0)
ax.set_yticks(sp.arange(-1.0, 2.1, 1.0))
ax.set_yticks(sp.arange(-0.5, 1.6, 1.0), minor=True)
ax.tick_params(axis='both', which='major')

ax.set_xlabel('Time (days)')
ax.set_ylabel('AP position (mm)')

    
cbar = plt.colorbar(sm, ax = ax, ticks = sp.linspace(-L0/1000.0, 0, 5))
cbar.set_label('Intitial position (mm)', labelpad=9)
plt.savefig('../../figure_plots/Fig2_clone_trajectories.svg')
plt.show()
p.line?
from bokeh.plotting import figure, output_notebook, show
output_notebook()
TOOLS = 'hover'
p = figure(tools = TOOLS)
for ID, IDdata in clone_trajectories.groupby('ID'):
    color = cmap(norm(float(IDdata.query('time == 0')['pos']) /1000.0))
    p.line(IDdata['time'], IDdata['pos'] / 1000.0, color = mpl.colors.rgb2hex(color), name = "ID_{}".format(ID))
show(p)
clone_trajectories.query('time == 4').sort_values('pos')<jupyter_output><empty_output><jupyter_text>ID 8 seems to be a good candidate for showing a represantative image in Fig. 2I.  Velocities<jupyter_code>def velocities(binsize, mintime=0, maxtime=8, clone_trajectories=clone_trajectories):
    clone_trajectories = clone_trajectories.query('@mintime <= time <= @maxtime').copy()
    
    clone_trajectories['bin'] = (clone_trajectories['pos'] / binsize).round() * binsize

    def linregress_v(group):
        if len(group) > 1:
            v = sp.polyfit(group['time'], group['pos'], 1)[0]
        else:
            v = sp.nan
        return v

    clone_velocities = pd.DataFrame(clone_trajectories.groupby(['ID', 'bin']).apply(linregress_v).dropna()).reset_index()
    clone_velocities.columns = ['ID', 'pos', 'v']
    meanclone_velocity = clone_velocities[['pos', 'v']].groupby('pos').filter(lambda x: len(x) > 2).groupby('pos').agg(['mean', 'sem'])
    meanclone_velocity.columns = meanclone_velocity.columns.droplevel()
    meanclone_velocity = meanclone_velocity.reset_index()

    return clone_velocities, meanclone_velocity



clone_velocities, meanclone_velocity = velocities(L0)
clone_velocities.to_csv('../lg_model/clone_velocities.csv', index = False)<jupyter_output><empty_output><jupyter_text>Plot the mean clone velocity<jupyter_code>fig, ax = plt.subplots(figsize = (30.0 / 25.4, 37.0 / 25.4))
fig.patch.set_alpha(1.0)

# ax.errorbar(meanclonespeed['bin'], meanclonespeed['mean'], meanclonespeed['sem'], marker = 'o', color = 'black')
width = L0 / 1000.0
ax.bar((meanclone_velocity['pos'] / 1000.0- 0.5 * width), meanclone_velocity['mean'], 
       width = width, yerr = meanclone_velocity['sem'], 
       color = 'white', ecolor = 'black')

ax.set_xlim(-1.4, 2.2)
ax.set_xticks([-0.800, 0, 0.800, 1.600])
ax.set_ylim(0, 400)
ax.set_yticks(sp.arange(0, 401, 100))
ax.set_yticks(sp.arange(50, 401, 100), minor = True)

ax.tick_params(axis='both', which='major')

ax.set_xlabel('AP position (mm)')
ax.set_ylabel(u'Velocity (μm / day)', labelpad = 8)
plt.savefig('../../figure_plots/Fig2_clone_velocities.svg')
plt.show()<jupyter_output><empty_output><jupyter_text>Clone velocity at -800 $\mu m$ is not significantly different from 0:<jupyter_code>meanclone_velocity
sp.stats.ttest_1samp(clone_velocities.query('pos == -@L0')['v'], 0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17619353457503747, 'fraction_numerical': 0.03896381931064012, 'mean_word_length': 5.005141388174807, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '380145', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1790, 'n_words': 307, 'path': 'calculations/clone_velocities/clone_velocities.ipynb', 'repo_name': 'fabianrost84/Rost-Rodrigo-Albors-et-al.-2016', 'size': '4671'}