aboutsummaryrefslogtreecommitdiffhomepage
path: root/notebooks/latex_plotting.ipynb
blob: 1ec8ab61c73ba9be5e5de0579d1f468d1a98a78d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting notebook\n",
    "\n",
    "This notebook generates plots for paper submission. Prior to running this, make sure to run `temporal_trends` and `vulnerabilities` notebooks to produce necessary CSV files into `RESULTS_DIR` folder."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import scipy.stats as stats\n",
    "import seaborn as sns\n",
    "\n",
    "from sec_certs.dataset import CCDataset\n",
    "\n",
    "RESULTS_DIR = Path(\"./results\")\n",
    "FIGURE_DIR = RESULTS_DIR / \"figures/\"\n",
    "\n",
    "if not FIGURE_DIR.exists():\n",
    "    FIGURE_DIR.mkdir()\n",
    "\n",
    "# matplotlib.use(\"pgf\")\n",
    "sns.set_theme(style=\"white\")\n",
    "plt.rcParams[\"axes.linewidth\"] = 0.5\n",
    "plt.rcParams[\"legend.fontsize\"] = 6.5\n",
    "plt.rcParams[\"xtick.labelsize\"] = 8\n",
    "plt.rcParams[\"ytick.labelsize\"] = 8\n",
    "plt.rcParams[\"ytick.left\"] = True\n",
    "plt.rcParams[\"ytick.major.size\"] = 5\n",
    "plt.rcParams[\"ytick.major.width\"] = 0.5\n",
    "plt.rcParams[\"ytick.major.pad\"] = 0\n",
    "plt.rcParams[\"xtick.bottom\"] = True\n",
    "plt.rcParams[\"xtick.major.size\"] = 5\n",
    "plt.rcParams[\"xtick.major.width\"] = 0.5\n",
    "plt.rcParams[\"xtick.major.pad\"] = 0\n",
    "plt.rcParams[\"pgf.texsystem\"] = \"pdflatex\"\n",
    "plt.rcParams[\"font.family\"] = \"serif\"\n",
    "plt.rcParams[\"text.usetex\"] = True\n",
    "plt.rcParams[\"pgf.rcfonts\"] = False\n",
    "plt.rcParams[\"axes.titlesize\"] = 7\n",
    "plt.rcParams[\"axes.labelsize\"] = 7\n",
    "plt.rcParams[\"legend.handletextpad\"] = 0.3\n",
    "plt.rcParams[\"lines.markersize\"] = 4\n",
    "sns.set_palette(\"deep\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Histogram CVE disclosure dates vs. date of certification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(RESULTS_DIR / \"exploded_cves.csv\").rename(columns={\"Unnamed: 0\": \"dgst\"}).set_index(\"dgst\")\n",
    "\n",
    "hist = sns.histplot(df.n_days_after_certification, kde=False)\n",
    "hist.set(\n",
    "    xlim=(-2200, 4600),\n",
    "    ylim=(0, 1800),\n",
    "    xlabel=\"Number of days after date of certification\",\n",
    "    ylabel=\"Frequency of CVEs\",\n",
    ")\n",
    "hist.axvline(0, color=\"red\", linewidth=\"1\", label=\"Certification date\")\n",
    "hist.legend(loc=\"upper right\")\n",
    "\n",
    "fig = matplotlib.pyplot.gcf()\n",
    "fig.set_size_inches(3.35, 2)\n",
    "fig.savefig(FIGURE_DIR / \"cve_hist.pgf\", bbox_inches=\"tight\")\n",
    "fig.savefig(FIGURE_DIR / \"cve_hist.pdf\", bbox_inches=\"tight\")\n",
    "plt.close(fig)\n",
    "\n",
    "# QQ plot of for vulnerability disclosure vs. certification date (compared against normal distribution)\n",
    "# stats.probplot(df.n_days_after_certification, dist=\"norm\", plot=plt)\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Validity boxplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_validity = pd.read_csv(RESULTS_DIR / \"df_validity.csv\")\n",
    "\n",
    "box = sns.boxplot(data=df_validity, x=\"year_from\", y=\"validity_period\", linewidth=0.75, flierprops={\"marker\": \"x\"})\n",
    "box.set(\n",
    "    xlabel=\"Year of certification\",\n",
    "    ylabel=\"Lifetime of certificates (in years)\",\n",
    "    title=\"Boxplot of certificate validity periods\",\n",
    ")\n",
    "box.tick_params(axis=\"x\", rotation=75)\n",
    "\n",
    "fig = matplotlib.pyplot.gcf()\n",
    "fig.set_size_inches(3.5, 2.5)\n",
    "fig.savefig(FIGURE_DIR / \"boxplot_validity.pgf\", bbox_inches=\"tight\")\n",
    "fig.savefig(FIGURE_DIR / \"boxplot_validity.pdf\", bbox_inches=\"tight\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cells for 3-subplot figure\n",
    "\n",
    "Contains: Average EAL levels, Interesting schemes evolution, Stackplot of categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Downloading CC Dataset: 100%|██████████| 164M/164M [02:07<00:00, 1.35MB/s] \n"
     ]
    }
   ],
   "source": [
    "figure_width = 2.3\n",
    "figure_height = 1.8\n",
    "\n",
    "dset = CCDataset.from_web()  # local instantiation\n",
    "df = dset.to_pandas()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "avg_levels = pd.read_csv(RESULTS_DIR / \"avg_eal.csv\")\n",
    "eal_to_num_mapping = {eal: index for index, eal in enumerate(df[\"eal\"].cat.categories)}\n",
    "avg_levels[\"smartcard_category\"] = avg_levels.category.map(\n",
    "    lambda x: x if x == \"ICs, Smartcards\" else \"Other 14 categories\"\n",
    ")\n",
    "line = sns.lineplot(\n",
    "    data=avg_levels,\n",
    "    x=\"year_from\",\n",
    "    y=\"eal_number\",\n",
    "    hue=\"smartcard_category\",\n",
    "    errorbar=None,\n",
    "    style=\"smartcard_category\",\n",
    "    markers=True,\n",
    ")\n",
    "line.set(xlabel=None, ylabel=None, title=None, xlim=(1999.6, 2023.4))\n",
    "ymin = 1\n",
    "ymax = 9\n",
    "ylabels = [\n",
    "    x if \"+\" in x else x + r\"\\phantom{+}\" for x in list(eal_to_num_mapping.keys())[ymin : ymax + 1]\n",
    "]  # this also aligns the labels by adding phantom spaces\n",
    "line.set_yticks(range(ymin, ymax + 1), ylabels)\n",
    "line.set_xticks([1998, 2003, 2008, 2013, 2018, 2023])\n",
    "line.legend(title=None, labels=avg_levels.smartcard_category.unique())\n",
    "\n",
    "fig = matplotlib.pyplot.gcf()\n",
    "fig.set_size_inches(figure_width, figure_height)\n",
    "fig.tight_layout(pad=0.1)\n",
    "fig.savefig(FIGURE_DIR / \"temporal_trends_categories.pgf\")\n",
    "fig.savefig(FIGURE_DIR / \"temporal_trends_categories.pdf\")\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "interesting_schemes = pd.read_csv(RESULTS_DIR / \"interesting_schemes.csv\")\n",
    "\n",
    "line = sns.lineplot(\n",
    "    data=interesting_schemes,\n",
    "    x=\"year_from\",\n",
    "    y=\"size\",\n",
    "    hue=\"scheme\",\n",
    "    style=\"scheme\",\n",
    "    markers=True,\n",
    "    dashes=True,\n",
    ")\n",
    "line.set(xlabel=None, ylabel=None, title=None, xlim=(1999.6, 2023.4), ylim=(0, 90))\n",
    "line.set_xticks([1998, 2003, 2008, 2013, 2018, 2023])\n",
    "line.legend(title=None)\n",
    "fig = matplotlib.pyplot.gcf()\n",
    "fig.set_size_inches(figure_width, figure_height)\n",
    "fig.tight_layout(pad=0.1)\n",
    "fig.savefig(FIGURE_DIR / \"temporal_trends_schemes.pgf\")\n",
    "fig.savefig(FIGURE_DIR / \"temporal_trends_schemes.pdf\")\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_certs = pd.read_csv(RESULTS_DIR / \"popular_categories.csv\").astype({\"year_from\": \"category\"})\n",
    "dct = {\n",
    "    \"ICs, Smart Cards and Smart Card-Related Devices and Systems\": \"ICs and Smart Cards\",\n",
    "    \"Network and Network-Related Devices and Systems\": \"Network-Related Devices\",\n",
    "    \"Other Devices and Systems\": \"Other Devices\",\n",
    "    \"One of 11 other categories\": \"11 Other Categories\",\n",
    "}\n",
    "n_certs.popular_categories = n_certs.popular_categories.map(lambda x: dct.get(x, x))\n",
    "\n",
    "cats = n_certs.popular_categories.unique()\n",
    "years = n_certs.year_from.cat.categories[:-1]\n",
    "data = [n_certs.loc[n_certs.popular_categories == c, \"size\"].tolist()[:-1] for c in cats]\n",
    "\n",
    "# palette = sns.color_palette(\"Spectral\", 5).as_hex()\n",
    "# colors = \",\".join(palette)\n",
    "\n",
    "plt.stackplot(\n",
    "    years,\n",
    "    data,\n",
    "    labels=cats,\n",
    ")\n",
    "plt.legend(loc=\"upper center\", bbox_to_anchor=(0.38, 1.02))\n",
    "plt.xticks([1998, 2003, 2008, 2013, 2018, 2023])\n",
    "# plt.title(\"(c) Popularity of categories\")\n",
    "plt.xlim(1997, 2023)\n",
    "\n",
    "fig = matplotlib.pyplot.gcf()\n",
    "fig.set_size_inches(figure_width, figure_height)\n",
    "fig.tight_layout(pad=0.1)\n",
    "fig.savefig(FIGURE_DIR / \"temporal_trends_stackplot.pdf\")\n",
    "fig.savefig(FIGURE_DIR / \"temporal_trends_stackplot.pgf\")\n",
    "plt.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "a5b8c5b127d2cfe5bc3a1c933e197485eb9eba25154c3661362401503b4ef9d4"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}