{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":83481,"databundleVersionId":9399961,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\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","execution":{"iopub.status.busy":"2024-08-23T19:05:46.040377Z","iopub.execute_input":"2024-08-23T19:05:46.041182Z","iopub.status.idle":"2024-08-23T19:05:47.006998Z","shell.execute_reply.started":"2024-08-23T19:05:46.041112Z","shell.execute_reply":"2024-08-23T19:05:47.004292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install duckdb --quiet\nimport duckdb\nprint(duckdb.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-08-23T19:05:47.011138Z","iopub.execute_input":"2024-08-23T19:05:47.012126Z","iopub.status.idle":"2024-08-23T19:06:07.496824Z","shell.execute_reply.started":"2024-08-23T19:05:47.012015Z","shell.execute_reply":"2024-08-23T19:06:07.495229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Nearest Witness\n\nCreate a very simple submission that uses the stongest signal witness's location","metadata":{}},{"cell_type":"code","source":"import duckdb\nimport pandas as pd\nimport numpy as np\n\n# Connect to DuckDB in memory\ndb = duckdb.connect()\n\n# Specify the date range\nstart_date = '2024-06-01'\nend_date = '2024-06-30'\n\n# SQL query to calculate the strongest average signal witness for each beacon\ncmd = f\"\"\"\n    WITH StrongestSignalWitness AS (\n        -- Calculate the strongest average signal witness for each beacon (pocId)\n        SELECT \n            pocId,\n            beacon.report_pubKey as hotspot_pubKey,\n            witness.report_pubKey as strongest_witness_pubKey,\n            witness.latitude as latitude,\n            witness.longitude as longitude,\n            AVG(witness.report_signal) as avg_signal_strength\n        FROM read_parquet('/kaggle/input/radio-direction-finding-challenge/test_beacons.parquet/**/*.parquet')\n        WHERE date BETWEEN '{start_date}' AND '{end_date}'\n        GROUP BY pocId, beacon.report_pubKey, witness.report_pubKey, witness.latitude, witness.longitude\n    )\n    SELECT DISTINCT hotspot_pubKey as pubKey, latitude, longitude, avg_signal_strength\n    FROM StrongestSignalWitness\n\"\"\"\n\n# Execute the query and load the result into a DataFrame\nresult_df = db.query(cmd).to_df()\n\n# Group by pubKey and retain the row with the highest signal strength for each pubKey\nresult_df = result_df.loc[result_df.groupby('pubKey')['avg_signal_strength'].idxmax()]\n\n# Normalize the confidence score based on the signal strength\nmin_signal = result_df['avg_signal_strength'].min()\nmax_signal = result_df['avg_signal_strength'].max()\nresult_df['confidence'] = 100 * (result_df['avg_signal_strength'] - min_signal) / (max_signal - min_signal)\n\n# Remove the 'avg_signal_strength' column as it is no longer needed\nresult_df = result_df.drop(columns=['avg_signal_strength'])\n\n# Display the DataFrame to verify the results\ndisplay(result_df)\n\n# Ensure that each pubKey is unique\nassert result_df['pubKey'].is_unique, \"Error: pubKey is not unique.\"\n\n# Save the result to a CSV file\nresult_df.to_csv('submission.csv', index=False)\n\nprint(\"Updated sample submission saved to 'submission.csv'\")\n","metadata":{"execution":{"iopub.status.busy":"2024-08-23T20:15:11.996641Z","iopub.execute_input":"2024-08-23T20:15:11.997717Z","iopub.status.idle":"2024-08-23T20:17:25.265976Z","shell.execute_reply.started":"2024-08-23T20:15:11.997645Z","shell.execute_reply":"2024-08-23T20:17:25.263936Z"},"trusted":true},"execution_count":null,"outputs":[]}]}