{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":67356,"databundleVersionId":8006601,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-05-10T13:46:04.859159Z","iopub.execute_input":"2024-05-10T13:46:04.859507Z","iopub.status.idle":"2024-05-10T13:46:05.850841Z","shell.execute_reply.started":"2024-05-10T13:46:04.859482Z","shell.execute_reply":"2024-05-10T13:46:05.849447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert Input Files to Pivot Form\n\nThe input files <code>trian.csv</code> and <code>test.csv</code> have a tall and skinny format. For one, this is wastes storage, and secondly processing them takes more time than necessary. This notebook develops code to convert the raw input files to pivoted version, which contain the equivalent information, while ignoring redundant data.\n\nThe key for pivoting is the combination of <code>buildingblock1_smiles,buildingblock2_smiles,buildingblock3_smiles,</code> and <code>molecule_smiles.</code> For <code>train.csv</code>, the new data columns contain the <code>binds</code> values for the matching rows for the respective target. For the <code>test.csv</code> file, the new columns receive the value of the <code>id</code> column to be able to refer to them in the submission.","metadata":{}},{"cell_type":"code","source":"import csv\nimport time\n\ndef pivotFile(input_name=None, output_name=None, key_from=1, key_to=4, pivot_index=-1, pivot_values=[], value_index=-1, max_lines=None) :\n    # perform the pivoting and assume that keys are sorted\n    fout = open(output_name, \"w\")\n    writer = csv.writer(fout, delimiter=\",\")\n    indexer = {_:_i for _i,_ in enumerate(pivot_values)}\n    with open(input_name, \"r\") as f:\n        reader = csv.reader(f, delimiter=\",\")\n        for i, line in enumerate(reader):\n            if i == 0 :\n                writer.writerow(line[key_from:key_to+1] + pivot_values)\n                value_array = None\n                current_keys = [None]*(key_to-key_from+1)\n                continue\n            if max_lines is not None and i >= max_lines : break\n            if i % 200000 == 0 : print ('.', end='')\n            if i % 10000000 == 0 : print ('\\t', i)\n            if line[key_from:key_to+1] != current_keys :\n                if value_array is not None :\n                    writer.writerow(current_keys + value_array)\n                    # if value_array[0] != '0' and value_array[2] != '0' and value_array != ['0','0','0'] : print (current_keys + value_array)\n                current_keys = line[key_from:key_to+1]\n                value_array = [None]*len(pivot_values)\n            value_array[indexer[line[pivot_index]]] = line[value_index]\n        if value_array is not None : # write remaining row\n            writer.writerow(current_keys + value_array)\n    print ('\\t', i)\n    fout.close()\n\nstartTime = time.time()\npivotFile(input_name=\"/kaggle/input/leash-BELKA/test.csv\", output_name=\"pivoted_test.csv\", max_lines=None, key_from=1, key_to=4, pivot_index=5, pivot_values=[\"BRD4\",\"HSA\",\"sEH\"], value_index=0)\nprint (\"Pivoting test set took\",time.time()-startTime,\"seconds\")\n\nstartTime = time.time()\npivotFile(input_name=\"/kaggle/input/leash-BELKA/train.csv\", output_name=\"pivoted_train.csv\", max_lines=None, key_from=1, key_to=4, pivot_index=5, pivot_values=[\"BRD4\",\"HSA\",\"sEH\"], value_index=6)\nprint (\"Pivoting training set took\",time.time()-startTime,\"seconds\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-10T13:54:08.360933Z","iopub.execute_input":"2024-05-10T13:54:08.363129Z","iopub.status.idle":"2024-05-10T14:14:38.658801Z","shell.execute_reply.started":"2024-05-10T13:54:08.363083Z","shell.execute_reply":"2024-05-10T14:14:38.656765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!grep \",,\" /kaggle/working/pivoted_test.csv | tail","metadata":{"execution":{"iopub.status.busy":"2024-05-10T14:23:04.752622Z","iopub.execute_input":"2024-05-10T14:23:04.753049Z","iopub.status.idle":"2024-05-10T14:23:06.048614Z","shell.execute_reply.started":"2024-05-10T14:23:04.753019Z","shell.execute_reply":"2024-05-10T14:23:06.046879Z"},"trusted":true},"execution_count":null,"outputs":[]}]}