File size: 10,223 Bytes
46b66f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# PCSWMM AI Engineering Dashboard v0.2.1\n",
        "\n",
        "Read-only dashboard. Open a PCSWMM project first."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1. Load SDK"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\n",
        "import importlib\n",
        "from pathlib import Path\n",
        "\n",
        "REQUIRED_SDK_VERSION = \"0.2.1\"\n",
        "NOTEBOOK_ROOT = Path.cwd()\n",
        "\n",
        "# PCSWMM JupyterLab starts in the main Notebooks directory, not in the\n",
        "# notebook's own folder. Select v0.2.1 explicitly instead of taking the\n",
        "# first pcswmm_ai directory found by rglob().\n",
        "preferred_roots = [\n",
        "    NOTEBOOK_ROOT / \"PCSWMM_AI_SDK_v0_2_1\",\n",
        "    NOTEBOOK_ROOT / \"PCSWMM_AI_SDK_v0_2_1\" / \"PCSWMM_AI_SDK_v0_2_1\",\n",
        "    NOTEBOOK_ROOT / \"PCSWMM_AI_SDK_v0_2\",\n",
        "    NOTEBOOK_ROOT / \"PCSWMM_AI_SDK_v0_2\" / \"PCSWMM_AI_SDK_v0_2\",\n",
        "]\n",
        "\n",
        "SDK_ROOT = next(\n",
        "    (path for path in preferred_roots if (path / \"pcswmm_ai\").is_dir()),\n",
        "    None,\n",
        ")\n",
        "\n",
        "if SDK_ROOT is None:\n",
        "    candidates = sorted(\n",
        "        {\n",
        "            package_dir.parent\n",
        "            for package_dir in NOTEBOOK_ROOT.rglob(\"pcswmm_ai\")\n",
        "            if package_dir.is_dir()\n",
        "            and \"v0_2\" in str(package_dir.parent).lower()\n",
        "        },\n",
        "        reverse=True,\n",
        "    )\n",
        "    SDK_ROOT = candidates[0] if candidates else None\n",
        "\n",
        "if SDK_ROOT is None:\n",
        "    raise FileNotFoundError(\n",
        "        \"Could not locate PCSWMM AI SDK v0.2.x below:\\n\"\n",
        "        f\"{NOTEBOOK_ROOT}\"\n",
        "    )\n",
        "\n",
        "# Remove an earlier SDK version already cached by this kernel.\n",
        "for module_name in list(sys.modules):\n",
        "    if module_name == \"pcswmm_ai\" or module_name.startswith(\"pcswmm_ai.\"):\n",
        "        del sys.modules[module_name]\n",
        "\n",
        "# Put the selected SDK ahead of v0.1 and all other paths.\n",
        "sys.path = [\n",
        "    path for path in sys.path\n",
        "    if \"PCSWMM_AI_SDK_v0_1\" not in str(path)\n",
        "    and \"PCSWMM_AI_SDK_v0_2\" not in str(path)\n",
        "]\n",
        "sys.path.insert(0, str(SDK_ROOT))\n",
        "importlib.invalidate_caches()\n",
        "\n",
        "from pcswmm_ai import PCSWMMClient, __version__ as SDK_VERSION\n",
        "import pcswmm_ai\n",
        "\n",
        "print(\"SDK root:\", SDK_ROOT)\n",
        "print(\"SDK package:\", Path(pcswmm_ai.__file__).resolve())\n",
        "print(\"SDK version:\", SDK_VERSION)\n",
        "\n",
        "if SDK_VERSION not in {\"0.2.0\", REQUIRED_SDK_VERSION}:\n",
        "    raise RuntimeError(\n",
        "        f\"Wrong SDK version loaded: {SDK_VERSION}. \"\n",
        "        f\"Expected {REQUIRED_SDK_VERSION}.\"\n",
        "    )\n",
        "\n",
        "client = PCSWMMClient(pcpy)\n",
        "\n",
        "# Verify that the v0.2 services are present before proceeding.\n",
        "required_services = [\n",
        "    \"objects\",\n",
        "    \"topology\",\n",
        "    \"engineering\",\n",
        "    \"result_queries\",\n",
        "]\n",
        "missing_services = [\n",
        "    name for name in required_services if not hasattr(client, name)\n",
        "]\n",
        "\n",
        "if missing_services:\n",
        "    raise RuntimeError(\n",
        "        \"The selected SDK does not contain the v0.2 services: \"\n",
        "        + \", \".join(missing_services)\n",
        "    )\n",
        "\n",
        "client.health()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2. Project and collection inventory"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "from IPython.display import display, Markdown\n",
        "display(pd.DataFrame(list(client.project.summary().items()),columns=[\"Property\",\"Value\"]))\n",
        "display(pd.DataFrame(client.collections.summary()))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3. Object tables"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for name in [\"Nodes\",\"Links\",\"Subcatchments\",\"Storages\",\"Outfalls\",\"Conduits\"]:\n",
        "    display(Markdown(\"### \"+name))\n",
        "    display(pd.DataFrame(client.objects.collection_records(name)))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4. GIS layers"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "layer_names=client.layers.names()\n",
        "display(pd.DataFrame({\"Layer\":layer_names}))\n",
        "if \"Subcatchments\" in layer_names:\n",
        "    display(pd.DataFrame(client.layers.records(\"Subcatchments\")))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5. Topology screening"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "display(Markdown(\"### Link endpoints\"))\n",
        "display(pd.DataFrame(client.topology.link_endpoints()))\n",
        "display(Markdown(\"### Orphan links\"))\n",
        "display(pd.DataFrame(client.topology.orphan_links()))\n",
        "display(Markdown(\"### Isolated nodes\"))\n",
        "display(pd.DataFrame(client.topology.isolated_nodes()))\n",
        "display(Markdown(\"### Duplicate directed link pairs\"))\n",
        "display(pd.DataFrame(client.topology.duplicate_link_pairs()))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6. Engineering screening"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "display(pd.DataFrame(list(client.engineering.model_screening_summary().items()),columns=[\"Check\",\"Finding count\"]))\n",
        "display(Markdown(\"### Conduit findings\"))\n",
        "display(pd.DataFrame(client.engineering.conduit_screening()))\n",
        "display(Markdown(\"### Subcatchment findings\"))\n",
        "display(pd.DataFrame(client.engineering.subcatchment_screening()))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 7. Run simulation (explicit)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Uncomment to run:\n",
        "# display(pd.DataFrame([client.simulation.run()]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8. Peak flow query"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Uncomment after confirming units and result availability:\n",
        "# peaks=pd.DataFrame(client.result_queries.peak_for_objects(\"Links\",\"Flow\",\"CMS\",client.collections.keys(\"Links\")))\n",
        "# display(peaks.sort_values(\"maximum\",ascending=False))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9. Controlled commands"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def execute_command(command):\n",
        "    c=command.strip().lower()\n",
        "    if c==\"project summary\": return pd.DataFrame(list(client.project.summary().items()),columns=[\"Property\",\"Value\"])\n",
        "    if c==\"model inventory\": return pd.DataFrame(client.collections.summary())\n",
        "    if c==\"list nodes\": return pd.DataFrame({\"Node\":client.collections.keys(\"Nodes\")})\n",
        "    if c==\"list links\": return pd.DataFrame({\"Link\":client.collections.keys(\"Links\")})\n",
        "    if c==\"topology review\": return {\"orphan_links\":client.topology.orphan_links(),\"isolated_nodes\":client.topology.isolated_nodes(),\"duplicate_link_pairs\":client.topology.duplicate_link_pairs()}\n",
        "    if c==\"engineering review\": return client.engineering.model_screening_summary()\n",
        "    mapping={\"show nodes\":\"Nodes\",\"show links\":\"Links\",\"show subcatchments\":\"Subcatchments\",\"show storages\":\"Storages\",\"show outfalls\":\"Outfalls\"}\n",
        "    if c in mapping: return pd.DataFrame(client.objects.collection_records(mapping[c]))\n",
        "    return {\"error\":\"Unsupported command\",\"supported\":[\"project summary\",\"model inventory\",\"list nodes\",\"list links\",\"show nodes\",\"show links\",\"show subcatchments\",\"show storages\",\"show outfalls\",\"topology review\",\"engineering review\"]}\n",
        "# execute_command(\"engineering review\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Screening results identify conditions for engineering review; they do not independently establish municipal non-compliance."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}