{"cells":[{"metadata":{},"cell_type":"markdown","source":"# It's all wrong!"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tqdm import tqdm_notebook\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures = pd.read_csv('../input/structures.csv')\n\nstructures","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xyz = structures[['x','y','z']].values\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ss = structures.groupby('molecule_name').size()\nss = ss.cumsum()\nss","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ssx = np.zeros(len(ss) + 1, 'int')\nssx[1:] = ss\nssx","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"molecule_id = 20\nprint(ss.index[molecule_id])\nstart_molecule = ssx[molecule_id]\nend_molecule = ssx[molecule_id+1]\nxyz[start_molecule:end_molecule]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can compare with the information we get from the original pandas dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"structures_idx = structures.set_index('molecule_name')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures_idx.loc['dsgdb9nsd_000022'][['x', 'y', 'z']].values","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Looks good.\n\nWe can now rewrite our function using our arrays.\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_fast_dist_matrix(xyz, ssx, molecule_id):\n    start_molecule, end_molecule = ssx[molecule_id], ssx[molecule_id+1]\n    locs = xyz[start_molecule:end_molecule]    \n    num_atoms = end_molecule - start_molecule\n    loc_tile = np.tile(locs.T, (num_atoms,1,1))\n    dist_mat = np.sqrt((loc_tile - loc_tile.T)**2).sum(axis=1)\n    return dist_mat","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's check we get the same result with both techniques.  We use methane, molecule_id=0"},{"metadata":{"trusted":true},"cell_type":"code","source":"molecule_id = 0\nmolecule = ss.index[molecule_id]\nprint(molecule)\nget_fast_dist_matrix(xyz, ssx, molecule_id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"# Okay, that is the distance matrix for molecule_id==0. Let's check which molecule it is."},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ss.index[molecule_id])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# As expected, it is dsgdb9nsd_000001. But we know it should be very symmetric with the same distances. Let's calculate it using the structures file."},{"metadata":{"trusted":true},"cell_type":"code","source":"structures.loc[structures['molecule_name']=='dsgdb9nsd_000001']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Let's calculate the distances from the Carbon atom to the Hydrogens"},{"metadata":{"trusted":true},"cell_type":"code","source":"carbon_x = -0.012698\ncarbon_y = 1.085804\ncarbon_z = 0.008001","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def distance_from_carbon_to_xyz(xyz):\n    return np.sqrt( (carbon_x - xyz[0])**2 + (carbon_y - xyz[1])**2 + (carbon_z - xyz[2])**2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures.loc[structures['molecule_name']=='dsgdb9nsd_000001', ['x','y','z']].apply(distance_from_carbon_to_xyz, 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# We can see the distances are 0 and 1.09 pretty much. Let's check the distance matrix again."},{"metadata":{"trusted":true},"cell_type":"code","source":"molecule_id = 0\nmolecule = ss.index[molecule_id]\nprint(molecule)\nget_fast_dist_matrix(xyz, ssx, molecule_id)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# That distance is not there. Therefore the distance computation is wrong."}],"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}