{"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 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":2,"outputs":[{"output_type":"stream","text":"['test.csv', 'mulliken_charges.csv', 'dipole_moments.csv', 'train.csv', 'structures.csv', 'magnetic_shielding_tensors.csv', 'potential_energy.csv', 'sample_submission.csv', 'scalar_coupling_contributions.csv', 'structures']\n","name":"stdout"}]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/train.csv')\ntest_df = pd.read_csv('../input/test.csv')\nstructures = pd.read_csv('../input/structures.csv')","execution_count":4,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from torchvision import transforms, utils\nfrom torch.utils.data import Dataset, DataLoader\n\nimport random\nimport numbers\nimport math\nimport collections\n\nimport numpy as np\nimport torch","execution_count":5,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# should add CV\nclass MolecularDataset(Dataset):\n    def __init__(self, df, structures, mode=\"train\", transform=\"augmentation\"):\n        self.mode = mode\n        self.transform = transform\n        self.df = df\n        self.structures = structures\n#         self.df.reset_index(drop=True)\n#         self.structures.reset_index(drop=True)\n        self.types = ['1JHC', '2JHH', '1JHN', '2JHN', '2JHC', '3JHH', '3JHC', '3JHN']\n        \n    def __len__(self):\n        if self.mode == \"train\":\n            return 4000000\n        else:\n            return 658147\n    \n    def points_to_3D_map(self, points):\n        '''points: n*4(x,y,z,index) '''\n        grid = np.zeros((256,256,256))\n        for point in points:\n#             print(point)\n            x = int(point[0]*10)+128\n            y = int(point[1]*10)+128\n            z = int(point[2]*10)+128\n            grid[x][y][z] = point[3]\n        return grid\n    \n    def type_to_num(self, type):\n        num = 0\n        for i in range(8):\n            if type == self.types[i]:\n                num = i\n                break\n        return num\n            \n            \n#         index                                      0\n# id                                         0    [0]\n# molecule_name               dsgdb9nsd_000001    [1]\n# atom_index_0                               1    [2]\n# atom_index_1                               0\n# type                                    1JHC\n# scalar_coupling_constant             84.8076\n# atom_count                                 5\n# Name: 0, dtype: object\n    def __getitem__(self, idx):\n        if self.mode == \"train\":\n            infomation = np.zeros((3,))\n#             print(\"**xxxxxxxxx\")\n#             print(self.df.loc[idx][2])\n#             print(self.df.loc[idx][3])\n#             print(self.df.loc[idx][4])\n            infomation[0] = self.df.loc[idx]['atom_index_0']\n            infomation[1] = self.df.loc[idx]['atom_index_1']\n            infomation[2] = self.type_to_num(self.df.loc[idx]['type'])\n            \n            name = self.df.loc[idx]['molecule_name']\n#             print(\"here 3\")\n            structures_of_name = structures.loc[structures['molecule_name'] == name]\n#             print(\"here 4\")\n            points = np.zeros((structures_of_name.shape[0], 4))\n            for i in range(structures_of_name.shape[0]):\n                points[i, 0] = structures_of_name.loc[i]['x']\n                points[i, 1] = structures_of_name.loc[i]['y']\n                points[i, 2] = structures_of_name.loc[i]['z']\n                points[i, 3] = structures_of_name.loc[i]['atom_index']\n#             print(\"here 5\")\n#             grid = self.points_to_3D_map(points)\n            \n            target = np.array(self.df.loc[idx]['scalar_coupling_constant'])\n            return torch.from_numpy(infomation), torch.from_numpy(points), torch.from_numpy(target)\n        else:\n            infomation = np.zeros((3,))\n            infomation[0] = self.df.loc[idx]['atom_index_0']\n            infomation[1] = self.df.loc[idx]['atom_index_1']\n            infomation[2] = self.type_to_num(self.df.loc[idx]['type'])\n            \n            name = self.df.loc[idx][1]\n            structures_of_name = structures.loc[structures['molecule_name'] == name]\n            points = np.zeros((structures_of_name.shape[0], 4))\n            for i in range(structures_of_name.shape[0]):\n                points[i, 0] = structures_of_name.loc[i]['x']\n                points[i, 1] = structures_of_name.loc[i]['y']\n                points[i, 2] = structures_of_name.loc[i]['z']\n                points[i, 3] = structures_of_name.loc[i]['atom_index']\n    \n#             grid = self.points_to_3D_map(points)\n            \n            target = np.array(self.df.loc[idx]['scalar_coupling_constant'])\n            return torch.from_numpy(infomation), torch.from_numpy(points), torch.from_numpy(target)","execution_count":6,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataloader = MolecularDataset(df=train_df, structures=structures, mode='train')\nfor i in range(len(dataloader)):\n    information, grid, target = dataloader[i]\n    print(\"*****in *****\")\n    print(information)\n    print(\"*****grid*****\")\n    print(grid)\n    print(\"*****target*****\")\n    print(target)\n    if i == 0:\n        break","execution_count":7,"outputs":[{"output_type":"stream","text":"*****in *****\ntensor([1., 0., 0.], dtype=torch.float64)\n*****grid*****\ntensor([[-1.2698e-02,  1.0858e+00,  8.0010e-03,  0.0000e+00],\n        [ 2.1504e-03, -6.0313e-03,  1.9761e-03,  1.0000e+00],\n        [ 1.0117e+00,  1.4638e+00,  2.7657e-04,  2.0000e+00],\n        [-5.4082e-01,  1.4475e+00, -8.7664e-01,  3.0000e+00],\n        [-5.2381e-01,  1.4379e+00,  9.0640e-01,  4.0000e+00]],\n       dtype=torch.float64)\n*****target*****\ntensor(84.8076, dtype=torch.float64)\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}