{"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\n\ntrain_path = '/kaggle/input/benetech-making-graphs-accessible/train'\n\ncount = 0\ntotal = len(os.listdir(train_path+'/annotations'))\n\ndef get_json(filename):\n    global count\n    \n    with open(filename) as js:\n        annotation = json.load(js)\n        \n    count += 1\n    print(f'{count}/{total}                   ', end='\\r')\n    \n    return annotation\n\nimages = sorted([train_path+'/images/'+image for image in os.listdir(train_path+'/images/')])\nannotations = sorted([train_path+'/annotations/'+annotation for annotation in os.listdir(train_path+'/annotations/')])\nannotations = [get_json(annotation) for annotation in annotations]\nprint('                  ')\n\nm = 59\nn = 196\nx = 0\ny = 0\nfor a in annotations:\n    m = max(m, len(a['text']))\n    n = max(n, len(a['data-series']))\n    x = max(x, len(a['axes']['x-axis']['ticks']))\n    y = max(y, len(a['axes']['y-axis']['ticks']))\n    \ndataframe = dict()\ndataframe['name'] = [None for _ in range(len(annotations))]\ndataframe['source'] = [None for _ in range(len(annotations))]\ndataframe['chart-type'] = [None for _ in range(len(annotations))]\n\nfor i in range(4):\n    dataframe[f'plot-bb_{i}'] = [None for _ in range(len(annotations))]\n    \nfor i in range(m):\n    dataframe[f'text_{i}'] = [None for _ in range(len(annotations))]\n\ndataframe['x-axis-tick-type'] = [None for _ in range(len(annotations))]\ndataframe['x-axis-values-type'] = [None for _ in range(len(annotations))]\n\nfor i in range(x):\n    dataframe[f'x-axis_{i}'] = [None for _ in range(len(annotations))]\n\ndataframe['y-axis-tick-type'] = [None for _ in range(len(annotations))]\ndataframe['y-axis-values-type'] = [None for _ in range(len(annotations))]\n\nfor i in range(y):\n    dataframe[f'y-axis_{i}'] = [None for _ in range(len(annotations))]\n    \nfor i in range(n):\n    dataframe[f'data-series_{i}'] = [None for _ in range(len(annotations))]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-23T17:55:53.514009Z","iopub.execute_input":"2023-03-23T17:55:53.514418Z","iopub.status.idle":"2023-03-23T18:00:09.227439Z","shell.execute_reply.started":"2023-03-23T17:55:53.514384Z","shell.execute_reply":"2023-03-23T18:00:09.226121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for a, annotation in enumerate(annotations):\n    print(f'{a}/{len(annotations)}', end='                    \\r')\n    dataframe['name'][a] = images[a].split('/')[-1].split('.')[0]\n    dataframe['source'][a] = annotation['source']\n    dataframe['chart-type'][a] = annotation['chart-type']\n    \n    for i, prop in enumerate(['height', 'width', 'x0', 'y0']):\n        dataframe[f'plot-bb_{i}'][a] = annotation['plot-bb'][prop]\n        \n    for i, text in enumerate(annotation['text']):\n        text_poly = ''\n        for p in text['polygon'].values():\n            text_poly += f'{p};'\n            \n        dataframe[f'text_{i}'][a] = text_poly\n        \n    dataframe['x-axis-tick-type'][a] = annotation['axes']['x-axis']['tick-type']\n    dataframe['x-axis-values-type'][a] = annotation['axes']['x-axis']['values-type']\n    \n    for i, tick in enumerate(annotation['axes']['x-axis']['ticks']):\n        dataframe[f'x-axis_{i}'][a] = str(tick['tick_pt']['x']) + ';' + str(tick['tick_pt']['y'])\n        \n    dataframe['y-axis-tick-type'][a] = annotation['axes']['y-axis']['tick-type']\n    dataframe['y-axis-values-type'][a] = annotation['axes']['y-axis']['values-type']\n    \n    for i, tick in enumerate(annotation['axes']['y-axis']['ticks']):\n        dataframe[f'y-axis_{i}'][a] = str(tick['tick_pt']['x']) + ';' + str(tick['tick_pt']['y'])\n        \n    for i, data_series in enumerate(annotation['data-series']):\n        dataframe[f'data-series_{i}'][a] = str(data_series['x']) + ';' + str(data_series['y'])","metadata":{"execution":{"iopub.status.busy":"2023-03-23T18:00:09.229790Z","iopub.execute_input":"2023-03-23T18:00:09.230266Z","iopub.status.idle":"2023-03-23T18:00:18.172543Z","shell.execute_reply.started":"2023-03-23T18:00:09.230222Z","shell.execute_reply":"2023-03-23T18:00:18.171744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndataframe = pd.DataFrame.from_dict(dataframe)\ndataframe.head()\ndataframe.to_csv('train.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T18:00:18.173410Z","iopub.execute_input":"2023-03-23T18:00:18.173724Z","iopub.status.idle":"2023-03-23T18:00:30.023708Z","shell.execute_reply.started":"2023-03-23T18:00:18.173675Z","shell.execute_reply":"2023-03-23T18:00:30.022731Z"},"trusted":true},"execution_count":null,"outputs":[]}]}