{"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":"markdown","source":"Published on September 15, 2023. By Marília Prata, mpwolke","metadata":{"_kg_hide-input":false}},{"cell_type":"markdown","source":"#The \"Zzzs\"\n\n\"The “Zzzs” you catch each night are crucial for your overall health. Sleep affects everything from your development to cognitive functioning. Even so, research into sleep has proved challenging, due to the lack of naturalistic data capture alongside accurate annotation. If data science could help researchers better analyze wrist-worn accelerometer data for sleep monitoring, sleep experts could more easily conduct large-scale studies of sleep, thus improving the understanding of sleep's importance and function.\"\n\nhttps://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states","metadata":{}},{"cell_type":"markdown","source":"#I hope that child isn't sleeping. Zzzs tight Kiddo! \n\nArm rotation using angle-z, angle-y and angle-x as parameters\n\n<iframe width=\"408\" height=\"396\" src=\"https://www.youtube.com/embed/KGpMEk3TSYs\" title=\"body movement\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen></iframe>\n\nhttps://www.youtube.com/watch?v=KGpMEk3TSYs\nVideo by kaggler Paulo Yun Cha","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom datetime import datetime as dt\nfrom itertools import groupby\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nimport optuna\nimport lightgbm as lgb\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nplt.style.use('ggplot')","metadata":{"execution":{"iopub.status.busy":"2023-09-16T19:19:23.760813Z","iopub.execute_input":"2023-09-16T19:19:23.761168Z","iopub.status.idle":"2023-09-16T19:19:28.708667Z","shell.execute_reply.started":"2023-09-16T19:19:23.761139Z","shell.execute_reply":"2023-09-16T19:19:28.707520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Hi Carl I couldn't miss your Zzzs Dataset!\n\nBesides, I needed a file with Zzzs name. Here I am. 😴 💤","metadata":{}},{"cell_type":"code","source":"Zzzs = pd.read_parquet('/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train.parquet')\nZzzs.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-16T19:19:28.710404Z","iopub.execute_input":"2023-09-16T19:19:28.710721Z","iopub.status.idle":"2023-09-16T19:19:37.823489Z","shell.execute_reply.started":"2023-09-16T19:19:28.710692Z","shell.execute_reply":"2023-09-16T19:19:37.822580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Van Hees - Estimating z-axis angle, making basic assumptions about sleep interruptions\n\nEstimating sleep parameters using an accelerometer without sleep diary\n\nCitation: van Hees, V.T., Sabia, S., Jones, S.E. et al. Estimating sleep parameters using an accelerometer without sleep diary. Sci Rep 8, 12975 (2018). https://doi.org/10.1038/s41598-018-31266-z\n\n\"Wrist worn raw-data accelerometers are used increasingly in large-scale population research. The authors examined whether sleep parameters can be estimated from these data in the absence of sleep diaries. Their heuristic algorithm used the variance in estimated z-axis angle and makes basic assumptions about sleep interruptions.\"\n\nhttps://www.nature.com/articles/s41598-018-31266-z","metadata":{}},{"cell_type":"markdown","source":"#Smartphone placement attached to the subject's thorax with an elastic band. \n\nThe orientation of the smartphone accelerometer's axes and the sleep and stand angles are also indicated.\n\n![](https://www.researchgate.net/publication/355846940/figure/fig1/AS:1085682944610304@1635858393029/Smartphone-placement-attached-to-the-subjects-thorax-with-an-elastic-band-The.png)https://www.researchgate.net/figure/Smartphone-placement-attached-to-the-subjects-thorax-with-an-elastic-band-The_fig1_355846940","metadata":{}},{"cell_type":"code","source":"Zzzs.tail()","metadata":{"execution":{"iopub.status.busy":"2023-09-16T19:19:46.247008Z","iopub.execute_input":"2023-09-16T19:19:46.247376Z","iopub.status.idle":"2023-09-16T19:19:46.266189Z","shell.execute_reply.started":"2023-09-16T19:19:46.247346Z","shell.execute_reply":"2023-09-16T19:19:46.265127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Sarthak Jain https://www.kaggle.com/code/sarthak333/predict-sleep\n\nZzzs[Zzzs['series_id']=='d5e47b94477e']['awake'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-16T19:20:02.495591Z","iopub.execute_input":"2023-09-16T19:20:02.496938Z","iopub.status.idle":"2023-09-16T19:20:03.666212Z","shell.execute_reply.started":"2023-09-16T19:20:02.496900Z","shell.execute_reply":"2023-09-16T19:20:03.665057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Sarthak Jain https://www.kaggle.com/code/sarthak333/predict-sleep\n\nfig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=Zzzs[Zzzs['series_id']=='d5e47b94477e'], x=\"step\", y=\"anglez\",hue=\"awake\", linewidth = 0.5)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-16T19:20:08.921653Z","iopub.execute_input":"2023-09-16T19:20:08.922018Z","iopub.status.idle":"2023-09-16T19:20:12.485333Z","shell.execute_reply.started":"2023-09-16T19:20:08.921975Z","shell.execute_reply":"2023-09-16T19:20:12.483882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Sarthak Jain https://www.kaggle.com/code/sarthak333/predict-sleep\n\nZzzsParquet = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\nprint(ZzzsParquet.shape)\nprint(ZzzsParquet['series_id'].unique())\nZzzsParquet.tail()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-16T19:20:24.092454Z","iopub.execute_input":"2023-09-16T19:20:24.093238Z","iopub.status.idle":"2023-09-16T19:20:24.119820Z","shell.execute_reply.started":"2023-09-16T19:20:24.093197Z","shell.execute_reply":"2023-09-16T19:20:24.118720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Cool Anglez below. It seems a Electrocardiogram.","metadata":{}},{"cell_type":"code","source":"#By Sarthak Jain https://www.kaggle.com/code/sarthak333/predict-sleep\n\nfig, ax = plt.subplots(figsize=(20, 3))\nsns.lineplot(data=ZzzsParquet[ZzzsParquet['series_id']=='0402a003dae9'], x=\"step\", y=\"anglez\", linewidth = 0.5)\nplt.show()\n# Since we don't have awake section for this hue is None","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-16T19:20:33.564872Z","iopub.execute_input":"2023-09-16T19:20:33.565301Z","iopub.status.idle":"2023-09-16T19:20:33.831031Z","shell.execute_reply.started":"2023-09-16T19:20:33.565267Z","shell.execute_reply":"2023-09-16T19:20:33.829897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#The Meaning of Zzz\n\n\"Zzz is an onomatopoeic representation of snoring. It was commonly used in media where sound effects were not an option, notably in comic books. That’s where it got its association with sleeping, even though it wasn’t the only device used to symbolize snoring.\"\n\n\"It’s unclear why zzz came to represent snoring. Henry David Thoreau was the first writer to represent nonvocal sounds with the letter Z, but he was writing about the sound of crickets, not snoring.\"\n\nhttps://www.grammarly.com/blog/zzz-meaning/#:~:text=Zzz%20is%20an%20onomatopoeic%20representation,Zs%2C%20which%20means%20to%20sleep.","metadata":{}},{"cell_type":"markdown","source":"#Hi Woodstock, it isn't easy to sleep with Snoopy. \n\n\"Woodstock is a fictional character in Charles M. Schulz's comic strip Peanuts. He is a small yellow bird and Snoopy's best friend.\"\n\n![](https://media.tenor.com/iW_viTCd7WcAAAAC/nap-snoopy.gif)https://tenor.com/pt-BR/view/nap-snoopy-zzz-fell-down-sleeping-gif-15167756","metadata":{}},{"cell_type":"markdown","source":"#Obstructive sleep apnea (OSA)\n\n\"Obstructive sleep apnea (OSA) remains a significantly underdiagnosed condition, despite its high prevalence. Fortunately, several straightforward diagnostic tools are readily available, and even more sophisticated ones, driven by artificial intelligence, are on the horizon.\"\n\n\"Once the possibility of OSA is considered, the next step is to ask patients about their symptoms.\"\n\n* Do you SNORE loudly (louder than talking or loud enough to be heard through closed doors)?\n\n* Do you often feel TIRED, fatigued, or sleepy during daytime?\n\n* Has anyone OBSERVED you stop breathing during your sleep?\n\n* Do you have or are you being treated for high blood PRESSURE?\n\nCLINICAL ATTRIBUTES:\n\n* Obesity (BMI > 35 kg/m2)\n\n* Age (>50 years)\n\n* Neck size (>40 cm, or 16 inches)\n\n* Sex\n\nCOMORBIDITIES and OCCUPATION\n\n\"Patients with resistant hypertension, pulmonary hypertension, and recurrent atrial fibrillation following cardioversion/ablation should be prioritized for diagnostic testing for OSA (Obstructive sleep apnea). Patients with other conditions, such as coronary artery disease or cerebrovascular disease, should also be referred to a sleep center if OSA is suspected on the basis of comprehensive sleep assessment. OSA has also been associated with type 2 diabetes, metabolic syndrome, and asthma.\"\n\n\"Patients are classified as being at low, intermediate, or high risk for OSA.\"\n\nhttps://www.medscape.com/viewarticle/996296?ecd=mkm_ret_230916_mscpmrk-OUS_IntStories_etid5848668&uac=102514CX&impID=5848668#vp_2","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgements:\n\nSarthak Jain https://www.kaggle.com/code/sarthak333/predict-sleep\n\nPauloYunCha https://www.youtube.com/watch?v=KGpMEk3TSYs\n\nCarl McBride Ellis https://www.kaggle.com/datasets/carlmcbrideellis/zzzs-lightweight-training-dataset-target","metadata":{}}]}