{"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":30732,"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-07-02T08:29:25.271317Z","iopub.execute_input":"2024-07-02T08:29:25.27169Z","iopub.status.idle":"2024-07-02T08:29:26.235746Z","shell.execute_reply.started":"2024-07-02T08:29:25.27166Z","shell.execute_reply":"2024-07-02T08:29:26.234761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pip install duckdb\n%pip install rdkit ","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:29:29.870467Z","iopub.execute_input":"2024-07-02T08:29:29.870822Z","iopub.status.idle":"2024-07-02T08:29:59.109486Z","shell.execute_reply.started":"2024-07-02T08:29:29.870794Z","shell.execute_reply":"2024-07-02T08:29:59.108335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import duckdb\nimport pandas as pd\n\ntrain_path = '/kaggle/input/leash-BELKA/train.parquet'\ntest_path = '/kaggle/input/leash-BELKA/test.parquet'\n\ncon = duckdb.connect()\n\ndf = con.query(f\"\"\"(SELECT *\n                        FROM parquet_scan('{train_path}')\n                        WHERE binds = 0\n                        ORDER BY random()\n                        LIMIT 30000)\n                        UNION ALL\n                        (SELECT *\n                        FROM parquet_scan('{train_path}')\n                        WHERE binds = 1\n                        ORDER BY random()\n                        LIMIT 30000)\"\"\").df()\n\ncon.close()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:29:59.111585Z","iopub.execute_input":"2024-07-02T08:29:59.111905Z","iopub.status.idle":"2024-07-02T08:30:50.953107Z","shell.execute_reply.started":"2024-07-02T08:29:59.111876Z","shell.execute_reply":"2024-07-02T08:30:50.952227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T08:30:52.004417Z","iopub.execute_input":"2024-07-02T08:30:52.005142Z","iopub.status.idle":"2024-07-02T08:30:52.029089Z","shell.execute_reply.started":"2024-07-02T08:30:52.00511Z","shell.execute_reply":"2024-07-02T08:30:52.028095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom rdkit import Chem\nfrom rdkit.Chem import AllChem\nfrom rdkit.Chem import DataStructs\n\nclass FingerprintGenerator:\n    def __init__(self, model):\n        self.model = model\n        self.mols = None\n        self.fp = None\n    def set_molecules(self, data):\n        self.mols = [Chem.MolFromSmiles(x) for x in data[\"SMILES\"]]\n    def generate_fingerprints(self):\n        if self.model == \"morg_fp\":\n            self.fp = [AllChem.GetMorganFingerprintAsBitVect(m, 2, nBits=2048) for m in self.mols]\n        elif self.model == \"morg_fp3\":\n            self.fp = [AllChem.GetMorganFingerprintAsBitVect(m, 3, nBits=2048) for m in self.mols]\n        elif self.model == \"rdk_fp\":\n            self.fp = [AllChem.RDKFingerprint(m) for m in self.mols]\n        elif self.model == 'ap_fp':\n            self.fp = [Chem.GetHashedAtomPairFingerprintAsBitVect(m) for m in self.mols]\n        elif self.model == 'torsion_fp':\n            self.fp = [Chem.GetHashedTopologicalTorsionFingerprintAsBitVect(m) for m in self.mols]\n\n        fp_np = []\n        for x_fp in self.fp:\n            arr = np.zeros((1,))\n            DataStructs.ConvertToNumpyArray(x_fp, arr)\n            fp_np.append(arr)\n        fp_np = np.array(fp_np).astype(np.int8)\n\n        if self.model == \"morg_fp\":\n            np.save(\"ecfp4_fp.npy\", fp_np)\n        elif self.model == \"morg_fp3\":\n            np.save(\"ecfp6_fp.npy\", fp_np)\n        elif self.model == \"rdk_fp\":\n            np.save(\"rd_fp.npy\", fp_np)\n        elif self.model == \"ap_fp\":\n            np.save(\"ap_fp.npy\", fp_np)\n        else:\n            np.save(\"torsion_fp.npy\", fp_np)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nclass Split_data:\n    def __init__ (self,test_size  ):\n        self.test_size = test_size\n    def split (self, x , y):\n        x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=self.test_size, random_state=1)\n        return (x_train, y_train, x_test, y_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = \"rdk_fp\"\nfingerprint_generator = FingerprintGenerator(model)\nmols = fingerprint_generator.set_molecules(data)\nfingerprint_generator.generate_fingerprints()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_rd = np.load('rd_fp.npy')\nsplit_size  = Split_data (0.2)\nx_train, y_train, x_test, y_test = split_size.split (x_rd,data_y)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neural_network import MLPClassifier\nmodel = MLPClassifier(hidden_layer_sizes=(100,50,25),\n                            max_iter = 100,activation = 'relu',\n                            solver = 'adam')\nmodel_fit =model.fit(x_train,y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}