{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:49.875838Z","iopub.execute_input":"2023-03-15T01:30:49.876229Z","iopub.status.idle":"2023-03-15T01:30:53.621756Z","shell.execute_reply.started":"2023-03-15T01:30:49.876195Z","shell.execute_reply":"2023-03-15T01:30:53.620422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Goal of the Competition**\n\nThe goal of this competition is to detect freezing of gait (FOG), a debilitating symptom that afflicts many people with Parkinson’s disease. You will develop a machine learning model trained on data collected from a wearable 3D lower back sensor.\n\nYour work will help researchers better understand when and why FOG episodes occur. This will improve the ability of medical professionals to optimally evaluate, monitor, and ultimately, prevent FOG events.\n\n**I will continue to work and update this notebook. Please upvote it if you find it useful in this interesting challenge!**\n\n## **Context**\n\nAn estimated 7 to 10 million people around the world have Parkinson’s disease, many of whom suffer from freezing of gait (FOG). During a FOG episode, a patient's feet are “glued” to the ground, preventing them from moving forward despite their attempts. FOG has a profound negative impact on health-related quality of life—people who suffer from FOG are often depressed, have an increased risk of falling, are likelier to be confined to wheelchair use, and have restricted independence.\n\nWhile researchers have multiple theories to explain when, why, and in whom FOG occurs, there is still no clear understanding of its causes. The ability to objectively and accurately quantify FOG is one of the keys to advancing its understanding and treatment. Collection and analysis of FOG events, such as with your data science skills, could lead to potential treatments.\n\nThere are many methods of evaluating FOG, though most involve FOG-provoking protocols. People with FOG are filmed while performing certain tasks that are likely to increase its occurrence. Experts then review the video to score each frame, indicating when FOG occurred. While scoring in this manner is relatively reliable and sensitive, it is extremely time-consuming and requires specific expertise. Another method involves augmenting FOG-provoking testing with wearable devices. With more sensors, the detection of FOG becomes easier, however, compliance and usability may be reduced. Therefore, a combination of these two methods may be the best approach. When combined with machine learning methods, the accuracy of detecting FOG from a lower back accelerometer is relatively high. However, the datasets used to train and test these algorithms have been relatively small and generalizability is limited to date. Furthermore, the emphasis has been on achieving high levels of accuracy, while precision, for example, has largely been ignored.\n\nCompetition host, the Center for the Study of Movement, Cognition, and Mobility (CMCM), Neurological Institute, Tel Aviv Sourasky Medical Center, aims to improve the personalized treatment of age-related movement, cognition, and mobility disorders and to alleviate the associated burden. They leverage a combination of clinical, engineering, and neuroscience expertise to: 1) Gain new understandings into the physiologic and pathophysiologic mechanisms that contribute to cognitive and motor function, the factors that influence these functions, and their changes with aging and disease (e.g., Parkinson’s disease, Alzheimer’s). 2) Develop new methods and tools for the early detection and tracking of cognitive and motor decline. A major focus is on using leveraging wearable devices and digital technologies; and 3) Develop and evaluate novel methods for the prevention and treatment of gait, falls, and cognitive function.\n\nYour work will help advance the evaluation, understanding and treatment of FOG, improving the lives of the many people who suffer from this debilitating Parkinson’s disease symptom.\n\n## **File and Field Descriptions**\n\n**train/** Folder containing the data series in the training set within three subfolders: **tdcsfog/**, **defog/**, and **notype/**. Series in the notype folder are from the defog dataset but lack event-type annotations. The fields present in these series vary by folder.\n\n* **Time** An integer timestep. Series from the tdcsfog dataset are recorded at 128Hz (128 timesteps per second), while series from the **defog** and **daily** series are recorded at 100Hz (100 timesteps per second).\n* **AccV**, **AccML**, and **AccAP** Acceleration in units of g, from a lower-back sensor on three axes: V - vertical, ML - mediolateral, AP - anteroposterior.\n* **StartHesitation**, **Turn**, **Walking** Indicator variables for the occurrence of each of the event types.\n* **Event** Indicator variable for the occurrence of any FOG-type event. Present only in the notype series, which lack type-level annotations.\n* **Valid** There were cases during the video annotation that were hard for the annotator to decide if there was an Akinetic (i.e., essentially no movement) FoG or the subject stopped voluntarily. Only event annotations where the series is marked true should be considered as unambiguous.\n* **Task** Series were only annotated where this value is true. Portions marked false should be considered unannotated.","metadata":{}},{"cell_type":"code","source":"defog_example = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog/be9d33541d.csv')\ndefog_example","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:30:53.624189Z","iopub.execute_input":"2023-03-15T01:30:53.625403Z","iopub.status.idle":"2023-03-15T01:30:53.974629Z","shell.execute_reply.started":"2023-03-15T01:30:53.625330Z","shell.execute_reply":"2023-03-15T01:30:53.973428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get a summary of the data\nprint(defog_example.describe())\n\n# check for missing values\nprint(defog_example.isnull().sum())\n\n# check the data types of columns\nprint(defog_example.dtypes)\n\n# check the correlation between variables\nprint(defog_example.corr())","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:53.975848Z","iopub.execute_input":"2023-03-15T01:30:53.976167Z","iopub.status.idle":"2023-03-15T01:30:54.103751Z","shell.execute_reply.started":"2023-03-15T01:30:53.976137Z","shell.execute_reply":"2023-03-15T01:30:54.101327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a scatter plot of AccV and AccML\nplt.subplot(1, 2, 1)\nplt.scatter(defog_example['AccV'], defog_example['AccML'])\nplt.xlabel('AccV')\nplt.ylabel('AccML')\n\n# create a scatter plot of AccV and AccAP\nplt.subplot(1, 2, 2)\nplt.scatter(defog_example['AccV'], defog_example['AccAP'])\nplt.xlabel('AccV')\nplt.ylabel('AccAP')\n\n# Adjust the layout of the subplots\nplt.tight_layout()\n\n# display the subplots\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:54.106738Z","iopub.execute_input":"2023-03-15T01:30:54.107316Z","iopub.status.idle":"2023-03-15T01:30:55.132771Z","shell.execute_reply.started":"2023-03-15T01:30:54.107277Z","shell.execute_reply":"2023-03-15T01:30:55.131194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a histogram of AccAP\nplt.hist(defog_example['AccAP'])\nplt.xlabel('AccAP')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:55.134482Z","iopub.execute_input":"2023-03-15T01:30:55.134963Z","iopub.status.idle":"2023-03-15T01:30:55.391188Z","shell.execute_reply.started":"2023-03-15T01:30:55.134913Z","shell.execute_reply":"2023-03-15T01:30:55.388426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(3, 1, sharex='col', sharey='row', figsize=(16,10))\nfig.suptitle('Sensor Values over Time')\n\ndata = {'V': defog_example.AccV, 'ML': defog_example.AccML, 'AP': defog_example.AccAP}\n\nfor i, (name, values) in enumerate(data.items()):\n    ax = axes[i]\n    ax.plot(values, label=name, color=f'C{i}')\n    a, b = np.polyfit(values.index, values, 1)\n    ax.plot(values.index, a*values.index+b, color='red')\n    ax.set_xlabel('Time')\n    ax.set_ylabel('Sensor Value')\n    ax.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:30:55.393953Z","iopub.execute_input":"2023-03-15T01:30:55.394823Z","iopub.status.idle":"2023-03-15T01:30:56.724480Z","shell.execute_reply.started":"2023-03-15T01:30:55.394755Z","shell.execute_reply":"2023-03-15T01:30:56.723031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot acceleration on each axis over time\nfig, ax = plt.subplots(figsize=(12,6))\ndefog_example.plot(x='Time', y=['AccV', 'AccML', 'AccAP'], ax=ax);\nax.set_xlabel('Time (s)');\nax.set_ylabel('Acceleration (g)');\nax.set_title('Acceleration over time');","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:37:50.758194Z","iopub.execute_input":"2023-03-15T01:37:50.758624Z","iopub.status.idle":"2023-03-15T01:37:51.572839Z","shell.execute_reply.started":"2023-03-15T01:37:50.758586Z","shell.execute_reply":"2023-03-15T01:37:51.571578Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a heatmap of the correlation matrix\ncorr_matrix = defog_example.corr()\nplt.imshow(corr_matrix, cmap='hot', interpolation='nearest')\nplt.colorbar()\nplt.xticks(range(len(corr_matrix)), corr_matrix.columns, rotation=90)\nplt.yticks(range(len(corr_matrix)), corr_matrix.columns)\nplt.title('Correlation Matrix Heatmap')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:56.726138Z","iopub.execute_input":"2023-03-15T01:30:56.726928Z","iopub.status.idle":"2023-03-15T01:30:57.003580Z","shell.execute_reply.started":"2023-03-15T01:30:56.726880Z","shell.execute_reply":"2023-03-15T01:30:57.002413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a scatter plot matrix using Seaborn\nsns.pairplot(defog_example)\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:38:25.341328Z","iopub.execute_input":"2023-03-15T01:38:25.341759Z","iopub.status.idle":"2023-03-15T01:39:03.712303Z","shell.execute_reply.started":"2023-03-15T01:38:25.341724Z","shell.execute_reply":"2023-03-15T01:39:03.711030Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a violin plot of acceleration on the vertical axis (AccV)\nsns.violinplot(x='Valid', y='AccV', data=defog_example)\n\n# add title to the plot\nplt.title('Distribution of AccV by Validity')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:43:19.814088Z","iopub.execute_input":"2023-03-15T01:43:19.814545Z","iopub.status.idle":"2023-03-15T01:43:20.430735Z","shell.execute_reply.started":"2023-03-15T01:43:19.814505Z","shell.execute_reply":"2023-03-15T01:43:20.429529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**tdcsfog_metadata.csv** Identifies each series in the tdcsfog dataset by a unique **Subject, Visit, Test, Medication condition**.\n\n* **Visit** Lab visits consist of a baseline assessment, two post-treatment assessments for different treatment stages, and one follow-up assessment.\n* **Test** Which of three test types was performed, with 3 the most challenging.\n* **Medication** Subjects may have been either off or on anti-parkinsonian medication during the recording.","metadata":{}},{"cell_type":"code","source":"tdcsfog_meta = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tdcsfog_metadata.csv')\ntdcsfog_meta","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:30:57.156837Z","iopub.status.idle":"2023-03-15T01:30:57.157269Z","shell.execute_reply.started":"2023-03-15T01:30:57.157064Z","shell.execute_reply":"2023-03-15T01:30:57.157087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create subplots\nfig, axes = plt.subplots(nrows=2, ncols=3, figsize=(15, 10))\n\n# create histogram of Test column\nsns.histplot(x='Test', data=tdcsfog_meta, ax=axes[0, 0])\naxes[0, 0].set_xlabel('Test')\naxes[0, 0].set_ylabel('Count')\naxes[0, 0].set_title('Histogram of Test')\n\n# create boxplot of Test column\nsns.boxplot(x='Test', data=tdcsfog_meta, ax=axes[0, 1])\naxes[0, 1].set_xlabel('Test')\naxes[0, 1].set_title('Boxplot of Test')\n\n# create countplot of Medication column\nsns.countplot(x='Medication', data=tdcsfog_meta, ax=axes[0, 2])\naxes[0, 2].set_xlabel('Medication')\naxes[0, 2].set_ylabel('Count')\naxes[0, 2].set_title('Bar Chart of Medication')\n\n# create scatterplot of Visit vs Test\nsns.scatterplot(x='Visit', y='Test', data=tdcsfog_meta, ax=axes[1, 0])\naxes[1, 0].set_xlabel('Visit')\naxes[1, 0].set_ylabel('Test')\naxes[1, 0].set_title('Scatterplot of Visit vs Test')\n\n# create heatmap of correlation between numerical columns\ncorr = tdcsfog_meta[['Visit', 'Test']].corr()\nsns.heatmap(corr, annot=True, cmap='coolwarm', ax=axes[1, 1])\naxes[1, 1].set_title('Heatmap of Correlation Matrix')\n\n# remove empty subplot\nfig.delaxes(ax=axes[1, 2])\n\n# adjust layout and display figure\nfig.tight_layout()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.159140Z","iopub.status.idle":"2023-03-15T01:30:57.159615Z","shell.execute_reply.started":"2023-03-15T01:30:57.159389Z","shell.execute_reply":"2023-03-15T01:30:57.159415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# count the number of records for each combination of Subject and Visit\ntdcsfog_counts = tdcsfog_meta.groupby(['Subject', 'Visit']).size().reset_index(name='count')\n\n# create a treemap of the number of records for each combination of Subject and Visit\nfig = px.treemap(tdcsfog_counts, path=['Subject', 'Visit'], values='count')\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.162252Z","iopub.status.idle":"2023-03-15T01:30:57.163197Z","shell.execute_reply.started":"2023-03-15T01:30:57.162874Z","shell.execute_reply":"2023-03-15T01:30:57.162912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**subjects.csv** Metadata for each Subject in the study, including their Age and Sex as well as:\n\n* **Visit** Only available for subjects in the daily and defog datasets.\n* **YearsSinceDx** Years since Parkinson's diagnosis.\n* **UPDRSIIIOn/UPDRSIIIOff** Unified Parkinson's Disease Rating Scale score during on/off medication respectively.\n* **NFOGQ** Self-report FoG questionnaire score. See: \nhttps://pubmed.ncbi.nlm.nih.gov/19660949/\n\n## **[Unified Parkinson Disease Rating Scale](https://www.theracycle.com/resources/links-and-additional-resources/updrs-scale/)**\n> *The UPDRS scale refers to Unified Parkinson Disease Rating Scale, and it is a rating tool used to gauge the course of Parkinson’s disease in patients. The UPDRS scale has been modified over the years by several medical organizations, and continues to be one of the bases of treatment and research in PD clinics. The UPDRS scale includes series of ratings for typical Parkinson’s symptoms that cover all of the movement hindrances of Parkinson’s disease. The UPDRS scale consists of the following five segments: 1) Mentation, Behavior, and Mood, 2) ADL, 3) Motor sections, 4) Modified Hoehn and Yahr Scale, and 5) Schwab and England ADL scale.*\n\nWe are provided patient scores for the 3rd motor scale UPDRS III and whether the patient was on or off medication (**UPDRSIII_On** and **UPDRSIII_Off**).","metadata":{}},{"cell_type":"code","source":"subjects = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/subjects.csv')\nsubjects","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:30:57.164809Z","iopub.status.idle":"2023-03-15T01:30:57.165721Z","shell.execute_reply.started":"2023-03-15T01:30:57.165417Z","shell.execute_reply":"2023-03-15T01:30:57.165451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pywaffle\nfrom pywaffle import Waffle","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.167304Z","iopub.status.idle":"2023-03-15T01:30:57.168231Z","shell.execute_reply.started":"2023-03-15T01:30:57.167906Z","shell.execute_reply":"2023-03-15T01:30:57.167939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_counts = subjects[\"Sex\"].value_counts()\n\nplt.figure(\n    FigureClass=Waffle,\n    rows=5,\n    columns=10,\n    values=gender_counts,\n    title={'label': 'Gender Distribution', 'loc': 'left'},\n    labels=[\"{}({})\".format(a, b) for a, b in zip(gender_counts.index, gender_counts) ],\n    # Set the position of the legend\n    legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)},\n    dpi=100\n)\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.169880Z","iopub.status.idle":"2023-03-15T01:30:57.170797Z","shell.execute_reply.started":"2023-03-15T01:30:57.170492Z","shell.execute_reply":"2023-03-15T01:30:57.170525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the shape, data types, and missing values\nprint(subjects.shape)\nprint(subjects.dtypes)\nprint(subjects.isnull().sum())\n\n# Check summary statistics of numerical variables\nprint(subjects.describe())\n\n# Check distribution of numerical variables\nfig, axs = plt.subplots(nrows=2, ncols=3, figsize=(15, 10))\nsns.histplot(subjects[\"Age\"], kde=True, ax=axs[0,0]).set(title='Age Distribution')\nsns.histplot(subjects[\"YearsSinceDx\"], kde=True, ax=axs[0,1]).set(title='Years Since Diagnosis Distribution')\nsns.histplot(subjects[\"UPDRSIII_On\"].dropna(), kde=True, ax=axs[0,2]).set(title='UPDRS III On Medication Distribution')\nsns.histplot(subjects[\"UPDRSIII_Off\"].dropna(), kde=True, ax=axs[1,0]).set(title='UPDRS III Off Medication Distribution')\nsns.histplot(subjects[\"NFOGQ\"], kde=True, ax=axs[1,1]).set(title='NFOGQ Distribution')\nplt.delaxes(axs[1, 2])\nplt.tight_layout()\nplt.show()\n\n# Check distribution of categorical variable\nsns.catplot(x=\"Sex\", kind=\"count\", data=subjects).set(title='Gender Distribution')\n\n# Check correlation between numerical variables\ncorr = subjects[[\"Age\", \"YearsSinceDx\", \"UPDRSIII_On\", \"UPDRSIII_Off\", \"NFOGQ\"]].corr()\nprint(corr)\n\n# Visualize correlation matrix using heatmap\nsns.heatmap(corr, annot=True, cmap=\"coolwarm\").set(title='Correlation Matrix')\n\n# Create subplots to analyze relationship between numerical and categorical variables\nfig, ax = plt.subplots(nrows=2, ncols=2, figsize=(10,10))\nsns.boxplot(x=\"Sex\", y=\"Age\", data=subjects, ax=ax[0,0]).set(title='Age by Gender')\nsns.boxplot(x=\"Sex\", y=\"YearsSinceDx\", data=subjects, ax=ax[0,1]).set(title='Years Since Diagnosis by Gender')\nsns.boxplot(x=\"Sex\", y=\"UPDRSIII_On\", data=subjects, ax=ax[1,0]).set(title='UPDRS III On Medication by Gender')\nsns.boxplot(x=\"Sex\", y=\"NFOGQ\", data=subjects, ax=ax[1,1]).set(title='NFOGQ by Gender')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.172239Z","iopub.status.idle":"2023-03-15T01:30:57.173288Z","shell.execute_reply.started":"2023-03-15T01:30:57.173041Z","shell.execute_reply":"2023-03-15T01:30:57.173077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a new figure and axis\nfig, ax = plt.subplots()\n\n# Plot histograms for UPDRSIII_On and UPDRSIII_Off\nax.hist(subjects['UPDRSIII_On'], alpha=0.5, label='UPDRSIII_On')\nax.hist(subjects['UPDRSIII_Off'], alpha=0.5, label='UPDRSIII_Off')\n\n# Add median lines\nax.axvline(subjects['UPDRSIII_On'].median(), color='blue', linestyle='dashed', linewidth=1)\nax.axvline(subjects['UPDRSIII_Off'].median(), color='orange', linestyle='dashed', linewidth=1)\n\n# Set axis labels and title\nax.set_xlabel('UPDRSIII')\nax.set_ylabel('Frequency')\nax.set_title('Distribution of UPDRSIII_On and UPDRSIII_Off')\n\n# Add a legend\nax.legend()\n\n# Add a text box with the median values\nx_offset = 0.1\ny_offset = 0.1\nx_pos = ax.get_xlim()[1] - x_offset\ny_pos = ax.get_ylim()[0] + y_offset\nax.text(x_pos, y_pos, f'Median UPDRSIII_On: {subjects[\"UPDRSIII_On\"].median():.2f}\\nMedian UPDRSIII_Off: {subjects[\"UPDRSIII_Off\"].median():.2f}', verticalalignment='bottom', horizontalalignment='right')\n\n# Show the plot\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.174827Z","iopub.status.idle":"2023-03-15T01:30:57.175248Z","shell.execute_reply.started":"2023-03-15T01:30:57.175048Z","shell.execute_reply":"2023-03-15T01:30:57.175070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a new figure and axis\nfig, ax = plt.subplots()\n\n# Scatter plot of UPDRSIII_On and UPDRSIII_Off by Age\nsns.scatterplot(x='Age', y='UPDRSIII_On', data=subjects, color='blue', alpha=0.5, label='UPDRSIII_On', marker='o')\nsns.scatterplot(x='Age', y='UPDRSIII_Off', data=subjects, color='orange', alpha=0.5, label='UPDRSIII_Off', marker='s')\n\n# Add a regression line\nsns.regplot(x='Age', y='UPDRSIII_On', data=subjects, scatter=False, color='blue', label=None)\nsns.regplot(x='Age', y='UPDRSIII_Off', data=subjects, scatter=False, color='orange', label=None)\n\n# Set axis labels and title\nax.set_xlabel('Age')\nax.set_ylabel('UPDRSIII')\nax.set_title('Distribution of UPDRSIII_On and UPDRSIII_Off by Age')\n\n# Add a legend\nax.legend()\n\n# Add grid lines\nax.grid(True)\n\n# Show the plot\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.176273Z","iopub.status.idle":"2023-03-15T01:30:57.176698Z","shell.execute_reply.started":"2023-03-15T01:30:57.176502Z","shell.execute_reply":"2023-03-15T01:30:57.176524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**events.csv** Metadata for each FoG event in all data series. The event times agree with the labels in the data series.\n\n* **Id** The data series the event occured in.\n* **Init** Time (s) the event began.\n* **Completion** Time (s) the event ended.\n* **Type** Whether **StartHesitation**, **Turn**, or **Walking**.\n* **Kinetic** Whether the event was kinetic (1) and involved movement, or akinetic (0) and static.","metadata":{}},{"cell_type":"code","source":"events = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/events.csv')\nevents","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:30:57.178137Z","iopub.status.idle":"2023-03-15T01:30:57.178543Z","shell.execute_reply.started":"2023-03-15T01:30:57.178324Z","shell.execute_reply":"2023-03-15T01:30:57.178344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the data\nprint(events.shape)\nprint(events.dtypes)\nprint(events.describe())\n\n# Create a figure with four subplots\nfig, axs = plt.subplots(2, 2, figsize=(10, 10))\n\n# Plot the count of events by type in the first subplot\nsns.countplot(x='Type', data=events, ax=axs[0, 0])\naxs[0, 0].set_title('Event Counts by Type')\naxs[0, 0].set_xlabel('Type')\naxs[0, 0].set_ylabel('Count')\n\n# Plot the distribution of event durations in the second subplot\nsns.histplot(events['Completion'] - events['Init'], ax=axs[0, 1])\naxs[0, 1].set_title('Distribution of Event Durations')\naxs[0, 1].set_xlabel('Duration (s)')\naxs[0, 1].set_ylabel('Density')\n\n# Plot the boxplot of event durations by type in the third subplot\nsns.boxplot(x='Type', y=events['Completion'] - events['Init'], data=events, ax=axs[1, 0])\naxs[1, 0].set_title('Boxplot of Event Durations by Type')\naxs[1, 0].set_xlabel('Type')\naxs[1, 0].set_ylabel('Duration (s)')\n\n# Plot the histogram of event start times in the fourth subplot\nsns.histplot(x='Init', data=events, ax=axs[1, 1])\naxs[1, 1].set_title('Histogram of Event Start Times')\naxs[1, 1].set_xlabel('Start Time')\naxs[1, 1].set_ylabel('Count')\n\n# Adjust the layout of the subplots\nfig.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.180515Z","iopub.status.idle":"2023-03-15T01:30:57.180896Z","shell.execute_reply.started":"2023-03-15T01:30:57.180705Z","shell.execute_reply":"2023-03-15T01:30:57.180725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**tasks.csv** Task metadata for series in the defog dataset. (Not relevant for the series in the fog or daily datasets.)\n\n* **Id** The data series where the task was measured.\n* **Begin** Time (s) the task began.\n* **End** Time (s) the task ended.\n* **Task** One of seven tasks types in the DeFOG protocol, described on this page.\n* **Description** Description of the task.","metadata":{}},{"cell_type":"code","source":"tasks = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tasks.csv')\ntasks","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:30:57.182628Z","iopub.status.idle":"2023-03-15T01:30:57.183161Z","shell.execute_reply.started":"2023-03-15T01:30:57.182961Z","shell.execute_reply":"2023-03-15T01:30:57.182984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate task durations\ntasks['Duration (s)'] = tasks['End'] - tasks['Begin']\n\n# Group the tasks by type and calculate the sum of the duration for each group\ntasks_duration = tasks.groupby('Task')['Duration (s)'].agg('sum')\n\n# Sort the tasks by duration in descending order\ntasks_duration = tasks_duration.sort_values(ascending=False)\n\n# Create a bar plot with task types on the x-axis and task durations on the y-axis\nfig, ax = plt.subplots(figsize=(10, 6))\ntasks_duration.plot(kind='bar', ax=ax)\n\n# Set the x-axis label\nax.set_xlabel('Task Type')\n\n# Set the y-axis label\nax.set_ylabel('Duration (s)')\n\n# Set the title of the plot\nax.set_title('Total Duration of Tasks in DeFOG Dataset')\n\n# Show the plot\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.184465Z","iopub.status.idle":"2023-03-15T01:30:57.185290Z","shell.execute_reply.started":"2023-03-15T01:30:57.185082Z","shell.execute_reply":"2023-03-15T01:30:57.185106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Group tasks by series ID\ngrouped_series = tasks.groupby('Id')\n\n# Create a box plot with series on the x-axis and task durations on the y-axis\nfig, ax = plt.subplots(figsize=(20, 25))\ngrouped_series.boxplot(column='Duration (s)', by='Id', ax=ax, rot=90)\n\n# Set the x-axis label\nax.set_xlabel('Series ID')\n\n# Set the y-axis label\nax.set_ylabel('Duration (s)')\n\n# Set the title of the plot\nax.set_title('Task Durations by Series for DeFOG Dataset')\n\n# Show the plot\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.187153Z","iopub.status.idle":"2023-03-15T01:30:57.189076Z","shell.execute_reply.started":"2023-03-15T01:30:57.188743Z","shell.execute_reply":"2023-03-15T01:30:57.188784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**daily_metadata.csv** Each series in the daily dataset is identified by the Subject id. This file also contains the time of day the recording began.","metadata":{}},{"cell_type":"code","source":"daily_meta = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/daily_metadata.csv')\ndaily_meta","metadata":{"execution":{"iopub.status.busy":"2023-03-15T01:30:57.190432Z","iopub.status.idle":"2023-03-15T01:30:57.191777Z","shell.execute_reply.started":"2023-03-15T01:30:57.191446Z","shell.execute_reply":"2023-03-15T01:30:57.191479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot a histogram of the distribution of recordings by time of day\nplt.figure(figsize=(10, 8))\nplt.hist(daily_meta['Beginning of recording [00:00-23:59]'], bins=24)\nplt.xlabel('Time of day')\nplt.ylabel('Number of recordings')\nplt.title('Distribution of recordings by time of day')\nplt.show()\n\n# plot a bar chart of the number of recordings per subject\nplt.figure(figsize=(10, 8))\ndaily_meta.groupby('Subject')['Id'].count().plot(kind='bar')\nplt.xlabel('Subject')\nplt.ylabel('Number of recordings')\nplt.title('Number of recordings per subject')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-03-15T01:30:57.193095Z","iopub.status.idle":"2023-03-15T01:30:57.194332Z","shell.execute_reply.started":"2023-03-15T01:30:57.193999Z","shell.execute_reply":"2023-03-15T01:30:57.194033Z"},"trusted":true},"execution_count":null,"outputs":[]}]}