{"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":"from glob import glob\nimport PIL.Image as Image\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nannotations = glob(\"/kaggle/input/benetech-making-graphs-accessible/train/annotations/*.json\")\n\"Total Training Samples\", len(annotations)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-22T14:19:24.052289Z","iopub.execute_input":"2023-03-22T14:19:24.053226Z","iopub.status.idle":"2023-03-22T14:19:24.271100Z","shell.execute_reply.started":"2023-03-22T14:19:24.053184Z","shell.execute_reply":"2023-03-22T14:19:24.269700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nfrom tqdm import tqdm\nannotations_data = [json.load(open(path,'r')) for path in tqdm(annotations, total=len(annotations))]","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:12:03.749865Z","iopub.execute_input":"2023-03-22T14:12:03.750868Z","iopub.status.idle":"2023-03-22T14:19:05.290437Z","shell.execute_reply.started":"2023-03-22T14:12:03.750803Z","shell.execute_reply":"2023-03-22T14:19:05.289253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = annotations_data[0]\nprint(test.keys())\nprint(test['plot-bb'].keys())\nprint(test['axes'].keys())","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:19:05.291724Z","iopub.execute_input":"2023-03-22T14:19:05.292123Z","iopub.status.idle":"2023-03-22T14:19:05.298429Z","shell.execute_reply.started":"2023-03-22T14:19:05.292086Z","shell.execute_reply":"2023-03-22T14:19:05.297277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain = pd.DataFrame(annotations, columns=['path'])","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:19:05.301821Z","iopub.execute_input":"2023-03-22T14:19:05.302272Z","iopub.status.idle":"2023-03-22T14:19:05.318815Z","shell.execute_reply.started":"2023-03-22T14:19:05.302227Z","shell.execute_reply":"2023-03-22T14:19:05.317327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train\ntrain['name'] = train['path'].apply(lambda x: x.split('/')[-1].replace(\".json\", \"\"))\ntrain['annotation'] = \"annotations/\"+train['name']+\".json\"\ntrain['image'] = \"images/\"+train['name']+\".jpg\"\ntrain['source'] = [x['source'] for x in annotations_data]\ntrain['chart-type'] = [x['chart-type'] for x in annotations_data]\nfor key in test['plot-bb'].keys():\n    train[f\"{key}\"] = [x['plot-bb'][key] for x in annotations_data]\n    #train[f\"{key}_len\"] = [len(x['plot-bb'][key]) for x in annotations_data]\nfor key in test['axes'].keys():\n    train[f\"{key}\"] = [x['axes'][key] for x in annotations_data]\n    #train[f\"{key}_len\"] = [len(x['axes'][key]) for x in annotations_data]\ntrain['text'] = [x['text'] for x in annotations_data]\n\ntrain['data-series'] = [x['data-series'] for x in annotations_data]\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:19:05.320363Z","iopub.execute_input":"2023-03-22T14:19:05.320910Z","iopub.status.idle":"2023-03-22T14:19:06.431713Z","shell.execute_reply.started":"2023-03-22T14:19:05.320870Z","shell.execute_reply":"2023-03-22T14:19:06.430912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(by=['source'])['path'].count()","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:19:06.433409Z","iopub.execute_input":"2023-03-22T14:19:06.434199Z","iopub.status.idle":"2023-03-22T14:19:06.477394Z","shell.execute_reply.started":"2023-03-22T14:19:06.434150Z","shell.execute_reply":"2023-03-22T14:19:06.476027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## private dataset having only extracted so we have to use 1118 for CV !!","metadata":{}},{"cell_type":"code","source":"train.groupby(by=['source', 'chart-type'])['path'].count()","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:19:06.478844Z","iopub.execute_input":"2023-03-22T14:19:06.479205Z","iopub.status.idle":"2023-03-22T14:19:06.543406Z","shell.execute_reply.started":"2023-03-22T14:19:06.479171Z","shell.execute_reply":"2023-03-22T14:19:06.542167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **This competition's dataset comprises about 65,000 comprehensively-annotated scientific figures of four kinds: bar graphs, dot plots, line graphs, and scatter plots.**","metadata":{}},{"cell_type":"markdown","source":"## horizontal_bar charts are missing in generated list and dot charts are missing in extracted","metadata":{}},{"cell_type":"code","source":"train.to_csv(\"train.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:19:11.297725Z","iopub.execute_input":"2023-03-22T14:19:11.298060Z","iopub.status.idle":"2023-03-22T14:19:24.049223Z","shell.execute_reply.started":"2023-03-22T14:19:11.298028Z","shell.execute_reply":"2023-03-22T14:19:24.048221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = \"/kaggle/input/benetech-making-graphs-accessible/train\"","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:19:48.942558Z","iopub.execute_input":"2023-03-22T14:19:48.943046Z","iopub.status.idle":"2023-03-22T14:19:48.948017Z","shell.execute_reply.started":"2023-03-22T14:19:48.943008Z","shell.execute_reply":"2023-03-22T14:19:48.946828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 0\ngraph = np.array(Image.open(root_dir+\"/\"+train['image'][index]+\"\"))\nfig, ax = plt.subplots(1,1)\nax.set_title(label=train['name'][index])\nax.imshow(graph, cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:20:50.938151Z","iopub.execute_input":"2023-03-22T14:20:50.938572Z","iopub.status.idle":"2023-03-22T14:20:51.185971Z","shell.execute_reply.started":"2023-03-22T14:20:50.938537Z","shell.execute_reply":"2023-03-22T14:20:51.184986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rect = (train['x0'][index], train['y0'][index], train['width'][index], train['height'][index])\nfig, ax = plt.subplots()\nax.set_title(label=f\"{train['name'][index]}-chart\")\nax.imshow(graph, cmap = \"gray\")\npatch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\nax.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:22:50.089682Z","iopub.execute_input":"2023-03-22T14:22:50.090992Z","iopub.status.idle":"2023-03-22T14:22:50.340586Z","shell.execute_reply.started":"2023-03-22T14:22:50.090929Z","shell.execute_reply":"2023-03-22T14:22:50.339355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nax.set_title(label=f\"{train['name'][index]}-x-axis-ticks\")\nwidth_step = 4/2\nax.imshow(graph, cmap = \"gray\")\nfor xaxis in  train['x-axis'][index]['ticks']:\n    rect = (xaxis['tick_pt']['x']-width_step, xaxis['tick_pt']['y']-width_step, +width_step, +width_step)\n    patch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\n    ax.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:23:10.948247Z","iopub.execute_input":"2023-03-22T14:23:10.948646Z","iopub.status.idle":"2023-03-22T14:23:11.149181Z","shell.execute_reply.started":"2023-03-22T14:23:10.948605Z","shell.execute_reply":"2023-03-22T14:23:11.147962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nwidth_step = 4/2\nax.imshow(graph, cmap = \"gray\")\nax.set_title(label=f\"{train['name'][index]}-y-axis-ticks\")\nfor yaxis in  train['y-axis'][index]['ticks']:\n    rect = (yaxis['tick_pt']['x']-width_step, yaxis['tick_pt']['y']-width_step, +width_step, +width_step)\n    patch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\n    ax.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:23:27.067818Z","iopub.execute_input":"2023-03-22T14:23:27.068231Z","iopub.status.idle":"2023-03-22T14:23:27.345267Z","shell.execute_reply.started":"2023-03-22T14:23:27.068194Z","shell.execute_reply":"2023-03-22T14:23:27.344081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nwidth_step = 4/2\nax.imshow(graph, cmap = \"gray\")\nax.set_title(label=f\"{train['name'][index]}-axis-data-and-chart-meta\")\nfor text in  train['text'][index]:\n    rect = (text['polygon']['x0'],text['polygon']['y0'], text['polygon']['x1'] - text['polygon']['x0'], text['polygon']['y2'] - text['polygon']['y1'])\n    patch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\n    ax.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:24:11.526146Z","iopub.execute_input":"2023-03-22T14:24:11.526593Z","iopub.status.idle":"2023-03-22T14:24:11.796896Z","shell.execute_reply.started":"2023-03-22T14:24:11.526542Z","shell.execute_reply":"2023-03-22T14:24:11.795768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What we are predicting? - chart-type, data-series","metadata":{}},{"cell_type":"code","source":"train['chart-type'][index], train['data-series'][index]","metadata":{"execution":{"iopub.status.busy":"2023-03-22T14:22:01.873243Z","iopub.execute_input":"2023-03-22T14:22:01.874033Z","iopub.status.idle":"2023-03-22T14:22:01.882963Z","shell.execute_reply.started":"2023-03-22T14:22:01.873991Z","shell.execute_reply":"2023-03-22T14:22:01.881556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}