<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_ = '/mnt/archive/Antonio/data/manta/smdata/2017-04-30/'\n",
    "fname = dir_ + '03_10e4x_D700uW_A700uW_inplace.hdf5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fname = Path(fname)\n",
    "assert fname.is_file()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlabel = '{}_{}'.format(fname.parts[-2], fname.stem[:2])\n",
    "mlabel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pathlib import Path\n",
    "import numpy as np\n",
    "from IPython.display import display\n",
    "from tqdm import tqdm_notebook, tnrange\n",
    "import pandas as pd\n",
    "\n",
    "%config InlineBackend.figure_format = 'retina'\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "#sns.set_context('notebook', font_scale=1)\n",
    "\n",
    "import phconvert as phc\n",
    "phc.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fretbursts import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns = init_notebook(fs=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.options.display.max_columns = 48\n",
    "pd.options.display.max_rows = 48\n",
    "\n",
    "def make_df_bursts(list_of_columns):\n",
    "    ncols = 48\n",
    "    assert len(list_of_columns) == ncols\n",
    "    nrows = max(len(x) for x in list_of_columns)\n",
    "    columns = np.arange(ncols)\n",
    "    df = pd.DataFrame(columns=columns, index=np.arange(nrows), dtype=float)\n",
    "    df.columns.name = 'spot'\n",
    "    for col, col_data in zip(columns, list_of_columns):\n",
    "        df.iloc[:len(col_data), col] = col_data\n",
    "    return df\n",
    "\n",
    "def make_df_spots(list_of_tuples=None):\n",
    "    nrows = 48\n",
    "    df = pd.DataFrame(index=np.arange(nrows))\n",
    "    if list_of_tuples is None:\n",
    "        list_of_tuples = []\n",
    "    for col, col_data in list_of_tuples:\n",
    "        df[col] = col_data\n",
    "    return df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = loader.photon_hdf5(str(fname), ondisk=True)  # We have plenty of memory on the server\n",
    "fname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.description"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Laser power:  %d mW @ 532    %d mW @ 628' % \n",
    "      tuple(d.setup['excitation_input_powers']*1e3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated and 'A_ON' not in d:\n",
    "    d.add(A_ON=(100, 1950), D_ON=(2150, 4000), offset=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.alex_period, d.offset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    bpl.plot_alternation_hist(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    loader.alex_apply_period(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.acquisition_duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.calc_bg_cache(bg.exp_fit, time_s=10, tail_min_us='auto', F_bg=1.7)\n",
    "d.calc_bg_cache(bg.exp_fit, time_s=5, tail_min_us='auto', F_bg=1.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "num_time_points = 5\n",
    "\n",
    "# Timepoints equally distributed along the measurement\n",
    "time_points = np.round(np.linspace(d.time_min+1, d.time_max-2, num=num_time_points))\n",
    "\n",
    "for i in time_points:\n",
    "    dplot(d, timetrace, tmin=i, tmax=i+1, figsize=(24, 8));\n",
    "    plt.ylim(-100, 100)\n",
    "    #savefig(\"%s_timetrace_t=%d-%d.png\" % (mlabel, i, i+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot(d, timetrace_bg, show_da=d.alternated);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bg_AexDem = d.bg_from(Ph_sel(Aex='Dem'))\n",
    "bg_AexAem = d.bg_from(Ph_sel(Aex='Aem'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 2, figsize=(12, 4), sharex=True, sharey=True)\n",
    "fig.suptitle(\"BG A-excitation period\")\n",
    "ax[0].plot(np.array(bg_AexDem).T, color='g', alpha=0.5);\n",
    "ax[1].plot(np.array(bg_AexAem).T, color='r', alpha=0.5);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bg_DexDem = d.bg_from(Ph_sel(Dex='Dem'))\n",
    "bg_DexAem = d.bg_from(Ph_sel(Dex='Aem'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 2, figsize=(12, 4), sharex=True, sharey=True)\n",
    "fig.suptitle(\"BG D-excitation period\")\n",
    "ax[0].plot(np.array(bg_DexDem).T, color='g', alpha=0.5);\n",
    "ax[1].plot(np.array(bg_DexAem).T, color='r', alpha=0.5);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.burst_search(min_rate_cps=50e3, pax=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.calc_max_rate(m=10, ph_sel=Ph_sel('all'))\n",
    "\n",
    "phrate_DAexB = make_df_bursts(d.max_rate)\n",
    "#display(phrate_DAexB.sample(10))\n",
    "phrate_DAex = (make_df_spots()\n",
    "                .assign(**{'num_bursts': d.num_bursts})\n",
    "                .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "              )\n",
    "phrate_DAex.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    d.calc_max_rate(m=10, ph_sel=Ph_sel(Dex='DAem'), compact=True)\n",
    "\n",
    "    phrate_DexB = make_df_bursts(d.max_rate)\n",
    "    #display(phrate_DexB.sample(10))\n",
    "    phrate_Dex = (make_df_spots()\n",
    "                    .assign(**{'num_bursts': d.num_bursts})\n",
    "                    .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                    .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                    .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "                 )\n",
    "    phrate_Dex.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    d.calc_max_rate(m=10, ph_sel=Ph_sel(Aex='DAem'), compact=True)\n",
    "\n",
    "    phrate_AexB = make_df_bursts(d.max_rate)\n",
    "    #display(phrate_AexB.sample(10))\n",
    "    phrate_Aex = (make_df_spots()\n",
    "                    .assign(**{'num_bursts': d.num_bursts})\n",
    "                    .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                    .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                    .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "                 )\n",
    "    phrate_Aex.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    d.calc_max_rate(m=10, ph_sel=Ph_sel(Aex='Aem'), compact=True)\n",
    "\n",
    "    phrate_AexAemB = make_df_bursts(d.max_rate)\n",
    "    #display(phrate_AexB.sample(10))\n",
    "    phrate_AexAem = (make_df_spots()\n",
    "                    .assign(**{'num_bursts': d.num_bursts})\n",
    "                    .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                    .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                    .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "                 )\n",
    "    phrate_AexAem.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    d.calc_max_rate(m=10, ph_sel=Ph_sel(Aex='Dem'), compact=True)\n",
    "\n",
    "    phrate_AexDemB = make_df_bursts(d.max_rate)\n",
    "    #display(phrate_AexB.sample(10))\n",
    "    phrate_AexDem = (make_df_spots()\n",
    "                    .assign(**{'num_bursts': d.num_bursts})\n",
    "                    .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                    .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                    .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "                 )\n",
    "    phrate_AexDem.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot_48ch(phrate_DexB, hist_burst_phrate);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    dplot_48ch(phrate_AexB, hist_burst_phrate);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    dplot_48ch(phrate_AexDemB, hist_burst_phrate);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    dplot_48ch(phrate_AexAemB, hist_burst_phrate);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot(d, hist_size);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "th1 = 40\n",
    "ds = d.select_bursts(select_bursts.size, th1=th1, gamma=0.5, add_aex=True)\n",
    "\n",
    "Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(ds.nd, ds.na, ds.naa)]\n",
    "ds.add(Su=Su)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "dplot(ds, hist_fret, pdf=False, figsize=(18, 6));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not d.alternated:\n",
    "    stop_here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot(ds, hist_S, pdf=False, figsize=(18, 6));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot(ds, hist_burst_data, data_name='Su', pdf=False, figsize=(18, 6));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kws = dict(bins=np.arange(-0.1, 2.1, 0.025), histtype='step', lw=1.5)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.hist(ds.S[i], label='CH%d' % i, **kws);\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kws = dict(bins=np.arange(-0.1, 2.1, 0.025), histtype='step', lw=1.5)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.hist(ds.Su[i], label='CH%d' % i, **kws);\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(ds, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws)\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(ds, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws, S_name='Su')\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d2 = bext.burst_search_and_gate(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "th1 = 40\n",
    "ds2 = d2.select_bursts(select_bursts.size, th1=th1, gamma=0.5, add_aex=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(ds2.nd, ds2.na, ds2.naa)]\n",
    "ds2.add(Su=Su)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(ds2, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws)\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(ds2, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws, S_name='Su')\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dsaa = d.select_bursts(select_bursts.naa, th1=15)\n",
    "Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dsaa.nd, dsaa.na, dsaa.naa)]\n",
    "dsaa.add(Su=Su)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(dsaa, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws, S_name='Su')\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "hide_input": false,
  "kernelspec": {
   "display_name": "py36",
   "language": "python",
   "name": "py36"
  },
  "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.6.0"
  },
  "nav_menu": {},
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "68px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": true,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
<commit_msg>Notebook: add print<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_ = '/mnt/archive/Antonio/data/manta/smdata/2017-04-30/'\n",
    "fname = dir_ + '03_10e4x_D700uW_A700uW_inplace.hdf5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fname = Path(fname)\n",
    "assert fname.is_file()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mlabel = '{}_{}'.format(fname.parts[-2], fname.stem[:2])\n",
    "mlabel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pathlib import Path\n",
    "import numpy as np\n",
    "from IPython.display import display\n",
    "from tqdm import tqdm_notebook, tnrange\n",
    "import pandas as pd\n",
    "\n",
    "%config InlineBackend.figure_format = 'retina'\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "#sns.set_context('notebook', font_scale=1)\n",
    "\n",
    "import phconvert as phc\n",
    "phc.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fretbursts import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns = init_notebook(fs=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.options.display.max_columns = 48\n",
    "pd.options.display.max_rows = 48\n",
    "\n",
    "def make_df_bursts(list_of_columns):\n",
    "    ncols = 48\n",
    "    assert len(list_of_columns) == ncols\n",
    "    nrows = max(len(x) for x in list_of_columns)\n",
    "    columns = np.arange(ncols)\n",
    "    df = pd.DataFrame(columns=columns, index=np.arange(nrows), dtype=float)\n",
    "    df.columns.name = 'spot'\n",
    "    for col, col_data in zip(columns, list_of_columns):\n",
    "        df.iloc[:len(col_data), col] = col_data\n",
    "    return df\n",
    "\n",
    "def make_df_spots(list_of_tuples=None):\n",
    "    nrows = 48\n",
    "    df = pd.DataFrame(index=np.arange(nrows))\n",
    "    if list_of_tuples is None:\n",
    "        list_of_tuples = []\n",
    "    for col, col_data in list_of_tuples:\n",
    "        df[col] = col_data\n",
    "    return df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = loader.photon_hdf5(str(fname), ondisk=True)  # We have plenty of memory on the server\n",
    "fname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(d.description.decode())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Laser power:  %d mW @ 532    %d mW @ 628' % \n",
    "      tuple(d.setup['excitation_input_powers']*1e3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated and 'A_ON' not in d:\n",
    "    d.add(A_ON=(100, 1950), D_ON=(2150, 4000), offset=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.alex_period, d.offset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    bpl.plot_alternation_hist(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    loader.alex_apply_period(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.acquisition_duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.calc_bg_cache(bg.exp_fit, time_s=10, tail_min_us='auto', F_bg=1.7)\n",
    "d.calc_bg_cache(bg.exp_fit, time_s=5, tail_min_us='auto', F_bg=1.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "num_time_points = 5\n",
    "\n",
    "# Timepoints equally distributed along the measurement\n",
    "time_points = np.round(np.linspace(d.time_min+1, d.time_max-2, num=num_time_points))\n",
    "\n",
    "for i in time_points:\n",
    "    dplot(d, timetrace, tmin=i, tmax=i+1, figsize=(24, 8));\n",
    "    plt.ylim(-100, 100)\n",
    "    #savefig(\"%s_timetrace_t=%d-%d.png\" % (mlabel, i, i+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot(d, timetrace_bg, show_da=d.alternated);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bg_AexDem = d.bg_from(Ph_sel(Aex='Dem'))\n",
    "bg_AexAem = d.bg_from(Ph_sel(Aex='Aem'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 2, figsize=(12, 4), sharex=True, sharey=True)\n",
    "fig.suptitle(\"BG A-excitation period\")\n",
    "ax[0].plot(np.array(bg_AexDem).T, color='g', alpha=0.5);\n",
    "ax[1].plot(np.array(bg_AexAem).T, color='r', alpha=0.5);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bg_DexDem = d.bg_from(Ph_sel(Dex='Dem'))\n",
    "bg_DexAem = d.bg_from(Ph_sel(Dex='Aem'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 2, figsize=(12, 4), sharex=True, sharey=True)\n",
    "fig.suptitle(\"BG D-excitation period\")\n",
    "ax[0].plot(np.array(bg_DexDem).T, color='g', alpha=0.5);\n",
    "ax[1].plot(np.array(bg_DexAem).T, color='r', alpha=0.5);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.burst_search(min_rate_cps=50e3, pax=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.calc_max_rate(m=10, ph_sel=Ph_sel('all'))\n",
    "\n",
    "phrate_DAexB = make_df_bursts(d.max_rate)\n",
    "#display(phrate_DAexB.sample(10))\n",
    "phrate_DAex = (make_df_spots()\n",
    "                .assign(**{'num_bursts': d.num_bursts})\n",
    "                .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "              )\n",
    "phrate_DAex.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    d.calc_max_rate(m=10, ph_sel=Ph_sel(Dex='DAem'), compact=True)\n",
    "\n",
    "    phrate_DexB = make_df_bursts(d.max_rate)\n",
    "    #display(phrate_DexB.sample(10))\n",
    "    phrate_Dex = (make_df_spots()\n",
    "                    .assign(**{'num_bursts': d.num_bursts})\n",
    "                    .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                    .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                    .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "                 )\n",
    "    phrate_Dex.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    d.calc_max_rate(m=10, ph_sel=Ph_sel(Aex='DAem'), compact=True)\n",
    "\n",
    "    phrate_AexB = make_df_bursts(d.max_rate)\n",
    "    #display(phrate_AexB.sample(10))\n",
    "    phrate_Aex = (make_df_spots()\n",
    "                    .assign(**{'num_bursts': d.num_bursts})\n",
    "                    .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                    .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                    .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "                 )\n",
    "    phrate_Aex.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    d.calc_max_rate(m=10, ph_sel=Ph_sel(Aex='Aem'), compact=True)\n",
    "\n",
    "    phrate_AexAemB = make_df_bursts(d.max_rate)\n",
    "    #display(phrate_AexB.sample(10))\n",
    "    phrate_AexAem = (make_df_spots()\n",
    "                    .assign(**{'num_bursts': d.num_bursts})\n",
    "                    .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                    .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                    .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "                 )\n",
    "    phrate_AexAem.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    d.calc_max_rate(m=10, ph_sel=Ph_sel(Aex='Dem'), compact=True)\n",
    "\n",
    "    phrate_AexDemB = make_df_bursts(d.max_rate)\n",
    "    #display(phrate_AexB.sample(10))\n",
    "    phrate_AexDem = (make_df_spots()\n",
    "                    .assign(**{'num_bursts': d.num_bursts})\n",
    "                    .assign(**{'num_nans': [np.isnan(x).sum() for x in d.max_rate]})\n",
    "                    .assign(**{'num_valid': lambda x: x.num_bursts - x.num_nans})\n",
    "                    .assign(**{'valid_fraction': lambda x: 100 * x.num_valid / x.num_bursts})\n",
    "                 )\n",
    "    phrate_AexDem.valid_fraction.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot_48ch(phrate_DexB, hist_burst_phrate);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    dplot_48ch(phrate_AexB, hist_burst_phrate);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    dplot_48ch(phrate_AexDemB, hist_burst_phrate);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if d.alternated:\n",
    "    dplot_48ch(phrate_AexAemB, hist_burst_phrate);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot(d, hist_size);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "th1 = 40\n",
    "ds = d.select_bursts(select_bursts.size, th1=th1, gamma=0.5, add_aex=True)\n",
    "\n",
    "Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(ds.nd, ds.na, ds.naa)]\n",
    "ds.add(Su=Su)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "dplot(ds, hist_fret, pdf=False, figsize=(18, 6));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not d.alternated:\n",
    "    stop_here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot(ds, hist_S, pdf=False, figsize=(18, 6));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dplot(ds, hist_burst_data, data_name='Su', pdf=False, figsize=(18, 6));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kws = dict(bins=np.arange(-0.1, 2.1, 0.025), histtype='step', lw=1.5)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.hist(ds.S[i], label='CH%d' % i, **kws);\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kws = dict(bins=np.arange(-0.1, 2.1, 0.025), histtype='step', lw=1.5)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.hist(ds.Su[i], label='CH%d' % i, **kws);\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(ds, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws)\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(ds, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws, S_name='Su')\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d2 = bext.burst_search_and_gate(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "th1 = 40\n",
    "ds2 = d2.select_bursts(select_bursts.size, th1=th1, gamma=0.5, add_aex=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(ds2.nd, ds2.na, ds2.naa)]\n",
    "ds2.add(Su=Su)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(ds2, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws)\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(ds2, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws, S_name='Su')\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dsaa = d.select_bursts(select_bursts.naa, th1=15)\n",
    "Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dsaa.nd, dsaa.na, dsaa.naa)]\n",
    "dsaa.add(Su=Su)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Smin, Smax = 0, 1\n",
    "jkws = dict(extent=(-0.2 + Smin, Smax + 0.2, -0.2 + Smin, Smax + 0.2), gridsize=80*(Smax-Smin))\n",
    "mkws = dict(bins=np.arange(-0.2 + Smin, Smax + 0.2, 0.03), show_kde=False)\n",
    "for i in (17, 18, 29, 30):\n",
    "    plt.figure()\n",
    "    g = alex_jointplot(dsaa, i=i, cmap='viridis', vmax_fret=False, \n",
    "                       joint_kws=jkws, marginal_kws=mkws, S_name='Su')\n",
    "    g.ax_joint.set_ylim(-0.2, 1.2)\n",
    "    g.ax_joint.set_xlim(-0.2, 1.2)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "hide_input": false,
  "kernelspec": {
   "display_name": "py36",
   "language": "python",
   "name": "py36"
  },
  "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.6.0"
  },
  "nav_menu": {},
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "68px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": true,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}

{'fraction_non_alphanumeric': 0.22632919624952577, 'fraction_numerical': 0.022708796271204812, 'mean_word_length': 2.486019270734933, 'pattern_counts': {'":': 606, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6176800', 'n_tokens_mistral': 16278, 'n_tokens_neox': 15471, 'n_words': 2398}