{"cells":[{"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)\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 all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This code gen data for the old version data of chams scalar coupling"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt \n%matplotlib inline \nimport pickle\nfrom scipy.spatial import *\nimport os ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_path = '../input/champsscalarold/'\nstructures_path = data_path + 'structures.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures_df = pd.read_csv(structures_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures_df.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_value_distances_matrix_dict = {}\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_columns = list([f'eigen_value_{x+1}' for x in range(29)])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_value_coulomb_matrix_columns = list([f'eigen_value_coulomb_matrix_{x+1}' for x in range(29)])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_value_coulomb_matrix_columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_value = pd.DataFrame(structures_df['molecule_name'].nunique(), index=np.arange(structures_df['molecule_name'].nunique()), columns=eigen_columns + ['molecule_name'], dtype=float)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"atom_Z_dict = {'C': 6, 'O': 8, 'H': 1, 'N': 7, 'F': 9}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_values = []\neigen_values_coulomb_matrix = []\neigen_value_coulomb_matrix_dict = {}\nwith tqdm(total=len(structures_df['molecule_name'].unique())) as pbar:\n    for i, (name, df) in enumerate(structures_df.groupby('molecule_name')):\n        xyz = df[['x', 'y', 'z']]\n        matrix = distance_matrix(xyz, xyz)\n        inverse_matrix = np.where(matrix != 0, 1/ matrix, 0)\n        atom_type = df['atom'].values\n        atom_Z = np.array([atom_Z_dict[type_] for type_ in atom_type])\n        Z_ij = np.repeat(atom_Z, len(atom_Z)).reshape(-1, len(atom_Z), order='F')\n        for i in range(len(atom_Z)):\n            Z_ij[i] = Z_ij[i] * atom_Z[i]\n        coulomb_matrix = np.multiply(Z_ij, inverse_matrix) + 1/2 * (np.multiply((np.eye(len(atom_Z))), Z_ij) ** 2.4)\n        v_coulomb_matrix, _ = np.linalg.eig(coulomb_matrix)\n        v_coulomb_matrix_pad = np.zeros(29)\n        v_coulomb_matrix_pad[:v_coulomb_matrix.shape[0]] = v_coulomb_matrix\n        \n        v,_ = np.linalg.eig(matrix)\n        v_pad = np.zeros(29)\n        v_pad[:v.shape[0]] = v\n        eigen_value_distances_matrix_dict[name] = v_pad\n        eigen_values.append(v_pad)\n        eigen_values_coulomb_matrix.append(v_coulomb_matrix_pad)\n        eigen_value_coulomb_matrix_dict[name] = v_coulomb_matrix_pad\n        \n        pbar.update()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_value = pd.DataFrame(eigen_values, columns = eigen_columns, dtype=float)\neigen_value['molecule_name'] = structures_df['molecule_name'].unique()\neigen_value","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_values_coulomb_matrix = pd.DataFrame(eigen_values_coulomb_matrix, columns = eigen_value_coulomb_matrix_columns, dtype=float)\neigen_values_coulomb_matrix['molecule_name'] = structures_df['molecule_name'].unique()\neigen_values_coulomb_matrix","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('eigen_value_coulomb_matrix.pkl', 'wb') as f:\n    pickle.dump(eigen_value_coulomb_matrix_dict, f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('eigen_value_distance_matrix.pkl', 'wb') as f:\n    pickle.dump(eigen_value_distances_matrix_dict, f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_value.to_csv('eigen_values.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eigen_values_coulomb_matrix.to_csv('eigen_values_coulomb_matrix.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"atom_weight = {'C' : 12, 'F' : 19, 'O': 16, 'H': 1, 'N': 14}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"molecule_weight_avg ={}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with tqdm(total=len(structures_df['molecule_name'].unique())) as pbar:\n    for name, df in structures_df.groupby('molecule_name'):\n        atom_type = df['atom']\n        n_atoms = len(atom_type)\n        atom_weights = np.array(list(map(lambda x: atom_weight[x], atom_type)))\n#         print(atom_weights)\n        molecule_weight_avg[name] = atom_weights.mean()\n#         print(molecule_weight_avg)\n        pbar.update()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('molecule_avg_weight.pkl', 'wb') as f:\n    pickle.dump(molecule_weight_avg, f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# with tqdm(total=len(structures_df['molecule_name']))","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":1}