{"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":"# 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\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 read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-23T18:10:57.786768Z","iopub.execute_input":"2021-10-23T18:10:57.787076Z","iopub.status.idle":"2021-10-23T18:10:57.803136Z","shell.execute_reply.started":"2021-10-23T18:10:57.787039Z","shell.execute_reply":"2021-10-23T18:10:57.801969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torch-scatter torch-sparse torch-cluster torch-spline-conv torch-geometric -f https://data.pyg.org/whl/torch-1.9.0+cpu.html","metadata":{"execution":{"iopub.status.busy":"2021-10-23T18:11:20.041482Z","iopub.execute_input":"2021-10-23T18:11:20.041770Z","iopub.status.idle":"2021-10-23T18:11:48.000646Z","shell.execute_reply.started":"2021-10-23T18:11:20.041738Z","shell.execute_reply":"2021-10-23T18:11:47.999531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch_geometric.data import DataLoader,Dataset,Data\nimport pandas as pd\nimport numpy as np\nimport gc\nimport torch.nn.functional as F\n\nimport numpy as np\nfrom tqdm import tqdm\ndevice = 'cpu'\n\n\n\ndef create_data_list(file_i,file_t=None,chunksize=1000):\n    df_i = pd.read_csv(file_i, iterator=True, chunksize=chunksize)\n\n    has_t = (not file_t is None)\n    if has_t:\n        \"\"\" If we are also getting the target values, put it as part of the generator.\n        \"\"\"\n        df_t = pd.read_csv(file_t, iterator=True, chunksize=chunksize)\n        total = 100000//chunksize\n        gen = tqdm(enumerate(zip(df_t,df_i)),total=total)\n    else:\n        \n        total = 2000//chunksize\n        gen = tqdm(enumerate(df_i),total=total)\n        \n    L = []\n    def mod(x):\n        return x%200\n    def div(x):\n        return x//200\n    for i,chunks in gen:\n        #We load chunks of the dataset at a time to save on memory.\n        if file_t is None:\n            chunk_i = chunks\n            I = np.array(chunk_i.values) #input data\n            T = np.zeros_like(I) #empty targets\n        else:\n            chunk_t, chunk_i = chunks\n            T = np.array(chunk_t.values)#input data\n            I = np.array(chunk_i.values)#target data\n        \n        if I.shape[1] == 601:\n            I = I[:,1:]\n            print(I.shape)\n        for j in range(chunk_i.shape[0]):\n            x1 = torch.as_tensor(I[j,:200]).view(-1,1)\n            x2 = torch.as_tensor(I[j,200:400]).view(-1,1)\n            x = torch.cat([x1,x2],axis=1)\n\n            m = torch.as_tensor(I[j,400:]).bool().view(-1)\n\n            data = Data(x=x,m=m)\n            if has_t:\n                y = np.flatnonzero(T[j,:])\n                y_row = np.apply_along_axis(div,0,y)[:,None]\n                y_col = np.apply_along_axis(mod,0,y)[:,None]\n                y_index = torch.IntTensor(np.concatenate([y_row,y_col],axis=1))\n                data.y_index = y_index\n            \n            L.append(data)\n    return L\nL = create_data_list('/kaggle/input/kddbr-2021/public/TSP100Kinput.csv',\n                     '/kaggle/input/kddbr-2021/public/TSP100Ktarget.csv')\ntorch.save(L, 'train.pt')\nprint(\"Training example: \")\nprint(L[0])\n\nL = create_data_list('/kaggle/input/kddbr-2021/public/TSP2Kinput.csv',None)\ntorch.save(L, 'test.pt')\nprint(\"Test example: \")\nprint(L[0])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-23T18:12:00.274616Z","iopub.execute_input":"2021-10-23T18:12:00.274930Z","iopub.status.idle":"2021-10-23T18:15:42.883628Z","shell.execute_reply.started":"2021-10-23T18:12:00.274895Z","shell.execute_reply":"2021-10-23T18:15:42.882492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}