{"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 os\nimport json\nfrom PIL import Image\nfrom typing import Dict\n\nimport random\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as mpatch\nfrom rapidfuzz.distance.Levenshtein import distance as levenshtein","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-12T23:14:37.91607Z","iopub.execute_input":"2023-04-12T23:14:37.917151Z","iopub.status.idle":"2023-04-12T23:14:41.21579Z","shell.execute_reply.started":"2023-04-12T23:14:37.917104Z","shell.execute_reply":"2023-04-12T23:14:41.214694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    train_img_path: str = \"/kaggle/input/benetech-making-graphs-accessible/train/images\"\n    train_ann_path: str = \"/kaggle/input/benetech-making-graphs-accessible/train/annotations\"\n        \n    test_img_path: str = \"/kaggle/input/benetech-making-graphs-accessible/test/images\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:14:52.767164Z","iopub.execute_input":"2023-04-12T23:14:52.768499Z","iopub.status.idle":"2023-04-12T23:14:52.773697Z","shell.execute_reply.started":"2023-04-12T23:14:52.768452Z","shell.execute_reply":"2023-04-12T23:14:52.772554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class color:\n   PURPLE = '\\033[95m'\n   CYAN = '\\033[96m'\n   DARKCYAN = '\\033[36m'\n   BLUE = '\\033[94m'\n   GREEN = '\\033[92m'\n   YELLOW = '\\033[93m'\n   RED = '\\033[91m'\n   BOLD = '\\033[1m'\n   UNDERLINE = '\\033[4m'\n   END = '\\033[0m'","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:15:04.76789Z","iopub.execute_input":"2023-04-12T23:15:04.768307Z","iopub.status.idle":"2023-04-12T23:15:04.774218Z","shell.execute_reply.started":"2023-04-12T23:15:04.768269Z","shell.execute_reply":"2023-04-12T23:15:04.77323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_annotations():\n    annotations_path = \"/kaggle/input/benetech-making-graphs-accessible/train/annotations\"\n    \n    annotations = []\n    for annotation_path in os.listdir(CFG.train_ann_path):\n        with open(f\"{CFG.train_ann_path}/{annotation_path}\") as annotation_f:\n            annotations.append(json.load(annotation_f))\n    return annotations","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:16:29.947908Z","iopub.execute_input":"2023-04-12T23:16:29.949059Z","iopub.status.idle":"2023-04-12T23:16:29.954467Z","shell.execute_reply.started":"2023-04-12T23:16:29.949013Z","shell.execute_reply":"2023-04-12T23:16:29.953471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_chart_type_counts(annotations):\n    chart_type_counts = {\n        \"dot\": 0,\n        \"line\": 0,\n        \"scatter\": 0,\n        \"vertical_bar\": 0,\n        \"horizontal_bar\": 0\n    }\n    \n    for annotation in annotations:\n        chart_type_counts[annotation[\"chart-type\"]] += 1\n            \n    return chart_type_counts","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:18:07.303737Z","iopub.execute_input":"2023-04-12T23:18:07.304202Z","iopub.status.idle":"2023-04-12T23:18:07.31169Z","shell.execute_reply.started":"2023-04-12T23:18:07.304164Z","shell.execute_reply":"2023-04-12T23:18:07.310203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_annotation(name: str) -> Dict:\n    with open(f\"{CFG.train_ann_path}/{name}.json\") as annotation_f:\n        ann_example = json.load(annotation_f)\n    return ann_example","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:19:18.348364Z","iopub.execute_input":"2023-04-12T23:19:18.348771Z","iopub.status.idle":"2023-04-12T23:19:18.354795Z","shell.execute_reply.started":"2023-04-12T23:19:18.348727Z","shell.execute_reply":"2023-04-12T23:19:18.353429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_coords(polygon, img_height):\n    xs = [\n        polygon[\"x0\"], \n        polygon[\"x1\"], \n        polygon[\"x2\"], \n        polygon[\"x3\"], \n        polygon[\"x0\"]\n    ]\n    \n    ys = [\n        -polygon[\"y0\"] + img_height, \n        -polygon[\"y1\"] + img_height, \n        -polygon[\"y2\"] + img_height, \n        -polygon[\"y3\"] + img_height, \n        -polygon[\"y0\"] + img_height\n    ]\n    \n    return xs, ys","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:20:50.492359Z","iopub.execute_input":"2023-04-12T23:20:50.492822Z","iopub.status.idle":"2023-04-12T23:20:50.501075Z","shell.execute_reply.started":"2023-04-12T23:20:50.49278Z","shell.execute_reply":"2023-04-12T23:20:50.499494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_line_breaks(text: str, break_num: int = 7) -> str:\n    words = text.split()\n    new_text = \"\"\n    for i, word in enumerate(words, start=1):\n        new_text += word\n        if i % break_num == 0:\n            new_text += \"<br>\"\n        else:\n            new_text += \" \"\n    return new_text","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:22:11.841948Z","iopub.execute_input":"2023-04-12T23:22:11.842396Z","iopub.status.idle":"2023-04-12T23:22:11.85011Z","shell.execute_reply.started":"2023-04-12T23:22:11.842358Z","shell.execute_reply":"2023-04-12T23:22:11.848816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_tick_value(name, data_series):\n    for el in data_series:\n        if el[\"x\"] == name:\n            return el[\"y\"]\n        elif el[\"y\"] == name:\n            return el[\"x\"]","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:22:43.543264Z","iopub.execute_input":"2023-04-12T23:22:43.543704Z","iopub.status.idle":"2023-04-12T23:22:43.550391Z","shell.execute_reply.started":"2023-04-12T23:22:43.543666Z","shell.execute_reply":"2023-04-12T23:22:43.549052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_annotated_image(name: str, scale_factor: int = 1.0) -> None:\n    img_example = Image.open(f\"{CFG.train_img_path}/{name}.jpg\")\n    ann_example = load_annotation(name)\n    \n    # create figure\n    fig = go.Figure()\n\n    # constants\n    img_width = img_example.size[0]\n    img_height = img_example.size[1]\n    \n\n    # add invisible scatter trace\n    fig.add_trace(\n        go.Scatter(\n            x=[0, img_width],\n            y=[0, img_height],\n            mode=\"markers\",\n            marker_opacity=0\n        )\n    )\n\n    # configure axes\n    fig.update_xaxes(\n        visible=False,\n        range=[0, img_width]\n    )\n\n    fig.update_yaxes(\n        visible=False,\n        range=[0, img_height],\n        # the scaleanchor attribute ensures that the aspect ratio stays constant\n        scaleanchor=\"x\"\n    )\n\n    # add image\n    fig.add_layout_image(dict(\n        x=0,\n        sizex=img_width,\n        y=img_height,\n        sizey=img_height,\n        xref=\"x\", yref=\"y\",\n        opacity=1.0,\n        layer=\"below\",\n        sizing=\"stretch\",\n        source=img_example\n    ))\n    \n    # add bounding box\n    fig.add_shape(\n        type=\"rect\",\n        x0=ann_example[\"plot-bb\"][\"x0\"], \n        y0=-ann_example[\"plot-bb\"][\"y0\"] + img_height, \n        x1=ann_example[\"plot-bb\"][\"x0\"] + ann_example[\"plot-bb\"][\"width\"], \n        y1=-(ann_example[\"plot-bb\"][\"y0\"] + ann_example[\"plot-bb\"][\"height\"]) + img_height,\n        line=dict(color=\"RoyalBlue\"),\n    )\n    \n    # add polygons\n    for text in ann_example[\"text\"]:\n        name = text[\"text\"]\n        \n        if text[\"role\"] == \"tick_label\":\n            tick_value = get_tick_value(name, ann_example[\"data-series\"])\n            if tick_value:\n                name = f'Text: {name}<br>Value: {tick_value}'\n        \n        xs, ys = get_coords(text[\"polygon\"], img_height)\n        fig.add_trace(go.Scatter(\n            x=xs, y=ys, fill=\"toself\",\n            name=add_line_breaks(name),\n            hovertemplate=\"%{name}\",\n            mode='lines'\n        ))\n    \n    # add x-axis dots\n    xs = [dot[\"tick_pt\"][\"x\"] for dot in ann_example[\"axes\"][\"x-axis\"][\"ticks\"]]\n    ys = [-dot[\"tick_pt\"][\"y\"] + img_height for dot in ann_example[\"axes\"][\"x-axis\"][\"ticks\"]]\n    fig.add_trace(go.Scatter(\n        x=xs, y=ys, mode='markers',\n        name=\"x-axis\"\n    ))\n    \n    # add y-axis dots\n    xs = [dot[\"tick_pt\"][\"x\"] for dot in ann_example[\"axes\"][\"y-axis\"][\"ticks\"]]\n    ys = [-dot[\"tick_pt\"][\"y\"] + img_height for dot in ann_example[\"axes\"][\"y-axis\"][\"ticks\"]]\n    fig.add_trace(go.Scatter(\n        x=xs, y=ys, mode='markers',\n        name=\"y-axis\"\n    ))\n\n    # configure other layout\n    fig.update_layout(\n        width=img_width * scale_factor,\n        height=img_height * scale_factor,\n        margin={\"l\": 0, \"r\": 0, \"t\": 0, \"b\": 0},\n        showlegend=False\n    )\n\n    # disable the autosize on double click because it adds unwanted margins around the image\n    # and finally show figure\n    fig.show(config={'doubleClick': 'reset'})","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:32:13.222895Z","iopub.execute_input":"2023-04-12T23:32:13.223336Z","iopub.status.idle":"2023-04-12T23:32:13.242201Z","shell.execute_reply.started":"2023-04-12T23:32:13.223292Z","shell.execute_reply":"2023-04-12T23:32:13.240843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations = get_annotations()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:35:02.463332Z","iopub.execute_input":"2023-04-12T23:35:02.463777Z","iopub.status.idle":"2023-04-12T23:41:24.461974Z","shell.execute_reply.started":"2023-04-12T23:35:02.463738Z","shell.execute_reply":"2023-04-12T23:41:24.46019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotation_example = load_annotation(\"0000ae6cbdb1\")\n\n# remove some data to make output it more readable\nannotation_example[\"text\"] = [annotation_example[\"text\"][0]]\nannotation_example[\"axes\"][\"x-axis\"][\"ticks\"] = [annotation_example[\"axes\"][\"x-axis\"][\"ticks\"][0]]\nannotation_example[\"axes\"][\"y-axis\"][\"ticks\"] = [annotation_example[\"axes\"][\"y-axis\"][\"ticks\"][0]]\nannotation_example[\"data-series\"] = [annotation_example[\"data-series\"][0]]\n\nprint(json.dumps(annotation_example, indent=2))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:41:24.479445Z","iopub.execute_input":"2023-04-12T23:41:24.48007Z","iopub.status.idle":"2023-04-12T23:41:24.491344Z","shell.execute_reply.started":"2023-04-12T23:41:24.480011Z","shell.execute_reply":"2023-04-12T23:41:24.489895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chart_type_counts = get_chart_type_counts(annotations)\n\nfig = px.pie(values=chart_type_counts.values(), names=chart_type_counts.keys())\nfig.update_traces(textposition='inside', textfont_size=14)\nfig.update_layout(\n    title={\n        'text': \"Pie distribution of chart-type label\",\n        'y':0.95,\n        'x':0.5,\n        'xanchor': 'center',\n        'yanchor': 'top'\n    }\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:41:24.580217Z","iopub.execute_input":"2023-04-12T23:41:24.580768Z","iopub.status.idle":"2023-04-12T23:41:26.056833Z","shell.execute_reply.started":"2023-04-12T23:41:24.580717Z","shell.execute_reply":"2023-04-12T23:41:26.055681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(\"/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:41:26.087906Z","iopub.execute_input":"2023-04-12T23:41:26.088291Z","iopub.status.idle":"2023-04-12T23:41:26.14976Z","shell.execute_reply.started":"2023-04-12T23:41:26.088254Z","shell.execute_reply":"2023-04-12T23:41:26.148746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vb_example_name = \"0000ae6cbdb1\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:41:26.151072Z","iopub.execute_input":"2023-04-12T23:41:26.151753Z","iopub.status.idle":"2023-04-12T23:41:26.15716Z","shell.execute_reply.started":"2023-04-12T23:41:26.151716Z","shell.execute_reply":"2023-04-12T23:41:26.155946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(f\"/kaggle/input/benetech-making-graphs-accessible/train/images/{vb_example_name}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:41:26.188915Z","iopub.execute_input":"2023-04-12T23:41:26.189296Z","iopub.status.idle":"2023-04-12T23:41:26.228419Z","shell.execute_reply.started":"2023-04-12T23:41:26.18926Z","shell.execute_reply":"2023-04-12T23:41:26.226966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_annotated_image(vb_example_name, scale_factor = 1.5)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:41:26.237275Z","iopub.execute_input":"2023-04-12T23:41:26.23769Z","iopub.status.idle":"2023-04-12T23:41:26.3598Z","shell.execute_reply.started":"2023-04-12T23:41:26.237658Z","shell.execute_reply":"2023-04-12T23:41:26.358418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hb_example_name = \"8b6935f7ef04\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:41:26.361961Z","iopub.execute_input":"2023-04-12T23:41:26.362445Z","iopub.status.idle":"2023-04-12T23:41:26.368801Z","shell.execute_reply.started":"2023-04-12T23:41:26.362401Z","shell.execute_reply":"2023-04-12T23:41:26.366796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(f\"/kaggle/input/benetech-making-graphs-accessible/train/images/{hb_example_name}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:41:40.943114Z","iopub.execute_input":"2023-04-12T23:41:40.944348Z","iopub.status.idle":"2023-04-12T23:41:40.975669Z","shell.execute_reply.started":"2023-04-12T23:41:40.944296Z","shell.execute_reply":"2023-04-12T23:41:40.974302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_annotated_image(hb_example_name, scale_factor = 1.5)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:42:07.016486Z","iopub.execute_input":"2023-04-12T23:42:07.017655Z","iopub.status.idle":"2023-04-12T23:42:07.064546Z","shell.execute_reply.started":"2023-04-12T23:42:07.017598Z","shell.execute_reply":"2023-04-12T23:42:07.063187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h_example_name = \"00cee4e08d80\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:43:02.574972Z","iopub.execute_input":"2023-04-12T23:43:02.575414Z","iopub.status.idle":"2023-04-12T23:43:02.581391Z","shell.execute_reply.started":"2023-04-12T23:43:02.575379Z","shell.execute_reply":"2023-04-12T23:43:02.579945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(f\"/kaggle/input/benetech-making-graphs-accessible/train/images/{h_example_name}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:43:25.259988Z","iopub.execute_input":"2023-04-12T23:43:25.26041Z","iopub.status.idle":"2023-04-12T23:43:25.305946Z","shell.execute_reply.started":"2023-04-12T23:43:25.26037Z","shell.execute_reply":"2023-04-12T23:43:25.304711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_annotated_image(h_example_name, scale_factor = 1.5)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:43:36.454744Z","iopub.execute_input":"2023-04-12T23:43:36.455177Z","iopub.status.idle":"2023-04-12T23:43:36.5354Z","shell.execute_reply.started":"2023-04-12T23:43:36.455141Z","shell.execute_reply":"2023-04-12T23:43:36.534186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd_example_name = \"000917f5d829\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:44:00.777021Z","iopub.execute_input":"2023-04-12T23:44:00.777418Z","iopub.status.idle":"2023-04-12T23:44:00.782015Z","shell.execute_reply.started":"2023-04-12T23:44:00.777383Z","shell.execute_reply":"2023-04-12T23:44:00.780636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(f\"/kaggle/input/benetech-making-graphs-accessible/train/images/{cd_example_name}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:44:29.081354Z","iopub.execute_input":"2023-04-12T23:44:29.081768Z","iopub.status.idle":"2023-04-12T23:44:29.119797Z","shell.execute_reply.started":"2023-04-12T23:44:29.081724Z","shell.execute_reply":"2023-04-12T23:44:29.118464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_annotated_image(cd_example_name, scale_factor = 1.3)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:44:43.377431Z","iopub.execute_input":"2023-04-12T23:44:43.377833Z","iopub.status.idle":"2023-04-12T23:44:43.4399Z","shell.execute_reply.started":"2023-04-12T23:44:43.377799Z","shell.execute_reply":"2023-04-12T23:44:43.438492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nd_example_name = \"000944919c5c\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:44:48.941989Z","iopub.execute_input":"2023-04-12T23:44:48.942852Z","iopub.status.idle":"2023-04-12T23:44:48.947314Z","shell.execute_reply.started":"2023-04-12T23:44:48.942803Z","shell.execute_reply":"2023-04-12T23:44:48.946216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(f\"/kaggle/input/benetech-making-graphs-accessible/train/images/{nd_example_name}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:45:13.572813Z","iopub.execute_input":"2023-04-12T23:45:13.573849Z","iopub.status.idle":"2023-04-12T23:45:13.612937Z","shell.execute_reply.started":"2023-04-12T23:45:13.5738Z","shell.execute_reply":"2023-04-12T23:45:13.611964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_annotated_image(nd_example_name, scale_factor = 1.3)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:45:26.995732Z","iopub.execute_input":"2023-04-12T23:45:26.996156Z","iopub.status.idle":"2023-04-12T23:45:27.053757Z","shell.execute_reply.started":"2023-04-12T23:45:26.996119Z","shell.execute_reply":"2023-04-12T23:45:27.052355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l_example_name = \"0005413054c9\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:45:44.20863Z","iopub.execute_input":"2023-04-12T23:45:44.209061Z","iopub.status.idle":"2023-04-12T23:45:44.213649Z","shell.execute_reply.started":"2023-04-12T23:45:44.209021Z","shell.execute_reply":"2023-04-12T23:45:44.212773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(f\"/kaggle/input/benetech-making-graphs-accessible/train/images/{l_example_name}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:46:07.324186Z","iopub.execute_input":"2023-04-12T23:46:07.324581Z","iopub.status.idle":"2023-04-12T23:46:07.366305Z","shell.execute_reply.started":"2023-04-12T23:46:07.324547Z","shell.execute_reply":"2023-04-12T23:46:07.364998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_annotated_image(l_example_name, scale_factor=1.3)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:46:23.477092Z","iopub.execute_input":"2023-04-12T23:46:23.477493Z","iopub.status.idle":"2023-04-12T23:46:23.538588Z","shell.execute_reply.started":"2023-04-12T23:46:23.477458Z","shell.execute_reply":"2023-04-12T23:46:23.537338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s_example_name = \"0005e64fdc6e\"","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:46:31.415116Z","iopub.execute_input":"2023-04-12T23:46:31.415522Z","iopub.status.idle":"2023-04-12T23:46:31.421095Z","shell.execute_reply.started":"2023-04-12T23:46:31.415487Z","shell.execute_reply":"2023-04-12T23:46:31.420089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(f\"/kaggle/input/benetech-making-graphs-accessible/train/images/{s_example_name}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:47:07.962438Z","iopub.execute_input":"2023-04-12T23:47:07.962877Z","iopub.status.idle":"2023-04-12T23:47:08.007201Z","shell.execute_reply.started":"2023-04-12T23:47:07.962827Z","shell.execute_reply":"2023-04-12T23:47:08.005504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_annotated_image(s_example_name, scale_facotr = 1.3)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:47:51.450957Z","iopub.execute_input":"2023-04-12T23:47:51.451352Z","iopub.status.idle":"2023-04-12T23:47:51.638769Z","shell.execute_reply.started":"2023-04-12T23:47:51.451319Z","shell.execute_reply":"2023-04-12T23:47:51.637526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"load_annotation(s_example_name)[\"data-series\"]","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:48:02.276653Z","iopub.execute_input":"2023-04-12T23:48:02.277087Z","iopub.status.idle":"2023-04-12T23:48:02.288071Z","shell.execute_reply.started":"2023-04-12T23:48:02.27705Z","shell.execute_reply":"2023-04-12T23:48:02.286935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sigmoid(x):\n    return 2 - (2 / (1 + np.exp(-x)))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:49:01.595763Z","iopub.execute_input":"2023-04-12T23:49:01.596263Z","iopub.status.idle":"2023-04-12T23:49:01.603234Z","shell.execute_reply.started":"2023-04-12T23:49:01.596159Z","shell.execute_reply":"2023-04-12T23:49:01.601807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.linspace(0, 10, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:49:09.014158Z","iopub.execute_input":"2023-04-12T23:49:09.014572Z","iopub.status.idle":"2023-04-12T23:49:09.021674Z","shell.execute_reply.started":"2023-04-12T23:49:09.014536Z","shell.execute_reply":"2023-04-12T23:49:09.019975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.line(x=x, y=sigmoid(x))\nfig.update_layout(\n    title={\n        'text': \"Sigmoid transformation mapping plot\",\n        'y':0.98,\n        'x':0.5,\n        'xanchor': 'center',\n        'yanchor': 'top'\n    },\n    margin=dict(l=0, r=0, t=35, b=0)\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:50:45.958264Z","iopub.execute_input":"2023-04-12T23:50:45.959346Z","iopub.status.idle":"2023-04-12T23:50:46.055715Z","shell.execute_reply.started":"2023-04-12T23:50:45.959298Z","shell.execute_reply":"2023-04-12T23:50:46.054543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rmse(y_true, y_pred):\n    return np.sqrt(np.mean(np.square(np.subtract(y_true, y_pred))))\n\n\ndef normalized_rmse(y_true, y_pred):\n    return sigmoid(rmse(y_true, y_pred) / rmse(y_true, np.mean(y_true)))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:52:12.471809Z","iopub.execute_input":"2023-04-12T23:52:12.472266Z","iopub.status.idle":"2023-04-12T23:52:12.479174Z","shell.execute_reply.started":"2023-04-12T23:52:12.472227Z","shell.execute_reply":"2023-04-12T23:52:12.477806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalized_levenshtein_score(y_true, y_pred):\n\n    total_distance = np.sum([levenshtein(yt, yp) for yt, yp in zip(y_true, y_pred)])\n\n    length_sum = np.sum([len(yt) for yt in y_true])\n    \n    return sigmoid(total_distance / length_sum)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:53:54.491849Z","iopub.execute_input":"2023-04-12T23:53:54.492293Z","iopub.status.idle":"2023-04-12T23:53:54.499484Z","shell.execute_reply.started":"2023-04-12T23:53:54.492254Z","shell.execute_reply":"2023-04-12T23:53:54.498137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def score_series(y_true, y_pred):\n    if len(y_true) != len(y_pred):\n        return 0.0\n    if isinstance(y_true[0], str):\n        return normalized_levenshtein_score(y_true, y_pred)\n    else:\n        return normalized_rmse(y_true, y_pred)\n\n\ndef benetech_score(ground_truth: pd.DataFrame, predictions: pd.DataFrame) -> float:\n    \"\"\"Evaluate predictions using the metric from the Benetech - Making Graphs Accessible.\n    \n    Parameters\n    ----------\n    ground_truth: pd.DataFrame\n        Has columns `[data_series, chart_type]` and an index `id`. Values in `data_series` \n        should be either arrays of floats or arrays of strings.\n    \n    predictions: pd.DataFrame\n    \"\"\"\n    if not ground_truth.index.equals(predictions.index):\n        raise ValueError(\"Must have exactly one prediction for each ground-truth instance.\")\n    if not ground_truth.columns.equals(predictions.columns):\n        raise ValueError(f\"Predictions must have columns: {ground_truth.columns}.\")\n    pairs = zip(ground_truth.itertuples(index=False), predictions.itertuples(index=False))\n    scores = []\n    for (gt_series, gt_type), (pred_series, pred_type) in pairs:\n        if gt_type != pred_type:  # Check chart_type condition\n            scores.append(0.0)\n        else:  # Score with RMSE or Levenshtein as appropriate\n            scores.append(score_series(gt_series, pred_series))\n    return np.mean(scores)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T23:57:28.080037Z","iopub.execute_input":"2023-04-12T23:57:28.080445Z","iopub.status.idle":"2023-04-12T23:57:28.091843Z","shell.execute_reply.started":"2023-04-12T23:57:28.080408Z","shell.execute_reply":"2023-04-12T23:57:28.090234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img_1 = \"000b92c3b098.jpg\"\ntest_img_2 = \"00dcf883a459.jpg\"\nfig, axs = plt.subplots(1, 2, figsize=(10, 30), dpi=350)\n\naxs[0].axis('off')\naxs[0].text(\n    0.5, 1.08, test_img_1, fontweight='bold', fontsize=18,\n    transform=axs[0].transAxes, horizontalalignment='center'\n)\naxs[0].imshow(Image.open(f\"{CFG.test_img_path}/{test_img_1}\"), cmap='gray')\n\naxs[1].axis('off')\naxs[1].text(\n    0.5, 1.08, test_img_2, fontweight='bold', fontsize=18,\n    transform=axs[1].transAxes, horizontalalignment='center'\n)\naxs[1].imshow(Image.open(f\"{CFG.test_img_path}/{test_img_2}\"), cmap='gray');","metadata":{"execution":{"iopub.status.busy":"2023-04-13T00:02:57.316672Z","iopub.execute_input":"2023-04-13T00:02:57.317109Z","iopub.status.idle":"2023-04-13T00:02:58.678292Z","shell.execute_reply.started":"2023-04-13T00:02:57.317072Z","shell.execute_reply":"2023-04-13T00:02:58.677163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ground_truth = pd.DataFrame.from_dict({\n    '000b92c3b098_x': ([0, 6, 12, 18, 24], 'line'),\n    '000b92c3b098_y': ([0, -0.8, -1.5, -2.1, -2.8], 'line'),\n    \n    '00dcf883a459_x': ([\"Group 1\", \"Group 2\"], 'vertical_bar'),\n    '00dcf883a459_y': ([3.6, 8.4], 'vertical_bar'),\n}, orient='index', columns=['data_series', 'chart_type']).rename_axis('id')\n\nground_truth","metadata":{"execution":{"iopub.status.busy":"2023-04-13T00:03:34.753401Z","iopub.execute_input":"2023-04-13T00:03:34.753824Z","iopub.status.idle":"2023-04-13T00:03:34.782153Z","shell.execute_reply.started":"2023-04-13T00:03:34.753784Z","shell.execute_reply":"2023-04-13T00:03:34.780832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pd.DataFrame.from_dict({\n    '000b92c3b098_x': ([0, 6, 12, 18, 24], 'line'),\n    '000b92c3b098_y': ([0, -0.9, -1.6, -2.2, -2.9], 'line'),\n    \n    '00dcf883a459_x': ([\"Group 1\", \"Group 2\"], 'vertical_bar'),\n    '00dcf883a459_y': ([3.0, 8.8], 'vertical_bar'),\n}, orient='index', columns=['data_series', 'chart_type']).rename_axis('id')\n\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-04-13T00:03:43.916728Z","iopub.execute_input":"2023-04-13T00:03:43.917204Z","iopub.status.idle":"2023-04-13T00:03:43.9345Z","shell.execute_reply.started":"2023-04-13T00:03:43.917163Z","shell.execute_reply":"2023-04-13T00:03:43.933133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_score = benetech_score(ground_truth, predictions)\n\nprint(f\"Test Benetech Score: {color.BOLD}{color.CYAN}{test_score}{color.END}\")","metadata":{"execution":{"iopub.status.busy":"2023-04-13T00:05:17.226836Z","iopub.execute_input":"2023-04-13T00:05:17.227318Z","iopub.status.idle":"2023-04-13T00:05:17.242817Z","shell.execute_reply.started":"2023-04-13T00:05:17.227276Z","shell.execute_reply":"2023-04-13T00:05:17.241287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.to_csv(\"sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-13T00:05:33.166197Z","iopub.execute_input":"2023-04-13T00:05:33.166615Z","iopub.status.idle":"2023-04-13T00:05:33.17613Z","shell.execute_reply.started":"2023-04-13T00:05:33.166576Z","shell.execute_reply":"2023-04-13T00:05:33.175198Z"},"trusted":true},"execution_count":null,"outputs":[]}]}