{"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":"2023-10-15T01:26:28.050049Z","iopub.execute_input":"2023-10-15T01:26:28.050433Z","iopub.status.idle":"2023-10-15T01:26:28.058070Z","shell.execute_reply.started":"2023-10-15T01:26:28.050403Z","shell.execute_reply":"2023-10-15T01:26:28.057009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings \nwarnings.filterwarnings('ignore')\n\nfrom datetime import datetime, timedelta\n\n\nfile_train_series = '/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet'\nfile_sample_submission = '/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv'\nfile_train_events = '/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv'\nfile_test_series = '/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet'","metadata":{"execution":{"iopub.status.busy":"2023-10-15T01:26:28.060063Z","iopub.execute_input":"2023-10-15T01:26:28.060539Z","iopub.status.idle":"2023-10-15T01:26:28.076860Z","shell.execute_reply.started":"2023-10-15T01:26:28.060509Z","shell.execute_reply":"2023-10-15T01:26:28.075751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df1 = pd.read_parquet(file_train_series)\ndf1 = pd.read_parquet(file_test_series)\nprint(df1.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T01:26:28.078427Z","iopub.execute_input":"2023-10-15T01:26:28.078830Z","iopub.status.idle":"2023-10-15T01:26:30.419905Z","shell.execute_reply.started":"2023-10-15T01:26:28.078794Z","shell.execute_reply":"2023-10-15T01:26:30.418720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# =============================================================================\n# \n# sort data into separate dataframes according to each id\n# \n# =============================================================================\n\ndf_SortId = df1.series_id.value_counts(ascending=True).to_frame().reset_index()\ndf_SortId.columns = ['series_id', 'counts']\ndf_SortId = df_SortId.sort_values(\"series_id\",ascending=True).reset_index(drop=True)\n# df_SortId.to_csv('Sorted_ID.csv')\nprint(df_SortId.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T01:26:30.420982Z","iopub.execute_input":"2023-10-15T01:26:30.421392Z","iopub.status.idle":"2023-10-15T01:26:30.432569Z","shell.execute_reply.started":"2023-10-15T01:26:30.421355Z","shell.execute_reply":"2023-10-15T01:26:30.431243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# =============================================================================\n# \n# assign dummy answers according to series_id, sequentially\n# \n# =============================================================================\n\n# good case: series_id = def21f50dd3c \n\nindex = 0\ndf_answer = pd.DataFrame()","metadata":{"execution":{"iopub.status.busy":"2023-10-15T01:26:30.435800Z","iopub.execute_input":"2023-10-15T01:26:30.436715Z","iopub.status.idle":"2023-10-15T01:26:30.446570Z","shell.execute_reply.started":"2023-10-15T01:26:30.436670Z","shell.execute_reply":"2023-10-15T01:26:30.445587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for input_number in range (0,len(df_SortId)):\n# for input_number in range (0,3):\n    \n    series_id = str(df_SortId['series_id'].iloc[input_number])\n    \n    df2 = df1[ df1['series_id'] == series_id]\n       \n    df2[['date','time_all']] = df2['timestamp'].str.split('T', expand=True)\n    df2[['time_1','time_2']] = df2['time_all'].str.split('-', expand=True)\n    df2['dt'] = df2['date'] + ' ' + df2['time_1']\n    df2['datetime'] = pd.to_datetime(df2['dt'])\n    \n    df3 = df2[['series_id','step','datetime','date','time_1','anglez','enmo']]\n \n    dates = df3['date'].unique()\n    \n    date_start = df3['date'].iloc[0]\n    date_end =   df3['date'].iloc[-1]\n    \n    print('data =',input_number,', series_id =',series_id, ',', len(dates), 'days')\n\n    for i in range (0,len(dates)):\n        \n        time_tmp = dates[i] + ' 14:00:00'\n        time_start = datetime.strptime(time_tmp, \"%Y-%m-%d %H:%M:%S\")\n        time_end = time_start + timedelta(days=1)\n        print(i)\n        print('from', time_start, 'to', time_end)\n\n        time_tmp = dates[i] + ' 22:30:00'\n        time_sleep = datetime.strptime(time_tmp, \"%Y-%m-%d %H:%M:%S\")   \n        time_wakeup = time_sleep + timedelta(minutes=510)\n        \n        df4 = df3[(df3['datetime'] > time_start) & (df3['datetime'] <= time_end)]\n\n        try:\n            df5 = df4[ df4['datetime'] > time_sleep]\n            step_sleep = df5['step'].iloc[0].astype(int)\n            # print('   series_id =', series_id, ', step =', step_sleep,  ', event = onset, score = 0.7')\n            data_add = [{'row_id': index, 'series_id':series_id, 'step': step_sleep,\\\n                          'event': 'onset', 'score':0.9}]\n            df_add = pd.DataFrame(data_add)  \n            df_answer = pd.concat([df_answer,df_add], axis=0)\n            index = index + 1\n        except:\n            print(' ----- ')\n            \n        try:\n            df5 = df4[ df4['datetime'] > time_wakeup]\n            step_wakeup = df5['step'].iloc[0].astype(int)\n            # print('   series_id =', series_id, ', step =', step_wakeup, ', event = wakeup, score = 0.7 \\n')\n            data_add = [{'row_id': index, 'series_id':series_id, 'step': step_wakeup,\\\n                          'event': 'wakeup', 'score':0.9}]\n            df_add = pd.DataFrame(data_add)  \n            df_answer = pd.concat([df_answer,df_add], axis=0)\n            index = index + 1\n        except:\n            print(' ----- ')","metadata":{"execution":{"iopub.status.busy":"2023-10-15T01:26:30.447858Z","iopub.execute_input":"2023-10-15T01:26:30.448315Z","iopub.status.idle":"2023-10-15T01:26:30.588136Z","shell.execute_reply.started":"2023-10-15T01:26:30.448254Z","shell.execute_reply":"2023-10-15T01:26:30.587056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if(len(df_answer) > 0):\n    print(df_answer.shape)\n    print(df_answer.head())\n    print(df_answer.tail())","metadata":{"execution":{"iopub.status.busy":"2023-10-15T01:26:30.589649Z","iopub.execute_input":"2023-10-15T01:26:30.590086Z","iopub.status.idle":"2023-10-15T01:26:30.595031Z","shell.execute_reply.started":"2023-10-15T01:26:30.590058Z","shell.execute_reply":"2023-10-15T01:26:30.593898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if(len(df_answer) > 0):\n    df_answer.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T01:26:30.596740Z","iopub.execute_input":"2023-10-15T01:26:30.597464Z","iopub.status.idle":"2023-10-15T01:26:30.609130Z","shell.execute_reply.started":"2023-10-15T01:26:30.597425Z","shell.execute_reply":"2023-10-15T01:26:30.607962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}