{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Why\nIt took me some time and a bunch of failed submissions just to figure out how the inputs and outputs work in this competition.  \nhopefully this could help others getting started with basic results submission","metadata":{}},{"cell_type":"markdown","source":"# Reading the files","metadata":{}},{"cell_type":"code","source":"import os\nfrom glob import glob \n\ndef get_file_names(path):\n    file_names = glob(os.path.join(path, *['*'] * 3, '*.jpg'))\n    return file_names\n\ndef convert_to_image_ids(fnames):\n    image_ids = []\n    for fname in fnames:\n        image_id = os.path.splitext(os.path.basename(fname))[0]\n        image_ids.append(image_id)\n    return image_ids\n\ndef get_image_ids(path):\n    file_names = get_file_names(path)\n    image_ids = convert_to_image_ids(file_names)\n    return image_ids","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-20T22:05:30.088465Z","iopub.execute_input":"2021-08-20T22:05:30.089069Z","iopub.status.idle":"2021-08-20T22:05:30.09764Z","shell.execute_reply.started":"2021-08-20T22:05:30.089009Z","shell.execute_reply":"2021-08-20T22:05:30.09683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generating random sample submission","metadata":{}},{"cell_type":"code","source":"import csv\nimport numpy as np \nfrom dataclasses import dataclass\nfrom typing import List\n\n@dataclass\nclass RetrievalResult:\n    test_id: str\n    chosen_ids: List[str]        \n\n        \ndef generate_random_results(test_ids, index_ids):\n    for test_id in test_ids:\n        chosen_ids = np.random.choice(index_ids, 100, replace=False)\n        yield RetrievalResult(\n            test_id=test_id,\n            chosen_ids=chosen_ids\n        )\n\ndef write_submission(output_fname, results: List[RetrievalResult]):\n    with open(output_fname, 'w') as f:\n        writer = csv.DictWriter(f, fieldnames=('id', 'images'))\n        writer.writeheader()\n        for result in results:\n            writer.writerow(\n                {\n                    'id': result.test_id,\n                    'images': ' '.join(result.chosen_ids)\n                }\n            )","metadata":{"execution":{"iopub.status.busy":"2021-08-20T22:05:30.0993Z","iopub.execute_input":"2021-08-20T22:05:30.099951Z","iopub.status.idle":"2021-08-20T22:05:30.114373Z","shell.execute_reply.started":"2021-08-20T22:05:30.099903Z","shell.execute_reply":"2021-08-20T22:05:30.113559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Run it","metadata":{}},{"cell_type":"code","source":"data_path = '../input/landmark-retrieval-2021'\nindex_path = os.path.join(data_path, 'index')\ntest_path = os.path.join(data_path, 'test')\n\nindex_ids = get_image_ids(index_path)\ntest_ids = get_image_ids(test_path)\n\nresults = generate_random_results(test_ids, index_ids)\nwrite_submission(output_fname='submission.csv', results=results)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T22:05:30.116084Z","iopub.execute_input":"2021-08-20T22:05:30.116503Z","iopub.status.idle":"2021-08-20T22:05:56.492926Z","shell.execute_reply.started":"2021-08-20T22:05:30.11646Z","shell.execute_reply":"2021-08-20T22:05:56.49182Z"},"trusted":true},"execution_count":null,"outputs":[]}]}