{"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-14T07:30:08.876986Z","iopub.execute_input":"2023-10-14T07:30:08.877349Z","iopub.status.idle":"2023-10-14T07:30:09.234220Z","shell.execute_reply.started":"2023-10-14T07:30:08.877323Z","shell.execute_reply":"2023-10-14T07:30:09.233197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/child-mind-institute-detect-sleep-states/'\ndf_test_data = pd.read_parquet(path + 'test_series.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:30:15.259162Z","iopub.execute_input":"2023-10-14T07:30:15.259753Z","iopub.status.idle":"2023-10-14T07:30:15.558680Z","shell.execute_reply.started":"2023-10-14T07:30:15.259716Z","shell.execute_reply":"2023-10-14T07:30:15.557992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FileLocation_data = 'ID-data' \nprint(' ')\ntry:\n    os.mkdir(FileLocation_data)\nexcept OSError:\n    print (\"Creation of the directory %s failed\" % FileLocation_data)\nelse:\n    print (\"Successfully created the directory %s \" % FileLocation_data)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:30:18.387335Z","iopub.execute_input":"2023-10-14T07:30:18.387696Z","iopub.status.idle":"2023-10-14T07:30:18.393617Z","shell.execute_reply.started":"2023-10-14T07:30:18.387667Z","shell.execute_reply":"2023-10-14T07:30:18.392679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport warnings \nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:30:20.241838Z","iopub.execute_input":"2023-10-14T07:30:20.242203Z","iopub.status.idle":"2023-10-14T07:30:20.246335Z","shell.execute_reply.started":"2023-10-14T07:30:20.242173Z","shell.execute_reply":"2023-10-14T07:30:20.245449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_SortId = df_test_data.series_id.value_counts(ascending=True).to_frame().reset_index()\ndf_SortId.columns = [ ['series_id', 'counts']]\ndf_SortId.to_csv('Sorted_ID.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:30:22.288295Z","iopub.execute_input":"2023-10-14T07:30:22.288631Z","iopub.status.idle":"2023-10-14T07:30:22.302655Z","shell.execute_reply.started":"2023-10-14T07:30:22.288604Z","shell.execute_reply":"2023-10-14T07:30:22.301917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# =============================================================================\n# \n# write to individual file\n# \n# =============================================================================\n        \nfor i in range(0,len(df_SortId)):\n\n    name_id = df_SortId.series_id.loc[i][0]   \n    df2 = df_test_data[df_test_data.series_id == name_id].reset_index(drop=True)\n    str1 = FileLocation_data  + '/data-' + str(i).zfill(3) + '-' + name_id\n    str_data = str1 + '-' + str(df2.shape[0]) + 'x' + str(df2.shape[1])\n    \n    df2.to_csv(str_data)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:30:25.082127Z","iopub.execute_input":"2023-10-14T07:30:25.082764Z","iopub.status.idle":"2023-10-14T07:30:25.096616Z","shell.execute_reply.started":"2023-10-14T07:30:25.082724Z","shell.execute_reply":"2023-10-14T07:30:25.096003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os import listdir\n\ndata_dir = './ID-data/'\ndata_paths = [data_dir + f for f in listdir(data_dir)] # 之後寫的東西只要對這個list裡面的每個東西都做就可以了","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:30:30.962191Z","iopub.execute_input":"2023-10-14T07:30:30.962566Z","iopub.status.idle":"2023-10-14T07:30:30.967475Z","shell.execute_reply.started":"2023-10-14T07:30:30.962530Z","shell.execute_reply":"2023-10-14T07:30:30.966666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 處裡一個file的data\ndef deal_with_one_date(which_file: str,answer: list):\n    # answer [[series_id, step, event, score: always 1.0]]\n    # event = 1 代表下一個是onset\n    event = 1\n    # 讀取資料\n    this_data = pd.read_csv(which_file, index_col=0)\n    # 處理讀進來的資料\n    diff_date = this_data[['series_id','step','timestamp','anglez','enmo']]\n    diff_date[['date','time']] = diff_date['timestamp'].str.split('T', expand=True)\n    # 取得所有不同的日期\n    dates = diff_date['date'].unique()\n    # 增加一個新的欄位並且套用公式 std( anglez ) + 20 * MA( enmo )\n    from sklearn.preprocessing import StandardScaler\n    scaler = StandardScaler()\n    diff_date['anglez'] = scaler.fit_transform(diff_date[['anglez']])\n    diff_date['total'] = diff_date['anglez'] + 20 * (diff_date['enmo'].rolling(window=300, min_periods=1).mean())\n    for date in dates:\n        period_data = diff_date[diff_date['date'] == date]\n        period_answer = diff_date[diff_date['total'] == min(diff_date['total'])]\n        event = 0\n        answer.append([list(period_answer['series_id'])[0], int(period_answer['step']), 'onset', 1.0])\n        period_answer = diff_date[diff_date['total'] == max(diff_date['total'])]\n        event = 1\n        answer.append([list(period_answer['series_id'])[0], int(period_answer['step']), 'wakeup', 0.0])\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:31:14.385116Z","iopub.execute_input":"2023-10-14T07:31:14.385471Z","iopub.status.idle":"2023-10-14T07:31:14.393842Z","shell.execute_reply.started":"2023-10-14T07:31:14.385443Z","shell.execute_reply":"2023-10-14T07:31:14.392856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_the_answer = []\nfor data_path in data_paths:\n    deal_with_one_date(data_path, all_the_answer)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:31:18.770345Z","iopub.execute_input":"2023-10-14T07:31:18.770662Z","iopub.status.idle":"2023-10-14T07:31:19.258565Z","shell.execute_reply.started":"2023-10-14T07:31:18.770637Z","shell.execute_reply":"2023-10-14T07:31:19.257839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, a in enumerate(all_the_answer):\n    a.reverse()\n    a.append(i)\n    a.reverse()\ndo_everything = {\n    \"row_id\":[],\n    \"series_id\":[],\n    \"step\":[],\n    \"event\":[],\n    \"score\":[]\n}\nfor a in all_the_answer:\n    do_everything[\"row_id\"].append(a[0])\n    do_everything[\"series_id\"].append(a[1])\n    do_everything[\"step\"].append(a[2])\n    do_everything[\"event\"].append(a[3])\n    do_everything[\"score\"].append(a[4])\ndo_everything\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:33:09.214010Z","iopub.execute_input":"2023-10-14T07:33:09.214401Z","iopub.status.idle":"2023-10-14T07:33:09.223299Z","shell.execute_reply.started":"2023-10-14T07:33:09.214372Z","shell.execute_reply":"2023-10-14T07:33:09.222462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(do_everything)\ndf.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T07:33:45.025094Z","iopub.execute_input":"2023-10-14T07:33:45.025456Z","iopub.status.idle":"2023-10-14T07:33:45.032452Z","shell.execute_reply.started":"2023-10-14T07:33:45.025427Z","shell.execute_reply":"2023-10-14T07:33:45.031263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}