|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from pylatex import Document, NoEscape, StandAloneGraphic, Figure, SubFigure, Command\n", |
| 10 | + "import pylatex" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 2, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "def generate_figure_section(doc, fig_data):\n", |
| 20 | + " '''Generates latex code related to figure (with optional sub figures) section\n", |
| 21 | + " \n", |
| 22 | + " Parameters\n", |
| 23 | + " ----------\n", |
| 24 | + " fig_data : dict<str,str>\n", |
| 25 | + " eg: fig_data = {\n", |
| 26 | + " // figure details\n", |
| 27 | + " 'fig_caption': r'<fig_caption>',\n", |
| 28 | + " 'fig_caption_option': r'<fig_caption_option>',\n", |
| 29 | + " 'fig_label': r'<fig_label>',\n", |
| 30 | + " \n", |
| 31 | + " [OPTIONAL] 'sub_figs': [\n", |
| 32 | + " {\n", |
| 33 | + " // sub-figure details\n", |
| 34 | + " }\n", |
| 35 | + " ],\n", |
| 36 | + " \n", |
| 37 | + " [OPTIONAL] 'img_file': '<img_file_path>',\n", |
| 38 | + " }\n", |
| 39 | + " '''\n", |
| 40 | + " \n", |
| 41 | + " fig_caption = fig_data['fig_caption']\n", |
| 42 | + " fig_caption_option = fig_data['fig_caption_option'] \n", |
| 43 | + " \n", |
| 44 | + " figure = Figure(position='!h')\n", |
| 45 | + " with doc.create(figure) as fig:\n", |
| 46 | + " # add centering tag\n", |
| 47 | + " fig.append(Command('centering'))\n", |
| 48 | + " \n", |
| 49 | + " if 'sub_figs' in fig_data.keys():\n", |
| 50 | + " sub_figs = fig_data['sub_figs']\n", |
| 51 | + " sub_fig_width = r'{0:0.2f}\\textwidth'.format(1/len(sub_figs))\n", |
| 52 | + "\n", |
| 53 | + " # iterate per sub figure\n", |
| 54 | + " for sub_fig_data in sub_figs:\n", |
| 55 | + " sub_fig_caption = sub_fig_data['fig_caption']\n", |
| 56 | + " sub_fig_caption_option = sub_fig_data['fig_caption_option']\n", |
| 57 | + " sub_fig_img_file = sub_fig_data['img_file']\n", |
| 58 | + " \n", |
| 59 | + " # if label is not provided, use image file name instead\n", |
| 60 | + " if 'fig_label' in sub_fig_data.keys():\n", |
| 61 | + " sub_fig_label = sub_fig_data['fig_label']\n", |
| 62 | + " else:\n", |
| 63 | + " sub_fig_label = sub_fig_img_file.replace('figures/', 'fig:').replace('.pdf', '')\n", |
| 64 | + "\n", |
| 65 | + " sub_figure = SubFigure(position='b', width=NoEscape(sub_fig_width))\n", |
| 66 | + " with doc.create(sub_figure) as sub_fig:\n", |
| 67 | + " # add centering tag\n", |
| 68 | + " sub_fig.append(Command('centering'))\n", |
| 69 | + "\n", |
| 70 | + " # add sub figure image\n", |
| 71 | + " sub_fig.add_image(sub_fig_img_file, width=NoEscape(r'\\textwidth'))\n", |
| 72 | + " # sub_fig.append(StandAloneGraphic(image_options='width=' + NoEscape(sub_fig_img_width), filename=sub_fig_img_file))\n", |
| 73 | + "\n", |
| 74 | + " # add sub figure caption\n", |
| 75 | + " sub_fig.append(Command('caption', NoEscape(sub_fig_caption), NoEscape(sub_fig_caption_option)))\n", |
| 76 | + "\n", |
| 77 | + " # add sub figure label\n", |
| 78 | + " sub_fig.append(Command('label', NoEscape(sub_fig_label)))\n", |
| 79 | + " \n", |
| 80 | + " # add figure image\n", |
| 81 | + " if 'img_file' in fig_data.keys():\n", |
| 82 | + " fig_img_file = fig_data['img_file']\n", |
| 83 | + " fig.add_image(fig_img_file, width=NoEscape(r'\\textwidth'))\n", |
| 84 | + " # fig.append(StandAloneGraphic(image_options='width=' + NoEscape(fig_img_width), filename=fig_img_file))\n", |
| 85 | + " \n", |
| 86 | + " # if label is not provided, use image file name instead\n", |
| 87 | + " if 'fig_label' in fig_data.keys():\n", |
| 88 | + " fig_label = fig_data['fig_label']\n", |
| 89 | + " else:\n", |
| 90 | + " fig_label = fig_img_file.replace('figures/', 'fig:').replace('.pdf', '')\n", |
| 91 | + " \n", |
| 92 | + " # add figure caption\n", |
| 93 | + " fig.append(Command('caption', NoEscape(fig_caption), NoEscape(fig_caption_option)))\n", |
| 94 | + " \n", |
| 95 | + " # add figure label\n", |
| 96 | + " fig.append(Command('label', NoEscape(fig_label)))" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 3, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "save_file_name = './testing'\n", |
| 106 | + "doc = Document(save_file_name)\n", |
| 107 | + "\n", |
| 108 | + "# sample example\n", |
| 109 | + "fig_data = {\n", |
| 110 | + " 'fig_caption': r'{\\small Training Learning Curves for DQN \\textbf{when varying delay and sequence lengths}. Please note the different colorbar scales.}',\n", |
| 111 | + " 'fig_caption_option': r'DQN',\n", |
| 112 | + " 'fig_label': r'fig:dqn_seq_del_train_curves',\n", |
| 113 | + " \n", |
| 114 | + " 'img_file': 'figures/dqn_seq_del_train_learning_curves_episode_reward_mean.pdf',\n", |
| 115 | + "}\n", |
| 116 | + "generate_figure_section(doc, fig_data)\n", |
| 117 | + "\n", |
| 118 | + "# sample example with sub figures\n", |
| 119 | + "sub_fig_data = {\n", |
| 120 | + " 'fig_caption': r'Mean episodic reward (limited to 100 timesteps) at the end of training for three different tabular baseline algorithms \\textbf{when varying reward delay}. Error bars represent 1 standard deviation.',\n", |
| 121 | + " 'fig_caption_option': r' Mean episodic reward at the end of training ',\n", |
| 122 | + " 'fig_label': r'fig:tabular_delay',\n", |
| 123 | + " \n", |
| 124 | + " 'sub_figs' : [\n", |
| 125 | + " {\n", |
| 126 | + " 'fig_caption': r'{\\small Q-Learning}',\n", |
| 127 | + " 'fig_caption_option': r'Q-Learning',\n", |
| 128 | + " 'fig_label': r'fig:q_learn_tabular_del_train_final_reward_delay_episode_reward_mean_1d',\n", |
| 129 | + " 'img_file': 'figures/q_learn_tabular_del_train_final_reward_delay_episode_reward_mean_1d.pdf',\n", |
| 130 | + " },\n", |
| 131 | + " {\n", |
| 132 | + " 'img_file': 'figures/sarsa_tabular_del_train_final_reward_delay_episode_reward_mean_1d.pdf',\n", |
| 133 | + " 'fig_caption': r'{\\small SARSA}',\n", |
| 134 | + " 'fig_caption_option': r'SARSA',\n", |
| 135 | + " },\n", |
| 136 | + " {\n", |
| 137 | + " 'img_file': 'figures/double_q_learn_tabular_del_train_final_reward_delay_episode_reward_mean_1d.pdf',\n", |
| 138 | + " 'fig_caption': r'{\\small Double Q-Learning}',\n", |
| 139 | + " 'fig_caption_option': r'Double Q-Learning',\n", |
| 140 | + " },\n", |
| 141 | + " ],\n", |
| 142 | + "}\n", |
| 143 | + "generate_figure_section(doc, sub_fig_data)\n", |
| 144 | + "\n", |
| 145 | + "doc.generate_tex()" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": null, |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [], |
| 153 | + "source": [] |
| 154 | + } |
| 155 | + ], |
| 156 | + "metadata": { |
| 157 | + "kernelspec": { |
| 158 | + "display_name": "Python 3", |
| 159 | + "language": "python", |
| 160 | + "name": "python3" |
| 161 | + }, |
| 162 | + "language_info": { |
| 163 | + "codemirror_mode": { |
| 164 | + "name": "ipython", |
| 165 | + "version": 3 |
| 166 | + }, |
| 167 | + "file_extension": ".py", |
| 168 | + "mimetype": "text/x-python", |
| 169 | + "name": "python", |
| 170 | + "nbconvert_exporter": "python", |
| 171 | + "pygments_lexer": "ipython3", |
| 172 | + "version": "3.6.12" |
| 173 | + } |
| 174 | + }, |
| 175 | + "nbformat": 4, |
| 176 | + "nbformat_minor": 4 |
| 177 | +} |
0 commit comments