{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport torch\n","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:05:03.150355Z","iopub.execute_input":"2023-11-21T08:05:03.151203Z","iopub.status.idle":"2023-11-21T08:05:07.562141Z","shell.execute_reply.started":"2023-11-21T08:05:03.151156Z","shell.execute_reply":"2023-11-21T08:05:07.561011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","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"}},{"cell_type":"code","source":"train_event=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-11-21T08:05:07.564914Z","iopub.execute_input":"2023-11-21T08:05:07.565565Z","iopub.status.idle":"2023-11-21T08:06:02.851213Z","shell.execute_reply.started":"2023-11-21T08:05:07.565519Z","shell.execute_reply":"2023-11-21T08:06:02.849862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_event=train_event.dropna()\ntrain_event.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:06:02.852484Z","iopub.execute_input":"2023-11-21T08:06:02.852822Z","iopub.status.idle":"2023-11-21T08:06:02.880103Z","shell.execute_reply.started":"2023-11-21T08:06:02.852794Z","shell.execute_reply":"2023-11-21T08:06:02.878880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_event['timestamp']=pd.to_datetime(train_event['timestamp'],utc=True)\ntrain_event['timestamp'].head()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:06:02.883431Z","iopub.execute_input":"2023-11-21T08:06:02.883983Z","iopub.status.idle":"2023-11-21T08:06:02.986576Z","shell.execute_reply.started":"2023-11-21T08:06:02.883936Z","shell.execute_reply":"2023-11-21T08:06:02.985387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series.isnull().any().any()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:06:02.987905Z","iopub.execute_input":"2023-11-21T08:06:02.988229Z","iopub.status.idle":"2023-11-21T08:06:29.547463Z","shell.execute_reply.started":"2023-11-21T08:06:02.988200Z","shell.execute_reply":"2023-11-21T08:06:29.546217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"trainseries没有缺失数据","metadata":{}},{"cell_type":"code","source":"train_series.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:06:29.548922Z","iopub.execute_input":"2023-11-21T08:06:29.549238Z","iopub.status.idle":"2023-11-21T08:06:29.562340Z","shell.execute_reply.started":"2023-11-21T08:06:29.549211Z","shell.execute_reply":"2023-11-21T08:06:29.561210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# create a series_id to index dictionary\nunique_ids=train_event['series_id'].unique()\nseries_id_to_idx={series_ids:idx for idx, series_ids in enumerate(unique_ids)}\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:06:29.564088Z","iopub.execute_input":"2023-11-21T08:06:29.564537Z","iopub.status.idle":"2023-11-21T08:06:29.573828Z","shell.execute_reply.started":"2023-11-21T08:06:29.564495Z","shell.execute_reply":"2023-11-21T08:06:29.572660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seriestemp=train_series.groupby('series_id')\nseriestemp2=seriestemp.get_group('038441c925bb')\nseriestemp2.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:06:29.575313Z","iopub.execute_input":"2023-11-21T08:06:29.575865Z","iopub.status.idle":"2023-11-21T08:06:48.333592Z","shell.execute_reply.started":"2023-11-21T08:06:29.575825Z","shell.execute_reply":"2023-11-21T08:06:48.332402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series['state']=None\nts=train_series.groupby('series_id')\nte=train_event.groupby('series_id')","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:06:48.335397Z","iopub.execute_input":"2023-11-21T08:06:48.335904Z","iopub.status.idle":"2023-11-21T08:06:49.572772Z","shell.execute_reply.started":"2023-11-21T08:06:48.335862Z","shell.execute_reply":"2023-11-21T08:06:49.571257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_ts = pd.DataFrame(columns=['series_id', 'step', 'timestamp', 'anglez','enmo','state'])\nfrom tqdm import tqdm\nimport time\nfor uid in tqdm(unique_ids):\n    events=te.get_group(uid)\n    series=ts.get_group(uid)\n    for event in events.itertuples():\n        if event.event=='onset':\n            series.loc[((event.step-360)<=series['step']) & (series['step']<=(event.step+360))]['state']=0\n            series.loc[(series['step']<(event.step-360)) & (series['state'].isnull())]['state']=3\n        \n        elif event.event=='wakeup':\n            series.loc[((event.step-360)<=series['step']) & (series['step']<=(event.step+360))]['state']=2\n            series.loc[(series['step']<(event.step-360)) & (series['state'].isnull())]['state']=1\n    \n    result_ts=pd.concat([result_ts,series],ignore_index=True)\n\n    #result_ts.tail()\n    #print(f'uid={uid}')\n            ","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:06:49.574899Z","iopub.execute_input":"2023-11-21T08:06:49.575243Z","iopub.status.idle":"2023-11-21T08:09:56.578363Z","shell.execute_reply.started":"2023-11-21T08:06:49.575215Z","shell.execute_reply":"2023-11-21T08:09:56.576450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_ts.to_parquet('/kaggle/train_series_result.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-11-21T08:09:56.579495Z","iopub.status.idle":"2023-11-21T08:09:56.580336Z","shell.execute_reply.started":"2023-11-21T08:09:56.580101Z","shell.execute_reply":"2023-11-21T08:09:56.580122Z"},"trusted":true},"execution_count":null,"outputs":[]}]}