{"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":[{"sourceType":"competition","sourceId":59093,"databundleVersionId":7469972},{"sourceType":"datasetVersion","sourceId":8095965,"datasetId":4780118,"databundleVersionId":8212957},{"sourceType":"modelInstanceVersion","sourceId":27914,"databundleVersionId":8183365,"modelInstanceId":23497},{"sourceType":"modelInstanceVersion","sourceId":29250,"databundleVersionId":8211881,"modelInstanceId":24641},{"sourceType":"modelInstanceVersion","sourceId":28922,"databundleVersionId":8205284,"modelInstanceId":24355},{"sourceType":"modelInstanceVersion","sourceId":27920,"databundleVersionId":8183406,"modelInstanceId":23502},{"sourceType":"modelInstanceVersion","sourceId":29256,"databundleVersionId":8212010,"modelInstanceId":24647}],"dockerImageVersionId":30684,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip uninstall keras -y","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:39:11.074945Z","iopub.execute_input":"2024-04-12T02:39:11.075365Z","iopub.status.idle":"2024-04-12T02:39:15.143727Z","shell.execute_reply.started":"2024-04-12T02:39:11.075309Z","shell.execute_reply":"2024-04-12T02:39:15.142385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install --no-index --find-links /kaggle/input/hms-dependencies keras","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:39:15.146241Z","iopub.execute_input":"2024-04-12T02:39:15.146699Z","iopub.status.idle":"2024-04-12T02:39:32.000552Z","shell.execute_reply.started":"2024-04-12T02:39:15.146655Z","shell.execute_reply":"2024-04-12T02:39:31.999329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install --no-index --find-links /kaggle/input/hms-dependencies sktime","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:39:32.002156Z","iopub.execute_input":"2024-04-12T02:39:32.002569Z","iopub.status.idle":"2024-04-12T02:39:50.331655Z","shell.execute_reply.started":"2024-04-12T02:39:32.002534Z","shell.execute_reply":"2024-04-12T02:39:50.330233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\nfrom sklearn import preprocessing\nimport multiprocessing as mp\nfrom pathos.multiprocessing import Pool\n\nimport tqdm\nfrom scipy import signal\n\nfrom sktime.classification.deep_learning.lstmfcn import LSTMFCNClassifier\nfrom sktime.regression.deep_learning.mcdcnn import MCDCNNRegressor\nfrom sktime.networks.mcdcnn import MCDCNNNetwork\n\nfrom sktime.regression.base import BaseRegressor\n\nimport tensorflow as tf\n\nimport pickle\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-04-12T02:39:55.139173Z","iopub.execute_input":"2024-04-12T02:39:55.139606Z","iopub.status.idle":"2024-04-12T02:40:20.936054Z","shell.execute_reply.started":"2024-04-12T02:39:55.139568Z","shell.execute_reply":"2024-04-12T02:40:20.934790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nkeras.__version__","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:40:20.938153Z","iopub.execute_input":"2024-04-12T02:40:20.939381Z","iopub.status.idle":"2024-04-12T02:40:20.952434Z","shell.execute_reply.started":"2024-04-12T02:40:20.939332Z","shell.execute_reply":"2024-04-12T02:40:20.948379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lstmfcn_cls = LSTMFCNClassifier()\n\nlstmfcn_cls.model_ = tf.keras.models.load_model(\"/kaggle/input/hms_trained_lstmfcn/keras/v3/1/model.keras\")\nlstmfcn_cls.classes_ = [0, 1, 2, 3, 4, 5]","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:47:23.321397Z","iopub.execute_input":"2024-04-12T02:47:23.321989Z","iopub.status.idle":"2024-04-12T02:47:24.198383Z","shell.execute_reply.started":"2024-04-12T02:47:23.321939Z","shell.execute_reply":"2024-04-12T02:47:24.197184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_master = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/test.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:42:09.364895Z","iopub.execute_input":"2024-04-12T02:42:09.365540Z","iopub.status.idle":"2024-04-12T02:42:09.375310Z","shell.execute_reply.started":"2024-04-12T02:42:09.365497Z","shell.execute_reply":"2024-04-12T02:42:09.374324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pairs = [['Fp1', 'F7'], ['F7', 'T3'], ['T3', 'T5'], \n         ['T5', 'O1'], ['Fp2', 'F8'], ['F8', 'T4'], \n         ['T4', 'T6'], ['T6', 'O2'], ['Fp1', 'F3'], \n        ['F3', 'C3'], ['C3', 'P3'], ['P3', 'O1'], \n        ['Fp2', 'F4'], ['F4', 'C4'], ['C4', 'P4'], \n         ['P4', 'O2'], ['Fz', 'Cz'], ['Cz', 'Pz']]\n\ndef get_features_for_entry(ir, q=2):\n    \n    r = ir[1]\n    \n    ex_eeg = pd.read_parquet(\"../input/hms-harmful-brain-activity-classification/test_eegs/{}.parquet\".format(r['eeg_id']))\n    ex_spec = pd.read_parquet(\"../input/hms-harmful-brain-activity-classification/test_spectrograms/{}.parquet\".format(r['spectrogram_id']))\n    \n    eeg = ex_eeg\n    eeg_subsample = eeg.iloc[20*200:30*200]\n    times = np.arange(0, eeg_subsample.shape[0]/200, 1/200.) - 5.\n    \n    middle_50_spec = ex_spec.loc[(ex_spec.time>=275)\n                     &(ex_spec.time<325)]\n \n    \n    output_eeg_X = np.array([])\n    \n    for p in pairs:\n        row = np.nan_to_num(eeg_subsample[p[0]].values - eeg_subsample[p[1]].values)\n        #downsample\n        row = signal.decimate(row, q)\n        \n        if output_eeg_X.shape[0] == 0:\n            output_eeg_X = preprocessing.normalize([row])[0]\n        else:\n            output_eeg_X = np.vstack((output_eeg_X, preprocessing.normalize([row])[0]))\n    \n            \n    return pd.Series({\"X_eeg\": output_eeg_X, \n                      'spec_values': middle_50_spec.iloc[:, 1:].values})","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:42:10.590071Z","iopub.execute_input":"2024-04-12T02:42:10.590529Z","iopub.status.idle":"2024-04-12T02:42:10.604930Z","shell.execute_reply.started":"2024-04-12T02:42:10.590492Z","shell.execute_reply":"2024-04-12T02:42:10.603635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for item in test_master.iterrows():\n    res = get_features_for_entry(item)","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:42:10.915074Z","iopub.execute_input":"2024-04-12T02:42:10.915859Z","iopub.status.idle":"2024-04-12T02:42:11.013798Z","shell.execute_reply.started":"2024-04-12T02:42:10.915817Z","shell.execute_reply":"2024-04-12T02:42:11.012616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(res)","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:42:11.184750Z","iopub.execute_input":"2024-04-12T02:42:11.185905Z","iopub.status.idle":"2024-04-12T02:42:11.227434Z","shell.execute_reply.started":"2024-04-12T02:42:11.185864Z","shell.execute_reply":"2024-04-12T02:42:11.226309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if len(res['X_eeg'].shape) == 2:\n     prediction = lstmfcn_cls._predict_proba(np.array([res['X_eeg']]))\nelse:\n     prediction = lstmfcn_cls._predict_proba(np.array(res['X_eeg'].values.tolist()))","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:42:12.084798Z","iopub.execute_input":"2024-04-12T02:42:12.085462Z","iopub.status.idle":"2024-04-12T02:42:12.195629Z","shell.execute_reply.started":"2024-04-12T02:42:12.085429Z","shell.execute_reply":"2024-04-12T02:42:12.194385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_names=[\"eeg_id\", \"seizure_vote\", \"lpd_vote\", \"gpd_vote\", \"lrda_vote\", \"grda_vote\", \"other_vote\"]\n\nnormed = np.round(prediction, 4)\n\nrows = []\n\nfor i in range(test_master.shape[0]):\n     rows.append(np.append(test_master['eeg_id'].values[i], normed/normed.sum()))\n\n\nresult_df = pd.DataFrame(rows, columns = col_names)\n\ndisplay(result_df)\nresult_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:42:12.819892Z","iopub.execute_input":"2024-04-12T02:42:12.820354Z","iopub.status.idle":"2024-04-12T02:42:12.840270Z","shell.execute_reply.started":"2024-04-12T02:42:12.820309Z","shell.execute_reply":"2024-04-12T02:42:12.838679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###Was going to try to reweight somehow...\n#model i trained refuses to load, so may need to try something else","metadata":{"execution":{"iopub.status.busy":"2024-04-12T02:04:11.710461Z","iopub.execute_input":"2024-04-12T02:04:11.712474Z","iopub.status.idle":"2024-04-12T02:04:11.725131Z","shell.execute_reply.started":"2024-04-12T02:04:11.712423Z","shell.execute_reply":"2024-04-12T02:04:11.723769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}