|
14 | 14 | }, |
15 | 15 | { |
16 | 16 | "cell_type": "code", |
17 | | - "execution_count": 1, |
| 17 | + "execution_count": null, |
18 | 18 | "id": "bc1528b8-61cd-4219-993f-e3f1ac79e801", |
19 | 19 | "metadata": {}, |
20 | 20 | "outputs": [], |
|
24 | 24 | "import glob\n", |
25 | 25 | "import random\n", |
26 | 26 | "import math\n", |
| 27 | + "import sys\n", |
27 | 28 | "\n", |
28 | 29 | "from collections import Counter\n", |
29 | 30 | "\n", |
30 | 31 | "import numpy as np\n", |
31 | 32 | "import pandas as pd\n", |
| 33 | + "import xarray as xr\n", |
32 | 34 | "from scipy.stats import binom, entropy\n", |
33 | 35 | "from scipy.spatial import distance\n", |
34 | 36 | "from tqdm.auto import tqdm, trange\n", |
35 | 37 | "from anytree import PreOrderIter, Walker\n", |
36 | 38 | "from matplotlib import pyplot as plt\n", |
| 39 | + "from statsmodels.stats.proportion import proportion_confint\n", |
37 | 40 | "\n", |
38 | 41 | "from pyecsca.ec.mult import *\n", |
39 | 42 | "from pyecsca.misc.utils import TaskExecutor, silent\n", |
40 | 43 | "from pyecsca.sca.re.tree import Map, Tree\n", |
41 | 44 | "\n", |
42 | | - "from common import *\n", |
| 45 | + "from epare.divisors import divisor_map\n", |
| 46 | + "from epare.config import all_configs, Config\n", |
| 47 | + "from epare.error_model import all_error_models\n", |
| 48 | + "\n", |
| 49 | + "if sys.version_info >= (3, 14):\n", |
| 50 | + " from compression import zstd\n", |
| 51 | + "else:\n", |
| 52 | + " from backports import zstd\n", |
43 | 53 | "\n", |
44 | 54 | "%matplotlib ipympl" |
45 | 55 | ] |
|
55 | 65 | }, |
56 | 66 | { |
57 | 67 | "cell_type": "code", |
58 | | - "execution_count": 2, |
| 68 | + "execution_count": null, |
59 | 69 | "id": "119b1e18-eeb0-4b70-bec3-ff277641403f", |
60 | 70 | "metadata": {}, |
61 | 71 | "outputs": [], |
|
72 | 82 | }, |
73 | 83 | { |
74 | 84 | "cell_type": "code", |
75 | | - "execution_count": 3, |
| 85 | + "execution_count": null, |
76 | 86 | "id": "ccb00342-3c48-49c9-bedf-2341e5eae3a2", |
77 | 87 | "metadata": {}, |
78 | 88 | "outputs": [], |
|
91 | 101 | }, |
92 | 102 | { |
93 | 103 | "cell_type": "code", |
94 | | - "execution_count": 4, |
| 104 | + "execution_count": null, |
95 | 105 | "id": "3dbac9be-d098-479a-8ca2-f531f6668f7c", |
96 | 106 | "metadata": {}, |
97 | | - "outputs": [ |
98 | | - { |
99 | | - "data": { |
100 | | - "application/vnd.jupyter.widget-view+json": { |
101 | | - "model_id": "1e5c7654ba8c4e559d1e100a334a3331", |
102 | | - "version_major": 2, |
103 | | - "version_minor": 0 |
104 | | - }, |
105 | | - "text/plain": [ |
106 | | - " 0%| | 0/12480 [00:00<?, ?it/s]" |
107 | | - ] |
108 | | - }, |
109 | | - "metadata": {}, |
110 | | - "output_type": "display_data" |
111 | | - } |
112 | | - ], |
113 | | - "source": [ |
114 | | - "with open(f\"merged.pickle\", \"rb\") as f:\n", |
115 | | - " distributions_mults = pickle.load(f)\n", |
116 | | - "for probmap in tqdm(distributions_mults.values(), desc=f\"Narrowing probmaps to {divisor_name}.\"):\n", |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "with zstd.open(\"merged.zpickle\", \"rb\") as f:\n", |
| 110 | + " config_map = pickle.load(f)\n", |
| 111 | + "for probmap in tqdm(config_map.values(), desc=f\"Narrowing probmaps to {divisor_name}.\"):\n", |
117 | 112 | " probmap.narrow(allfeats)" |
118 | 113 | ] |
119 | 114 | }, |
120 | 115 | { |
121 | 116 | "cell_type": "code", |
122 | | - "execution_count": 8, |
| 117 | + "execution_count": null, |
123 | 118 | "id": "38c81e38-a37c-4e58-ac9e-927d14dad458", |
124 | 119 | "metadata": {}, |
125 | 120 | "outputs": [], |
126 | 121 | "source": [ |
127 | | - "allmults = list(distributions_mults.keys())\n", |
128 | | - "basemults = list(all_mults_with_ctr)\n", |
| 122 | + "allmults = list(config_map.keys())\n", |
| 123 | + "basemults = list(all_configs)\n", |
| 124 | + "\n", |
129 | 125 | "nbase = len(basemults)\n", |
130 | 126 | "nmults = len(allmults)\n", |
131 | 127 | "nallfeats = len(allfeats)" |
132 | 128 | ] |
133 | 129 | }, |
134 | 130 | { |
135 | 131 | "cell_type": "code", |
136 | | - "execution_count": 9, |
| 132 | + "execution_count": null, |
137 | 133 | "id": "206b385a-bb98-47e7-9312-94a864794faa", |
138 | 134 | "metadata": {}, |
139 | | - "outputs": [ |
140 | | - { |
141 | | - "name": "stdout", |
142 | | - "output_type": "stream", |
143 | | - "text": [ |
144 | | - "Have 12480 configurations (scalar multiplier x countermeasure x error model).\n", |
145 | | - "Have 390 base configurations (scalar multiplier x countermeasure).\n", |
146 | | - "Have 3215 base point orders (features).\n" |
147 | | - ] |
148 | | - } |
149 | | - ], |
| 135 | + "outputs": [], |
150 | 136 | "source": [ |
151 | 137 | "print(f\"Have {nmults} configurations (scalar multiplier x countermeasure x error model).\")\n", |
152 | 138 | "print(f\"Have {nbase} base configurations (scalar multiplier x countermeasure).\")\n", |
153 | 139 | "print(f\"Have {nallfeats} base point orders (features).\")" |
154 | 140 | ] |
155 | 141 | }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": null, |
| 145 | + "id": "e95a1551-3fec-468a-866d-1efa5721364d", |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "probs = xr.DataArray(np.zeros((nmults, nallfeats), dtype=np.float64), dims=(\"cfg\", \"divisor\"), coords={\"cfg\": allmults, \"divisor\": allfeats})\n", |
| 150 | + "for mult, probmap in tqdm(config_map.items(), desc=\"Convert probmaps to xarray.\"):\n", |
| 151 | + " probs.loc[mult, :] = [probmap[divisor] for divisor in allfeats]" |
| 152 | + ] |
| 153 | + }, |
156 | 154 | { |
157 | 155 | "cell_type": "markdown", |
158 | 156 | "id": "437bcd9c-1da5-428a-a979-0835326777f3", |
|
192 | 190 | { |
193 | 191 | "cell_type": "code", |
194 | 192 | "execution_count": null, |
195 | | - "id": "0b85fad7-392f-4701-9329-d75d39736bbb", |
| 193 | + "id": "bf81fa2e-8746-4af1-963a-8d8340ed22da", |
196 | 194 | "metadata": {}, |
197 | 195 | "outputs": [], |
198 | 196 | "source": [ |
199 | | - "# Now go over all divisors, cluster based on overlapping CI for given n?\n", |
200 | | - "io_map = {mult:{} for mult in allmults}\n", |
201 | | - "for divisor in tqdm(allfeats):\n", |
| 197 | + "def make_groups(column: np.ndarray, nbuild: int, alpha: float, method: str = \"wilson\"):\n", |
202 | 198 | " prev_ci_low = None\n", |
203 | 199 | " prev_ci_high = None\n", |
204 | | - " groups = {}\n", |
205 | | - " pvals = {}\n", |
206 | | - " group = 0\n", |
207 | | - " for mult, probmap in sorted(distributions_mults.items(), key=lambda item: -item[1][divisor]):\n", |
| 200 | + " groups = []\n", |
| 201 | + " ci_low, ci_high = proportion_confint(column * nbuild, nbuild, alpha=alpha, method=method) \n", |
| 202 | + " order = np.argsort(column)\n", |
| 203 | + " for mult_index in reversed(order.data):\n", |
208 | 204 | " # We are going from high to low p.\n", |
209 | | - " pval = probmap[divisor]\n", |
210 | | - " pvals[mult] = pval\n", |
211 | | - " ci_low, ci_high = conf_interval(pval, nbuild, alpha)\n", |
212 | | - " ci_low = max(ci_low, 0.0)\n", |
213 | | - " ci_high = min(ci_high, 1.0)\n", |
214 | | - " if (prev_ci_low is None and prev_ci_high is None) or prev_ci_low >= ci_high:\n", |
215 | | - " g = groups.setdefault(f\"arbitrary{group}\", set())\n", |
216 | | - " g.add(mult)\n", |
217 | | - " group += 1\n", |
| 205 | + " low = ci_low[mult_index]\n", |
| 206 | + " high = ci_high[mult_index]\n", |
| 207 | + " if (prev_ci_low is None and prev_ci_high is None) or prev_ci_low >= high:\n", |
| 208 | + " g = set()\n", |
| 209 | + " groups.append(g)\n", |
| 210 | + " #print(low, high, len(groups), prev_ci_low, prev_ci_high)\n", |
218 | 211 | " else:\n", |
219 | | - " g = groups.setdefault(f\"arbitrary{group}\", set())\n", |
220 | | - " g.add(mult)\n", |
221 | | - " prev_ci_low = ci_low\n", |
222 | | - " prev_ci_high = ci_high\n", |
223 | | - " \n", |
224 | | - " #print(f\"Divisor: {divisor}, num groups: {group}\", end=\"\\n\\t\")\n", |
225 | | - " #for g in groups.values():\n", |
| 212 | + " g = groups[-1]\n", |
| 213 | + " g.add(mult_index)\n", |
| 214 | + " prev_ci_low = low\n", |
| 215 | + " prev_ci_high = high\n", |
| 216 | + " return groups" |
| 217 | + ] |
| 218 | + }, |
| 219 | + { |
| 220 | + "cell_type": "code", |
| 221 | + "execution_count": null, |
| 222 | + "id": "0b85fad7-392f-4701-9329-d75d39736bbb", |
| 223 | + "metadata": {}, |
| 224 | + "outputs": [], |
| 225 | + "source": [ |
| 226 | + "# Now go over all divisors, cluster based on overlapping CI for given n?\n", |
| 227 | + "cfg_map = pd.DataFrame(list(range(len(allmults))), index=allmults, columns=[\"vals\"])\n", |
| 228 | + "codomain = set()\n", |
| 229 | + "mapping = np.zeros((nmults, nallfeats), dtype=int)\n", |
| 230 | + "for i, divisor in tqdm(enumerate(allfeats), desc=\"Computing groups.\", total=nallfeats):\n", |
| 231 | + " groups = make_groups(probs.loc[:, divisor], nbuild, alpha)\n", |
| 232 | + " #print(f\"Divisor: {divisor}, num groups: {len(groups)}\")\n", |
| 233 | + " #for g in groups:\n", |
226 | 234 | " # print(len(g), end=\", \")\n", |
227 | 235 | " #print()\n", |
228 | | - " for group, mults in groups.items():\n", |
229 | | - " mult_pvals = [pvals[mult] for mult in mults]\n", |
230 | | - " group_pval_avg = np.mean(mult_pvals)\n", |
231 | | - " group_pval_var = np.var(mult_pvals)\n", |
232 | | - " group_pval_min = np.min(mult_pvals)\n", |
233 | | - " group_pval_max = np.max(mult_pvals)\n", |
234 | | - " for mult in mults:\n", |
235 | | - " io_map[mult][divisor] = (group, group_pval_avg, group_pval_var, group_pval_min, group_pval_max)\n", |
| 236 | + " for group, mult_indices in enumerate(groups):\n", |
| 237 | + " codomain.add(group)\n", |
| 238 | + " for mult_index in mult_indices:\n", |
| 239 | + " mapping[mult_index, i] = group\n", |
236 | 240 | "\n", |
237 | 241 | "# then build dmap\n", |
238 | | - "dmap = Map.from_io_maps(set(distributions_mults.keys()), io_map)" |
| 242 | + "dmap = Map(mapping=pd.DataFrame(mapping), cfg_map=cfg_map, domain=allfeats, codomain=codomain)" |
239 | 243 | ] |
240 | 244 | }, |
241 | 245 | { |
|
285 | 289 | "cell_type": "code", |
286 | 290 | "execution_count": null, |
287 | 291 | "id": "5735e7d4-149c-4184-96f7-dcfd6017fbad", |
288 | | - "metadata": {}, |
| 292 | + "metadata": { |
| 293 | + "scrolled": true |
| 294 | + }, |
289 | 295 | "outputs": [], |
290 | 296 | "source": [ |
291 | 297 | "# build a tree\n", |
292 | | - "with silent():\n", |
293 | | - " tree = Tree.build(set(allmults), dmap)" |
| 298 | + "#with silent():\n", |
| 299 | + "tree = Tree.build(set(allmults), dmap)" |
294 | 300 | ] |
295 | 301 | }, |
296 | 302 | { |
|
1610 | 1616 | "name": "python", |
1611 | 1617 | "nbconvert_exporter": "python", |
1612 | 1618 | "pygments_lexer": "ipython3", |
1613 | | - "version": "3.13.5" |
| 1619 | + "version": "3.12.3" |
1614 | 1620 | } |
1615 | 1621 | }, |
1616 | 1622 | "nbformat": 4, |
|
0 commit comments