File size: 13,452 Bytes
50464c0
 
 
 
 
 
 
 
 
a319a4e
50464c0
 
a319a4e
50464c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a319a4e
 
50464c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cell-00",
   "metadata": {},
   "source": [
    "# Read one scene without the library\n",
    "\n",
    "The same link as the [library tutorial](https://huggingface.co/datasets/Intelligence-Networking-Lab/Channels4World/blob/main/docs/tutorial-library.md), **BS 0 → UE 14 of\n",
    "`urban_scene_019234` at 3.5 GHz**, read with NumPy and the standard library from Tiny's loose\n",
    "files: about 15 MB. Archives from larger tiers hold the same files; see\n",
    "[without the library](https://huggingface.co/datasets/Intelligence-Networking-Lab/Channels4World/blob/main/docs/without-library.md) for TAR access.\n",
    "\n",
    "Install with `pip install numpy huggingface_hub matplotlib`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-01",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import json\n",
    "import zipfile\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from huggingface_hub import snapshot_download\n",
    "from matplotlib.collections import PolyCollection"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-02",
   "metadata": {},
   "source": [
    "## 1. Choose the link\n",
    "\n",
    "The same identifiers as the library tutorial. A UE ID is a row of `ue_positions`, and the BS\n",
    "ID is both a row of `bs_positions` and the number in the record's file name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-03",
   "metadata": {},
   "outputs": [],
   "source": [
    "REPO, REVISION = \"Intelligence-Networking-Lab/Channels4World\", \"v1.0.0\"\n",
    "SCENE_ID, GROUP = \"urban_scene_019234\", \"test-urban-g00000\"  # GROUP: from the tiny_scenes catalog\n",
    "CARRIER_HZ, BS_ID, UE_ID = 3_500_000_000, 0, 14\n",
    "OTHER_HZ = 7_000_000_000\n",
    "DATA = Path(\"c4w\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-04",
   "metadata": {},
   "source": [
    "## 2. Download the scene's files\n",
    "\n",
    "NumPy needs only the record files: positions, geometry, and BS 0's core and detail at both\n",
    "carriers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-05",
   "metadata": {},
   "outputs": [],
   "source": [
    "scene_dir = DATA / \"tiny\" / \"records\" / f\"scene={SCENE_ID}\"\n",
    "remote = f\"tiny/records/scene={SCENE_ID}\"\n",
    "snapshot_download(REPO, repo_type=\"dataset\", revision=REVISION, local_dir=DATA, allow_patterns=[\n",
    "    f\"{remote}/positions.npz\", f\"{remote}/geometry.zip\", f\"{remote}/band_hz=*/component=*/bs{BS_ID:05d}.npz\"])\n",
    "\n",
    "def record_path(carrier_hz, component):\n",
    "    return scene_dir / f\"band_hz={carrier_hz}\" / f\"component={component}\" / f\"bs{BS_ID:05d}.npz\"\n",
    "\n",
    "print(sorted(p.relative_to(scene_dir).as_posix() for p in scene_dir.rglob(\"*\") if p.is_file()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-06",
   "metadata": {},
   "source": [
    "## 3. The hierarchy on disk\n",
    "\n",
    "Each scene folder holds `positions.npz` and `geometry.zip`, shared by both carriers, and one\n",
    "file per carrier, component and BS: `band_hz=<Hz>/component=<core|detail>/bs<NNNNN>.npz`.\n",
    "Every NPZ carries its identity in a JSON `__meta__` entry:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-07",
   "metadata": {},
   "outputs": [],
   "source": [
    "with np.load(record_path(CARRIER_HZ, \"core\"), allow_pickle=False) as npz:\n",
    "    meta = json.loads(npz[\"__meta__\"].item())\n",
    "print(json.dumps(meta[\"identity\"], indent=1))\n",
    "print(meta[\"dimensions\"])                      # retained links and paths in this record"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-08",
   "metadata": {},
   "source": [
    "## 4. Geometry and positions\n",
    "\n",
    "Geometry, positions and interaction points share one frame: scene metres, z up."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-09",
   "metadata": {},
   "outputs": [],
   "source": [
    "with np.load(scene_dir / \"positions.npz\", allow_pickle=False) as positions:\n",
    "    bs_positions, ue_positions = positions[\"bs_positions\"], positions[\"ue_positions\"]\n",
    "bs_xyz, ue_xyz = bs_positions[BS_ID], ue_positions[UE_ID]\n",
    "print(bs_positions.shape, ue_positions.shape)\n",
    "print(\"BS\", bs_xyz, \"UE\", ue_xyz, f\"distance {np.linalg.norm(ue_xyz - bs_xyz):.1f} m\")\n",
    "\n",
    "def top_view(read, names):\n",
    "    \"\"\"Building triangles projected onto the ground (x, y), from OBJ meshes.\"\"\"\n",
    "    triangles = []\n",
    "    for name in names:\n",
    "        verts, faces = [], []\n",
    "        for line in read(name).decode().splitlines():\n",
    "            if line.startswith(\"v \"):\n",
    "                verts.append([float(v) for v in line.split()[1:4]])\n",
    "            elif line.startswith(\"f \"):\n",
    "                idx = [int(t.split(\"/\")[0]) for t in line.split()[1:]]\n",
    "                faces += [[idx[0], idx[k], idx[k + 1]] for k in range(1, len(idx) - 1)]\n",
    "        v = np.array(verts)\n",
    "        triangles += [v[[i - 1 for i in f], :2] for f in faces]   # OBJ indices start at 1\n",
    "    return triangles\n",
    "\n",
    "with zipfile.ZipFile(scene_dir / \"geometry.zip\") as geometry:\n",
    "    print(\"entry point:\", json.loads(geometry.read(\"scene.json\"))[\"entrypoint\"])\n",
    "    meshes = [n for n in geometry.namelist() if n.endswith(\".obj\") and \"ground\" not in n]\n",
    "    triangles = top_view(geometry.read, meshes)\n",
    "print(len(meshes), \"building meshes\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-10",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(6, 6))\n",
    "ax.add_collection(PolyCollection(triangles, facecolor=\"#c3cbc2\", edgecolor=\"none\"))\n",
    "ax.scatter(ue_positions[:, 0], ue_positions[:, 1], s=0.2, color=\"#4d9688\", label=f\"{len(ue_positions):,} UEs\")\n",
    "ax.scatter(bs_positions[:, 0], bs_positions[:, 1], marker=\"^\", s=30, color=\"k\", label=f\"{len(bs_positions)} BSs\")\n",
    "ax.plot(*zip(bs_xyz[:2], ue_xyz[:2]), color=\"#e2733d\", lw=2, label=f\"BS {BS_ID} → UE {UE_ID}\")\n",
    "ax.set(xlim=(-250, 250), ylim=(-250, 250), aspect=\"equal\", xlabel=\"x (m)\", ylabel=\"y (m)\", title=SCENE_ID)\n",
    "ax.legend(loc=\"upper right\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-11",
   "metadata": {},
   "source": [
    "## 5. Find the link\n",
    "\n",
    "`link_ue_index` lists the UEs this BS reaches, in ascending order; the link's row is where\n",
    "`UE_ID` sits in that list. Link `row` owns paths `link_path_offsets[row]` to\n",
    "`link_path_offsets[row + 1]`. A UE that is not listed has no retained link to this BS (not a\n",
    "zero-valued channel)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-12",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load(carrier_hz, component):\n",
    "    with np.load(record_path(carrier_hz, component), allow_pickle=False) as npz:\n",
    "        return {name: npz[name] for name in npz.files if name != \"__meta__\"}\n",
    "\n",
    "def link_of(core, detail, ue_id):\n",
    "    ue_ids = core[\"link_ue_index\"]\n",
    "    row = int(np.searchsorted(ue_ids, ue_id))\n",
    "    if row == ue_ids.size or ue_ids[row] != ue_id:\n",
    "        return None                                        # no retained link to this UE\n",
    "    p0, p1 = map(int, core[\"link_path_offsets\"][row:row + 2])\n",
    "    link_core = {k: v[p0:p1] for k, v in core.items() if k not in (\"link_ue_index\", \"link_path_offsets\")}\n",
    "    i0, i1 = map(int, detail[\"path_interaction_offsets\"][[p0, p1]])\n",
    "    link_detail = {k: detail[k][i0:i1] for k in (\"interaction_rank\", \"interaction_type\", \"interaction_vertex\")}\n",
    "    link_detail[\"path_interaction_offsets\"] = detail[\"path_interaction_offsets\"][p0:p1 + 1] - i0\n",
    "    return row, link_core, link_detail\n",
    "\n",
    "core, detail = load(CARRIER_HZ, \"core\"), load(CARRIER_HZ, \"detail\")\n",
    "row, link_core, link_detail = link_of(core, detail, UE_ID)\n",
    "print(f\"UE {UE_ID}: link row {row} of {core['link_ue_index'].size}, {link_core['tau'].size} paths\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-13",
   "metadata": {},
   "source": [
    "## 6. What each path stores\n",
    "\n",
    "The same arrays as in the library tutorial: the complex coefficient `a_real + 1j*a_imag`\n",
    "(passband, without the carrier-delay phase, antennas included), the delay `tau` in seconds,\n",
    "departure and arrival angles in radians and, in detail, each interaction's type and location\n",
    "in order. Paths are stored strongest first, at most 16 per link above −150 dB."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-14",
   "metadata": {},
   "outputs": [],
   "source": [
    "NAMES = {1: \"specular reflection\", 2: \"diffuse reflection\", 4: \"refraction\", 8: \"diffraction\"}\n",
    "\n",
    "def describe(core, detail):\n",
    "    \"\"\"One line per retained path, strongest first.\"\"\"\n",
    "    re, im = core[\"a_real\"].astype(np.float64), core[\"a_imag\"].astype(np.float64)\n",
    "    power_db = 10 * np.log10(re**2 + im**2)\n",
    "    off = detail[\"path_interaction_offsets\"]\n",
    "    for p in range(len(core[\"tau\"])):\n",
    "        kinds = [NAMES.get(int(t), str(t)) for t in detail[\"interaction_type\"][off[p]:off[p + 1]]]\n",
    "        print(f\"rank {int(core['path_rank'][p]):2d}  {core['tau'][p] * 1e9:8.2f} ns  {power_db[p]:7.2f} dB  \"\n",
    "              f\"AoD az {np.degrees(core['phi_t'][p]):6.1f}°  AoA az {np.degrees(core['phi_r'][p]):6.1f}°  \"\n",
    "              f\"{' → '.join(kinds) or 'line of sight'}\")\n",
    "\n",
    "describe(link_core, link_detail)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-15",
   "metadata": {},
   "source": [
    "## 7. A power–delay profile\n",
    "\n",
    "Placing each path by delay and power gives the link's power–delay profile: a path-based view\n",
    "of the stored channel, not a sampled impulse response."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-16",
   "metadata": {},
   "outputs": [],
   "source": [
    "def stems(ax, tau, a_real, a_imag, label, marker, color):\n",
    "    power_db = 10 * np.log10(a_real.astype(np.float64)**2 + a_imag.astype(np.float64)**2)\n",
    "    ax.vlines(tau * 1e9, -150, power_db, color=color, lw=1.2)\n",
    "    ax.scatter(tau * 1e9, power_db, marker=marker, color=color, zorder=3, label=label)\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(7, 3))\n",
    "stems(ax, link_core[\"tau\"], link_core[\"a_real\"], link_core[\"a_imag\"], \"3.5 GHz\", \"o\", \"#e2733d\")\n",
    "ax.set(xlabel=\"delay (ns)\", ylabel=\"path power (dB)\", ylim=(-150, None),\n",
    "       title=f\"{SCENE_ID} · BS {BS_ID} → UE {UE_ID}\")\n",
    "ax.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-17",
   "metadata": {},
   "source": [
    "## 8. The same link at 7 GHz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-18",
   "metadata": {},
   "outputs": [],
   "source": [
    "other = link_of(load(OTHER_HZ, \"core\"), load(OTHER_HZ, \"detail\"), UE_ID)\n",
    "fig, ax = plt.subplots(figsize=(7, 3))\n",
    "stems(ax, link_core[\"tau\"], link_core[\"a_real\"], link_core[\"a_imag\"], \"3.5 GHz\", \"o\", \"#e2733d\")\n",
    "if other is None:\n",
    "    print(\"UE\", UE_ID, \"has no retained link at 7 GHz\")\n",
    "else:\n",
    "    _, core7, _ = other\n",
    "    stems(ax, core7[\"tau\"], core7[\"a_real\"], core7[\"a_imag\"], \"7 GHz\", \"s\", \"#4d9688\")\n",
    "    for c, label in ((link_core, \"3.5 GHz\"), (core7, \"7 GHz\")):\n",
    "        power = np.sum(c[\"a_real\"].astype(np.float64) ** 2 + c[\"a_imag\"].astype(np.float64) ** 2)\n",
    "        print(f\"{label}: {c['tau'].size} paths, link power {10 * np.log10(power):.2f} dB\")\n",
    "ax.set(xlabel=\"delay (ns)\", ylabel=\"path power (dB)\", ylim=(-150, None))\n",
    "ax.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-19",
   "metadata": {},
   "source": [
    "Ranks order the paths within one record, so the same rank at 3.5 and 7 GHz need not be\n",
    "the same route. Compare delays, interaction sequences or interaction points instead.\n",
    "\n",
    "## Next\n",
    "\n",
    "- [Without the library](https://huggingface.co/datasets/Intelligence-Networking-Lab/Channels4World/blob/main/docs/without-library.md): TAR archives, catalogs and byte-range reads.\n",
    "- [Schema reference](https://huggingface.co/datasets/Intelligence-Networking-Lab/Channels4World/blob/main/docs/schema-reference.md): every array, its dtype and its meaning."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}