{"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":"import pandas as pd\nimport numpy as np\nimport os\nimport sys\nimport json\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as pch\nimport cv2\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-03-27T09:49:14.416054Z","iopub.execute_input":"2023-03-27T09:49:14.417520Z","iopub.status.idle":"2023-03-27T09:49:14.424445Z","shell.execute_reply.started":"2023-03-27T09:49:14.417460Z","shell.execute_reply":"2023-03-27T09:49:14.422907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## sample_sub\nsample_sub = pd.read_csv('/kaggle/input/benetech-making-graphs-accessible/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-27T09:38:12.634464Z","iopub.execute_input":"2023-03-27T09:38:12.635458Z","iopub.status.idle":"2023-03-27T09:38:12.651239Z","shell.execute_reply.started":"2023-03-27T09:38:12.635403Z","shell.execute_reply":"2023-03-27T09:38:12.650124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T09:38:13.257326Z","iopub.execute_input":"2023-03-27T09:38:13.257768Z","iopub.status.idle":"2023-03-27T09:38:13.284494Z","shell.execute_reply.started":"2023-03-27T09:38:13.257725Z","shell.execute_reply":"2023-03-27T09:38:13.283597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### convert train annotations to dataframe\ndef convert_annot2df(path):\n    res = []\n    col_name = ['name', 'source', 'chart-type', 'plot-bb', 'text', 'x_axis', 'y_axis', 'data_series_x', 'data_series_y']\n    for i in tqdm(os.listdir(path)):\n        tmp_name = i[:-5]\n        tmp_data = [tmp_name]\n        tmp_json_file_path = os.path.join(path, i)\n        with open(tmp_json_file_path, 'r') as tmp_file:\n            tmp_json_data = json.load(tmp_file)\n        \n        tmp_data.append(tmp_json_data['source']) # generated / extracted\n        tmp_data.append(tmp_json_data['chart-type']) # dot, horizontal_bar, vertical_bar, line, scatter.\n        tmp_data.append(','.join(map(str, [tmp_json_data['plot-bb']['height'], tmp_json_data['plot-bb']['width'], \n                                           tmp_json_data['plot-bb']['x0'], tmp_json_data['plot-bb']['y0']])))\n        \n        # text\n        tmp_data.append(tmp_json_data['text'])\n        # axes\n        tmp_data.append(tmp_json_data['axes']['x-axis'])\n        tmp_data.append(tmp_json_data['axes']['y-axis'])\n        # data_series_x\n        tmp_data_series_x = []\n        # data_series_y\n        tmp_data_series_y = []\n        for ds in tmp_json_data['data-series']:\n            tmp_data_series_x.append(str(ds['x']))\n            tmp_data_series_y.append(str(ds['y']))\n        tmp_data.append(';'.join(tmp_data_series_x))\n        tmp_data.append(';'.join(tmp_data_series_y))\n        \n        res.append(tmp_data)\n    return pd.DataFrame(res, columns=col_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T09:38:14.060759Z","iopub.execute_input":"2023-03-27T09:38:14.061181Z","iopub.status.idle":"2023-03-27T09:38:14.073386Z","shell.execute_reply.started":"2023-03-27T09:38:14.061142Z","shell.execute_reply":"2023-03-27T09:38:14.072330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotation_df = convert_annot2df('/kaggle/input/benetech-making-graphs-accessible/train/annotations')","metadata":{"execution":{"iopub.status.busy":"2023-03-27T09:38:14.869941Z","iopub.execute_input":"2023-03-27T09:38:14.870620Z","iopub.status.idle":"2023-03-27T09:44:31.606503Z","shell.execute_reply.started":"2023-03-27T09:38:14.870571Z","shell.execute_reply":"2023-03-27T09:44:31.605138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotation_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:09:53.820691Z","iopub.execute_input":"2023-03-27T08:09:53.821200Z","iopub.status.idle":"2023-03-27T08:09:53.953219Z","shell.execute_reply.started":"2023-03-27T08:09:53.821151Z","shell.execute_reply":"2023-03-27T08:09:53.951528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotation_df.to_csv('/kaggle/working/annotation_df.csv', index=None)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:10:34.224352Z","iopub.execute_input":"2023-03-27T08:10:34.224828Z","iopub.status.idle":"2023-03-27T08:10:45.388158Z","shell.execute_reply.started":"2023-03-27T08:10:34.224788Z","shell.execute_reply":"2023-03-27T08:10:45.387236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### training set","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:12:48.572747Z","iopub.execute_input":"2023-03-27T08:12:48.573146Z","iopub.status.idle":"2023-03-27T08:12:48.578495Z","shell.execute_reply.started":"2023-03-27T08:12:48.573112Z","shell.execute_reply":"2023-03-27T08:12:48.577225Z"}}},{"cell_type":"code","source":"annotation_df.groupby(['source', 'chart-type'])['chart-type'].count()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:14:43.518123Z","iopub.execute_input":"2023-03-27T08:14:43.518555Z","iopub.status.idle":"2023-03-27T08:14:43.580317Z","shell.execute_reply.started":"2023-03-27T08:14:43.518518Z","shell.execute_reply":"2023-03-27T08:14:43.579142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot(name):\n    sample = cv2.imread('/kaggle/input/benetech-making-graphs-accessible/train/images/'+name+'.jpg')\n    plt.imshow(sample)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:25:09.649549Z","iopub.execute_input":"2023-03-27T08:25:09.650088Z","iopub.status.idle":"2023-03-27T08:25:09.655643Z","shell.execute_reply.started":"2023-03-27T08:25:09.650050Z","shell.execute_reply":"2023-03-27T08:25:09.654345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### generated vertical_bar sample","metadata":{"execution":{"iopub.status.busy":"2023-03-27T07:23:56.563966Z","iopub.execute_input":"2023-03-27T07:23:56.565092Z","iopub.status.idle":"2023-03-27T07:23:56.570158Z","shell.execute_reply.started":"2023-03-27T07:23:56.565038Z","shell.execute_reply":"2023-03-27T07:23:56.568536Z"}}},{"cell_type":"code","source":"gvb_sample_name = annotation_df.loc[(annotation_df['chart-type']=='vertical_bar').values & (annotation_df['source']=='generated').values].sample(n=1)['name'].values[0]\nprint(gvb_sample_name)\nplot(gvb_sample_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:30:30.861035Z","iopub.execute_input":"2023-03-27T08:30:30.861491Z","iopub.status.idle":"2023-03-27T08:30:31.152474Z","shell.execute_reply.started":"2023-03-27T08:30:30.861452Z","shell.execute_reply":"2023-03-27T08:30:31.151129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### extracted vertical_bar sample","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:19:55.828379Z","iopub.execute_input":"2023-03-27T08:19:55.828831Z","iopub.status.idle":"2023-03-27T08:19:55.834228Z","shell.execute_reply.started":"2023-03-27T08:19:55.828791Z","shell.execute_reply":"2023-03-27T08:19:55.832844Z"}}},{"cell_type":"code","source":"evb_sample_name = annotation_df.loc[(annotation_df['chart-type']=='vertical_bar').values & (annotation_df['source']=='extracted').values].sample(n=1)['name'].values[0]\nprint(evb_sample_name)\nplot(evb_sample_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:30:16.519317Z","iopub.execute_input":"2023-03-27T08:30:16.519751Z","iopub.status.idle":"2023-03-27T08:30:16.798659Z","shell.execute_reply.started":"2023-03-27T08:30:16.519710Z","shell.execute_reply":"2023-03-27T08:30:16.797459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### extracted line sample","metadata":{"execution":{"iopub.status.busy":"2023-03-27T07:24:30.337099Z","iopub.execute_input":"2023-03-27T07:24:30.337483Z","iopub.status.idle":"2023-03-27T07:24:30.342199Z","shell.execute_reply.started":"2023-03-27T07:24:30.337449Z","shell.execute_reply":"2023-03-27T07:24:30.341264Z"}}},{"cell_type":"code","source":"el_sample_name = annotation_df.loc[(annotation_df['chart-type']=='line').values & (annotation_df['source']=='extracted').values].sample(n=1)['name'].values[0]\nprint(el_sample_name)\nplot(el_sample_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:30:09.565542Z","iopub.execute_input":"2023-03-27T08:30:09.565950Z","iopub.status.idle":"2023-03-27T08:30:09.825236Z","shell.execute_reply.started":"2023-03-27T08:30:09.565916Z","shell.execute_reply":"2023-03-27T08:30:09.823441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### generated line sample","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:21:52.538089Z","iopub.execute_input":"2023-03-27T08:21:52.538508Z","iopub.status.idle":"2023-03-27T08:21:52.543484Z","shell.execute_reply.started":"2023-03-27T08:21:52.538472Z","shell.execute_reply":"2023-03-27T08:21:52.542381Z"}}},{"cell_type":"code","source":"gl_sample_name = annotation_df.loc[(annotation_df['chart-type']=='line').values & (annotation_df['source']=='generated').values].sample(n=1)['name'].values[0]\nprint(gl_sample_name)\nplot(gl_sample_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:29:56.421447Z","iopub.execute_input":"2023-03-27T08:29:56.421849Z","iopub.status.idle":"2023-03-27T08:29:56.744720Z","shell.execute_reply.started":"2023-03-27T08:29:56.421813Z","shell.execute_reply":"2023-03-27T08:29:56.743474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### extracted scatter sample","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:22:10.846377Z","iopub.execute_input":"2023-03-27T08:22:10.847143Z","iopub.status.idle":"2023-03-27T08:22:10.851073Z","shell.execute_reply.started":"2023-03-27T08:22:10.847103Z","shell.execute_reply":"2023-03-27T08:22:10.850157Z"}}},{"cell_type":"code","source":"es_sample_name = annotation_df.loc[(annotation_df['chart-type']=='scatter').values & (annotation_df['source']=='extracted').values].sample(n=1)['name'].values[0]\nprint(es_sample_name)\nplot(es_sample_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:29:21.285477Z","iopub.execute_input":"2023-03-27T08:29:21.285912Z","iopub.status.idle":"2023-03-27T08:29:21.610024Z","shell.execute_reply.started":"2023-03-27T08:29:21.285874Z","shell.execute_reply":"2023-03-27T08:29:21.608661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### generated scatter sample","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:22:20.084453Z","iopub.execute_input":"2023-03-27T08:22:20.084857Z","iopub.status.idle":"2023-03-27T08:22:20.089919Z","shell.execute_reply.started":"2023-03-27T08:22:20.084821Z","shell.execute_reply":"2023-03-27T08:22:20.088937Z"}}},{"cell_type":"code","source":"gs_sample_name = annotation_df.loc[(annotation_df['chart-type']=='scatter').values & (annotation_df['source']=='generated').values].sample(n=1)['name'].values[0]\nprint(gs_sample_name)\nplot(gs_sample_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:29:08.472633Z","iopub.execute_input":"2023-03-27T08:29:08.473057Z","iopub.status.idle":"2023-03-27T08:29:08.793635Z","shell.execute_reply.started":"2023-03-27T08:29:08.473011Z","shell.execute_reply":"2023-03-27T08:29:08.792312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### horizontal_bar (only extracted)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:22:44.925568Z","iopub.execute_input":"2023-03-27T08:22:44.925956Z","iopub.status.idle":"2023-03-27T08:22:44.931714Z","shell.execute_reply.started":"2023-03-27T08:22:44.925923Z","shell.execute_reply":"2023-03-27T08:22:44.930318Z"}}},{"cell_type":"code","source":"ehb_sample_name = annotation_df.loc[annotation_df['chart-type']=='horizontal_bar'].sample(n=1)['name'].values[0]\nprint(ehb_sample_name)\nplot(ehb_sample_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:31:55.789458Z","iopub.execute_input":"2023-03-27T08:31:55.789869Z","iopub.status.idle":"2023-03-27T08:31:56.062633Z","shell.execute_reply.started":"2023-03-27T08:31:55.789832Z","shell.execute_reply":"2023-03-27T08:31:56.061440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### dot (only generated)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:23:09.608327Z","iopub.execute_input":"2023-03-27T08:23:09.608759Z","iopub.status.idle":"2023-03-27T08:23:09.613186Z","shell.execute_reply.started":"2023-03-27T08:23:09.608722Z","shell.execute_reply":"2023-03-27T08:23:09.612138Z"}}},{"cell_type":"code","source":"gd_sample_name = annotation_df.loc[annotation_df['chart-type']=='dot'].sample(n=1)['name'].values[0]\nprint(gd_sample_name)\nplot(gd_sample_name)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:32:14.452965Z","iopub.execute_input":"2023-03-27T08:32:14.453368Z","iopub.status.idle":"2023-03-27T08:32:14.689863Z","shell.execute_reply.started":"2023-03-27T08:32:14.453333Z","shell.execute_reply":"2023-03-27T08:32:14.688409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### plot","metadata":{"execution":{"iopub.status.busy":"2023-03-27T09:37:56.505458Z","iopub.execute_input":"2023-03-27T09:37:56.505917Z","iopub.status.idle":"2023-03-27T09:37:56.524793Z","shell.execute_reply.started":"2023-03-27T09:37:56.505875Z","shell.execute_reply":"2023-03-27T09:37:56.523509Z"}}},{"cell_type":"code","source":"sample_data = annotation_df.sample(1)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T09:46:14.372876Z","iopub.execute_input":"2023-03-27T09:46:14.373281Z","iopub.status.idle":"2023-03-27T09:46:14.380496Z","shell.execute_reply.started":"2023-03-27T09:46:14.373244Z","shell.execute_reply":"2023-03-27T09:46:14.379235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data","metadata":{"execution":{"iopub.status.busy":"2023-03-27T09:46:18.327271Z","iopub.execute_input":"2023-03-27T09:46:18.327715Z","iopub.status.idle":"2023-03-27T09:46:18.366193Z","shell.execute_reply.started":"2023-03-27T09:46:18.327654Z","shell.execute_reply":"2023-03-27T09:46:18.365217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_rec_data = sample_data['text'].values[0]\nfig = plt.figure()\nax = fig.add_subplot(111)\nimage = cv2.imread('/kaggle/input/benetech-making-graphs-accessible/train/images/'+sample_data['name'].values[0]+'.jpg')\nplt.imshow(image)\n\nfor i in range(len(sample_rec_data)):\n    rect = pch.Rectangle(xy=(sample_rec_data[i]['polygon']['x0'], sample_rec_data[i]['polygon']['y0']), \n                           width=sample_rec_data[i]['polygon']['x1'] - sample_rec_data[i]['polygon']['x0'],\n                           height=sample_rec_data[i]['polygon']['y3'] - sample_rec_data[i]['polygon']['y0'],\n                        color='red', fill=False)\n    ax.add_patch(rect)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T10:04:35.034088Z","iopub.execute_input":"2023-03-27T10:04:35.034527Z","iopub.status.idle":"2023-03-27T10:04:35.331331Z","shell.execute_reply.started":"2023-03-27T10:04:35.034486Z","shell.execute_reply":"2023-03-27T10:04:35.330117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}