{"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"toc":{"base_numbering":1,"nav_menu":{},"number_sections":true,"sideBar":true,"skip_h1_title":false,"title_cell":"Table of Contents","title_sidebar":"Contents","toc_cell":false,"toc_position":{"height":"calc(100% - 180px)","left":"10px","top":"150px","width":"165px"},"toc_section_display":true,"toc_window_display":true},"varInspector":{"cols":{"lenName":16,"lenType":16,"lenVar":40},"kernels_config":{"python":{"delete_cmd_postfix":"","delete_cmd_prefix":"del ","library":"var_list.py","varRefreshCmd":"print(var_dic_list())"},"r":{"delete_cmd_postfix":") ","delete_cmd_prefix":"rm(","library":"var_list.r","varRefreshCmd":"cat(var_dic_list()) "}},"types_to_exclude":["module","function","builtin_function_or_method","instance","_Feature"],"window_display":false},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%config Completer.use_jedi = False ","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA: plotting anglez, enmo, events and non-events for the first 21 people","metadata":{}},{"cell_type":"code","source":"#| default_exp core\nfrom fastcore.all import Path\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IN = Path('../input')\nRAW = IN/'child-mind-institute-detect-sleep-states'\nsteps_in_day = 12 * 60 * 24 # based on steps being 5 seconds\nfirst_n_rows = 10_000_000\nprint(f'There are {steps_in_day} steps in a day')\nprint(f'We will consider data from the first {first_n_rows:,} rows in the train series data')","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Input files","metadata":{}},{"cell_type":"code","source":"[x.stem + x.suffix for x in RAW.ls()]","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss = pd.read_csv(RAW/'sample_submission.csv')\ntrain_events = pd.read_csv(RAW/'train_events.csv')\ntrain_series = pd.read_parquet(RAW/'train_series.parquet')\ntre = train_events\ntrs = train_series.iloc[:first_n_rows, :]\nsids = trs.series_id.unique()[:-1] # Only sids with full data\nprint(f'We have {len(sids)} sids to consider and they are', sids)\ntrs = trs[trs.series_id.isin(sids)]\ntre = tre[tre.series_id.isin(sids)]","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plotting","metadata":{}},{"cell_type":"code","source":"def plot(df_series, df_events, title='time_chunk'): \n    fig, ax = plt.subplots(figsize=(15, 5))\n    df_series['anglez'].plot(ax=ax, alpha=.3, color='b')\n    ax.set_ylabel('anglez', color='b')\n    handles = [ax.get_legend_handles_labels()[0][-1]]\n    labels = ['anglez']\n    ax1 = ax.twinx()\n    df_series['enmo'].plot(ax=ax1, color='g', alpha=.3)\n    ax1.set_ylabel('enmo', color='g')\n    \n    noon = df_series[df_series.is_noon]\n    steps = [(df_series.index[0] - 1, 'noon')]\n    for i, step in enumerate(noon.index): \n        steps.append((step, 'noon'))\n        ax.axvline(step, color='black', alpha=1, linestyle='-.', label='noon')\n        if i == 0: \n            handles.append(ax.get_legend_handles_labels()[0][-1])\n            labels.append('noon')\n    for i, step in enumerate(df_events.query('event == \"onset\"').index): \n        steps.append((step, 'onset'))\n        ax.axvline(step, color='r', alpha=1, linestyle='-', label='onset')\n        if i == 0: \n            handles.append(ax.get_legend_handles_labels()[0][-1])\n            labels.append('onset')\n    for i, step in enumerate(df_events.query('event == \"wakeup\"').index): \n        steps.append((step, 'wakeup'))\n        ax.axvline(step, color='orange', alpha=1, linestyle='-', label='wakeup')\n        if i == 0: \n            handles.append(ax.get_legend_handles_labels()[0][-1])\n            labels.append('wakeup')\n    steps = sorted(steps)\n    for i, (step, kind) in enumerate(steps[1:], start=1): \n        if kind == 'noon': \n            if steps[i - 1][1] == 'onset': \n                ax.axvline(step - steps_in_day // 4, color='purple', alpha=.6, \n                           linestyle='-', label='no_wakeup', linewidth=15.0)\n                handles.append(ax.get_legend_handles_labels()[0][-1])\n                labels.append('no_wakeup')\n            if steps[i - 1][1] == 'noon': \n                ax.axvline(step - steps_in_day // 2, color='brown', alpha=.6, \n                           linestyle='-', label='no_onset', linewidth=15.0)\n                handles.append(ax.get_legend_handles_labels()[0][-1])\n                labels.append('no_onset')\n    ax.legend(loc='upper left', handles=handles, labels=labels)\n    ax1.legend()\n    plt.suptitle(title)\n    plt.show()","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sid(sid = '0ce74d6d2106', days_per_graph = 3, return_dfs=False):\n    df = trs[(trs.series_id == sid)]\n    df = df.assign(is_noon=df.timestamp.str[11:19] == '12:00:00',\n                  mod_step=df.step % steps_in_day).set_index('step')\n    dfe_with_nans = tre[(tre.series_id == sid)]\n    nights = dfe_with_nans.night.max()\n    print('', '*' * 100, '\\n', '*' * 38, sid, nights, 'nights', '*' * 38, '\\n', '*' * 100)\n    dfe = dfe_with_nans.query('step >= 0')\n    dfe = dfe.assign(step = dfe.step.astype(int)).set_index('step')\n    first_noon_index = int(df[df.is_noon].index[0])\n    if first_noon_index > 0:\n        df_series = df.iloc[:first_noon_index]\n        df_events = dfe.join(df_series[[]], how='inner') \n        plot(df_series, df_events, title=f'sid {sid}: data until first noon')\n\n    for i, chunk in enumerate(range(first_noon_index + 1, len(df), days_per_graph * steps_in_day)): \n        total_chunks = len(df) // (days_per_graph * steps_in_day) + 1\n        df_series = df.iloc[chunk: chunk + days_per_graph * steps_in_day]\n        df_events = dfe.join(df_series[[]], how='inner')  \n        plot(df_series, df_events, title = f'sid {sid}: chunk {i + 1} of {total_chunks}: interval {days_per_graph} days')\n    if return_dfs: return df, dfe_with_nans","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for sid in sids: \n    plot_sid(sid)","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]}]}