{"cells":[{"metadata":{},"cell_type":"markdown","source":"In this notebook I'm going to sample characters for positions"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from collections import Counter\nfrom multiprocessing import Pool\n\nimport numpy as np\nimport pandas as pd\nfrom tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/bms-molecular-translation/train_labels.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/bms-molecular-translation/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# let's see shortest and longest strings\ndf_lengths = df.InChI.apply(len)\nprint(df_lengths.max())\nprint(df_lengths.min())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_lengths.plot(kind=\"hist\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"positions_freqs = [Counter() for _ in range(df_lengths.max())]\nfor inch in df.InChI:\n    for pos, char in enumerate(inch):\n        positions_freqs[pos][char] += 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(positions_freqs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"positions_freqs[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"positions_freqs[10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"positions_freqs[20]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnts = np.array([v for v in positions_freqs[20].values()])\nsum(cnts / sum(cnts)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_inchi_lens_freqs = df_lengths.value_counts(normalize=True)\ndef sample_length(size=1):\n    return np.random.choice(a=df_inchi_lens_freqs.index.to_list(), p=df_inchi_lens_freqs.values.tolist(), size=size)\nsample_length(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pos2char_proba = []\nfor cnt in positions_freqs:\n    counts = np.array(list(cnt.values()))\n    freqs = counts / sum(counts)\n    sampl_chars = list(cnt)\n    pos2char_proba.append((sampl_chars, freqs))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def sample_characters(leng: int):\n    chars = []\n    for i in range(leng):\n        sampl_chars, freqs = pos2char_proba[i]\n        chars.append(np.random.choice(sampl_chars, p=freqs))\n    return \"\".join(chars)\n\nsample_characters(400)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This is ridiculous, but okay for baseline"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_lengths = sample_length(test_df.shape[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = []\nwith Pool(16) as pool:\n    for res in tqdm(pool.imap(sample_characters, test_lengths, chunksize=60), total=test_lengths.shape[0]):\n        result.append(res)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df[\"InChI\"] = result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.to_csv(\"output.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}