{"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-22T20:59:39.730808Z","iopub.execute_input":"2024-08-22T20:59:39.731667Z","iopub.status.idle":"2024-08-22T20:59:39.785259Z","shell.execute_reply.started":"2024-08-22T20:59:39.731618Z","shell.execute_reply":"2024-08-22T20:59:39.784100Z"},"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-23T02:39:59.498588Z","iopub.execute_input":"2024-08-23T02:39:59.498989Z","iopub.status.idle":"2024-08-23T02:40:14.633989Z","shell.execute_reply.started":"2024-08-23T02:39:59.498954Z","shell.execute_reply":"2024-08-23T02:40:14.632759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Center of Witnesses\n\nCreate a very simple submission that is the average of the witnesses 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 average center positions for all distinct pubKeys\ncmd = f\"\"\"\n    WITH BeaconCenters AS (\n        -- Calculate the center (average lat/lon) for each beacon (pocId)\n        SELECT \n            pocId,\n            beacon.report_pubKey as hotspot_pubKey,\n            AVG(witness.latitude) as center_latitude,\n            AVG(witness.longitude) as center_longitude\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\n    ),\n    HotspotAverageCenters AS (\n        -- Calculate the average center position for each hotspot\n        SELECT \n            hotspot_pubKey as pubKey,\n            AVG(center_latitude) as latitude,\n            AVG(center_longitude) as longitude\n        FROM BeaconCenters\n        GROUP BY hotspot_pubKey\n    )\n    SELECT DISTINCT(pubKey),latitude,longitude FROM HotspotAverageCenters\n\"\"\"\n\n# Execute the query and load the result into a DataFrame\nresult_df = db.query(cmd).to_df()\n\nresult_df['confidence'] = np.random.uniform(0, 100, size=len(result_df)).astype(int)\n\n# Display the DataFrame to verify the results\ndisplay(result_df)\n\n# Save the result to a CSV file\nresult_df.to_csv('submission.csv', index=False)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-23T02:45:48.582774Z","iopub.execute_input":"2024-08-23T02:45:48.583252Z","iopub.status.idle":"2024-08-23T02:47:05.587256Z","shell.execute_reply.started":"2024-08-23T02:45:48.583212Z","shell.execute_reply":"2024-08-23T02:47:05.585743Z"},"trusted":true},"execution_count":null,"outputs":[]}]}