{
  "id": 542612,
  "title": "How to use lags for online submission?",
  "url": "/competitions/jane-street-real-time-market-data-forecasting/discussion/542612",
  "author_name": "",
  "post_date": "2024-10-25T19:07:43.618223200Z",
  "votes": 2,
  "comment_count": 5,
  "views": 0,
  "content": "<p>Hi everyone, does anybody have an idea or code snippet on how to use lags for online submission.</p>\n<pre><code> () -&gt; pl.DataFrame | pd.DataFrame:\n    \n     model, model_trained\n\n    \n     lags_\n     lags   :\n        lags_ = lags\n\n    \n      model_trained:\n         os.path.exists(MODEL_PATH):\n            ()\n            load_model()\n        :\n            ()\n            train_model()\n              model_trained:\n                 RuntimeError()\n\n\n\n    \n    :\n        \n        ()\n        row_ids = test[].to_numpy()\n        test = test.join(lags, on=[, , ], how=)\n        test = test.drop([,, ])\n        test = preprocess_data(test)\n        test_features = test.values\n     Exception  e:\n        ()\n        \n\n    \n    :\n        ()\n        test_preds = model.predict(test_features).ravel()\n     Exception  e:\n        ()\n        \n\n    \n    predictions = pd.DataFrame({\n        : row_ids,\n        : test_preds\n    })\n\n    \n    predictions = pl.DataFrame(predictions)\n    (predictions)\n     predictions\n\n\n\ninference_server = JSInferenceServer(predict)\n\n\n os.getenv():\n    ()\n    inference_server.serve()\n:\n    ()\n    inference_server.run_local_gateway(\n        (\n            ,\n            ,\n        )\n    )\n</code></pre>\n<p>I also tried to do this, rather than dropping is_scored</p>\n<pre><code>all_columns = [col for col in test.columns if col != 'is_scored']\n= test[all_columns]\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F9778207%2F3dd9ffd0f2ce5794b4849ec7b177bbfd%2FScreenshot%202024-10-25%20150457.png?generation=1729883124447298&amp;alt=media\" alt=\"\"><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F9778207%2Fb529bdff9db3e4a6b8237f2323f9beec%2FScreenshot%202024-10-25%20150625.png?generation=1729883199975948&amp;alt=media\" alt=\"\"><br>\nBoth of them are working for local_gateway, my notebook is running without any error, but as soon as scoring starts it throws an error</p>",
  "messages": [
    {
      "id": "3028264",
      "postDate": "10/25/2024 19:07:43",
      "content": "<p>Hi everyone, does anybody have an idea or code snippet on how to use lags for online submission.</p>\n<pre><code> () -&gt; pl.DataFrame | pd.DataFrame:\n    \n     model, model_trained\n\n    \n     lags_\n     lags   :\n        lags_ = lags\n\n    \n      model_trained:\n         os.path.exists(MODEL_PATH):\n            ()\n            load_model()\n        :\n            ()\n            train_model()\n              model_trained:\n                 RuntimeError()\n\n\n\n    \n    :\n        \n        ()\n        row_ids = test[].to_numpy()\n        test = test.join(lags, on=[, , ], how=)\n        test = test.drop([,, ])\n        test = preprocess_data(test)\n        test_features = test.values\n     Exception  e:\n        ()\n        \n\n    \n    :\n        ()\n        test_preds = model.predict(test_features).ravel()\n     Exception  e:\n        ()\n        \n\n    \n    predictions = pd.DataFrame({\n        : row_ids,\n        : test_preds\n    })\n\n    \n    predictions = pl.DataFrame(predictions)\n    (predictions)\n     predictions\n\n\n\ninference_server = JSInferenceServer(predict)\n\n\n os.getenv():\n    ()\n    inference_server.serve()\n:\n    ()\n    inference_server.run_local_gateway(\n        (\n            ,\n            ,\n        )\n    )\n</code></pre>\n<p>I also tried to do this, rather than dropping is_scored</p>\n<pre><code>all_columns = [col for col in test.columns if col != 'is_scored']\n= test[all_columns]\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F9778207%2F3dd9ffd0f2ce5794b4849ec7b177bbfd%2FScreenshot%202024-10-25%20150457.png?generation=1729883124447298&amp;alt=media\" alt=\"\"><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F9778207%2Fb529bdff9db3e4a6b8237f2323f9beec%2FScreenshot%202024-10-25%20150625.png?generation=1729883199975948&amp;alt=media\" alt=\"\"><br>\nBoth of them are working for local_gateway, my notebook is running without any error, but as soon as scoring starts it throws an error</p>",
      "rawMarkdown": "Hi everyone, does anybody have an idea or code snippet on how to use lags for online submission.\n\n```\ndef predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"\n    Make a prediction.\n    \n    Parameters:\n    - test: pl.DataFrame containing the test data batch.\n    - lags: pl.DataFrame | None containing lag features (optional).\n    \n    Returns:\n    - pl.DataFrame or pd.DataFrame with columns ['row_id', 'responder_6']\n    \"\"\"\n    global model, model_trained\n    \n    # Handle lags if provided\n    global lags_\n    if lags is not None:\n        lags_ = lags\n    \n    # If the model is not trained yet, attempt to load it\n    if not model_trained:\n        if os.path.exists(MODEL_PATH):\n            print(\"Loading the saved model...\")\n            load_model()\n        else:\n            print(\"Training model as no saved model was found...\")\n            train_model()\n            if not model_trained:\n                raise RuntimeError(\"Model training failed. Cannot proceed with predictions.\")\n    \n\n#     feature_cols = [col for col in test.columns if 'feature' in col]\n    # Prepare the test data\n    try:\n        # Preprocess the test data using the preprocess_data function\n        print(\"Preprocessing test data...\")\n        row_ids = test['row_id'].to_numpy()\n        test = test.join(lags, on=[\"symbol_id\", \"date_id\", \"time_id\"], how=\"left\")\n        test = test.drop(['row_id','responder_6_lag_1', 'is_scored'])\n        test = preprocess_data(test)\n        test_features = test.values\n    except Exception as e:\n        print(f\"Error processing test data: {e}\")\n        raise\n    \n    # Make predictions using the trained model\n    try:\n        print(\"Making predictions...\")\n        test_preds = model.predict(test_features).ravel()\n    except Exception as e:\n        print(f\"Error during prediction: {e}\")\n        raise\n    \n    # Create the predictions DataFrame (using Pandas as it is already converted)\n    predictions = pd.DataFrame({\n        'row_id': row_ids,\n        'responder_6': test_preds\n    })\n    \n    # Convert back to Polars if needed for further processing\n    predictions = pl.DataFrame(predictions)\n    print(predictions)\n    return predictions\n\n\n# Initialize the inference server with the predict function\ninference_server = JSInferenceServer(predict)\n\n# Start the inference server\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    print(\"Serving predictions on Kaggle inference server...\")\n    inference_server.serve()\nelse:\n    print(\"Running local gateway for inference server...\")\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    )\n```\nI also tried to do this, rather than dropping is_scored\n```\nall_columns = [col for col in test.columns if col != 'is_scored']\ntest = test[all_columns]\n```\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F9778207%2F3dd9ffd0f2ce5794b4849ec7b177bbfd%2FScreenshot%202024-10-25%20150457.png?generation=1729883124447298&alt=media)\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F9778207%2Fb529bdff9db3e4a6b8237f2323f9beec%2FScreenshot%202024-10-25%20150625.png?generation=1729883199975948&alt=media)\nBoth of them are working for local_gateway, my notebook is running without any error, but as soon as scoring starts it throws an error",
      "votes": null
    },
    {
      "id": "3028269",
      "postDate": "10/25/2024 19:16:42",
      "content": "<p>maybe it took more than one minute to predict the batch?</p>",
      "rawMarkdown": "maybe it took more than one minute to predict the batch?",
      "votes": null
    },
    {
      "id": "3028293",
      "postDate": "10/25/2024 19:57:45",
      "content": "<p>The first prediction batch doesn't have 1 minute deadline. Moreover, in those case the error raise is either \"Notebook Timeout\" or \"Notebook Inference Server Error\". <br>\nThis is different error, My best guess is that, somehow, the handling of lags is not right or something for online inference.</p>",
      "rawMarkdown": "The first prediction batch doesn't have 1 minute deadline. Moreover, in those case the error raise is either \"Notebook Timeout\" or \"Notebook Inference Server Error\". \nThis is different error, My best guess is that, somehow, the handling of lags is not right or something for online inference.",
      "votes": null
    },
    {
      "id": "3028297",
      "postDate": "10/25/2024 20:05:20",
      "content": "<p>you can use the synthetic test data to teat all corner cases. Symbols &amp; time ids are not always aligned between dates.</p>\n<p>The synthetic test data can be found in the public notebook. Simply add it as a dataset to your notebook and replace the parquet path in run_local_gateway()</p>",
      "rawMarkdown": "you can use the synthetic test data to teat all corner cases. Symbols & time ids are not always aligned between dates.\n\nThe synthetic test data can be found in the public notebook. Simply add it as a dataset to your notebook and replace the parquet path in run_local_gateway()",
      "votes": null
    },
    {
      "id": "3030352",
      "postDate": "10/28/2024 12:53:23",
      "content": "<p>I have successfully used lags, and I hope what I am going to say next will be helpful to you:</p>\n<p>I synthesized test data and lags using training data, and when I called the inference function, I found that there were approximately 39 data points per call.</p>\n<p>We know that there are approximately 37000 test data and lags in a day.</p>\n<p>Every time the function is called, there are 39 test datas, but lags will read 37000 at once, so there needs to be a global  <b>lags_</b> to store them.</p>\n<p>By the way, I would like to remind you to pay attention to the difference between 'lags_' and' lags'.</p>",
      "rawMarkdown": "I have successfully used lags, and I hope what I am going to say next will be helpful to you:\n\nI synthesized test data and lags using training data, and when I called the inference function, I found that there were approximately 39 data points per call.\n\nWe know that there are approximately 37000 test data and lags in a day.\n\nEvery time the function is called, there are 39 test datas, but lags will read 37000 at once, so there needs to be a global  <b>lags_</b> to store them.\n\nBy the way, I would like to remind you to pay attention to the difference between 'lags_' and' lags'.",
      "votes": null
    },
    {
      "id": "3055689",
      "postDate": "11/26/2024 02:48:35",
      "content": "<p>resurfacing an old post here :)!</p>\n<p>Did you get it to work?  I have the same problem!</p>\n<p>I`m using lags_ and merging with test. It should not matter if the current test batch has different symbols than the lag_, as with the merge the lags_ will be null and I just fill them. But, somehow, I get the Notebook Threw Exception Error and the logs seem fine.</p>",
      "rawMarkdown": "resurfacing an old post here :)!\n\nDid you get it to work?  I have the same problem!\n\nI`m using lags_ and merging with test. It should not matter if the current test batch has different symbols than the lag_, as with the merge the lags_ will be null and I just fill them. But, somehow, I get the Notebook Threw Exception Error and the logs seem fine.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 3028269,
      "author_name": "aymanallawi",
      "author_url": "",
      "post_date": "10/25/2024 19:16:42",
      "content": "<p>maybe it took more than one minute to predict the batch?</p>",
      "votes": null,
      "replies": [
        {
          "id": 3028293,
          "author_name": "jayshrivastava",
          "author_url": "",
          "post_date": "10/25/2024 19:57:45",
          "content": "<p>The first prediction batch doesn't have 1 minute deadline. Moreover, in those case the error raise is either \"Notebook Timeout\" or \"Notebook Inference Server Error\". <br>\nThis is different error, My best guess is that, somehow, the handling of lags is not right or something for online inference.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 3028297,
      "author_name": "shiyili",
      "author_url": "",
      "post_date": "10/25/2024 20:05:20",
      "content": "<p>you can use the synthetic test data to teat all corner cases. Symbols &amp; time ids are not always aligned between dates.</p>\n<p>The synthetic test data can be found in the public notebook. Simply add it as a dataset to your notebook and replace the parquet path in run_local_gateway()</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 3030352,
      "author_name": "yunsuxiaozi",
      "author_url": "",
      "post_date": "10/28/2024 12:53:23",
      "content": "<p>I have successfully used lags, and I hope what I am going to say next will be helpful to you:</p>\n<p>I synthesized test data and lags using training data, and when I called the inference function, I found that there were approximately 39 data points per call.</p>\n<p>We know that there are approximately 37000 test data and lags in a day.</p>\n<p>Every time the function is called, there are 39 test datas, but lags will read 37000 at once, so there needs to be a global  <b>lags_</b> to store them.</p>\n<p>By the way, I would like to remind you to pay attention to the difference between 'lags_' and' lags'.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 3055689,
      "author_name": "tomazrochadamota",
      "author_url": "",
      "post_date": "11/26/2024 02:48:35",
      "content": "<p>resurfacing an old post here :)!</p>\n<p>Did you get it to work?  I have the same problem!</p>\n<p>I`m using lags_ and merging with test. It should not matter if the current test batch has different symbols than the lag_, as with the merge the lags_ will be null and I just fill them. But, somehow, I get the Notebook Threw Exception Error and the logs seem fine.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "3028264": "Hi everyone, does anybody have an idea or code snippet on how to use lags for online submission.\n\n```\ndef predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"\n    Make a prediction.\n    \n    Parameters:\n    - test: pl.DataFrame containing the test data batch.\n    - lags: pl.DataFrame | None containing lag features (optional).\n    \n    Returns:\n    - pl.DataFrame or pd.DataFrame with columns ['row_id', 'responder_6']\n    \"\"\"\n    global model, model_trained\n    \n    # Handle lags if provided\n    global lags_\n    if lags is not None:\n        lags_ = lags\n    \n    # If the model is not trained yet, attempt to load it\n    if not model_trained:\n        if os.path.exists(MODEL_PATH):\n            print(\"Loading the saved model...\")\n            load_model()\n        else:\n            print(\"Training model as no saved model was found...\")\n            train_model()\n            if not model_trained:\n                raise RuntimeError(\"Model training failed. Cannot proceed with predictions.\")\n    \n\n#     feature_cols = [col for col in test.columns if 'feature' in col]\n    # Prepare the test data\n    try:\n        # Preprocess the test data using the preprocess_data function\n        print(\"Preprocessing test data...\")\n        row_ids = test['row_id'].to_numpy()\n        test = test.join(lags, on=[\"symbol_id\", \"date_id\", \"time_id\"], how=\"left\")\n        test = test.drop(['row_id','responder_6_lag_1', 'is_scored'])\n        test = preprocess_data(test)\n        test_features = test.values\n    except Exception as e:\n        print(f\"Error processing test data: {e}\")\n        raise\n    \n    # Make predictions using the trained model\n    try:\n        print(\"Making predictions...\")\n        test_preds = model.predict(test_features).ravel()\n    except Exception as e:\n        print(f\"Error during prediction: {e}\")\n        raise\n    \n    # Create the predictions DataFrame (using Pandas as it is already converted)\n    predictions = pd.DataFrame({\n        'row_id': row_ids,\n        'responder_6': test_preds\n    })\n    \n    # Convert back to Polars if needed for further processing\n    predictions = pl.DataFrame(predictions)\n    print(predictions)\n    return predictions\n\n\n# Initialize the inference server with the predict function\ninference_server = JSInferenceServer(predict)\n\n# Start the inference server\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    print(\"Serving predictions on Kaggle inference server...\")\n    inference_server.serve()\nelse:\n    print(\"Running local gateway for inference server...\")\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    )\n```\nI also tried to do this, rather than dropping is_scored\n```\nall_columns = [col for col in test.columns if col != 'is_scored']\ntest = test[all_columns]\n```\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F9778207%2F3dd9ffd0f2ce5794b4849ec7b177bbfd%2FScreenshot%202024-10-25%20150457.png?generation=1729883124447298&alt=media)\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F9778207%2Fb529bdff9db3e4a6b8237f2323f9beec%2FScreenshot%202024-10-25%20150625.png?generation=1729883199975948&alt=media)\nBoth of them are working for local_gateway, my notebook is running without any error, but as soon as scoring starts it throws an error",
    "3028269": "maybe it took more than one minute to predict the batch?",
    "3028293": "The first prediction batch doesn't have 1 minute deadline. Moreover, in those case the error raise is either \"Notebook Timeout\" or \"Notebook Inference Server Error\". \nThis is different error, My best guess is that, somehow, the handling of lags is not right or something for online inference.",
    "3028297": "you can use the synthetic test data to teat all corner cases. Symbols & time ids are not always aligned between dates.\n\nThe synthetic test data can be found in the public notebook. Simply add it as a dataset to your notebook and replace the parquet path in run_local_gateway()",
    "3030352": "I have successfully used lags, and I hope what I am going to say next will be helpful to you:\n\nI synthesized test data and lags using training data, and when I called the inference function, I found that there were approximately 39 data points per call.\n\nWe know that there are approximately 37000 test data and lags in a day.\n\nEvery time the function is called, there are 39 test datas, but lags will read 37000 at once, so there needs to be a global  <b>lags_</b> to store them.\n\nBy the way, I would like to remind you to pay attention to the difference between 'lags_' and' lags'.",
    "3055689": "resurfacing an old post here :)!\n\nDid you get it to work?  I have the same problem!\n\nI`m using lags_ and merging with test. It should not matter if the current test batch has different symbols than the lag_, as with the merge the lags_ will be null and I just fill them. But, somehow, I get the Notebook Threw Exception Error and the logs seem fine."
  },
  "source": "meta"
}