{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.14"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":4.669361,"end_time":"2024-10-10T13:05:46.686069","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-10-10T13:05:42.016708","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\nimport pandas as pd\nimport polars as pl\n\nimport kaggle_evaluation.jane_street_inference_server","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2024-12-20T15:16:01.598918Z","iopub.execute_input":"2024-12-20T15:16:01.599310Z","iopub.status.idle":"2024-12-20T15:16:01.604334Z","shell.execute_reply.started":"2024-12-20T15:16:01.599279Z","shell.execute_reply":"2024-12-20T15:16:01.603201Z"},"papermill":{"duration":1.223703,"end_time":"2024-10-10T13:05:45.825911","exception":false,"start_time":"2024-10-10T13:05:44.602208","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The evaluation API requires that you set up a server which will respond to inference requests. We have already defined the server; you just need write the predict function. When we evaluate your submission on the hidden test set the client defined in `jane_street_gateway` will run in a different container with direct access to the hidden test set and hand off the data timestep by timestep.\n\n\n\nYour code will always have access to the published copies of the files.","metadata":{"papermill":{"duration":0.002051,"end_time":"2024-10-10T13:05:45.83073","exception":false,"start_time":"2024-10-10T13:05:45.828679","status":"completed"},"tags":[]}},{"cell_type":"code","source":"last_train_date = 1698","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T15:17:30.996201Z","iopub.execute_input":"2024-12-20T15:17:30.996624Z","iopub.status.idle":"2024-12-20T15:17:31.002006Z","shell.execute_reply.started":"2024-12-20T15:17:30.996556Z","shell.execute_reply":"2024-12-20T15:17:31.000660Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lags_ : pl.DataFrame | None = None\nday_count_ = -1\n\ndef predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"Make a prediction.\"\"\"\n\n    global lags_, day_count_\n    if lags is not None:\n        lags_ = lags\n        day_count_ += 1\n\n        lags_date_id = lags[\"date_id\"].unique()[0]\n        test_date_id = test[\"date_id\"].unique()[0]\n        assert test_date_id == lags_date_id, \"date_id mismatch between lags and test\"\n\n    \n    # test_date_id = test[\"date_id\"].unique()[0]\n    # assert test_date_id == day_count_, \"date_id mismatch\"\n    \n\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n\n\n    if isinstance(predictions, pl.DataFrame):\n        assert predictions.columns == ['row_id', 'responder_6']\n    elif isinstance(predictions, pd.DataFrame):\n        assert (predictions.columns == ['row_id', 'responder_6']).all()\n    else:\n        raise TypeError('The predict function must return a DataFrame')\n    # Confirm has as many rows as the test data.\n    assert len(predictions) == len(test)\n\n    return predictions","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2024-10-10T13:05:45.837368Z","iopub.status.busy":"2024-10-10T13:05:45.836455Z","iopub.status.idle":"2024-10-10T13:05:45.846687Z","shell.execute_reply":"2024-10-10T13:05:45.845862Z"},"papermill":{"duration":0.015917,"end_time":"2024-10-10T13:05:45.848958","exception":false,"start_time":"2024-10-10T13:05:45.833041","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pl.read_parquet('/kaggle/input/jane-street-real-time-market-data-forecasting/test.parquet')\ntest","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T15:18:30.028119Z","iopub.execute_input":"2024-12-20T15:18:30.028497Z","iopub.status.idle":"2024-12-20T15:18:30.214302Z","shell.execute_reply.started":"2024-12-20T15:18:30.028461Z","shell.execute_reply":"2024-12-20T15:18:30.213110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lags = pl.read_parquet('/kaggle/input/jane-street-real-time-market-data-forecasting/lags.parquet')\nlags","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T15:30:14.922958Z","iopub.execute_input":"2024-12-20T15:30:14.923321Z","iopub.status.idle":"2024-12-20T15:30:14.945173Z","shell.execute_reply.started":"2024-12-20T15:30:14.923290Z","shell.execute_reply":"2024-12-20T15:30:14.943776Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"When your notebook is run on the hidden test set, inference_server.serve must be called within 15 minutes of the notebook starting or the gateway will throw an error. If you need more than 15 minutes to load your model you can do so during the very first `predict` call, which does not have the usual 1 minute response deadline.","metadata":{"papermill":{"duration":0.00196,"end_time":"2024-10-10T13:05:45.853279","exception":false,"start_time":"2024-10-10T13:05:45.851319","status":"completed"},"tags":[]}},{"cell_type":"code","source":"inference_server = kaggle_evaluation.jane_street_inference_server.JSInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        (\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/test.parquet',\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/lags.parquet',\n        )\n    )","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2024-10-10T13:05:45.859184Z","iopub.status.busy":"2024-10-10T13:05:45.858751Z","iopub.status.idle":"2024-10-10T13:05:46.160987Z","shell.execute_reply":"2024-10-10T13:05:46.159859Z"},"papermill":{"duration":0.308219,"end_time":"2024-10-10T13:05:46.163573","exception":false,"start_time":"2024-10-10T13:05:45.855354","status":"completed"},"tags":[]},"outputs":[],"execution_count":null}]}