{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Display utility for 2D FTICR Spectra\n",
    "\n",
    "*This little utility allows to interactively explore large 2D FTICR-MS datasets.*\n",
    "\n",
    "You find here a simple interface the reads and displays the multiresolution 2D files created by `SPIKE` when processing 2D data-sets (usually called `xxx_mr.msh5`).\n",
    "\n",
    "It is based on the capabilities of both the `SPIKE` library and the `jupyter notebook` interface.\n",
    "Thanks to the technology behind, these extremely large files can be accessed rapidly, even on a laptop computer.\n",
    "\n",
    "This program supersedes the old `Visu2D` program, developped in `Qt`, which could no longer be maintained.\\ables\\\n",
    "In addition to a regular anaconda python 3 distribution, it requires `pytables` and `ipympl` - to be installed with conda.\n",
    "\n",
    "\n",
    "*This is a work inprogress - additional utilities should come soon !*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## To use it, \n",
    "- Select the executable cell below (marked with the `In[x]`) and run it by cliking on the `▶Run` icon on the top of the window (the python code should be hidden after this).\n",
    "- select the file you want look at, and Load, it will show-up as a full width 2D image.\n",
    "*ignore eventual warnings about missing attributes*\n",
    "    - the F2/horizontal axis is the high resolution, direct axis. You find fragments along this line\n",
    "    - the F1/vertical axis is the low resolution, indirect axis. You have parents along this axis. \n",
    "- with the zoom tool (the square below the spectrum) you can select the region you want to display, you can also dial it in on the top box.\n",
    "- with the `scale` slider, you can select the display level.\n",
    "*Think to this as an archipelago viewed on the map; the slider changes the sea level, raising the scale raises the floor (or lower the sea level)*\n",
    "- The data have a hierarchical multiresolution structure. Zooming does not change the resolution, to force it, either click on the `Redraw` button, or change the scale.\n",
    "The smaller the zoom box, the better the resolution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-25T17:25:21.456649Z",
     "start_time": "2019-11-25T17:25:18.960977Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "## STARTING Environment..."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    ========================\n",
      "          SPIKE\n",
      "    ========================\n",
      "    Version     : 0.99.14\n",
      "    Date        : 29-10-2019\n",
      "    Revision Id : 440\n",
      "    ========================\n",
      "*** bokeh_display not loaded ***\n",
      "*** wavelet not loaded ***\n",
      "*** zoom3D not loaded ***\n",
      "plugins loaded:\n",
      "Bruker_NMR_FT,  Bucketing,  FTMS_calib,  Fitter,  Integrate,  Linear_prediction,  PALMA,  Peaks,  apmin,  bcorr,  fastclean,  gaussenh,  pg_sane,  rem_ridge,  sane,  sg,  test,  urQRd, \n",
      "\n",
      "spike.plugins.report() for a short description of each plugins\n",
      "spike.plugins.report('module_name') for complete documentation on one plugin\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "## ... program is ready"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "<style>hr {height: 2px; border: 0;border-top: 1px solid #ccc;margin: 1em 0;padding: 0; }</style>\n",
       "<script>\n",
       "code_show=true; \n",
       "function code_toggle() { if (code_show){ $('div.input').hide(); } else { $('div.input').show(); } code_show = !code_show } \n",
       "$( document ).ready(code_toggle);\n",
       "</script>\n",
       "<form action=\"javascript:code_toggle()\">\n",
       "<input type=\"submit\" style=\"border:1px solid black; background-color:#DDD\" value=\"hide/show the python code.\">\n",
       "\n",
       "</form>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1f021720b98a49f68521591540f38d55",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Output(), Tab(children=(Output(), Output(), Output()), _titles={'0': '2D Display', '1': '1D ext…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### Initialization of the environment\n",
    "### the following cell should be run only once *(but no harm if you run it twice)* .\n",
    "\n",
    "from IPython.display import display, HTML, Markdown, Image\n",
    "display(Markdown('## STARTING Environment...'))\n",
    "%matplotlib widget\n",
    "import spike\n",
    "from spike.Interactive.INTER import hidecode\n",
    "import spike.Interactive.FTICR_INTER_v2 as IF2\n",
    "display(Markdown('## ... program is ready'))\n",
    "from importlib import reload  # the two following lines are debugging help\n",
    "reload(IF2)                   # and can be removed safely when in production\n",
    "hidecode(initial='hide', message=False)\n",
    "ms = IF2.MS2Dscene(root='/DATA')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### to come\n",
    "- calibration\n",
    "- peak detection\n",
    "- superimposition\n",
    "- extraction of arbitrary 1D \n",
    "- locate/remove artifacts due to harmonics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "with ms.out2D:\n",
    "    plt.plot([1,2,3],[2,4,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with ms.out2D:\n",
    "    IF2.MR_interact(ms.FC.selected,  show=True, figsize=(8,8), Debug=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IF2.MR_interact(ms.FC.selected,  show=True, figsize=(8,8), Debug=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "encoding": "# -*- coding: utf-8 -*-",
   "formats": "ipynb,py:percent"
  },
  "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
}
