{"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":"code","source":"import os\nimport numpy as np\nimport pandas as pd\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n\nfrom collections import defaultdict","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain_df=pd.read_csv('/kaggle/input/champs-scalar-coupling/train.csv')\nstructures_df=pd.read_csv('../input/champs-scalar-coupling/structures.csv')\nstructures_df=structures_df.sort_values(['molecule_name', 'atom_index'])\n\ntrain_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"structures_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number Of Molecules:', train_df.molecule_name.nunique())\nprint('Number Of Coupling Types:', train_df.type.nunique())\n\nprint('Number Of Atoms:', structures_df.atom.nunique())\nprint('Number Of Atom Index:', structures_df.atom_index.nunique())\nprint('Number Of Training Records:', len(train_df))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.type.value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"structures_df.sample()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Number Of the Atoms in the molecule","metadata":{}},{"cell_type":"code","source":"atom_count_df=structures_df.groupby('molecule_name')[['atom_index']].count().rename(columns={'atom_index': 'num_atoms'}).reset_index()\natom_count_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\nplt.title('Number Of Atoms (vs) Number Of Molecules')\nsns.countplot(data=atom_count_df, \n              x='num_atoms')\n\nplt.xlabel('Number Of Atoms')\nplt.ylabel('Number Of Molecules')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=atom_count_df, x='num_atoms')\nplt.xlabel('Number Of Atoms')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Is there any impact of the number of atoms in the molecules with the coupling constants","metadata":{}},{"cell_type":"code","source":"coupling_agg_df=train_df.groupby('molecule_name')[['scalar_coupling_constant']].agg(['mean', 'min', 'max'])\ncoupling_agg_df.columns=['coupling_mean', 'coupling_min', 'coupling_max']\ncoupling_agg_df=coupling_agg_df.reset_index()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coupling_agg_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coupling_agg_df=coupling_agg_df.merge(atom_count_df)\ncoupling_agg_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nplt.title('Distribution of Mean Coupling Constant with Number Of Atoms')\nsns.boxplot(data=coupling_agg_df, x='num_atoms', y='coupling_mean')\n\nplt.xlabel('Number Of Atoms')\nplt.ylabel('Coupling Constant Mean')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nplt.title('Distribution of Minimum Coupling Constant with Number Of Atoms')\nsns.boxplot(data=coupling_agg_df, x='num_atoms', y='coupling_min')\n\nplt.xlabel('Number Of Atoms')\nplt.ylabel('Coupling Constant Minimum')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nplt.title('Distribution of Maximum Coupling Constant with Number Of Atoms')\nsns.boxplot(data=coupling_agg_df, x='num_atoms', y='coupling_max')\n\nplt.xlabel('Number Of Atoms')\nplt.ylabel('Coupling Constant Maximum')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. Number Of Atoms in the molecule had an impact on the Mean of the coupling constants of the given atoms in molecule.\n2. Mean Coupling is reduced as the number of Atoms increase in the molecule. It could be because of some of the bonds could be unstable.\n","metadata":{}},{"cell_type":"markdown","source":"# Impact of the Type of Coupling","metadata":{}},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.type.value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(9, 5))\nsns.boxplot(data=train_df.sort_values('type'), \n            x='type',\n            y='scalar_coupling_constant'\n           )\n\nplt.xlabel('Coupling Type')\nplt.ylabel('Scalar Coupling Constant')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type_agg_df=train_df.groupby('type')[['scalar_coupling_constant']].agg('std').reset_index()\ntype_agg_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(data=type_agg_df,\n            x='type',\n            y='scalar_coupling_constant'\n           )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Scalar Coupling Constant reduces from 1 -> 3\n\n1JHC, 1JHN have high coupling constants\n\n3JHN --> very less amount of variation in the coupling constants and around zeros.","metadata":{}},{"cell_type":"markdown","source":"# Lets us check the impact of the distances on the atoms","metadata":{}},{"cell_type":"code","source":"%%time\ncoordinate_df=structures_df.groupby('molecule_name')[['x', 'y', 'z']].agg(list).reset_index()\ncoordinate_df=train_df.merge(coordinate_df)\n\ncoordinate_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_coordinate_x0(row):\n    atom_idx=row['atom_index_0']\n    return row['x'][atom_idx]\n\ndef get_coordinate_x1(row):\n    atom_idx=row['atom_index_1']\n    return row['x'][atom_idx]\n\n\ndef get_coordinate_y0(row):\n    atom_idx=row['atom_index_0']\n    return row['y'][atom_idx]\n\ndef get_coordinate_y1(row):\n    atom_idx=row['atom_index_1']\n    return row['y'][atom_idx]\n\ndef get_coordinate_z0(row):\n    atom_idx=row['atom_index_0']\n    return row['z'][atom_idx]\n\ndef get_coordinate_z1(row):\n    atom_idx=row['atom_index_1']\n    return row['z'][atom_idx]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ncoordinate_df['x0']=coordinate_df[['atom_index_0', 'x']].apply(get_coordinate_x0, axis=1)\ncoordinate_df['x1']=coordinate_df[['atom_index_1', 'x']].apply(get_coordinate_x1, axis=1)\n\ncoordinate_df['y0']=coordinate_df[['atom_index_0', 'y']].apply(get_coordinate_y0, axis=1)\ncoordinate_df['y1']=coordinate_df[['atom_index_1', 'y']].apply(get_coordinate_y1, axis=1)\n\ncoordinate_df['z0']=coordinate_df[['atom_index_0', 'z']].apply(get_coordinate_z0, axis=1)\ncoordinate_df['z1']=coordinate_df[['atom_index_1', 'z']].apply(get_coordinate_z1, axis=1)\n\ncoordinate_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nx0=coordinate_df['x0'].values\nx1=coordinate_df['x1'].values\n\ny0=coordinate_df['y0'].values\ny1=coordinate_df['y1'].values\n\nz0=coordinate_df['z0'].values\nz1=coordinate_df['z1'].values\n\ndist=(x0-x1)**2 + (y0-y1)**2+ (z0-z1)**2\n\ncoordinate_df['dist']=dist\ncoordinate_df=coordinate_df[['type', 'dist', 'scalar_coupling_constant']]\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coordinate_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data=coordinate_df, x='dist')\nplt.xlabel('Distance Between Atoms')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coordinate_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=coordinate_df,\n            x='type',\n            y='dist'\n           )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data=coordinate_df,\n            x='dist',\n            y='scalar_coupling_constant',\n            col='type',\n            hue='type',\n            facet_kws={}\n           )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. Distance along with the coupling Type had the impact on the scalar coupling constants\n2. 1JHC, 1JHN -> had less distance and have higher coulding constants.\n3. 2JHH, 2JHN, 2JHC -> have distances in range (2-8) and have negative coulings\n4. 3JHC, 3JHN, 3JHH -> have very less variation in coupling constant and have higher variation in the distances between atoms described.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}