Skip to content

Commit 079f67c

Browse files
committed
Fix 5 broken user guide notebooks
- 2_Proximity: cast bool line aggregate to int for proximity() - 11_Hydrology: fix undefined variable mfd -> mfd_fracs - 15_Balanced_Allocation: ensure ListedColormap has >= 2 entries - 27_Stream_Analysis: pass template DataArray to generate_terrain() - 28_Rasterize: use legend_kwds 'label' instead of removed 'title'
1 parent 19f722d commit 079f67c

File tree

5 files changed

+10
-126
lines changed

5 files changed

+10
-126
lines changed

examples/user_guide/11_Hydrology.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@
470470
{
471471
"cell_type": "code",
472472
"id": "btr592uiuve",
473-
"source": "flow_accum_mfd = xrspatial.flow_accumulation_mfd(mfd)\n\nfig, axes = plt.subplots(1, 2, figsize=(14, 5))\n\n# D8 accumulation (log scale)\nim0 = axes[0].imshow(np.log1p(flow_accum.values), cmap='Blues')\naxes[0].set_title('D8 flow accumulation (log)')\nfig.colorbar(im0, ax=axes[0], shrink=0.6)\n\n# MFD accumulation (log scale)\nim1 = axes[1].imshow(np.log1p(flow_accum_mfd.values), cmap='Blues')\naxes[1].set_title('MFD flow accumulation (log)')\nfig.colorbar(im1, ax=axes[1], shrink=0.6)\n\nplt.tight_layout()\nplt.show()",
473+
"source": "flow_accum_mfd = xrspatial.flow_accumulation_mfd(mfd_fracs)\n\nfig, axes = plt.subplots(1, 2, figsize=(14, 5))\n\n# D8 accumulation (log scale)\nim0 = axes[0].imshow(np.log1p(flow_accum.values), cmap='Blues')\naxes[0].set_title('D8 flow accumulation (log)')\nfig.colorbar(im0, ax=axes[0], shrink=0.6)\n\n# MFD accumulation (log scale)\nim1 = axes[1].imshow(np.log1p(flow_accum_mfd.values), cmap='Blues')\naxes[1].set_title('MFD flow accumulation (log)')\nfig.colorbar(im1, ax=axes[1], shrink=0.6)\n\nplt.tight_layout()\nplt.show()",
474474
"metadata": {},
475475
"execution_count": null,
476476
"outputs": []

examples/user_guide/15_Balanced_Allocation.ipynb

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,50 +48,7 @@
4848
"execution_count": null,
4949
"metadata": {},
5050
"outputs": [],
51-
"source": [
52-
"def make_raster(data, res=1.0):\n",
53-
" \"\"\"Helper: create a DataArray with y/x coordinates.\"\"\"\n",
54-
" h, w = data.shape\n",
55-
" raster = xr.DataArray(\n",
56-
" data.astype(np.float64),\n",
57-
" dims=['y', 'x'],\n",
58-
" attrs={'res': (res, res)},\n",
59-
" )\n",
60-
" raster['y'] = np.arange(h) * res\n",
61-
" raster['x'] = np.arange(w) * res\n",
62-
" return raster\n",
63-
"\n",
64-
"\n",
65-
"def plot_territories(alloc_arr, title, source_locs=None, ax=None):\n",
66-
" \"\"\"Plot a territory allocation with distinct colours per source.\"\"\"\n",
67-
" if ax is None:\n",
68-
" fig, ax = plt.subplots(figsize=(6, 5))\n",
69-
" data = alloc_arr.values if hasattr(alloc_arr, 'values') else alloc_arr\n",
70-
" ids = np.unique(data[np.isfinite(data)])\n",
71-
" colors = plt.cm.Set2(np.linspace(0, 1, max(len(ids), 3)))\n",
72-
" cmap = ListedColormap(colors[:len(ids)])\n",
73-
" im = ax.imshow(data, cmap=cmap, origin='upper', interpolation='nearest')\n",
74-
" ax.set_title(title)\n",
75-
" if source_locs is not None:\n",
76-
" for r, c in source_locs:\n",
77-
" ax.plot(c, r, 'k*', markersize=12)\n",
78-
" plt.colorbar(im, ax=ax, shrink=0.8)\n",
79-
" return ax\n",
80-
"\n",
81-
"\n",
82-
"def territory_stats(alloc_arr, friction_arr):\n",
83-
" \"\"\"Print cell count and cost-weighted area per territory.\"\"\"\n",
84-
" alloc = alloc_arr.values if hasattr(alloc_arr, 'values') else alloc_arr\n",
85-
" fric = friction_arr.values if hasattr(friction_arr, 'values') else friction_arr\n",
86-
" ids = sorted(np.unique(alloc[np.isfinite(alloc)]))\n",
87-
" print(f\"{'Source':>8} {'Cells':>6} {'Cost-weighted area':>18}\")\n",
88-
" print('-' * 38)\n",
89-
" for sid in ids:\n",
90-
" mask = alloc == sid\n",
91-
" n = int(np.sum(mask))\n",
92-
" w = float(np.sum(fric[mask]))\n",
93-
" print(f\"{sid:>8.0f} {n:>6d} {w:>18.1f}\")"
94-
]
51+
"source": "def make_raster(data, res=1.0):\n \"\"\"Helper: create a DataArray with y/x coordinates.\"\"\"\n h, w = data.shape\n raster = xr.DataArray(\n data.astype(np.float64),\n dims=['y', 'x'],\n attrs={'res': (res, res)},\n )\n raster['y'] = np.arange(h) * res\n raster['x'] = np.arange(w) * res\n return raster\n\n\ndef plot_territories(alloc_arr, title, source_locs=None, ax=None):\n \"\"\"Plot a territory allocation with distinct colours per source.\"\"\"\n if ax is None:\n fig, ax = plt.subplots(figsize=(6, 5))\n data = alloc_arr.values if hasattr(alloc_arr, 'values') else alloc_arr\n ids = np.unique(data[np.isfinite(data)])\n colors = plt.cm.Set2(np.linspace(0, 1, max(len(ids), 3)))\n cmap = ListedColormap(colors[:max(len(ids), 2)])\n im = ax.imshow(data, cmap=cmap, origin='upper', interpolation='nearest')\n ax.set_title(title)\n if source_locs is not None:\n for r, c in source_locs:\n ax.plot(c, r, 'k*', markersize=12)\n plt.colorbar(im, ax=ax, shrink=0.8)\n return ax\n\n\ndef territory_stats(alloc_arr, friction_arr):\n \"\"\"Print cell count and cost-weighted area per territory.\"\"\"\n alloc = alloc_arr.values if hasattr(alloc_arr, 'values') else alloc_arr\n fric = friction_arr.values if hasattr(friction_arr, 'values') else friction_arr\n ids = sorted(np.unique(alloc[np.isfinite(alloc)]))\n print(f\"{'Source':>8} {'Cells':>6} {'Cost-weighted area':>18}\")\n print('-' * 38)\n for sid in ids:\n mask = alloc == sid\n n = int(np.sum(mask))\n w = float(np.sum(fric[mask]))\n print(f\"{sid:>8.0f} {n:>6d} {w:>18.1f}\")"
9552
},
9653
{
9754
"cell_type": "markdown",
@@ -370,4 +327,4 @@
370327
},
371328
"nbformat": 4,
372329
"nbformat_minor": 4
373-
}
330+
}

examples/user_guide/27_Stream_Analysis_Dinf_MFD.ipynb

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,7 @@
4545
"execution_count": null,
4646
"metadata": {},
4747
"outputs": [],
48-
"source": [
49-
"W, H = 400, 400\n",
50-
"cvs_x = np.linspace(0, 1000, W)\n",
51-
"cvs_y = np.linspace(0, 1000, H)\n",
52-
"\n",
53-
"terrain = generate_terrain(x_range=(0, 1000), y_range=(0, 1000),\n",
54-
" width=W, height=H)\n",
55-
"\n",
56-
"fig, ax = plt.subplots(figsize=(6, 5))\n",
57-
"terrain.plot(ax=ax, cmap='terrain')\n",
58-
"ax.set_title('Synthetic elevation')\n",
59-
"ax.set_aspect('equal')\n",
60-
"plt.tight_layout()\n",
61-
"plt.show()"
62-
]
48+
"source": "W, H = 400, 400\n\ntemplate = xr.DataArray(np.zeros((H, W)), dims=['y', 'x'],\n coords={'y': np.linspace(0, 1000, H),\n 'x': np.linspace(0, 1000, W)})\n\nterrain = generate_terrain(template, x_range=(0, 1000), y_range=(0, 1000))\n\nfig, ax = plt.subplots(figsize=(6, 5))\nterrain.plot(ax=ax, cmap='terrain')\nax.set_title('Synthetic elevation')\nax.set_aspect('equal')\nplt.tight_layout()\nplt.show()"
6349
},
6450
{
6551
"cell_type": "markdown",
@@ -246,4 +232,4 @@
246232
},
247233
"nbformat": 4,
248234
"nbformat_minor": 4
249-
}
235+
}

examples/user_guide/28_Rasterize.ipynb

Lines changed: 4 additions & 57 deletions
Large diffs are not rendered by default.

examples/user_guide/2_Proximity.ipynb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,7 @@
177177
}
178178
},
179179
"outputs": [],
180-
"source": [
181-
"line_agg = cvs.line(df, x='x', y='y')\n",
182-
"line_shaded = dynspread(shade(line_agg, cmap=['salmon', 'salmon']),\n",
183-
" threshold=1,\n",
184-
" max_px=2)\n",
185-
"set_background(line_shaded, 'black')"
186-
]
180+
"source": "line_agg = cvs.line(df, x='x', y='y')\nline_agg = line_agg.astype(int)\nline_shaded = dynspread(shade(line_agg, cmap=['salmon', 'salmon']),\n threshold=1,\n max_px=2)\nset_background(line_shaded, 'black')"
187181
},
188182
{
189183
"cell_type": "markdown",

0 commit comments

Comments
 (0)