{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-08T11:22:34.144088Z","iopub.execute_input":"2023-09-08T11:22:34.145678Z","iopub.status.idle":"2023-09-08T11:22:34.693325Z","shell.execute_reply.started":"2023-09-08T11:22:34.145608Z","shell.execute_reply":"2023-09-08T11:22:34.692135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events = pd.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\")\ntrain_series = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\")","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:22:34.697569Z","iopub.execute_input":"2023-09-08T11:22:34.698126Z","iopub.status.idle":"2023-09-08T11:23:37.397361Z","shell.execute_reply.started":"2023-09-08T11:22:34.698091Z","shell.execute_reply":"2023-09-08T11:23:37.395902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:23:37.400178Z","iopub.execute_input":"2023-09-08T11:23:37.400704Z","iopub.status.idle":"2023-09-08T11:23:37.432087Z","shell.execute_reply.started":"2023-09-08T11:23:37.400658Z","shell.execute_reply":"2023-09-08T11:23:37.430761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:23:37.435401Z","iopub.execute_input":"2023-09-08T11:23:37.436702Z","iopub.status.idle":"2023-09-08T11:23:37.458924Z","shell.execute_reply.started":"2023-09-08T11:23:37.436647Z","shell.execute_reply":"2023-09-08T11:23:37.457417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = train_series.groupby('series_id')['step'].max()\nvalues = values.reset_index()\nseries = pd.unique(train_events['series_id'])","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:23:37.460226Z","iopub.execute_input":"2023-09-08T11:23:37.460669Z","iopub.status.idle":"2023-09-08T11:23:53.643522Z","shell.execute_reply.started":"2023-09-08T11:23:37.460628Z","shell.execute_reply":"2023-09-08T11:23:53.642237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = train_events[['series_id', 'event', 'step']]\n\nfor ser in series:\n    df = train_events[train_events['series_id'] == ser]\n    max_step = values[values['series_id'] == ser]['step'].max()\n    \n    if df['step'].isnull().all():\n        df_aux = pd.DataFrame({'series_id': [ser], 'event': ['None'], 'step': [max_step]})\n        df_train = pd.concat([df_train, df_aux], ignore_index = True)\n    \n    else:\n        df_next = df.bfill()\n\n        for i in df[df['step'].isnull()].index:\n            if not np.isnan(df_next['step'][i]):\n                df_aux = pd.DataFrame({'series_id': [ser], 'event': ['None'], 'step': [int(df_next['step'][i] - 1)]})\n                df_train = pd.concat([df_train, df_aux], ignore_index = True)\n\n        if df_next['step'].isnull().any():\n            df_aux = pd.DataFrame({'series_id': [ser], 'event': ['None'], 'step': [max_step]})\n            df_train = pd.concat([df_train, df_aux], ignore_index = True)\n\ndf_train = df_train.drop_duplicates()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:23:53.645737Z","iopub.execute_input":"2023-09-08T11:23:53.646254Z","iopub.status.idle":"2023-09-08T11:23:57.768608Z","shell.execute_reply.started":"2023-09-08T11:23:53.646219Z","shell.execute_reply":"2023-09-08T11:23:57.767114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.sort_values(by=['series_id', 'step'])\n\ndf_train = df_train.dropna()\ndf_train['step']  = df_train['step'].astype('int')\ndf_train['awake'] = df_train['event'].replace({'onset': 1,'wakeup': 0, 'None': -1})\n\ndf_train","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:23:57.770229Z","iopub.execute_input":"2023-09-08T11:23:57.770615Z","iopub.status.idle":"2023-09-08T11:23:57.812046Z","shell.execute_reply.started":"2023-09-08T11:23:57.770582Z","shell.execute_reply":"2023-09-08T11:23:57.810111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.merge(train_series, df_train[['series_id', 'step', 'awake']], on=['series_id', 'step'], how='left')\ntrain[\"awake\"] = train[\"awake\"].bfill(axis ='rows')\n\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:23:57.814091Z","iopub.execute_input":"2023-09-08T11:23:57.814574Z","iopub.status.idle":"2023-09-08T11:25:28.387818Z","shell.execute_reply.started":"2023-09-08T11:23:57.814522Z","shell.execute_reply":"2023-09-08T11:25:28.386178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(train[train['awake'] == -1].index)\ntrain['awake'] = train['awake'].astype('int')\n\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:26:16.976535Z","iopub.execute_input":"2023-09-08T11:26:16.977010Z","iopub.status.idle":"2023-09-08T11:26:32.177580Z","shell.execute_reply.started":"2023-09-08T11:26:16.976976Z","shell.execute_reply":"2023-09-08T11:26:32.176459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_parquet('CMI_DetectSleepStates.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:26:45.617979Z","iopub.execute_input":"2023-09-08T11:26:45.618452Z","iopub.status.idle":"2023-09-08T11:27:17.851745Z","shell.execute_reply.started":"2023-09-08T11:26:45.618415Z","shell.execute_reply":"2023-09-08T11:27:17.850269Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}