# -*- coding: utf-8 -*- """The 10 x 10 test grid of Figures 1-4 and its depth-first numbering. Two basins of 62 and 11 pixels, ArcGIS D8 codes, base threshold 4 pixels. number() gives every channel pixel its number, so that each basin, path, reach and upstream network is one range; the rest are drawing helpers. Imported by Fig01-Fig04; it draws nothing by itself. """ import sys import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt # noqa: F401 (Fig01-Fig04 take plt from here) from matplotlib.patches import Rectangle from toy_basin_style import BOLD, C_MAIN, C_TRIB, C_OTHER, T_DARK, T_GREY, FILL, F_OTHER, text_inches # noqa: F401 (also for Fig01-Fig04) FS, FT, LH = 10, 12, 0.165 # every text, every title (pt); line height (in) W, ML, MR = 7.48, 0.10, 0.06 # inches GW = 3.40; CELL = GW / 10 # one grid of 10 x 10 pixels (3.40 so that five panels fit one page) XB = W - MR - GW # left edge of (b) and (d) T_BASE = 4 # base threshold (pixels) PTS_ALL = (('A', 4, 'left'), ('B', 13, 'top'), ('C', 1, 'left'), ('D', 9, 'top'), ('E', 18, 'left')) # channel pixels by pixel_seq_id, the edge their letter sits on in (c) PTS = PTS_ALL[:2] Q_UP, Q_PATH = PTS[0][1], PTS[1][1] # (d): the channel pixels upstream of A, the flow path from B DRAFT = sys.argv[sys.argv.index('--draft') + 1] if '--draft' in sys.argv else None # ---------------------------------------------------------------- the test grid: D8 directions (ArcGIS codes) and the basin mask DIR = np.array([ [32, 32, 32, 2, 4, 8, 8, 8, 4, 8], [32, 32, 2, 2, 4, 8, 8, 8, 8, 8], [32, 2, 1, 2, 4, 8, 8, 8, 16, 16], [2, 2, 128, 2, 4, 8, 8, 16, 32, 32], [4, 2, 2, 2, 4, 8, 8, 8, 8, 8], [4, 2, 4, 2, 4, 8, 8, 8, 8, 8], [2, 1, 2, 4, 8, 8, 16, 16, 8, 8], [2, 2, 2, 4, 8, 8, 4, 8, 8, 8], [2, 0, 0, 0, 0, 8, 2, 4, 8, 8], [0, 0, 247, 247, 0, 0, 0, 0, 0, 0]]) MSK = np.array([ [0, 0, 0, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 0, 1, 1, 1, 1], [0, 0, 0, 1, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]]) OFF = {1: (0, 1), 2: (1, 1), 4: (1, 0), 8: (1, -1), 16: (0, -1), 32: (-1, -1), 64: (-1, 0), 128: (-1, 1)} def down(x): d = DIR[x] if d == 0: return None y = (x[0] + OFF[d][0], x[1] + OFF[d][1]) assert MSK[y], (x, y) # the directions stay inside the basins return y def is_up(q, x): while q is not None: if q == x: return True q = down(q) return False def basin_of(x): while down(x): x = down(x) return x def number(T): """FlowNest on the test basin at threshold T: drainage area, pixel_seq_id n, its upstream end o, reaches, paths, stored v.""" cells = [(r, c) for r in range(10) for c in range(10) if MSK[r, c]] up = {x: [] for x in cells} for x in cells: if down(x): up[down(x)].append(x) area = {} def acc(x): if x not in area: area[x] = 1 + sum(acc(u) for u in up[x]) return area[x] for x in cells: acc(x) assert all(area[x] == 1 + sum(area[u] for u in up[x]) for x in cells) ch = {x for x in cells if area[x] >= T} assert all(down(x) is None or down(x) in ch for x in ch) # the mask is closed downstream cup = {x: sorted((u for u in up[x] if u in ch), key=lambda u: (-area[u], u[0] * 10 + u[1])) for x in ch} n, o, reach, path = {}, {}, {}, {} R, P = [], [] # reaches [first, last, reach downstream, path]; paths: first number def walk(x, r, p): n[x] = len(n) + 1; reach[x] = r; path[x] = p; R[r - 1][1] = n[x] for i, u in enumerate(cup[x]): if len(cup[x]) == 1: walk(u, r, p); break R.append([len(n) + 1, None, r, p if i == 0 else len(P) + 1]) if i > 0: P.append(len(n) + 1) walk(u, len(R), R[-1][3]) o[x] = len(n) outs = sorted((x for x in cells if DIR[x] == 0), key=lambda x: -area[x]) basins = [] for x in outs: P.append(len(n) + 1); R.append([len(n) + 1, None, 0, len(P)]); b0 = len(n) + 1 walk(x, len(R), len(P)); basins.append((b0, len(n))) first = {} for x in cells: y = x while y not in ch: y = down(y) first[x] = y v = {x: n[x] if x in ch else -n[first[x]] for x in cells} inv = {k: x for x, k in n.items()} # every object is one range (3.2); the land of a reach is one range of |v| at the base threshold (3.3) for x in ch: assert {n[q] for q in ch if is_up(q, x)} == set(range(n[x], o[x] + 1)), x for i, (a, b, _, _) in enumerate(R, 1): assert {reach[inv[k]] for k in range(a, b + 1)} == {i} assert {abs(v[x]) for x in cells if reach[first[x]] == i} == set(range(a, b + 1)), i paths = [] for j in range(1, len(P) + 1): ks = sorted(n[x] for x in ch if path[x] == j); assert ks == list(range(ks[0], ks[-1] + 1)) and ks[0] == P[j - 1] paths.append((ks[0], ks[-1])) for b0, b1 in basins: assert len({basin_of(inv[k]) for k in range(b0, b1 + 1)}) == 1 return dict(cells=cells, area=area, ch=ch, n=n, o=o, R=R, P=P, paths=paths, reach=reach, path=path, v=v, first=first, basins=basins, outs=outs, cup=cup, inv=inv) # ---------------------------------------------------------------- colors: main stem blue, the longest other path orange, rest grey DARK = {'main': C_MAIN, 'trib': C_TRIB, 'other': C_OTHER} LIGHT = {'main': FILL['above'], 'trib': FILL['trib'], 'other': F_OTHER} # ---------------------------------------------------------------- grids def grid(ax, N, role, dark): ax.set_xlim(0, 10); ax.set_ylim(10, 0); ax.set_aspect('equal'); ax.axis('off') for r in range(10): for c in range(10): x = (r, c) if not MSK[x]: ax.add_patch(Rectangle((c, r), 1, 1, fc='white', ec='#e6e6e6', lw=0.5)); continue fc = 'white' if role is None else DARK[role(x)] if x in dark else LIGHT[role(x)] ax.add_patch(Rectangle((c, r), 1, 1, fc=fc, ec='#d9d9d9' if role is None else 'white', lw=0.8)) # ---------------------------------------------------------------- (d) the number line def flow_path_runs(N, k): """The numbers along the flow path from pixel k to the outlet, as runs of consecutive numbers (one per path crossed).""" seq, x = [], N['inv'][k] while x is not None: seq.append(N['n'][x]); x = down(x) runs = [[seq[0], seq[0]]] for a in seq[1:]: if a == runs[-1][0] - 1: runs[-1][0] = a else: runs.append([a, a]) for a, b in runs: assert len({N['path'][N['inv'][q]] for q in range(a, b + 1)}) == 1 return [tuple(r) for r in runs]