{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nfrom scipy import spatial","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/train.csv')\ndf.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_structures = pd.read_csv('../input/structures.csv')\ndf_structures.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"indices = None\ndistances = None\nfor c in tqdm(df_structures.molecule_name.unique()[:5]):\n    x = df_structures[df_structures.molecule_name==c][['x','y','z']]\n    z = spatial.KDTree(x,x.shape[0])\n    permol = z.query(x,x.shape[0])\n    if(indices is None):\n        distances = permol[0].tolist()\n        indices = permol[1].tolist()\n    else:\n        distances += permol[0].tolist()\n        indices += permol[1].tolist()\nnearestneighbors = pd.DataFrame(indices)\ndistanceneighbors = pd.DataFrame(distances)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"nearestneighbors.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"distanceneighbors.head(10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Or if you want to preserve the index structure (Note speed this up by label encoding the atoms!)"},{"metadata":{"trusted":true},"cell_type":"code","source":"distances = {}\natoms = {}\n\ni = 0\nfor k,groupdf in tqdm(df_structures.groupby('molecule_name')):\n    \n    x = groupdf[['atom']]\n    z = spatial.KDTree(groupdf[['x','y','z']],x.shape[0])\n    permol = z.query(groupdf[['x','y','z']],x.shape[0])\n    d = np.take_along_axis(permol[0],np.argsort(permol[1]),axis=1)\n    o1 = np.arange(d.shape[1]).reshape(1,-1)\n    o2 = np.repeat(o1,o1.shape[1],axis=0)[np.triu_indices(o1.shape[1], k = 1)]\n    o3 = np.repeat(o1,o1.shape[1],axis=0).T[np.triu_indices(o1.shape[1], k = 1)]\n    indices = np.array([str(i2) +'_'+str(i1) for i1,i2 in zip(o2,o3)]).tolist()\n    d = d[np.triu_indices(d.shape[0], k = 1)].tolist()\n    distances[k] =  dict(zip(indices, d))\n    a = x.atom.values.reshape(1,-1)\n    b = np.repeat(a,a.shape[1],axis=0)[np.triu_indices(a.shape[1], k = 1)]\n    c = np.repeat(a,a.shape[1],axis=0).T[np.triu_indices(a.shape[1], k = 1)]\n    atoms[k] = dict(zip(indices, (b+c).tolist()))\n  \n    \n    if(i==10):\n        break\n    i+=1\ndistanceneighbors = pd.DataFrame.from_dict(distances,orient='index')\natomneighbors = pd.DataFrame.from_dict(atoms,orient='index')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"distanceneighbors.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\natomneighbors.head(10)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}