{"cells":[{"metadata":{},"cell_type":"markdown","source":"### Notebook de exploração: https://www.kaggle.com/rpeer333/molecular-properties-detailed-eda"},{"metadata":{"trusted":true},"cell_type":"code","source":"! pip install torch torchvision\n! pip install --verbose --no-cache-dir torch-scatter\n! pip install --verbose --no-cache-dir torch-sparse\n! pip install --verbose --no-cache-dir torch-cluster\n! pip install --verbose --no-cache-dir torch-spline-conv (optional)\n! pip install torch-geometric","execution_count":null,"outputs":[]},{"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)\nimport matplotlib.pyplot as plt    \nfrom ase import Atoms\nimport ase.visualize\n\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\nimport os\nfor 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":{"trusted":true},"cell_type":"code","source":"source_path = '/kaggle/input/champs-scalar-coupling'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = pd.read_csv(os.path.join(source_path, 'train.csv'))\ntest_data = pd.read_csv(os.path.join(source_path, 'test.csv'))\nstructures = pd.read_csv(os.path.join(source_path, 'structures.csv'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures.molecule_name.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_data), len(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def view_molecule(structures=structures, name=None):\n    \"\"\"\n    Modified from:\n    https://www.kaggle.com/borisdee/how-to-easy-visualization-of-molecules\n    \"\"\"\n    if name is None:\n        name = np.random.choice(structures.molecule_name.unique())\n    print(f'molecule-name: {name}')\n    \n    molecule_df = structures.query('molecule_name == @name')\n    molecule    = Atoms(positions=molecule_df[['x', 'y', 'z']].values,\n                        symbols=molecule_df.atom.values)\n    \n    return ase.visualize.view(molecule, viewer=\"x3d\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"markdown","source":"view_molecule(name='dsgdb9nsd_000001')"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data[train_data.molecule_name == 'dsgdb9nsd_000001']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures[structures.molecule_name == 'dsgdb9nsd_000001']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nfrom torch_geometric.data import InMemoryDataset\n\n\nclass MoleculeDataset(InMemoryDataset):\n    def __init__(self, root, transform=None, pre_transform=None):\n        super(MyOwnDataset, self).__init__(root, transform, pre_transform)\n        self.data, self.slices = torch.load(self.processed_paths[0])\n\n    @property\n    def raw_file_names(self):\n        return []\n\n    @property\n    def processed_file_names(self):\n        return []\n\n    def download(self):\n        # Download to `self.raw_dir`.\n        pass\n\n    def process(self):\n        # Read data into huge `Data` list.\n        data_list = []\n\n        if self.pre_filter is not None:\n            data_list = [data for data in data_list if self.pre_filter(data)]\n\n        if self.pre_transform is not None:\n            data_list = [self.pre_transform(data) for data in data_list]\n\n        data, slices = self.collate(data_list)\n        torch.save((data, slices), self.processed_paths[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"grouped = train_data.groupby('molecule_name')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_list=[]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for molecule_name, group in tqdm(grouped):\n#     print(molecule_name, group)\n    group.reset_index(drop=True, inplace=True)\n    node_features = group.loc[group.molecule_name == molecule_name].values\n    node_features = torch.LongTensor(node_features).unsqueeze(1)\n    target_nodes = group.atom_index_1.values\n    source_nodes = group.atom_index_0.values\n    \n    edge_index = torch.tensor([source_nodes, target_nodes], dtype=torch.long)\n    \n    X = node_features\n    y = torch.FloatTensor([group.scalar_coupling_constant])\n    \n    data = Data(x=X, edge_index=edge_index, y=y)\n    data_list.append(data)","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}