{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FRAP geometries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fem_sol import frap_solver\n",
    "from matplotlib import rc, rcParams\n",
    "import fem_utils\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "fol = '/Users/hubatsch/Desktop/DropletFRAP/Latex/Figures/'\n",
    "sns.set_style(\"ticks\")\n",
    "rcParams['axes.linewidth'] = 0.75\n",
    "rcParams['xtick.major.width'] = 0.75\n",
    "rcParams['ytick.major.width'] = 0.75"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pylab as pl\n",
    "params = {'legend.fontsize': 9,\n",
    "          'legend.handlelength': 1}\n",
    "pl.rcParams.update(params)\n",
    "\n",
    "def nice_fig(xla, yla, xli, yli, size, fs=12): \n",
    "    rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})\n",
    "    plt.gcf().set_size_inches(size[0], size[1])\n",
    "    plt.xlabel(xla,fontsize=fs)  \n",
    "    plt.ylabel(yla,fontsize=fs)\n",
    "    plt.xlim(xli)\n",
    "    plt.ylim(yli)\n",
    "    plt.tick_params(axis='both', which='major', labelsize=fs)\n",
    "\n",
    "def save_nice_fig(name):\n",
    "    plt.savefig(name, format='pdf', dpi=300, bbox_inches='tight',\n",
    "                transparent=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "me = ['Meshes/multi_drop_gauss.xml', 'Meshes/multi_drop_gauss_med.xml',\n",
    "     'Meshes/multi_drop_gauss_far.xml', 'Meshes/multi_drop_gauss.xml',\n",
    "     'Meshes/multi_drop_gauss_med.xml', 'Meshes/multi_drop_gauss_far.xml']\n",
    "point_lists = [[[4, 4.5, 0.5], [4, 3.5, 0.5], [3.5, 4, 0.5], [4.5, 4, 0.5]],\n",
    "               [[4, 5, 0.5], [4, 3, 0.5], [3, 4, 0.5], [5, 4, 0.5]],\n",
    "               [[4, 5.5, 0.5], [4, 2.5, 0.5], [2.5, 4, 0.5], [5.5, 4, 0.5]],\n",
    "               [[4, 4.5, 0.5], [4, 3.5, 0.5], [3.5, 4, 0.5], [4.5, 4, 0.5]],\n",
    "               [[4, 5, 0.5], [4, 3, 0.5], [3, 4, 0.5], [5, 4, 0.5]],\n",
    "               [[4, 5.5, 0.5], [4, 2.5, 0.5], [2.5, 4, 0.5], [5.5, 4, 0.5]]]\n",
    "phi_tot_int = [.99, .99, .99, .9, .9, .9]\n",
    "phi_tot_ext = [.01, .01, .01, .1, .1, .1]\n",
    "G_in = [1, 1, 1, .1, .1, .1]\n",
    "G_out = [1, 1, 1, 0.99/0.9, 0.99/0.9, 0.99/0.9]\n",
    "\n",
    "f_i = []\n",
    "\n",
    "for p, m, p_i, p_e, G_i, G_o in zip(point_lists, me, phi_tot_int,\n",
    "                                    phi_tot_ext, G_in, G_out):\n",
    "    f = frap_solver([4, 4, 0.5], m, name='FRAP_multi_'+m[:-4]+str(G_i), point_list=p,\n",
    "                    T=50, phi_tot_int=p_i, phi_tot_ext=p_e, G_in=G_i, G_out=G_o)\n",
    "    f.solve_frap()\n",
    "    f_i.append(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alphas = np.linspace(0,2*np.pi, 20)\n",
    "ns = np.c_[np.cos(alphas), np.sin(alphas), np.zeros(len(alphas))]\n",
    "eps = np.linspace(0, 0.23, 100)\n",
    "profs = []\n",
    "for i in range(len(f_i)):\n",
    "#     if i>2:\n",
    "        profs.append([])\n",
    "        for j in range(50):\n",
    "            values=[]\n",
    "            fs = fem_utils.load_time_point(f_i[i].name+'t_p_'+str(j)+'.h5',\n",
    "                                           f_i[i].mesh)\n",
    "            for n in ns:\n",
    "                values.append([fs([4, 4, 0.5]+e*n) for e in eps])\n",
    "            profs[i].append(np.mean(np.transpose(values), 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('t_p.csv', profs, delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ft = f_i[1]\n",
    "meta_data = np.r_[ft.dt, ft.T, eps]\n",
    "np.savetxt('meta_data.csv', meta_data, delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(eps,np.transpose(profs)[:,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nice_fig('t [s]', 'intensity (a.u)', [0,50], [0,1.1], [1.5,2])\n",
    "plt.plot([np.mean(x)/f_i[0].phi_tot_int for x in profs[0]],\n",
    "         lw=2, label='d=0.5', ls='-')\n",
    "plt.plot([np.mean(x)/f_i[1].phi_tot_int for x in profs[1]],\n",
    "         lw=2, label='d=1', ls='--')\n",
    "plt.plot([np.mean(x)/f_i[2].phi_tot_int for x in profs[2]],\n",
    "         lw=2, label='d=1.5', ls=':')\n",
    "plt.plot(range(0, 100), np.ones(100), linestyle='--', color='k')\n",
    "plt.title('$\\Phi_{out}=0.01}$', size=12)\n",
    "plt.gca().get_yaxis().set_visible(False)\n",
    "save_nice_fig(fol+'Fig3/tot_recov_neighbours_bad.pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nice_fig('t [s]', 'intensity (a.u)', [0,30], [0,1.1], [1.5,2])\n",
    "plt.plot([np.mean(x)/f_i[3].phi_tot_int for x in profs[3]],\n",
    "         lw=2, label='d=0.5', ls='-')\n",
    "plt.plot([np.mean(x)/f_i[4].phi_tot_int for x in profs[4]],\n",
    "         lw=2, label='d=1', ls='--')\n",
    "plt.plot([np.mean(x)/f_i[5].phi_tot_int for x in profs[5]],\n",
    "         lw=2, label='d=1.5', ls=':')\n",
    "plt.plot(range(0, 100), np.ones(100), linestyle='--', color='k')\n",
    "plt.title('$\\Phi_{out}=0.1}$', size=12)\n",
    "plt.legend(prop={'size': 9}, frameon=False)\n",
    "save_nice_fig(fol+'Fig3/tot_recov_neighbours_good.pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nice_fig('x [$\\mu m$]', 'intensity (a.u)', [0,0.25], [0,1.1], [3.8,2])\n",
    "l_sim = plt.plot(eps, np.transpose(profs[0])[:,::8]/f_i[0].phi_tot_int, '#1f77b4', lw=2.5)\n",
    "plt.plot(range(0, 100), np.ones(100), linestyle='--', color='k')\n",
    "l_fit = plt.plot(np.linspace(0, 0.23, 100), np.transpose(ml)[:,::8],\n",
    "                 ls='--', c='orange', lw=1.5)\n",
    "plt.legend([l_sim[0], l_fit[0]], ['Simulation', 'Fit'], prop={'size': 9}, frameon=False)\n",
    "save_nice_fig(fol+'Fig3/spat_recov_neighbours.pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define parameters for all simulations\n",
    "point_list = [[4, 4, 0.5], [4, 4, 1.5], [4, 4, 4],\n",
    "              [4, 4, 0.5], [4, 4, 1.5], [4, 4, 4]]\n",
    "me = ['coverslip.xml', '1_5.xml', 'symmetric.xml',\n",
    "      'coverslip.xml', '1_5.xml', 'symmetric.xml']\n",
    "phi_tot_int = [.99, .99, .99, .9, .9, .9]\n",
    "phi_tot_ext = [.01, .01, .01, .1, .1, .1]\n",
    "G_in = [1, 1, 1, .1, .1, .1]\n",
    "G_out = [1, 1, 1, 0.99/0.9, 0.99/0.9, 0.99/0.9]\n",
    "f_cs = []\n",
    "\n",
    "# Zip all parameters, iterate\n",
    "for p, m, p_i, p_e, G_i, G_o in zip(point_list, me, phi_tot_int,\n",
    "                                   phi_tot_ext, G_in, G_out):\n",
    "    f_cs.append(frap_solver(p, 'Meshes/single_drop_'+m,\n",
    "                name='FRAP_'+m[:-4]+str(G_i), T=60, phi_tot_int=p_i,\n",
    "                phi_tot_ext=p_e, G_in=G_i, G_out=G_o))\n",
    "    f_cs[-1].solve_frap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "z = [0.5, 1.5, 4, 0.5, 1.5, 4]\n",
    "profs_cs = []\n",
    "for i, z_i in enumerate(z):\n",
    "    profs_cs.append([])\n",
    "    for j in range(50):\n",
    "        values=[]\n",
    "        fs = fem_utils.load_time_point(f_cs[i].name+'t_p_'+str(j)+'.h5',\n",
    "                                       f_cs[i].mesh)\n",
    "        for n in ns:\n",
    "            values.append([fs([4, 4, z_i]+e*n) for e in eps])\n",
    "        profs_cs[i].append(np.mean(np.transpose(values), 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('t_p_neighbours.csv', profs_cs[0], delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nice_fig('t [s]', '', [0,50], [0,1.1], [1.5,2])\n",
    "ls = ['-', '--', ':']\n",
    "for i, f in enumerate(f_cs[0:3]):\n",
    "    plt.plot([np.mean(x)/f.phi_tot_int for x in profs_cs[i]],\n",
    "             label='d='+str(z[i]), ls=ls[i], lw=2)\n",
    "plt.plot(range(0, 100), np.ones(100), linestyle='--', color='k')\n",
    "plt.title('$\\Phi_{out}=0.01}$', size=12)\n",
    "plt.gca().get_yaxis().set_visible(False)\n",
    "save_nice_fig(fol+'Fig3/tot_recov_cs_bad.pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nice_fig('t [s]', 'intensity (a.u)', [0,30], [0,1.1], [1.5,2])\n",
    "ls = ['-', '--', ':']\n",
    "for i, f in enumerate(f_cs[3:]):\n",
    "    plt.plot([np.mean(x)/f.phi_tot_int for x in profs_cs[i+3]],\n",
    "             label='h='+str(z[i]), lw=2, ls=ls[i])\n",
    "plt.plot(range(0, 100), np.ones(100), linestyle='--', color='k')\n",
    "plt.title('$\\Phi_{out}=0.1}$', size=12)\n",
    "plt.legend(prop={'size': 9}, frameon=False)\n",
    "save_nice_fig(fol+'Fig3/tot_recov_cs_good.pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ml_neigh = np.loadtxt('/Users/hubatsch/Desktop/DropletFRAP/matlab_fit_neigh.csv',\n",
    "                      delimiter=',')\n",
    "nice_fig('x [$\\mu m$]', 'intensity (a.u)', [0,0.25], [0,1.1], [3.8,2])\n",
    "l_sim = plt.plot(eps, np.transpose(profs_cs[0])[:,::8]/f_cs[0].phi_tot_int, '#1f77b4',\n",
    "                 lw=2.5, label='Simulation')\n",
    "plt.plot(range(0, 100), np.ones(100), linestyle='--', color='k')\n",
    "l_fit = plt.plot(np.linspace(0, 0.23, 100), np.transpose(ml_neigh)[:,::8],\n",
    "         ls='--', c='orange', lw=1.5)\n",
    "plt.legend([l_sim[0], l_fit[0]], ['Simulation', 'Fit'], frameon=False)\n",
    "save_nice_fig(fol+'Fig3/spat_recov_coverslip.pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure 1:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Panel: comparison PGL-3 diffusivity with Louise's viscosity**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "louise = pd.read_csv('/Users/hubatsch/Desktop/DropletFRAP/Latex/Figures/Fig1/Louise.csv')\n",
    "lars = pd.read_csv('/Users/hubatsch/Desktop/DropletFRAP/Latex/Figures/Fig1/Lars.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax1 = plt.subplots()\n",
    "ax2 = ax1.twinx()\n",
    "plt.sca(ax1)\n",
    "sns.lineplot(x=\"conc\", y=\"D\", data=lars, color=sns.color_palette()[1])\n",
    "sns.scatterplot(x=\"conc\", y=\"D\", data=lars, color=sns.color_palette()[1], alpha=0.7)\n",
    "plt.xlabel('$c_{salt}\\; [mM]$')\n",
    "plt.ylabel('$D_{in} \\;[\\mu m^2\\cdot s^{-1}]$', color=sns.color_palette()[1])\n",
    "plt.yticks([0, 0.05, 0.1], rotation=90, color = sns.color_palette()[1])\n",
    "plt.ylim(0, 0.1)\n",
    "ax1.set_zorder(1) \n",
    "ax1.patch.set_visible(False)\n",
    "plt.sca(ax2)\n",
    "sns.lineplot(x=\"conc\", y=\"vis\", data=louise, color=sns.color_palette()[0], label='data from ref[xxx]')\n",
    "nice_fig('c_{salt} [mM]', '$\\eta^{-1} \\;[Pa\\cdot s]^{-1}$', [40,190], [0,7.24], [2.3,2])\n",
    "plt.yticks(color = sns.color_palette()[0])\n",
    "plt.ylabel('$\\eta^{-1} \\;[Pa\\cdot s]^{-1}$ ', color = sns.color_palette()[0])\n",
    "plt.legend(frameon=False, fontsize=9)\n",
    "save_nice_fig(fol+'Fig1/Lars_vs_Louise.pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Panel:coacervates PLYS/ATP, CMD/PLYS**m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coacervates = pd.read_csv('/Users/hubatsch/Desktop/DropletFRAP/Latex/Figures/Fig1/Coacervates.csv')\n",
    "sns.stripplot(data=coacervates, jitter=0.35, alpha=0.8,**{'marker': '.', 'size': 8, 'edgecolor': 'black', 'color': 'k'})\n",
    "ax = sns.barplot(data=coacervates, facecolor=(1, 1, 1, 0), edgecolor=(0.6, 0.6, 0.6), errcolor=(0.6, 0.6, 0.6), capsize=.2, ci='sd', errwidth=1.5)\n",
    "plt.setp(ax.lines, zorder=100)\n",
    "nice_fig(None, '$D_{in} \\;[\\mu m^2\\cdot s^{-1}]$', [None, None], [0,6], [2.3,2])\n",
    "plt.xticks([0,1], ('CMD/PLYS', 'PLYS/ATP'), rotation=20)\n",
    "plt.xlim(-0.7, 1.7)\n",
    "save_nice_fig(fol+'Fig1/Coacervates.pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax.patches"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Panel: time course CMD**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "CMD = np.loadtxt(fol+'/Fig1/CMD_timecourse.csv', delimiter=',')\n",
    "CMD_fit = np.loadtxt(fol+'/Fig1/CMD_fit_timecourse.csv', delimiter=',')\n",
    "l_sim = plt.plot(CMD[:, 0], CMD[:, 1:], '#1f77b4', lw=3, label='Simulation')\n",
    "l_fit = plt.plot(CMD_fit[:, 0], CMD_fit[:, 1:], '--', lw=2, c=sns.color_palette()[1], label='Simulation')\n",
    "plt.plot(range(0, 10), np.ones(10)*np.min(CMD_fit[:, 1]), linestyle='--', color='k', lw=2)\n",
    "nice_fig('x [$\\mu m$]', 'intensity (a.u)', [0,np.max(CMD_fit[:, 0])], [0,0.6], [2.3,2])\n",
    "save_nice_fig(fol+'Fig1/CMD_spat_recov.pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Panel: time course PGL-3**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PGL = np.loadtxt(fol+'/Fig1/PGL_timecourse.csv', delimiter=',')\n",
    "PGL_fit = np.loadtxt(fol+'/Fig1/PGL_fit_timecourse.csv', delimiter=',')\n",
    "l_sim = plt.plot(PGL[:, 0], PGL[:, 1:], '#1f77b4', lw=3, label='Simulation')\n",
    "l_fit = plt.plot(PGL_fit[:, 0], PGL_fit[:, 1:], '--', lw=2, c=sns.color_palette()[1], label='Simulation')\n",
    "plt.plot(range(0, 10), np.ones(10)*np.min(PGL_fit[:, 1]), linestyle='--', color='k', lw=2)\n",
    "nice_fig('x [$\\mu m$]', 'intensity (a.u)', [0,np.max(PGL_fit[:, 0])], [0,None], [2.3,2])\n",
    "save_nice_fig(fol+'Fig1/PGL_spat_recov.pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Panel: time course total intensity**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PGL = np.loadtxt(fol+'/Fig1/PGL_tot.csv', delimiter=',')\n",
    "ATP = np.loadtxt(fol+'/Fig1/ATP_tot.csv', delimiter=',')\n",
    "CMD = np.loadtxt(fol+'/Fig1/CMD_tot.csv', delimiter=',')\n",
    "# fig, ax1 = plt.subplots()\n",
    "# ax2 = ax1.twiny()\n",
    "# plt.sca(ax1)\n",
    "nice_fig('$t/T_{max}$', 'intensity (a.u)', [0,200], [0,0.62], [2.3,2])\n",
    "# plt.sca(ax2)\n",
    "# ax2.tick_params(axis=\"x\",direction=\"in\")\n",
    "plt.plot(PGL[::10, 0]/np.max(PGL[1:-1:2, 0]), PGL[::10,1], label='PGL-3', c='#CC406E', markersize=3, alpha=0.7, lw=2)\n",
    "plt.plot(ATP[::1, 0]/np.max(ATP[:, 0]), ATP[::1,1], label='PLYS/ATP', c='#FF508A', markersize=3, alpha=0.7, lw=2)\n",
    "plt.plot(CMD[::5, 0]/np.max(CMD[:, 0]), CMD[::5,1], label='CMD/PLYS', c='#7F2845', markersize=3, alpha=0.7, lw=2)\n",
    "plt.legend(frameon=False, fontsize=9)\n",
    "plt.xlim(0, 1)\n",
    "save_nice_fig(fol+'Fig1/tot_recov.pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}