{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Summary\n",
    "\n",
    "`# TODO: summary here`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-22T23:49:49.402915Z",
     "start_time": "2020-11-22T23:49:49.378902Z"
    }
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-26T20:50:05.646342Z",
     "start_time": "2020-11-26T20:49:59.214415Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hmamin/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "/Users/hmamin/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "/Users/hmamin/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__\n",
      "  return f(*args, **kwds)\n",
      "/Users/hmamin/anaconda3/lib/python3.7/site-packages/thinc/neural/_custom_kernels.py:36: ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/hmamin/anaconda3/lib/python3.7/site-packages/thinc/neural/_custom_kernels.cu' mode='r' encoding='utf8'>\n",
      "  SRC = (PWD / \"_custom_kernels.cu\").open(\"r\", encoding=\"utf8\").read()\n",
      "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n",
      "/Users/hmamin/anaconda3/lib/python3.7/site-packages/thinc/neural/_custom_kernels.py:39: ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/hmamin/anaconda3/lib/python3.7/site-packages/thinc/neural/_murmur3.cu' mode='r' encoding='utf8'>\n",
      "  MMH_SRC = (PWD / \"_murmur3.cu\").open(\"r\", encoding=\"utf8\").read()\n",
      "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n"
     ]
    }
   ],
   "source": [
    "import gc\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import os\n",
    "import pandas as pd\n",
    "from pathlib import Path\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "from htools import *\n",
    "from incendio.nlp import FillMaskTransform, GenerativeTransform, \\\n",
    "    ParaphraseTransform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-22T23:49:57.351271Z",
     "start_time": "2020-11-22T23:49:57.344866Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Current directory: /Users/hmamin/incendio\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hmamin/anaconda3/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n",
      "  and should_run_async(code)\n"
     ]
    }
   ],
   "source": [
    "cd_root()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-26T20:50:05.720972Z",
     "start_time": "2020-11-26T20:50:05.649166Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hmamin/anaconda3/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n",
      "  and should_run_async(code)\n"
     ]
    }
   ],
   "source": [
    "TRANSFORMS = {\n",
    "    'fillmask': FillMaskTransform,\n",
    "    'paraphrase': ParaphraseTransform,\n",
    "    'generative': GenerativeTransform\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-22T23:50:09.356034Z",
     "start_time": "2020-11-22T23:50:02.220289Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of RobertaForMaskedLM were not initialized from the model checkpoint at distilroberta-base and are newly initialized: ['lm_head.decoder.bias']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
     ]
    }
   ],
   "source": [
    "fm_tfm = TRANSFORMS['fillmask'](n=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-26T20:50:07.792524Z",
     "start_time": "2020-11-26T20:50:07.783291Z"
    }
   },
   "outputs": [],
   "source": [
    "@immutify_defaults\n",
    "def generate(transform, source, dest=None, n=5, text_col='text', id_cols=(),\n",
    "             nrows=None, tfm_kwargs={}, call_kwargs={}):\n",
    "    # Use list args like --id_cols '[col1, col2]'\n",
    "    # Use dict args like --call_kwargs '{\"drop_pct\": .2, \"min_keep\": 4}'\n",
    "\n",
    "    # Load data.\n",
    "    if isinstance(source, (str, Path)):\n",
    "        df = pd.read_csv(Path(source), usecols=[text_col] + list(id_cols), \n",
    "                         nrows=nrows)\n",
    "    elif isinstance(source, pd.DataFrame):\n",
    "        df = source\n",
    "    else:\n",
    "        raise TypeError('`source` must be a str/Path or pd.DataFrame.')\n",
    "        \n",
    "    # Prepare for output file if necessary.\n",
    "    if isinstance(dest, (str, Path)):\n",
    "        dest = Path(dest)\n",
    "        os.makedirs(dest.parent, exist_ok=True)\n",
    "    elif dest is not None:\n",
    "        raise ValueError('`dest` must be a str/Path containing the output '\n",
    "                         'file name to create, or None if you just want to '\n",
    "                         'return a df.')\n",
    "\n",
    "    # For simplicity, we stick to one transform at a time. Slow to load so at\n",
    "    # least for now, let user pass in the transform itself.\n",
    "    transform = TRANSFORMS[transform](n=n, **tfm_kwargs) \\\n",
    "        if isinstance(transform, str) else transform\n",
    "    \n",
    "    # Generate new variations of input text.\n",
    "    call_kwargs['flat'] = True\n",
    "    res = transform(df[text_col].tolist(), **call_kwargs)\n",
    "    res = pd.DataFrame(res, columns=[text_col])\n",
    "    \n",
    "    # Attach identifier columns to output (e.g. we usually want to store \n",
    "    # labels and or sample IDs. Most of our augmentation methods make \n",
    "    # relatively minor changes to the input so all variations of 1 input \n",
    "    # should remain in the same set, usually training.).\n",
    "    if id_cols:\n",
    "        df_id = pd.concat([df[col].repeat(res.shape[0] // df.shape[0])\n",
    "                           for col in id_cols], axis=1).reset_index(drop=True)\n",
    "        res = pd.concat([df_id, res], axis=1)\n",
    "        \n",
    "    # Optionally save output.\n",
    "    if dest: res.to_csv(dest, index=False)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-23T00:28:51.351599Z",
     "start_time": "2020-11-23T00:28:14.848218Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hmamin/anaconda3/lib/python3.7/socket.py:660: ResourceWarning: unclosed <socket.socket fd=66, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.119', 60157), raddr=('52.217.48.254', 443)>\n",
      "  self._sock = None\n",
      "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n",
      "/Users/hmamin/anaconda3/lib/python3.7/socket.py:660: ResourceWarning: unclosed <socket.socket fd=66, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.119', 60160), raddr=('52.217.48.254', 443)>\n",
      "  self._sock = None\n",
      "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n"
     ]
    }
   ],
   "source": [
    "# res = generate(fm_tfm, \n",
    "#                '/Users/hmamin/msan631/hw3_sentence_pairs/data/train.csv', \n",
    "#                'data/tmp.csv',\n",
    "#                n=3, text_col='question1',\n",
    "#                id_cols=['id', 'qid1'], \n",
    "#                nrows=5,\n",
    "#                call_kwargs={'errors': 'warn', 'n_mask': 4,\n",
    "#                             'strategy': 'random'})\n",
    "\n",
    "res = generate('paraphrase', \n",
    "               '/Users/hmamin/msan631/hw3_sentence_pairs/data/train.csv', \n",
    "               'data/tmp.csv',\n",
    "               n=2, text_col='question1',\n",
    "               id_cols=['id', 'qid1'], \n",
    "               nrows=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-23T00:28:55.107592Z",
     "start_time": "2020-11-23T00:28:55.095150Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['What are the steps to invest in the share market in India?',\n",
       "       'There is a guide to investing in the share market in India.',\n",
       "       'What is the story of the diamond?',\n",
       "       'What is the story of the Diamond?',\n",
       "       'How can I increase the speed of my internet connection?',\n",
       "       'What can I do to increase the speed of my internet connection?'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res.question1.values"
   ]
  }
 ],
 "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
