{"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":"!pip3 install justshowit --quiet\nimport justshowit # show images without all the shinanigans\n\nimport pandas as pd\nfrom glob import glob\nimport seaborn as sns\nsns.set_style(\"whitegrid\")\nimport matplotlib.pyplot as plt\nimport json\nfrom tqdm.notebook import tqdm\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-04-17T16:58:36.000970Z","iopub.execute_input":"2023-04-17T16:58:36.001663Z","iopub.status.idle":"2023-04-17T16:58:47.721316Z","shell.execute_reply.started":"2023-04-17T16:58:36.001624Z","shell.execute_reply":"2023-04-17T16:58:47.720001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create csv-file","metadata":{}},{"cell_type":"code","source":"# Setup\njson_paths = sorted(glob(\"../input/benetech-making-graphs-accessible/train/annotations/*\"))\nimage_paths = sorted(glob(\"../input/benetech-making-graphs-accessible/train/images/*\"))\ndf = pd.DataFrame(columns=['source', 'chart-type', 'plot-bb', 'text', 'axes', 'data-series', \"file_name\", \"image_path\"])\n\n# Save json and image data into a single csv\npaths_combined = list(zip(json_paths, image_paths))\nfor i, (json_path, image_path) in enumerate(tqdm(paths_combined)):\n    # Checks\n    assert json_path.endswith(\".json\"), \"bad path\"\n    assert image_path.endswith(\".jpg\"), \"bad path\"\n    assert os.path.basename(image_path)[:-4] == os.path.basename(json_path)[:-5], \"bad path\"\n\n    # Extract json data\n    with open(json_path) as f:\n        data = json.load(f)\n\n    # Add everything to `df`\n    series = pd.Series([None]*len(df.columns), dtype=\"object\")\n    series[\"file_name\"] = os.path.basename(json_path)[:-5]\n    series[\"image_path\"] = image_path\n    for k,v in data.items():\n        series[k] = v\n    df.loc[len(df)] = series\n\ndf.to_csv(\"./data.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T15:56:32.301351Z","iopub.execute_input":"2023-04-17T15:56:32.302412Z","iopub.status.idle":"2023-04-17T16:23:23.477298Z","shell.execute_reply.started":"2023-04-17T15:56:32.302365Z","shell.execute_reply":"2023-04-17T16:23:23.476170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Simple stats","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(15,5))\nvalue_counts = df[\"chart-type\"].value_counts()\npercentages = df[\"chart-type\"].value_counts(normalize=True) * 100\nax.bar(value_counts.index, value_counts.values)\n\n# Add the percentages and count on top of each bar\nfor i, v in enumerate(value_counts.values):\n    ax.text(i, v + 200, f\"{v} ({percentages[i]:.1f}%)\", ha=\"center\")\n    \n# Set the title and axis labels\nax.set_title(\"Chart Types Count\")\nax.set_xlabel(\"Chart Type\")\nax.set_ylabel(\"Count\");","metadata":{"execution":{"iopub.status.busy":"2023-04-17T17:00:06.824811Z","iopub.execute_input":"2023-04-17T17:00:06.825245Z","iopub.status.idle":"2023-04-17T17:00:07.136684Z","shell.execute_reply.started":"2023-04-17T17:00:06.825210Z","shell.execute_reply":"2023-04-17T17:00:07.135749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display some random plots from the dataset","metadata":{}},{"cell_type":"code","source":"random_image_paths_to_show = list(df[\"image_path\"].sample(25).values)\njustshowit.show(random_image_paths_to_show, image_border=0)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T17:00:10.950199Z","iopub.execute_input":"2023-04-17T17:00:10.950634Z","iopub.status.idle":"2023-04-17T17:00:12.146682Z","shell.execute_reply.started":"2023-04-17T17:00:10.950597Z","shell.execute_reply":"2023-04-17T17:00:12.145091Z"},"trusted":true},"execution_count":null,"outputs":[]}]}