{ "cells": [ { "cell_type": "markdown", "id": "cell-00", "metadata": {}, "source": [ "# Read one scene with the channels4world library\n", "\n", "This tutorial follows one link of Channels4World from the catalog down to its paths:\n", "**BS 0 → UE 14 of `urban_scene_019234` at 3.5 GHz**, the link the project website shows.\n", "It downloads about 26 MB of one Tiny scene, walks the hierarchy, reads the geometry, the\n", "positions and the channel record, explains the stored arrays, and ends with the same link\n", "at 7 GHz. [The same tutorial without the library](https://huggingface.co/datasets/Intelligence-Networking-Lab/Channels4World/blob/main/docs/tutorial-numpy.md) reads the same link\n", "with NumPy alone.\n", "\n", "Install the library with `pip install channels4world` (Python 3.11+), then\n", "`pip install huggingface_hub matplotlib`." ] }, { "cell_type": "code", "execution_count": null, "id": "cell-01", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "import json\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\n", "\n", "import channels4world as c4w" ] }, { "cell_type": "markdown", "id": "cell-02", "metadata": {}, "source": [ "## 1. Choose the link\n", "\n", "Change these to read any other link. `GROUP` is the scene's `group` in the `tiny_scenes`\n", "catalog. BS IDs run from 0 to 31 and UE IDs from 0 to 16,383; both are rows\n", "of the scene's positions. Carriers are integers in Hz." ] }, { "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 what this tutorial reads\n", "\n", "Tiny is published as loose files under `tiny/`. The library needs Tiny's catalog, the shared\n", "schemas and the scene's group metadata (about 11 MB); the records are the scene's positions\n", "and geometry and BS 0's core and detail at both carriers (about 15 MB)." ] }, { "cell_type": "code", "execution_count": null, "id": "cell-05", "metadata": {}, "outputs": [], "source": [ "scene_dir = f\"tiny/records/scene={SCENE_ID}\"\n", "patterns = [\n", " \"tiny/c4w-root.json\", \"tiny/c4w-root/*\", \"tiny/schema/*\",\n", " f\"tiny/groups/{GROUP}/catalog/members.parquet\", f\"tiny/groups/{GROUP}/snapshot.json\",\n", " f\"{scene_dir}/positions.npz\", f\"{scene_dir}/geometry.zip\",\n", " f\"{scene_dir}/band_hz=*/component=*/bs{BS_ID:05d}.npz\",\n", "]\n", "snapshot_download(REPO, repo_type=\"dataset\", revision=REVISION, local_dir=DATA, allow_patterns=patterns)" ] }, { "cell_type": "markdown", "id": "cell-06", "metadata": {}, "source": [ "## 3. Walk the hierarchy\n", "\n", "The dataset is organized as **dataset → scene → carrier → base station → UE link → paths**.\n", "Opening reads only the small catalog; group metadata and records load when first needed." ] }, { "cell_type": "code", "execution_count": null, "id": "cell-07", "metadata": {}, "outputs": [], "source": [ "ds = c4w.open(DATA / \"tiny\")\n", "print(ds)\n", "scene = ds.scene(SCENE_ID)\n", "print(scene) # split, profile and smallest tier\n", "print(\"carriers (Hz):\", scene.frequencies.ids)\n", "print(\"base stations:\", scene.frequencies[CARRIER_HZ].base_stations.ids)" ] }, { "cell_type": "markdown", "id": "cell-08", "metadata": {}, "source": [ "`urban_scene_019234` is a **test** scene: every link and both carriers of a scene stay in\n", "one split, so a model trained on the training split has never seen this place.\n", "\n", "## 4. Geometry and positions\n", "\n", "Geometry, positions and interaction points share one frame: scene metres, z up. Positions\n", "hold all 32 BS and 16,384 UE coordinates of the scene; the geometry is a Mitsuba XML file\n", "with OBJ meshes in a ZIP." ] }, { "cell_type": "code", "execution_count": null, "id": "cell-09", "metadata": {}, "outputs": [], "source": [ "positions = scene.read_positions()\n", "bs_positions, ue_positions = positions.bs, positions.ue\n", "bs_xyz, ue_xyz = bs_positions[BS_ID], ue_positions[UE_ID]\n", "print(positions)\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 scene.open_geometry() 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. One channel record\n", "\n", "A record holds every retained link from one BS at one carrier. `get_link(UE_ID)` returns\n", "that UE's link, or `None` when the UE has no retained link to this BS (which is not a\n", "zero-valued channel). A UE ID is a row of the positions; the link's row inside the record\n", "is a different index." ] }, { "cell_type": "code", "execution_count": null, "id": "cell-12", "metadata": {}, "outputs": [], "source": [ "record = scene.frequencies[CARRIER_HZ].base_stations[BS_ID].read(detail=True)\n", "print(record)\n", "link = record.get_link(UE_ID)\n", "print(link, f\"- row {link.index} of the record's {record.n_links} links\")" ] }, { "cell_type": "markdown", "id": "cell-13", "metadata": {}, "source": [ "## 6. What each path stores\n", "\n", "For every retained path the core record stores 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 its interaction count and type flags; the detail\n", "record adds each interaction's type and location in order. Paths are stored strongest first,\n", "and each link keeps at most its 16 strongest paths 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. It is not a sampled impulse response; for a channel at a given\n", "bandwidth, `channels4world.transforms` provides baseband coefficients and frequency\n", "responses." ] }, { "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\n", "\n", "Every scene is simulated at both carriers with the same BS and UE positions. Read the same\n", "BS and UE at 7 GHz and compare on shared axes:" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-18", "metadata": {}, "outputs": [], "source": [ "link7 = scene.frequencies[OTHER_HZ].base_stations[BS_ID].read(detail=True).get_link(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 link7 is None:\n", " print(\"UE\", UE_ID, \"has no retained link at 7 GHz\")\n", "else:\n", " stems(ax, link7.core[\"tau\"], link7.core[\"a_real\"], link7.core[\"a_imag\"], \"7 GHz\", \"s\", \"#4d9688\")\n", " for l, label in ((link, \"3.5 GHz\"), (link7, \"7 GHz\")):\n", " a = l.core[\"a_real\"].astype(np.float64) + 1j * l.core[\"a_imag\"].astype(np.float64)\n", " print(f\"{label}: {l.n_paths} paths, link power {10 * np.log10(np.sum(np.abs(a) ** 2)):.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: rank 0 at 7 GHz need not be the route of rank 0\n", "at 3.5 GHz. To match routes across carriers, compare delays, interaction sequences or\n", "interaction points (`link7.detail`), not ranks.\n", "\n", "## Next\n", "\n", "- [From channel records to a DataLoader](https://huggingface.co/datasets/Intelligence-Networking-Lab/Channels4World/blob/main/docs/ml-guide.md): link samples, splits, padding and batches.\n", "- [Library guide](https://huggingface.co/datasets/Intelligence-Networking-Lab/Channels4World/blob/main/docs/library-guide.md): download plans for larger tiers, recipes, extraction and caches.\n", "- [Schema reference](https://huggingface.co/datasets/Intelligence-Networking-Lab/Channels4World/blob/main/docs/schema-reference.md): every array and its meaning." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }