{"cells":[{"metadata":{},"cell_type":"markdown","source":"# This notebook was only made to count the number of atoms and make csv.\n## You can use it if you like and Please comment if you have any more questions.\n\"\"\"\n### This notebook only counts atom's quantity, so same atoms are combined into one.\n### For exsample 'InChI=1S/C13H20OS/c1-9(2)8-15-13-6-5-10(3)7-12(13)11(4)14/h5-7,9,11,14H,8H2,1-4H3',\n### target is 'C13H20OS' and C:13, H:20, O:1, S:1.\n\"\"\""},{"metadata":{},"cell_type":"markdown","source":"# Load library"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport re\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\n\nfrom tqdm.auto import tqdm\ntqdm.pandas()\nfrom PIL import Image\nimport seaborn as sns\nimport Levenshtein\nfrom albumentations import Compose, Normalize, Resize, RandomResizedCrop,CenterCrop,HorizontalFlip,VerticalFlip,Rotate,RandomContrast,IAAAdditiveGaussianNoise\nfrom albumentations.pytorch import ToTensorV2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load data and else"},{"metadata":{"trusted":true},"cell_type":"code","source":"def load():\n    \"\"\"Load data and path.\"\"\"\n    def get_train_file_path(image_id):\n        return \"../input/bms-molecular-translation/train/{}/{}/{}/{}.png\".format(\n            image_id[0], image_id[1], image_id[2], image_id \n        )\n    def get_test_file_path(image_id):\n        return \"../input/bms-molecular-translation/test/{}/{}/{}/{}.png\".format(\n            image_id[0], image_id[1], image_id[2], image_id \n        )\n    train = pd.read_csv('../input/bms-molecular-translation/train_labels.csv')\n    test = pd.read_csv('../input/bms-molecular-translation/sample_submission.csv')\n    train['file_path'] = train['image_id'].progress_apply(get_train_file_path)\n    test['file_path'] = test['image_id'].progress_apply(get_test_file_path)\n    return train, test\n\ndef processing(df):\n    \"\"\"Make InChI_ row.\"\"\"\n    df['InChI_list'] = df['InChI'].progress_apply(lambda x: x.split('/'))\n    InChI_df = df['InChI_list'].progress_apply(pd.Series)\n    df = pd.concat([df, InChI_df.add_prefix('InChI_')], axis=1)\n    return df\n\ndef score(y_true, y_pred):\n    \"\"\"Calc score.\"\"\"\n    return np.mean([Levenshtein.distance(true, pred) for true, pred in zip(y_true, y_pred)])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_name():\n    \"\"\"Atom name.\"\"\"\n    name = np.array([\n        'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg',\n        'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr',\n        'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br',\n        'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd',\n        'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La',\n        'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er',\n        'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au',\n        'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac', 'Th',\n        'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md',\n        'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn',\n        'Nh', 'Fl', 'Mc', 'Lv', 'Ts', 'Og'], dtype='<U2')\n    sort_name = np.sort(name)[::-1]\n    changes = [\" \"+sort_name[i]+\"/\" for i in range(len(sort_name))]\n    return dict(zip(sort_name, changes))\n\ndef translate(text, kw, ignore_case=False):\n    search_keys = map(lambda x:re.escape(x), kw.keys())\n    if ignore_case:\n        kw = {k.lower():kw[k] for k in kw}\n        regex = re.compile('|'.join(search_keys), re.IGNORECASE)\n        res = regex.sub( lambda m:kw[m.group().lower()], text)\n    else:\n        regex = re.compile('|'.join(search_keys))\n        res = regex.sub( lambda m:kw[m.group()], text)\n\n    return res.split(\" \")[1:]\n\n\ndef _make_df(res, name):\n    \"\"\"Make new df.\"\"\"\n    df = pd.DataFrame(0, columns=name, index=range(len(res)))\n    for i, target in enumerate(tqdm(res)):\n        for j in target:\n            t_s = j.split(\"/\")\n            if t_s[1] == '':\n                t_s[1] = 1  \n            df.loc[i, t_s[0]] = int(t_s[1])\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Load csv\ntrain, test = load()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Processing"},{"metadata":{"trusted":true},"cell_type":"code","source":"#change train data\ntrains = processing(train)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Translate to train.csv"},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_df(InChI_1):\n    \"\"\"Make new df.\"\"\"\n    name = make_name()\n    res = [translate(target, name) for target in tqdm(InChI_1)]\n    df = _make_df(res, name)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#change df\ndf = make_df(trains.InChI_1)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv(\"atom_df.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"# Thank you for reading."}],"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}