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" ,
370327 },
371328 "nbformat" : 4 ,
372329 "nbformat_minor" : 4
373- }
330+ }
0 commit comments