{"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":"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)\nimport ast\nimport os\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-28T22:55:37.906932Z","iopub.execute_input":"2021-05-28T22:55:37.907227Z","iopub.status.idle":"2021-05-28T22:55:37.914698Z","shell.execute_reply.started":"2021-05-28T22:55:37.907162Z","shell.execute_reply":"2021-05-28T22:55:37.914145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/siim-covid19-detection/train_image_level.csv\", index_col = \"id\")\nid_lst = [ii[:-6] for ii in train_df.index.tolist()]\ntrain_df.index = id_lst\ntrain_df.index.name = \"id\"\nmeta_jpg = pd.read_csv(\"../input/siimcovid19converttojpg1024px/meta.csv\", index_col = \"image_id\")\ntrain_meta = meta_jpg[meta_jpg[\"split\"] == \"train\"]\ntrain_df = pd.concat([train_df, train_meta], axis=1, join=\"inner\")\ntrain_df.index.name = \"image_id\"\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:37.915644Z","iopub.execute_input":"2021-05-28T22:55:37.915931Z","iopub.status.idle":"2021-05-28T22:55:38.075817Z","shell.execute_reply.started":"2021-05-28T22:55:37.915903Z","shell.execute_reply":"2021-05-28T22:55:38.075057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.dropna(subset=[\"boxes\"])\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.077353Z","iopub.execute_input":"2021-05-28T22:55:38.077651Z","iopub.status.idle":"2021-05-28T22:55:38.113597Z","shell.execute_reply.started":"2021-05-28T22:55:38.077619Z","shell.execute_reply":"2021-05-28T22:55:38.112687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nassert type(result) is list","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.114976Z","iopub.execute_input":"2021-05-28T22:55:38.115269Z","iopub.status.idle":"2021-05-28T22:55:38.169511Z","shell.execute_reply.started":"2021-05-28T22:55:38.115236Z","shell.execute_reply":"2021-05-28T22:55:38.167056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.170187Z","iopub.status.idle":"2021-05-28T22:55:38.170641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str(result[bx_i][\"x\"]/train_df.loc[\"000a312787f2\", \"dim0\"])","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.171645Z","iopub.status.idle":"2021-05-28T22:55:38.172114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"this_bx","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.17289Z","iopub.status.idle":"2021-05-28T22:55:38.173219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_cls(cls_nm):\n    cls = ['Negative for Pneumonia',\n     'Typical Appearance',\n     'Indeterminate Appearance',\n     'Atypical Appearance']","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:25.285485Z","iopub.execute_input":"2021-05-28T22:56:25.285821Z","iopub.status.idle":"2021-05-28T22:56:25.292746Z","shell.execute_reply.started":"2021-05-28T22:56:25.285793Z","shell.execute_reply":"2021-05-28T22:56:25.292091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = ast.literal_eval(train_df.loc[\"000a312787f2\", \"boxes\"])\nassert type(result) is list\nresult","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:26.794994Z","iopub.execute_input":"2021-05-28T22:56:26.795246Z","iopub.status.idle":"2021-05-28T22:56:26.801317Z","shell.execute_reply.started":"2021-05-28T22:56:26.795223Z","shell.execute_reply":"2021-05-28T22:56:26.800711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"./labels/\")","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:28.8594Z","iopub.execute_input":"2021-05-28T22:56:28.859851Z","iopub.status.idle":"2021-05-28T22:56:28.86335Z","shell.execute_reply.started":"2021-05-28T22:56:28.859822Z","shell.execute_reply":"2021-05-28T22:56:28.862705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df = pd.read_csv(\"../input/siim-covid19-detection/train_study_level.csv\", index_col = \"id\")\nid_lst = [ii[:-6] for ii in label_df.index.tolist()]\nlabel_df.index = id_lst","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:31.988047Z","iopub.execute_input":"2021-05-28T22:56:31.989986Z","iopub.status.idle":"2021-05-28T22:56:32.021744Z","shell.execute_reply.started":"2021-05-28T22:56:31.989953Z","shell.execute_reply":"2021-05-28T22:56:32.021037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[\"8ae04761045f\", :]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:33.817947Z","iopub.execute_input":"2021-05-28T22:56:33.819872Z","iopub.status.idle":"2021-05-28T22:56:33.827409Z","shell.execute_reply.started":"2021-05-28T22:56:33.819837Z","shell.execute_reply":"2021-05-28T22:56:33.826408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[\"8ae04761045f\", \"boxes\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:35.434162Z","iopub.execute_input":"2021-05-28T22:56:35.434505Z","iopub.status.idle":"2021-05-28T22:56:35.439327Z","shell.execute_reply.started":"2021-05-28T22:56:35.434476Z","shell.execute_reply":"2021-05-28T22:56:35.438803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:37.235083Z","iopub.execute_input":"2021-05-28T22:56:37.23538Z","iopub.status.idle":"2021-05-28T22:56:37.246462Z","shell.execute_reply.started":"2021-05-28T22:56:37.235355Z","shell.execute_reply":"2021-05-28T22:56:37.245339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open(\"./label/\"+img+\".txt\", \"r\")\n","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:41.155021Z","iopub.execute_input":"2021-05-28T22:56:41.155392Z","iopub.status.idle":"2021-05-28T22:56:41.171446Z","shell.execute_reply.started":"2021-05-28T22:56:41.155362Z","shell.execute_reply":"2021-05-28T22:56:41.169957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.181821Z","iopub.status.idle":"2021-05-28T22:55:38.182141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.182945Z","iopub.status.idle":"2021-05-28T22:55:38.183259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"StudyInstanceUID\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.183899Z","iopub.status.idle":"2021-05-28T22:55:38.184215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[\"000a312787f2\", :]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:58:31.399846Z","iopub.execute_input":"2021-05-28T22:58:31.400122Z","iopub.status.idle":"2021-05-28T22:58:31.407209Z","shell.execute_reply.started":"2021-05-28T22:58:31.400099Z","shell.execute_reply":"2021-05-28T22:58:31.40624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cls = np.array([0, 1, 2, 3])\n\nfor img_i in range(len(train_df)):\n    \n    img = train_df.index[img_i]\n    res = ast.literal_eval(train_df.loc[img, \"boxes\"])\n    assert type(res) is list\n    \n    \n    label_inx = train_df.loc[img, \"StudyInstanceUID\"]\n    arr_01_cols = label_df.loc[label_inx, :].values.astype(bool)\n    \n    this_cls = cls[arr_01_cols]\n    \n    f = open(\"./labels/\"+img+\".txt\", \"a\")\n    \n    for bx_i in range(len(res)):\n\n        \n        f.write(str(0) +\" \"+ str((res[bx_i][\"x\"]+res[bx_i][\"width\"]/2)/train_df.loc[img, \"dim1\"]) + \" \" + str((res[bx_i][\"y\"]+res[bx_i][\"height\"]/2)/train_df.loc[img, \"dim0\"]) + \" \" + str(res[bx_i][\"width\"]/train_df.loc[img, \"dim1\"]) + \" \" + str(res[bx_i][\"height\"]/train_df.loc[img, \"dim0\"]))\n        f.write(\"\\n\")\n        \n    f.close()","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:56:50.671395Z","iopub.execute_input":"2021-05-28T22:56:50.671731Z","iopub.status.idle":"2021-05-28T22:56:52.354056Z","shell.execute_reply.started":"2021-05-28T22:56:50.671702Z","shell.execute_reply":"2021-05-28T22:56:52.353016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[\"000a312787f2\", \"boxes\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:57:43.267417Z","iopub.execute_input":"2021-05-28T22:57:43.267709Z","iopub.status.idle":"2021-05-28T22:57:43.273446Z","shell.execute_reply.started":"2021-05-28T22:57:43.267686Z","shell.execute_reply":"2021-05-28T22:57:43.272582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[\"000a312787f2\", \"label\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:57:45.571538Z","iopub.execute_input":"2021-05-28T22:57:45.571806Z","iopub.status.idle":"2021-05-28T22:57:45.576856Z","shell.execute_reply.started":"2021-05-28T22:57:45.571784Z","shell.execute_reply":"2021-05-28T22:57:45.575864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open(\"/kaggle/working/labels/000a312787f2.txt\", \"r\")\nf.readlines()","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.189221Z","iopub.status.idle":"2021-05-28T22:55:38.189552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.190304Z","iopub.status.idle":"2021-05-28T22:55:38.190618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr_01_cols","metadata":{"execution":{"iopub.status.busy":"2021-05-28T22:55:38.191221Z","iopub.status.idle":"2021-05-28T22:55:38.191552Z"},"trusted":true},"execution_count":null,"outputs":[]}]}