{"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":84795,"databundleVersionId":11281725,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"So long story short. Just making of proper test process and tryiing to track out the traces of files involved in failed tests is troublesome. There are too many questions like if reset of first prediction marker will be done as if assigning False value is kept like in example, the predict function gets full run only once. And a LOT of questions more Also repos are cut and therefore getting things done becomes too time and work consuming. This, I think is my last night I spend for the project as it is interesting, but takes my time off the main job. Now first 2 tox tests are sending me congratulations message (so, no failed_tests? Then why database has diff to fix them? Again, questions). Other 4 are making some py311: FAIL code 1 (144.32 seconds) evaluation failed :( (145.04 seconds)\nHope this notebook will help someone. If you win and this document really helped, send me a coffee and Lamborghini :D","metadata":{}},{"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","trusted":true,"execution":{"iopub.status.busy":"2025-03-05T01:55:53.145542Z","iopub.execute_input":"2025-03-05T01:55:53.146063Z","iopub.status.idle":"2025-03-05T01:55:58.020888Z","shell.execute_reply.started":"2025-03-05T01:55:53.145973Z","shell.execute_reply":"2025-03-05T01:55:58.019695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import io\nimport os\nimport shutil\nimport subprocess\nimport time\n\nstart_time = time.time()\n\nimport pandas as pd\nimport polars as pl\n\nimport kaggle_evaluation.konwinski_prize_inference_server\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T01:55:58.022761Z","iopub.execute_input":"2025-03-05T01:55:58.023128Z","iopub.status.idle":"2025-03-05T01:55:58.028493Z","shell.execute_reply.started":"2025-03-05T01:55:58.023098Z","shell.execute_reply":"2025-03-05T01:55:58.027203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import subprocess\nimport os\n\n# Attempt to simulate pre-load which seems failed\n\n\n\n# Run the pip download command and capture output\nresult = subprocess.run(\n    'pip download tox pygit py --dest extra_wheels',\n    shell=True,\n    executable=\"/bin/bash\",\n    capture_output=True,  # Capture stdout and stderr\n    text=True\n)\n\n# Print stdout and stderr for debugging\nprint(\"STDOUT:\\n\", result.stdout)\nprint(\"STDERR:\\n\", result.stderr)\n\n# Check if the directory exists and list files\nif os.path.exists('extra_wheels'):\n    files = os.listdir('extra_wheels/')\n    print(\"Contents of the folder:\")\n    for file in files:\n        print(file)\nelse:\n    print(\"The directory 'extra_wheels' does not exist.\")\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T01:55:58.030364Z","iopub.execute_input":"2025-03-05T01:55:58.030694Z","iopub.status.idle":"2025-03-05T01:56:02.123249Z","shell.execute_reply.started":"2025-03-05T01:55:58.030658Z","shell.execute_reply":"2025-03-05T01:56:02.121857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"instance_count = None\n\ndef get_number_of_instances(num_instances: int) -> None:\n    \"\"\" The very first message from the gateway will be the total number of instances to be served.\n    You don't need to edit this function.\n    \"\"\"\n    global instance_count\n    instance_count = num_instances","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T01:56:02.12464Z","iopub.execute_input":"2025-03-05T01:56:02.125133Z","iopub.status.idle":"2025-03-05T01:56:02.130682Z","shell.execute_reply.started":"2025-03-05T01:56:02.125088Z","shell.execute_reply":"2025-03-05T01:56:02.129401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# a little sugar for installing wheels from pip_packages\n\nimport glob\nimport os\nimport subprocess\n\ndef install_wheels(wheel_dir):\n    # Get a list of all .whl files in the directory\n    wheel_files = glob.glob(os.path.join(wheel_dir, \"*.whl\"))\n\n    if wheel_files:\n        for wheel_path in wheel_files:\n            print(f\"Installing: {wheel_path}\")\n\n            # Run both commands in a single call using '&&'\n            command = f\"source .venv/bin/activate && uv pip install {wheel_path}\"\n            \n            result = subprocess.run(\n                command,\n                shell=True,\n                executable=\"/bin/bash\",\n                cwd='repo',\n                capture_output=True,\n                text=True\n            )\n\n            # Print the output of the command\n            print(result.stdout)\n            if result.stderr:\n                #print(result.stderr)\n                print(f'Error on wheel{wheel_path}')\n\n    else:\n        print(f\"No .whl files found in {wheel_dir}\")\n\ndef run_command(command, cwd=None, env=None):\n    \"\"\"Helper function to run shell commands with subprocess.\"\"\"\n    esult = subprocess.run(command, cwd=cwd, env=env, shell=True, text=True, capture_output=True, executable=\"/bin/bash\")\n    if result.returncode != 0:\n        print(f\"Error: {command}\\n{result.stderr}\")\n    else:\n        print(result.stdout)\n    return result.returncode == 0\n    \n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T01:56:02.132216Z","iopub.execute_input":"2025-03-05T01:56:02.132615Z","iopub.status.idle":"2025-03-05T01:56:02.150657Z","shell.execute_reply.started":"2025-03-05T01:56:02.132574Z","shell.execute_reply":"2025-03-05T01:56:02.149476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"no_op_patch = \"\"\"\n--- /dev/null\n+++ b/docs/changes/table/17048.bugfix.rst\n@@ -0,0 +1,2 @@\n+Ensure that initializing a ``QTable`` with explicit units` also succeeds if\n+one of the units is ``u.one``.\n\"\"\"\n\nfirst_prediction = True\nnumber_of_run = 0\n\nimport re\nimport os\nimport git\n\ndef predict(problem_statement: str, repo_archive: io.BytesIO, pip_packages_archive: io.BytesIO, env_setup_cmds_templates: list[str]) -> str:\n    \"\"\" Replace this function with your inference code.\n    Args:\n        problem_statement: The text of the git issue.\n        repo_path: A BytesIO buffer path with a .tar containing the codebase that must be patched. The gateway will make this directory available immediately before this function runs.\n        pip_packages_archive: A BytesIO buffer path with a .tar containing the wheel files necessary for running unit tests.\n        env_setup_cmds_templates: Commands necessary for installing the pip_packages_archive.\n    \"\"\"\n        #print(problem_statement)\n    global number_of_run\n    number_of_run += 1\n    print(f'Running predict {number_of_run}')\n    # !!!!!!!!!!!!!!!!\n    # control of runs to focus on speified repo\n    # Example if number_of_run !=3 disables everything, but 3rd run\n    if number_of_run ==7:\n        return None  # Skip issue.\n        \n    # Unpack the codebase to be patched into a directory that won't be exported when\n    # the notebook is saved.\n    archive_path = '/tmp/repo_archive.tar'\n    with open(archive_path, 'wb') as f:\n        f.write(repo_archive.read())\n    repo_path = 'repo'\n    if os.path.exists(repo_path):\n        shutil.rmtree(repo_path)\n    shutil.unpack_archive(archive_path, extract_dir=repo_path)\n    os.remove(archive_path)\n\n    \"\"\"\n    Unpack pip_packages if you want to run unit tests on your patch.\n    Note that editing unit tests with your patch -- even to add valid tests -- can cause your submission to be flagged as a failure.\n    Most of the relevant repos use pytest for running tests. You will almost certainly need to run only a subset of the unit tests to avoid running out of inference time.\n    \"\"\"\n    pip_archive_dir = '/tmp/pip_packages_archive.tar'\n    with open(pip_archive_dir, 'wb') as f:\n        f.write(pip_packages_archive.read())\n    pip_packages_path = 'pip_packages'\n    if os.path.exists(pip_packages_path):\n        shutil.rmtree(pip_packages_path)\n    shutil.unpack_archive(pip_archive_dir, extract_dir=pip_packages_path)\n    os.remove(pip_archive_dir)\n\n    # Get env setup cmds by setting the pip_packages_path\n    env_setup_cmds = [cmd.format(pip_packages_path=pip_packages_path) for cmd in env_setup_cmds_templates]\n\n  \n    subprocess.run(\n        \"\\n\".join(env_setup_cmds),\n        shell=True,\n        executable=\"/bin/bash\",\n        cwd=repo_path,\n    )\n\n\n    files = os.listdir(repo_path)\n        \n    print(\"Contents of the folder:\")\n    for file in files:\n        print(file)\n    if os.path.exists('repo/tox.ini'): # and number_of_run >2:\n        print('Testing with tox')\n        git_path = os.path.join(repo_path, \".git\")\n        if not os.path.exists(git_path):\n            print('No .GIT ')\n            # Initialize Git repository\n            repo = git.Repo.init(repo_path)\n            print(f\"Initialized Git repository in {repo_path}\")\n            \n            # Configure Git user (only if not globally configured)\n            with repo.config_writer() as config:\n                config.set_value(\"user\", \"name\", \"Your Name\")\n                config.set_value(\"user\", \"email\", \"your_email@example.com\")\n            \n            # Add all files and commit\n            repo.git.add(all=True)\n            repo.index.commit(\"Fake commit\")\n            print(\"Committed changes.\")\n            \n            # Create an annotated tag\n            repo.create_tag(\"v1.0.0\", message=\"Set version tag\")\n            print(\"Tag v1.0.0 created.\")\n    \n            # print(\"Installing package using setuptools_scm.\")\n            \n            # Copy environment variables\n            env_vars = os.environ.copy()\n            env_vars[\"SETUPTOOLS_SCM_PRETEND_VERSION\"] = \"1.0.0\"\n            # Define the old and new project names\n            # Define old and new project names\n            old_name = \"astropy\"\n            new_name = \"my_project\"\n            \n            \n            # Rename in pyproject.toml (if it exists)\n            pyproject_toml_path = os.path.join(repo_path, \"pyproject.toml\")\n            if os.path.exists(pyproject_toml_path):\n                subprocess.run(\n                    f\"sed -i 's/^name = \\\"{old_name}\\\"/name = \\\"{new_name}\\\"/' pyproject.toml\",\n                    shell=True,\n                    check=True,\n                    executable=\"/bin/bash\",\n                    cwd=repo_path,\n                )\n\n\n            # run_command(f\"source .venv/bin/activate && uv pip install --no-index --find-links={pip_packages_path} -e {repo_path}\", env=env_vars)\n\n            # # Build C extensions\n            # run_command(f\"source .venv/bin/activate && uv {repo_path}/setup.py build_ext --inplace\")\n            # # Run setuptools_scm to check version\n            # env_vars[\"GIT_DIR\"] = os.path.join(repo_path, \".git\")\n            # env_vars[\"GIT_WORK_TREE\"] = repo_path\n            # run_command(f\"source .venv/bin/activate && uv run setuptools_scm\", env=env_vars)                  \n            run_command(f\"source .venv/bin/activate && uv pip install py tox pygit\", env=env_vars)\n            # run_command(f\"source .venv/bin/activate && uv pip install -e astropy[dev-all]\", env=env_vars)\n\n            extra_packages_path = 'extra_wheels' \n            install_wheels(extra_packages_path)\n            #install_wheels(pip_packages_path)\n\n            #run_command(f\"source .venv/bin/activate && uv pip install --no-index --find-links=pip_packages && pip install -e .[test]\")\n            run_command(f\"source .venv/bin/activate && uv pip install --no-index --find-links=pip_packages && pip install . \")\n            #run_command(f\"source .venv/bin/activate && uv pip uninstall asdf-astropy astropy\")\n\n\n            # 🚀 Run Tox Tests with Virtual Environment Activation\n            print(\"\\n🚀 Running tox tests...\")\n\n\n\n            \n            \n            # Verify the change\n            subprocess.run(\"grep 'name=' setup.py\", shell=True)\n\n            tox_ini_path = os.path.join(repo_path, \"tox.ini\")\n            #tox_command = f'source .venv/bin/activate && SETUPTOOLS_SCM_PRETEND_VERSION=\"1.2.3\" uv run tox -c {tox_ini_path} -e py311 --parallel auto -vv'\n            \n            tox_command = f'source .venv/bin/activate && uv pip install tox '\n\n            result = subprocess.run(\n                tox_command,\n                shell=True,\n                executable=\"/bin/bash\",\n                cwd=repo_path,\n                capture_output=True,\n                text=True\n            )\n            print(\"STDOUT:\\n\", result.stdout)\n            print(\"STDERR:\\n\", result.stderr)\n\n            if os.path.exists(tox_ini_path):\n                print(\"tox.ini exists!\")\n            tox_command = f'source .venv/bin/activate && uv run tox -c tox.ini -e py311 --parallel auto -vv'\n\n            result = subprocess.run(\n                tox_command,\n                shell=True,\n                executable=\"/bin/bash\",\n                cwd=repo_path,\n                capture_output=True,\n                text=True\n            )\n            \n            print(\"STDOUT:\\n\", result.stdout)\n            print(\"STDERR:\\n\", result.stderr)\n\n        \n    else:\n        print('tox.ini does not exist or ignored')\n        # Run pytest with -rxX to show xfailed and xpassed details\n        install_wheels(pip_packages_path)\n        result = subprocess.run(\n            \"source .venv/bin/activate && pytest\",\n            shell=True,\n            executable=\"/bin/bash\",\n            cwd='repo',\n            capture_output=True,\n            text=True\n        )\n        \n\n      \n        # Combine stdout and stderr\n        output = result.stdout + result.stderr  \n        \n        # Save raw output for debugging\n        timestamp = str(int(time.time() - start_time)).zfill(5)\n        \n        with open(f\"{timestamp}-out.txt\", \"w\") as f:\n            f.write(result.stdout)\n        \n        with open(f\"{timestamp}-err.txt\", \"w\") as f:\n            f.write(result.stderr)\n        \n        # Print full output\n        print(len(result.stdout))\n        print(\"\\n\".join(result.stdout.split(\"\\n\")))\n        \n        print(len(result.stderr))\n        print(\"\\n\".join(result.stderr.split(\"\\n\")))\n        \n        # 🔍 **Extract failed tests using regex**\n        failed_tests = re.findall(r\"FAILED (tests/[\\w/]+\\.py::[\\w\\[\\]]+)\", output)\n        \n        # Print and save failed tests\n        if failed_tests:\n            print(\"\\n❌ Failed Tests List:\")\n            for test in failed_tests:\n                print(test)\n            with open(f\"{timestamp}-failed-tests.txt\", \"w\") as f:\n                f.write(\"\\n\".join(failed_tests))\n        else:\n            print(\"\\n✅ No failed tests!\")\n\n\n    \n    # Cleanup\n    \n    if os.path.exists(repo_path):\n        shutil.rmtree(repo_path)\n    if os.path.exists(pip_packages_path):\n        shutil.rmtree(pip_packages_path)\n\n    return no_op_patch","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T01:56:02.151895Z","iopub.execute_input":"2025-03-05T01:56:02.152229Z","iopub.status.idle":"2025-03-05T01:56:02.178098Z","shell.execute_reply.started":"2025-03-05T01:56:02.1522Z","shell.execute_reply":"2025-03-05T01:56:02.176937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_server = kaggle_evaluation.konwinski_prize_inference_server.KPrizeInferenceServer(\n    get_number_of_instances,\n    predict\n)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        data_paths=(\n            '/kaggle/input/konwinski-prize/',  # Path to the entire competition dataset\n            '/kaggle/tmp/konwinski-prize/',   # Path to a scratch directory for unpacking data.a_zip.\n        ),\n        use_concurrency=True,  # This can safely be disabled for purposes of local testing if necessary.\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T01:56:02.17925Z","iopub.execute_input":"2025-03-05T01:56:02.179604Z","iopub.status.idle":"2025-03-05T02:21:54.058468Z","shell.execute_reply.started":"2025-03-05T01:56:02.179576Z","shell.execute_reply":"2025-03-05T02:21:54.057087Z"}},"outputs":[],"execution_count":null}]}