{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### This should be in a **.py** file and run from terminal with:\n> while [ 1 ]; do python normalize_inchis.py && break; done\n\n(to start from where it left when rdkit crashes with segmentation fault)","metadata":{}},{"cell_type":"markdown","source":"The idea is from [@stassl and @yasufuminakama](https://www.kaggle.com/c/bms-molecular-translation/discussion/228220)","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\nfrom rdkit import Chem\nfrom rdkit import RDLogger\nRDLogger.DisableLog('rdApp.*')\nfrom pathlib import Path\n\ndef normalize_inchi(inchi):\n    try:\n        mol = Chem.MolFromInchi(inchi)\n        return inchi if (mol is None) else Chem.MolToInchi(mol)\n    except: return inchi\n\n\n# Segfault in rdkit taken care of, run it with:\n# while [ 1 ]; do python normalize_inchis.py && break; done\nif __name__=='__main__':\n    # Input & Output\n    orig_path = Path('submission.csv')\n    norm_path = orig_path.with_name(orig_path.stem+'_norm.csv')\n    \n    # Do the job\n    N = norm_path.read_text().count('\\n') if norm_path.exists() else 0\n    print(N, 'number of predictions already normalized')\n\n    r = open(str(orig_path), 'r')\n    w = open(str(norm_path), 'a', buffering=1)\n\n    for _ in range(N):\n        r.readline()\n    line = r.readline()  # this line is the header or is where it segfaulted last time\n    w.write(line)\n\n    for line in tqdm(r):\n        splits = line[:-1].split(',')\n        image_id = splits[0]\n        inchi = ','.join(splits[1:]).replace('\"','')\n        inchi_norm = normalize_inchi(inchi)\n        w.write(f'{image_id},\"{inchi_norm}\"\\n')\n\n    r.close()\n    w.close()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# How much difference it made (optional)","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport edlib\nfrom tqdm import tqdm\n\nsub_df = pd.read_csv('submission.csv')\nsub_norm_df = pd.read_csv('submission_norm.csv')\n\nlev = 0\nN = len(sub_df)\nfor i in tqdm(range(N)):\n    inchi, inchi_norm = sub_df.iloc[i,1], sub_norm_df.iloc[i,1]\n    lev += edlib.align(inchi, inchi_norm)['editDistance']\n\nprint(lev/N)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}