diff --git a/growthAnalysis/grates.ipynb b/growthAnalysis/grates.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..07277ebe801f4b1b72b2c5acaa8b7964075508e2 --- /dev/null +++ b/growthAnalysis/grates.ipynb @@ -0,0 +1,478 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import patternTransitions as p\n", + "from itertools import repeat\n", + "import seaborn as sns\n", + "import matplotlib.pyplot as plt\n", + "from mpl_toolkits.mplot3d import Axes3D\n", + "import common.seg as seg\n", + "import _pickle as cPickle\n", + "import common.lin as lin\n", + "import aniso\n", + "import numpy as np\n", + "import matplotlib.ticker as ticker" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from os.path import join" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plot_params = {'01' : {'elev': 120, 'azim' : 270, 'vmax':0.4},\n", + " '02' : {'elev': 70, 'azim' : -20, 'vmax':0.3},\n", + " '03' : {'elev': 90, 'azim' : 270, 'vmax':0.2},\n", + " '04' : {'elev':80, 'azim': 50, 'vmax': 0.2},\n", + " '05' : {'elev':70, 'azim': 220, 'vmax':0.2},\n", + " '06' : {'elev':90, 'azim': 270, 'vmax':0.2}}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#fid='01'\n", + "\n", + "#tssFn = join(dataDir, \"lins_allflowers/f{fid}_tss.pkl\".format(fid=fid))\n", + "#linssFn = join(dataDir, \"lins_allflowers/f{fid}_linss.pkl\".format(fid=fid))\n", + "\n", + "#with open(tssFn, 'rb') as tssF:\n", + "# tss = cPickle.load(tssF, encoding='latin1')\n", + "\n", + "#with open(linssFn, 'rb') as linssF:\n", + "# linss = cPickle.load(linssF, encoding='latin1')\n", + "\n", + "#if fid=='01':\n", + "# lin.filterL1(tss)\n", + "#else:\n", + "# tss = dict([(t, seg.filterL1(ts)) for t, ts in tss.items()])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dataDir = \"../data/\"\n", + "tss, linss = lin.mkSeries1(dataDir)\n", + "lin.filterL1(tss)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def mkViolinPlot(d, ax, ws=0.7, vert=False):\n", + " xs_labs = sorted(d.keys())\n", + " xs = range(len(xs_labs))\n", + " ys = [d[x] for x in xs_labs]\n", + " \n", + " #ax.yaxis.set_major_locator(ticker.MultipleLocator(0.05))\n", + " sns.set_style(\"whitegrid\", {'grid.linestyle':'--'})\n", + " cls = sns.color_palette()\n", + " ax.set_xticks(xs)\n", + " ax.set_xticklabels(xs_labs)\n", + " violin_parts = ax.violinplot(ys, xs, showmeans=True, widths=ws)\n", + " for pc in violin_parts['bodies']:\n", + " pc.set_facecolor(\"#f5d742\")\n", + " pc.set_edgecolor('black')\n", + "\n", + " for partname in ('cbars','cmins','cmaxes','cmeans'):\n", + " vp = violin_parts[partname]\n", + " vp.set_edgecolor('red')\n", + " vp.set_linewidth(0.5)\n", + " \n", + " if vert:\n", + " plt.xticks(rotation=90)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Per pattern" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import grates as grs\n", + "\n", + "G = p.mkTGraphN()\n", + "grates = grs.grates_avg_cons(tss, linss)\n", + "grates_pats = p.addCombPatterns(grates, tss)\n", + "\n", + "grs_state = p.getGAnisosPerPattern(G, grates_pats)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.rcParams.update({'font.size': 12})\n", + "fig = plt.figure(figsize=(9, 4.5))\n", + "ax = fig.add_subplot('111')\n", + "\n", + "ax.set_xlabel(\"cell state\")\n", + "ax.set_ylabel(r'growth rate ($h^{-1}$)')\n", + "ax.set_ylim((-0.05, 0.15))\n", + "ax.yaxis.set_ticks(np.arange(-0.05, 0.15, 0.1))\n", + "ax.yaxis.set_major_locator(ticker.MultipleLocator(0.1))\n", + "\n", + "mkViolinPlot(grs_state, ax)\n", + "\n", + "fout = \"state_growth_rates.png\"\n", + "plt.savefig(fout, dpi=300, bbox_inches='tight')\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Per pattern on graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "p.addGRates(G, grates_pats) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "p.drawTGraph(G, p.gr)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Per gene" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "grates_gene = p.getGAnisosPerGene(tss, grates)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fig = plt.figure(figsize=(10, 5))\n", + "ax = fig.add_subplot('111')\n", + "\n", + "ax.set_xlabel(\"gene\")\n", + "ax.set_ylabel(r'growth rate ($h^{-1}$)')\n", + "ax.set_ylim((-0.05, 0.15))\n", + "ax.yaxis.set_ticks(np.arange(-0.05, 0.15, 0.1))\n", + "ax.yaxis.set_major_locator(ticker.MultipleLocator(0.1))\n", + "\n", + "mkViolinPlot(grates_gene, ax, vert=True)\n", + "\n", + "fout = \"gene_grates.png\"\n", + "plt.savefig(fout, dpi=300, bbox_inches='tight')\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Per gene, per stage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def tToStage(t):\n", + " if t==10: return 0\n", + " elif t==40: return 1\n", + " elif t==96: return 2\n", + " elif t==120: return 3\n", + " elif t==132: return 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "grates_gene_time = p.getGAnisosPerGeneTime(tss, grates)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for t in sorted(grates_gene_time.keys()):\n", + " fig = plt.figure(figsize=(10, 5))\n", + " ax = fig.add_subplot('111')\n", + " \n", + " ax.set_title(\"stage {t}\".format(t=tToStage(t)))\n", + " ax.set_xlabel(\"gene\")\n", + " ax.set_ylabel(r'growth rate ($h^{-1}$)')\n", + " ax.set_ylim((-0.05, 0.15))\n", + " ax.yaxis.set_major_locator(ticker.MultipleLocator(0.1))\n", + "\n", + " mkViolinPlot(grates_gene_time[t], ax, ws=0.4, vert=True)\n", + "\n", + " fout = \"gene_growth_{t}.png\".format(t=t)\n", + " plt.savefig(fout, dpi=300, bbox_inches='tight')\n", + " plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Growth rate distributions (all to all timepoints)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import grates as grs\n", + "timepoints = sorted(tss.keys())\n", + "n = len(timepoints)\n", + "\n", + "fig = plt.figure(figsize=(10, 8))\n", + "plt.subplots_adjust(hspace=0.85, wspace=0.5)\n", + "\n", + "for i, t1 in enumerate(timepoints):\n", + " for k, t2 in enumerate(timepoints):\n", + " print(t1, end=\",\")\n", + " print(t2, end=\" \")\n", + " if t1 > t2:\n", + " ax = plt.subplot2grid((n, n), (i, k))\n", + " #plot forward\n", + " plt.rcParams.update({'font.size': 8})\n", + " ax.set_title(str(t2) + \"h\" + \"-\" + str(t1)+\"h\")\n", + " plt.rcParams.update({'font.size': 10})\n", + " ax.set_xlim([-0.05, 0.15])\n", + " ax.set_xticks([0.0, 0.05, 0.1])\n", + " ans = grs.grates_forward(tss, linss, t2, t1)\n", + " ans_ = np.array([an for _, an in ans.items() if an])\n", + " sns.distplot(ans_, ax=ax) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Growth rate distributions (all to all timepoints) on templates" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import grates as grs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fid = '01'\n", + "timepoints = sorted(tss.keys())[::]\n", + "n = len(timepoints)\n", + "plt.rcParams.update({'font.size': 8})\n", + "def plot_ans(ts, ans, ax, s=10, vmax=1.0):\n", + " xs = [c.pos.x for c in ts]\n", + " ys = [c.pos.y for c in ts]\n", + " zs = [c.pos.z for c in ts]\n", + " cs = [ans.get(c.cid, 0.0) for c in ts]\n", + " \n", + " \n", + " points = ax.scatter(xs, ys, zs, c=cs, cmap=plt.cm.jet, alpha=0.9, s=s, vmin=0.0, vmax=vmax,\n", + " edgecolors='black', linewidths=0.5)\n", + " plt.colorbar(points, shrink=0.5, ticks=[0.0, vmax])\n", + " plt.axis('off')\n", + " \n", + " return\n", + "\n", + "fig = plt.figure(figsize=(9, 5))\n", + "\n", + "for i, t1 in enumerate(timepoints):\n", + " for k, t2 in enumerate(timepoints):\n", + " print(t1, t2, end=\" \")\n", + " if t1 < t2:\n", + " ax = plt.subplot2grid((n, n), (i, k), projection='3d')\n", + " ax.set_title(str(t1)+\"h\" + \"-\" + str(t2) + \"h\")\n", + " ax.view_init(elev=plot_params[fid]['elev'], azim=plot_params[fid]['azim'])\n", + " #plot\n", + " ans = grs.grates_forward(tss, linss, t1, t2)\n", + " ans_ = dict([(cid, an) for cid, an in ans.items() if an])\n", + " plot_ans(tss[t1], ans_, ax, vmax=0.15)\n", + " elif t1 > t2:\n", + " ax = plt.subplot2grid((n, n), (i, k), projection='3d')\n", + " ax.set_title(str(t2)+\"h\" + \"-\" + str(t1) + \"h\")\n", + " ax.view_init(elev=plot_params[fid]['elev'], azim=plot_params[fid]['azim'])\n", + " #plot\n", + " ans = grs.grates_backward(tss, linss, t2, t1)\n", + " ans_ = dict([(cid, an) for cid, an in ans.items() if an])\n", + " plot_ans(tss[t1], ans_, ax, vmax=0.15)\n", + " else:\n", + " ax = plt.subplot2grid((n, n), (i, k))\n", + " ax.set_xlim(0.0, 1.0)\n", + " ax.set_ylim(0.0, 1.0)\n", + " ax.text(0.4, 0.5, str(t1)+\"h\", transform = ax.transAxes, fontsize=11)\n", + " ax.axis('off')\n", + " \n", + "fout = \"gratesTs_f{fid}.png\".format(fid=fid)\n", + "plt.savefig(fout, dpi=300, bbox_inches='tight')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## All to last" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import grates as grs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def tToStage(t):\n", + " if t==10: return 0\n", + " elif t==40: return 1\n", + " elif t==96: return 2\n", + " elif t==120: return 3\n", + " elif t==132: return 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "timepoints = sorted(tss.keys())\n", + "n = len(timepoints)\n", + "plt.rcParams.update({'font.size': 8})\n", + "\n", + "def plot_ans(ts, ans, ax, s=10, vmax=1.0):\n", + " xs = [c.pos.x for c in ts]\n", + " ys = [c.pos.y for c in ts]\n", + " zs = [c.pos.z for c in ts]\n", + " cs = [ans.get(c.cid, 0.0) for c in ts]\n", + " \n", + " \n", + " points = ax.scatter(xs, ys, zs, c=cs, cmap=plt.cm.jet, alpha=0.9, s=s, vmin=0.0, vmax=vmax,\n", + " edgecolors='black', linewidths=0.5)\n", + " plt.colorbar(points, shrink=0.5, ticks=[0.0, vmax])\n", + " plt.axis('off')\n", + " \n", + " return\n", + "\n", + "fig = plt.figure(figsize=(6, 2.25))\n", + "\n", + "t2 = timepoints[-1]\n", + "for i, t1 in enumerate(timepoints[:-1]):\n", + " ax = plt.subplot2grid((2, n), (0, i), projection='3d')\n", + " ax.set_title(\"{s1} -> ({s2})\".format(s1=tToStage(t1), s2 = tToStage(t2)))\n", + " ax.view_init(elev=plot_params[fid]['elev'], azim=plot_params[fid]['azim'])\n", + " #plot\n", + " ans = grs.grates_forward(tss, linss, t1, t2)\n", + " ans_ = dict([(cid, an) for cid, an in ans.items() if an])\n", + " plot_ans(tss[t1], ans_, ax, vmax=0.15)\n", + " \n", + " ax = plt.subplot2grid((2, n), (1, i), projection='3d')\n", + " ax.set_title(\"({s1}) -> {s2}\".format(s1=tToStage(t1), s2 = tToStage(t2)))\n", + " ax.view_init(elev=plot_params[fid]['elev'], azim=plot_params[fid]['azim'])\n", + " #plot\n", + " ans = grs.grates_backward(tss, linss, t1, t2)\n", + " ans_ = dict([(cid, an) for cid, an in ans.items() if an])\n", + " plot_ans(tss[t2], ans_, ax, vmax=0.15)\n", + " \n", + "plt.savefig(\"presentation_figures/grates_tolast\", dpi=300, bbox_inches = 'tight') " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}