{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Warnings when exporting hdf with RDKit Mol objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import pandas as pd\n",
    "import rdkit\n",
    "from rdkit import Chem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python: 3.6.6 |Anaconda, Inc.| (default, Oct  9 2018, 12:34:16) \n",
      "[GCC 7.3.0]\n",
      "pandas: 0.23.4\n",
      "rdkit: 2018.09.1\n"
     ]
    }
   ],
   "source": [
    "print (f\"python: {sys.version}\")\n",
    "print(f\"pandas: {pd.__version__}\")\n",
    "print(f\"rdkit: {rdkit.__version__}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## With a column with Smiles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row 0: mol is of type: <class 'str'>\n",
      "row 1: mol is of type: <class 'str'>\n",
      "row 2: mol is of type: <class 'str'>\n",
      "row 3: mol is of type: <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({'mol': ['C1CCCCC1', 'FC1CCCCC1', 'OC1CCCCC1', 'NC1CCCCC1'],\n",
    "                   })\n",
    "for id, row in df.iterrows():\n",
    "    print(f'row {id}: mol is of type: {type(row[\"mol\"])}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['mol'].to_hdf('/tmp/test.hdf', key='test')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## With a column with RDKit molecules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row 0: mol is of type: <class 'rdkit.Chem.rdchem.Mol'>\n",
      "row 1: mol is of type: <class 'rdkit.Chem.rdchem.Mol'>\n",
      "row 2: mol is of type: <class 'rdkit.Chem.rdchem.Mol'>\n",
      "row 3: mol is of type: <class 'rdkit.Chem.rdchem.Mol'>\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({'mol': ['C1CCCCC1', 'FC1CCCCC1', 'OC1CCCCC1', 'NC1CCCCC1'],\n",
    "                   })\n",
    "df['mol'] = df['mol'].map(Chem.MolFromSmiles)\n",
    "\n",
    "for id, row in df.iterrows():\n",
    "    print(f'row {id}: mol is of type: {type(row[\"mol\"])}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/gally/Software/anaconda3/envs/npfc/lib/python3.6/site-packages/pandas/core/generic.py:1996: PerformanceWarning: \n",
      "your performance may suffer as PyTables will pickle object types that it cannot\n",
      "map directly to c-types [inferred_type->mixed,key->values] [items->None]\n",
      "\n",
      "  return pytables.to_hdf(path_or_buf, key, self, **kwargs)\n"
     ]
    }
   ],
   "source": [
    "df['mol'].to_hdf('/tmp/test.hdf', key='test')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using table format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['mol'].to_hdf('/tmp/test.hdf', key='test', type='table')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exporting RWMol object instead of Mol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = df.copy()\n",
    "df2['mol'] = df2['mol'].map(Chem.RWMol)\n",
    "df2['mol'].to_hdf('/tmp/test.hdf', key='test')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# When shutting down the Pandas warning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore', category=pd.io.pytables.PerformanceWarning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['mol'].to_hdf('/tmp/test.hdf', key='test')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But warnings still appear during automated tests with pytest..."
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
