{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Install RDKit"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!conda install -c rdkit rdkit -y","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Imports"},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport rdkit","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/bms-molecular-translation/train_labels.csv\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Draw molecules"},{"metadata":{"trusted":true},"cell_type":"code","source":"for _, row in df.head(5).iterrows():\n    # read original image from dataset\n    img_id = row[\"image_id\"]\n    img = cv2.imread(\"../input/bms-molecular-translation/train/{}/{}/{}/{}.png\".format(img_id[0], img_id[1], img_id[2], img_id), cv2.IMREAD_GRAYSCALE)\n\n    # read International Chemical Identifier\n    mol = rdkit.Chem.inchi.MolFromInchi(row[\"InChI\"])\n\n    # draw molecule with 0 degree rotation\n    d = rdkit.Chem.Draw.rdMolDraw2D.MolDraw2DCairo(512, 512)\n    d.drawOptions().useBWAtomPalette()\n    d.drawOptions().rotate = 0\n    d.drawOptions().bondLineWidth = 1\n    d.DrawMolecule(mol)\n    d.FinishDrawing()\n    d.WriteDrawingText(\"0.png\")\n    img0 = cv2.imread(\"0.png\", cv2.IMREAD_GRAYSCALE)\n\n    # draw molecule with 90 degree rotation\n    d = rdkit.Chem.Draw.rdMolDraw2D.MolDraw2DCairo(512, 512)\n    d.drawOptions().useBWAtomPalette()\n    d.drawOptions().rotate = 90\n    d.drawOptions().bondLineWidth = 1\n    d.DrawMolecule(mol)\n    d.FinishDrawing()\n    d.WriteDrawingText(\"90.png\")\n    img90 = cv2.imread(\"90.png\", cv2.IMREAD_GRAYSCALE)\n\n    # draw molecule with 180 degree rotation\n    d = rdkit.Chem.Draw.rdMolDraw2D.MolDraw2DCairo(512, 512)\n    d.drawOptions().useBWAtomPalette()\n    d.drawOptions().rotate = 180\n    d.drawOptions().bondLineWidth = 1\n    d.DrawMolecule(mol)\n    d.FinishDrawing()\n    d.WriteDrawingText(\"180.png\")\n    img180 = cv2.imread(\"180.png\", cv2.IMREAD_GRAYSCALE)\n\n    # show images\n    plt.figure(figsize=(20, 20))\n    plt.subplot(1, 4, 1), plt.imshow(img, \"gray\")\n    plt.subplot(1, 4, 2), plt.imshow(img0, \"gray\")\n    plt.subplot(1, 4, 3), plt.imshow(img90, \"gray\")\n    plt.subplot(1, 4, 4), plt.imshow(img180, \"gray\")\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Draw molecules with noise"},{"metadata":{"trusted":true},"cell_type":"code","source":"for _, row in df.head(10).iterrows():\n    # read original image from dataset\n    img_id = row[\"image_id\"]\n    img = cv2.imread(\"../input/bms-molecular-translation/train/{}/{}/{}/{}.png\".format(img_id[0], img_id[1], img_id[2], img_id), cv2.IMREAD_GRAYSCALE)\n\n    # read International Chemical Identifier\n    mol = rdkit.Chem.inchi.MolFromInchi(row[\"InChI\"])\n\n    # draw molecule\n    d = rdkit.Chem.Draw.rdMolDraw2D.MolDraw2DCairo(512, 512)\n    d.drawOptions().useBWAtomPalette()\n    d.drawOptions().rotate = 0\n    d.drawOptions().bondLineWidth = 1\n    d.DrawMolecule(mol)\n    d.FinishDrawing()\n    d.WriteDrawingText(\"0.png\")\n    img0 = cv2.imread(\"0.png\", cv2.IMREAD_GRAYSCALE)\n\n    # molecule pixel coordinates\n    y, x = np.where(img0 < 255)\n\n    # number of random pixels to replace (higher = more noise)\n    n_samples = np.random.randint(len(y))\n\n    # choose random pixel indices\n    i = np.random.randint(0, len(y), n_samples)\n\n    # replace pixels\n    img0_noisy = img0.copy()\n    img0_noisy[y[i], x[i]] = 255\n\n    # show images\n    plt.figure(figsize=(20, 20))\n    plt.subplot(1, 3, 1), plt.imshow(img, \"gray\")\n    plt.subplot(1, 3, 2), plt.imshow(img0, \"gray\")\n    plt.subplot(1, 3, 3), plt.imshow(img0_noisy, \"gray\")\n    plt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}