{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n\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-06-06T15:25:33.562317Z","iopub.execute_input":"2023-06-06T15:25:33.563687Z","iopub.status.idle":"2023-06-06T15:25:33.583797Z","shell.execute_reply.started":"2023-06-06T15:25:33.563620Z","shell.execute_reply":"2023-06-06T15:25:33.582676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\npath = r\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/tdcsfog\"\nall_files = glob.glob(os.path.join(path, \"*.csv\"))\ntdcsfog = pd.concat((pd.read_csv(f) for f in all_files), ignore_index=True)\ntdcsfog","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:25:33.586141Z","iopub.execute_input":"2023-06-06T15:25:33.586919Z","iopub.status.idle":"2023-06-06T15:25:47.903816Z","shell.execute_reply.started":"2023-06-06T15:25:33.586853Z","shell.execute_reply":"2023-06-06T15:25:47.902727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = r\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog\"\nall_defog = glob.glob(os.path.join(path, \"*.csv\"))\ndefog = pd.concat((pd.read_csv(f) for f in all_defog), ignore_index=True)\ndefog","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:25:47.905366Z","iopub.execute_input":"2023-06-06T15:25:47.905788Z","iopub.status.idle":"2023-06-06T15:26:05.985333Z","shell.execute_reply.started":"2023-06-06T15:25:47.905755Z","shell.execute_reply":"2023-06-06T15:26:05.983900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog = defog.drop(['Valid', 'Task'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:05.987380Z","iopub.execute_input":"2023-06-06T15:26:05.987979Z","iopub.status.idle":"2023-06-06T15:26:06.195397Z","shell.execute_reply.started":"2023-06-06T15:26:05.987947Z","shell.execute_reply":"2023-06-06T15:26:06.194156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([tdcsfog, defog], ignore_index= True )\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:06.198332Z","iopub.execute_input":"2023-06-06T15:26:06.198919Z","iopub.status.idle":"2023-06-06T15:26:06.443603Z","shell.execute_reply.started":"2023-06-06T15:26:06.198883Z","shell.execute_reply":"2023-06-06T15:26:06.442272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(['AccV', 'AccML', 'AccAP'], axis=1)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:06.445244Z","iopub.execute_input":"2023-06-06T15:26:06.445544Z","iopub.status.idle":"2023-06-06T15:26:06.634560Z","shell.execute_reply.started":"2023-06-06T15:26:06.445518Z","shell.execute_reply":"2023-06-06T15:26:06.632922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:06.636932Z","iopub.execute_input":"2023-06-06T15:26:06.637292Z","iopub.status.idle":"2023-06-06T15:26:06.659219Z","shell.execute_reply.started":"2023-06-06T15:26:06.637261Z","shell.execute_reply":"2023-06-06T15:26:06.658355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:06.660250Z","iopub.execute_input":"2023-06-06T15:26:06.660487Z","iopub.status.idle":"2023-06-06T15:26:06.666417Z","shell.execute_reply.started":"2023-06-06T15:26:06.660465Z","shell.execute_reply":"2023-06-06T15:26:06.664812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#считаем интервалы событий и их продолжительность \nintervals = {}\nfor col in df.columns[1:]:\n    intervals[col] = []\n    start = None\n    for i, val in enumerate(df[col]):\n        if val == 1 and start is None:\n            start = i\n        elif val == 0 and start is not None:\n            intervals[col].append((start, i-1))\n            start = None\n    if start is not None:\n        intervals[col].append((start, len(df)-1))\n\ndurations = []\nfor col, ints in intervals.items():\n    for start, end in ints:\n        durations.append({'Variable': col, 'Start': start, 'End': end, 'Duration': (end-start+1)})\n\ndf_intervals = pd.DataFrame(durations)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:06.668991Z","iopub.execute_input":"2023-06-06T15:26:06.669379Z","iopub.status.idle":"2023-06-06T15:26:22.576367Z","shell.execute_reply.started":"2023-06-06T15:26:06.669347Z","shell.execute_reply":"2023-06-06T15:26:22.575065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_intervals","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:22.577498Z","iopub.execute_input":"2023-06-06T15:26:22.577814Z","iopub.status.idle":"2023-06-06T15:26:22.590951Z","shell.execute_reply.started":"2023-06-06T15:26:22.577788Z","shell.execute_reply":"2023-06-06T15:26:22.589146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"turn_data = df_intervals.loc[df_intervals['Variable'] == 'Turn', 'Duration']\nwalking_data = df_intervals.loc[df_intervals['Variable'] == 'Walking', 'Duration']\nStartHesitation_data = df_intervals.loc[df_intervals['Variable'] == 'StartHesitation', 'Duration']","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:22.592993Z","iopub.execute_input":"2023-06-06T15:26:22.593558Z","iopub.status.idle":"2023-06-06T15:26:22.604713Z","shell.execute_reply.started":"2023-06-06T15:26:22.593503Z","shell.execute_reply":"2023-06-06T15:26:22.602991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#посмотрим на распределения для разного типа событий\n\nfig, axs = plt.subplots(1, 3, figsize=(15, 5))\n\naxs[0].hist(turn_data,bins=100, alpha=0.5, label='Turn')\naxs[0].set_title('Turn Duration histogram')\naxs[0].set_xlabel('Duration')\naxs[0].set_ylabel('Frequency')\n\naxs[1].hist(walking_data,bins=100, alpha=0.5, label='Walking')\naxs[1].set_title('Walking Duration histogram')\naxs[1].set_xlabel('Duration')\naxs[1].set_ylabel('Frequency')\n\naxs[2].hist(StartHesitation_data,bins=100, alpha=0.5, label='Start Hesitation')\naxs[2].set_title('Start Hesitation histogram')\naxs[2].set_xlabel('Duration')\naxs[2].set_ylabel('Frequency')\n\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:59.578390Z","iopub.execute_input":"2023-06-06T15:26:59.578815Z","iopub.status.idle":"2023-06-06T15:27:00.493532Z","shell.execute_reply.started":"2023-06-06T15:26:59.578785Z","shell.execute_reply":"2023-06-06T15:27:00.492249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(1, 3, figsize=(15, 5))\n\naxs[0].boxplot(turn_data, labels=['Turn'])\naxs[0].set_title('Turn Duration Boxplot')\naxs[0].set_ylabel('Duration')\n\naxs[1].boxplot(walking_data, labels=['Walking'])\naxs[1].set_title('Walking Duration Boxplot')\naxs[1].set_ylabel('Duration')\n\naxs[2].boxplot(StartHesitation_data, labels=['Start Hesitation'])\naxs[2].set_title('Start Hesitation Boxplot')\naxs[2].set_ylabel('Duration')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:23.183219Z","iopub.execute_input":"2023-06-06T15:26:23.183712Z","iopub.status.idle":"2023-06-06T15:26:23.615239Z","shell.execute_reply.started":"2023-06-06T15:26:23.183671Z","shell.execute_reply":"2023-06-06T15:26:23.614203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#чаще всего происходят события Turn\n#Количество событий Turn = 1957\n#Walking = 396\n#StartHesitation = 107.\nprint(\"Turn\", turn_data.describe(),\"\\n\")\nprint(\"Walking\", walking_data.describe(),\"\\n\")\nprint(\"StartHesitation\", StartHesitation_data.describe(), \"\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:23.617787Z","iopub.execute_input":"2023-06-06T15:26:23.618245Z","iopub.status.idle":"2023-06-06T15:26:23.633864Z","shell.execute_reply.started":"2023-06-06T15:26:23.618221Z","shell.execute_reply":"2023-06-06T15:26:23.632776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#очередность событий\ncolors = {'StartHesitation': 'red', 'Turn': 'green', 'Walking': 'blue'} \nfig, axs = plt.subplots(nrows=3, figsize=(8, 5), sharex=True) \nfor ax, (name, group) in zip(axs, intervals.items()): \n    for i, (start, end) in enumerate(group): \n        ax.broken_barh([(start, end-start+1)], (0, 1), facecolors=colors[name]) \n    ax.set_title(name) \n\n# Set a single y-axis label\nfig.text(0, 0.5, 'Intervals', ha='center', va='center', rotation='vertical')\n\naxs[2].set_xlabel('Time') \n \nplt.tight_layout() \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:23.636038Z","iopub.execute_input":"2023-06-06T15:26:23.636470Z","iopub.status.idle":"2023-06-06T15:26:34.204418Z","shell.execute_reply.started":"2023-06-06T15:26:23.636439Z","shell.execute_reply":"2023-06-06T15:26:34.203538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:34.207243Z","iopub.execute_input":"2023-06-06T15:26:34.207783Z","iopub.status.idle":"2023-06-06T15:26:34.867711Z","shell.execute_reply.started":"2023-06-06T15:26:34.207751Z","shell.execute_reply":"2023-06-06T15:26:34.866419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(df_intervals, x=\"Start\", y=\"Variable\", color=\"Variable\", size=\"Duration\", hover_data=[\"Duration\"])\n\nfig.update_layout({'plot_bgcolor': 'rgba (0, 0, 0, 0)', 'paper_bgcolor': 'rgba (0, 0, 0, 0)'})\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:34.869087Z","iopub.execute_input":"2023-06-06T15:26:34.869439Z","iopub.status.idle":"2023-06-06T15:26:36.631349Z","shell.execute_reply.started":"2023-06-06T15:26:34.869409Z","shell.execute_reply":"2023-06-06T15:26:36.630196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unlabeled = pd.read_parquet(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/unlabeled/00c4c9313d.parquet\")\nunlabeled # нет events","metadata":{"execution":{"iopub.status.busy":"2023-06-06T15:26:36.632659Z","iopub.execute_input":"2023-06-06T15:26:36.632978Z","iopub.status.idle":"2023-06-06T15:26:44.048121Z","shell.execute_reply.started":"2023-06-06T15:26:36.632954Z","shell.execute_reply":"2023-06-06T15:26:44.047206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}