{"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-11-13T14:37:00.376419Z","iopub.execute_input":"2023-11-13T14:37:00.377074Z","iopub.status.idle":"2023-11-13T14:37:00.849247Z","shell.execute_reply.started":"2023-11-13T14:37:00.377035Z","shell.execute_reply":"2023-11-13T14:37:00.847671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings \nwarnings.filterwarnings('ignore')\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-11-13T14:37:00.851608Z","iopub.execute_input":"2023-11-13T14:37:00.852074Z","iopub.status.idle":"2023-11-13T14:37:00.860750Z","shell.execute_reply.started":"2023-11-13T14:37:00.852041Z","shell.execute_reply":"2023-11-13T14:37:00.858832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.read_parquet(file_test_series)\nprint(df1.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T14:37:00.862320Z","iopub.execute_input":"2023-11-13T14:37:00.862745Z","iopub.status.idle":"2023-11-13T14:37:01.206917Z","shell.execute_reply.started":"2023-11-13T14:37:00.862713Z","shell.execute_reply":"2023-11-13T14:37:01.205802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_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-11-13T14:37:01.212436Z","iopub.execute_input":"2023-11-13T14:37:01.213432Z","iopub.status.idle":"2023-11-13T14:37:01.230541Z","shell.execute_reply.started":"2023-11-13T14:37:01.213381Z","shell.execute_reply":"2023-11-13T14:37:01.229451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 0\ndf_answer = pd.DataFrame()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T14:37:01.232149Z","iopub.execute_input":"2023-11-13T14:37:01.233081Z","iopub.status.idle":"2023-11-13T14:37:01.238242Z","shell.execute_reply.started":"2023-11-13T14:37:01.233046Z","shell.execute_reply":"2023-11-13T14:37:01.237273Z"},"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)-1):\n        \n        time_start = dates[i] + ' 14:00:00'\n        time_end = dates[i+1] + ' 14:00:00'\n        # print(i, 'from', time_start, 'to', time_end)\n        \n        time_sleep = dates[i] + ' 22:30:00'\n        time_wakeup = dates[i+1] + ' 06:30:00'\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-11-13T14:37:01.239866Z","iopub.execute_input":"2023-11-13T14:37:01.240604Z","iopub.status.idle":"2023-11-13T14:37:01.297727Z","shell.execute_reply.started":"2023-11-13T14:37:01.240571Z","shell.execute_reply":"2023-11-13T14:37:01.296446Z"},"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-11-13T14:37:01.299274Z","iopub.execute_input":"2023-11-13T14:37:01.299978Z","iopub.status.idle":"2023-11-13T14:37:01.311790Z","shell.execute_reply.started":"2023-11-13T14:37:01.299926Z","shell.execute_reply":"2023-11-13T14:37:01.310744Z"},"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-11-13T14:37:01.313340Z","iopub.execute_input":"2023-11-13T14:37:01.314195Z","iopub.status.idle":"2023-11-13T14:37:01.327804Z","shell.execute_reply.started":"2023-11-13T14:37:01.314157Z","shell.execute_reply":"2023-11-13T14:37:01.326782Z"},"trusted":true},"execution_count":null,"outputs":[]}]}