{"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":41880,"databundleVersionId":5677426,"sourceType":"competition"},{"sourceId":6996300,"sourceType":"datasetVersion","datasetId":4021657},{"sourceId":7003071,"sourceType":"datasetVersion","datasetId":4025928},{"sourceId":7021643,"sourceType":"datasetVersion","datasetId":4037772}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Import Libraries","metadata":{}},{"cell_type":"code","source":"!pip install --no-index --find-links /kaggle/input/stablebaselines3 stable_baselines3","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:04:08.362045Z","iopub.execute_input":"2023-11-21T21:04:08.362449Z","iopub.status.idle":"2023-11-21T21:04:22.935942Z","shell.execute_reply.started":"2023-11-21T21:04:08.362418Z","shell.execute_reply":"2023-11-21T21:04:22.934678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nfrom stable_baselines3 import PPO\nfrom stable_baselines3.common.env_util import make_vec_env","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:04:22.937929Z","iopub.execute_input":"2023-11-21T21:04:22.938335Z","iopub.status.idle":"2023-11-21T21:04:22.946984Z","shell.execute_reply.started":"2023-11-21T21:04:22.938298Z","shell.execute_reply":"2023-11-21T21:04:22.945451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_DATASET_DIR = \"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/\"","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:04:22.949222Z","iopub.execute_input":"2023-11-21T21:04:22.949992Z","iopub.status.idle":"2023-11-21T21:04:22.961037Z","shell.execute_reply.started":"2023-11-21T21:04:22.949786Z","shell.execute_reply":"2023-11-21T21:04:22.960073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_column(df, column_name):\n    min_val = df[column_name].min()\n    max_val = df[column_name].max()\n    df[column_name] = 2 * (df[column_name] - min_val) / (max_val - min_val) - 1\n\ndef remove_extension(filename):\n    filename, extension = os.path.splitext(filename)\n    return filename\n    \ndef remove_string_before_slash(input_string):\n    if '/' in input_string:\n        return input_string.rsplit('/', 1)[1]\n    else:\n        return input_string","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:04:22.963744Z","iopub.execute_input":"2023-11-21T21:04:22.964119Z","iopub.status.idle":"2023-11-21T21:04:22.975235Z","shell.execute_reply.started":"2023-11-21T21:04:22.964088Z","shell.execute_reply":"2023-11-21T21:04:22.974394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_model = PPO.load(\"/kaggle/input/rl-model-2/tdcsfog_without_cf_model.pkl\")\ndefog_model = PPO.load(\"/kaggle/input/rl-model-2/defog_without_cf_model.pkl\")","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:04:22.976790Z","iopub.execute_input":"2023-11-21T21:04:22.977197Z","iopub.status.idle":"2023-11-21T21:04:23.196927Z","shell.execute_reply.started":"2023-11-21T21:04:22.977168Z","shell.execute_reply":"2023-11-21T21:04:23.196002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"series = []\nfor type_name in os.listdir(TEST_DATASET_DIR):\n    test_data_dir = TEST_DATASET_DIR + \"/\" + type_name\n    if type_name.startswith('.'):\n            continue\n    for filename in os.listdir(test_data_dir):\n        if not filename.endswith('.csv'):\n            continue\n        file_path = os.path.join(test_data_dir, filename)\n        if os.path.isfile(file_path):\n            series.append(type_name + \"/\"+ filename)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:04:23.198567Z","iopub.execute_input":"2023-11-21T21:04:23.199179Z","iopub.status.idle":"2023-11-21T21:04:23.219564Z","shell.execute_reply.started":"2023-11-21T21:04:23.199145Z","shell.execute_reply":"2023-11-21T21:04:23.218457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_values = []","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:04:23.220912Z","iopub.execute_input":"2023-11-21T21:04:23.221263Z","iopub.status.idle":"2023-11-21T21:04:23.226576Z","shell.execute_reply.started":"2023-11-21T21:04:23.221233Z","shell.execute_reply":"2023-11-21T21:04:23.225322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for series_file_name in series:\n    model = defog_model\n    if \"tdcsfog\" in series_file_name:\n        model = tdcsfog_model\n    series_id = remove_extension(series_file_name)\n    series_test_df = pd.read_csv(TEST_DATASET_DIR + series_file_name)\n\n    normalize_column(series_test_df, 'AccV')\n    normalize_column(series_test_df, 'AccML')\n    normalize_column(series_test_df, 'AccAP')\n    \n    for i in range(len(series_test_df)):\n        action, _ = model.predict(series_test_df.iloc[i, 1:4])\n        predicted_values.append([remove_string_before_slash(f'{series_id}_{i}')] + list(action))\n        ","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:04:25.717450Z","iopub.execute_input":"2023-11-21T21:04:25.718153Z","iopub.status.idle":"2023-11-21T21:08:10.884163Z","shell.execute_reply.started":"2023-11-21T21:04:25.718115Z","shell.execute_reply":"2023-11-21T21:08:10.882739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame(predicted_values, columns = ['Id','StartHesitation', 'Turn', 'Walking'])","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:08:10.887022Z","iopub.execute_input":"2023-11-21T21:08:10.887515Z","iopub.status.idle":"2023-11-21T21:08:11.314948Z","shell.execute_reply.started":"2023-11-21T21:08:10.887474Z","shell.execute_reply":"2023-11-21T21:08:11.313757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T21:08:11.316825Z","iopub.execute_input":"2023-11-21T21:08:11.317449Z","iopub.status.idle":"2023-11-21T21:08:12.656924Z","shell.execute_reply.started":"2023-11-21T21:08:11.317414Z","shell.execute_reply":"2023-11-21T21:08:12.655731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}