{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Image Cleaning and Dilation\n",
    "\n",
    "Here we create an example shower image, do a tail-cuts (picture/boundary) cleaning, and then dilate the resulting cleaning mask by several neighbor pixels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt\n",
    "import astropy.units as u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ctapipe.instrument import CameraGeometry\n",
    "from ctapipe.visualization import CameraDisplay\n",
    "from ctapipe.image import toymodel, tailcuts_clean, dilate\n",
    "\n",
    "# Load the camera\n",
    "geom = CameraGeometry.from_name(\"NectarCam\")\n",
    "\n",
    "# Create a fake camera image to display:\n",
    "model = toymodel.Gaussian(\n",
    "    x=0.2 * u.m,\n",
    "    y=0.0 * u.m,\n",
    "    width=0.05 * u.m,\n",
    "    length=0.15 * u.m,\n",
    "    psi='35d',\n",
    ")\n",
    "\n",
    "image, sig, bg = model.generate_image(\n",
    "    geom, intensity=1500, nsb_level_pe=5\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Apply the image cleaning:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cleanmask = tailcuts_clean(geom, image, picture_thresh=10,\n",
    "                           boundary_thresh=5)\n",
    "clean = image.copy()\n",
    "clean[~cleanmask] = 0.0\n",
    "\n",
    "disp = CameraDisplay(geom, image=image)\n",
    "disp.highlight_pixels(cleanmask, color='red')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now dialte the mask a few times:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ctapipe.image.cleaning import dilate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_dilate(mask, times=1):\n",
    "    m = mask.copy()\n",
    "    for ii in range(times):\n",
    "        m = dilate(geom, m)\n",
    "    CameraDisplay(geom,image=(m.astype(int)+mask.astype(int)), title='dilate{}'.format(times))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(18,3))\n",
    "\n",
    "for ii in range(0,6):\n",
    "    plt.subplot(1,7,ii+1)\n",
    "    show_dilate(cleanmask.copy(), times=ii)"
   ]
  }
 ],
 "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
