File size: 6,465 Bytes
2955ecc | 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 | #!/usr/bin/env python3
"""Figure 1: scope-and-time alignment into atomic local segments."""
from __future__ import annotations
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from style import (
BLUE,
FULL_WIDTH_IN,
INK,
LIGHT_GREY,
MID_GREY,
ORANGE,
PALE_BLUE,
PALE_GREY,
SMALL_TEXT_PT,
HERE,
save_figure,
)
SPEC = HERE / "specs" / "segment_example.json"
OUTPUT = HERE / "fig_segment_alignment.pdf"
def load_spec() -> dict:
data = json.loads(SPEC.read_text(encoding="utf-8"))
if data.get("schema_version") != "1" or data.get("artifact_status") != "schematic":
raise ValueError("unexpected segment-example schema or status")
if data.get("time_points") != [f"t{i}" for i in range(7)]:
raise ValueError("the figure expects the declared t0,...,t6 cut points")
if data.get("selected_segment") != [3, 4]:
raise ValueError("selected segment changed; review the visual specification")
if [lane["id"] for lane in data["validity_lanes"]] != [
"inventory",
"maintenance",
"reservation",
"source_validity",
]:
raise ValueError("validity-lane contract changed")
if [lane["id"] for lane in data["observation_lanes"]] != [
"activity",
"fabric",
"storage",
]:
raise ValueError("observation-lane contract changed")
return data
def scope_label(value: str) -> str:
return value.replace(" -> ", " \u2192 ")
def main() -> None:
data = load_spec()
fig, ax = plt.subplots(figsize=(FULL_WIDTH_IN, 2.18))
fig.subplots_adjust(left=0.018, right=0.992, bottom=0.12, top=0.96)
ax.set_xlim(-2.03, 6.12)
ax.set_ylim(-0.15, 8.05)
ax.axis("off")
# One selected atomic segment, behind every lane.
ax.add_patch(
Rectangle(
(3.0, 0.42),
1.0,
7.0,
facecolor=PALE_BLUE,
edgecolor="none",
zorder=0,
)
)
lane_y = {
"inventory": 7.0,
"maintenance": 6.0,
"reservation": 5.0,
"source_validity": 4.0,
"activity": 3.0,
"fabric": 2.0,
"storage": 1.0,
}
# The union of all declared changes forms the segment boundaries.
for cut in range(7):
ax.plot(
[cut, cut],
[0.42, 7.40],
color=LIGHT_GREY,
lw=0.55,
zorder=1,
)
ax.text(
cut,
7.55,
rf"$t_{cut}$",
ha="center",
va="bottom",
fontsize=SMALL_TEXT_PT,
)
ax.text(-1.98, 7.55, "record lane", ha="left", va="bottom", fontsize=SMALL_TEXT_PT)
ax.text(-0.16, 7.55, "scope", ha="right", va="bottom", fontsize=SMALL_TEXT_PT)
validity_colours = {
"inventory": PALE_GREY,
"maintenance": "#F1D9BE",
"reservation": "#DFE7EC",
"source_validity": PALE_GREY,
}
validity_edges = {
"inventory": MID_GREY,
"maintenance": ORANGE,
"reservation": BLUE,
"source_validity": MID_GREY,
}
for lane in data["validity_lanes"]:
y = lane_y[lane["id"]]
ax.text(-1.98, y, lane["label"], ha="left", va="center")
ax.text(-0.16, y, scope_label(lane["scope"]), ha="right", va="center")
for start, end, label in lane["intervals"]:
ax.add_patch(
Rectangle(
(start, y - 0.18),
end - start,
0.36,
facecolor=validity_colours[lane["id"]],
edgecolor=validity_edges[lane["id"]],
lw=0.75,
zorder=2,
)
)
ax.text(
(start + end) / 2,
y,
label,
ha="center",
va="center",
fontsize=SMALL_TEXT_PT,
zorder=3,
)
for lane in data["observation_lanes"]:
y = lane_y[lane["id"]]
ax.text(-1.98, y, lane["label"], ha="left", va="center")
ax.text(-0.16, y, scope_label(lane["scope"]), ha="right", va="center")
if lane["kind"] == "point":
positions = lane["positions"]
if lane["id"] == "storage":
ax.vlines(positions, y - 0.15, y + 0.15, color=BLUE, lw=1.25, zorder=4)
else:
ax.scatter(
positions,
[y] * len(positions),
s=14,
marker="o",
facecolor=BLUE,
edgecolor=BLUE,
lw=0.4,
zorder=4,
)
elif lane["kind"] == "interval":
for start, end in lane["intervals"]:
ax.add_patch(
Rectangle(
(start, y - 0.11),
end - start,
0.22,
facecolor=BLUE,
edgecolor=BLUE,
lw=0.5,
zorder=4,
)
)
else:
raise ValueError(f"unsupported observation kind: {lane['kind']}")
unmapped = data["unmapped_observation"]
y = lane_y[unmapped["lane"]]
ax.scatter(
[unmapped["position"]],
[y + 0.31],
s=21,
marker="x",
color=MID_GREY,
lw=0.9,
zorder=5,
)
ax.text(
unmapped["position"] + 0.055,
y + 0.31,
unmapped["label"],
ha="left",
va="center",
color=MID_GREY,
fontsize=SMALL_TEXT_PT,
)
# Segment row: no arrows, only the atomic intervals implied by the cuts.
ax.text(-1.98, 0.08, "local segments", ha="left", va="center")
for index in range(6):
ax.plot([index + 0.04, index + 0.96], [0.08, 0.08], color=INK, lw=0.8)
ax.text(
index + 0.5,
0.08,
rf"$s_{index + 1}$",
ha="center",
va="center",
fontsize=SMALL_TEXT_PT,
bbox={"facecolor": "white", "edgecolor": "none", "pad": 0.6},
)
save_figure(
fig,
OUTPUT,
subject="Constructed alignment of datacenter records into atomic local segments",
)
if __name__ == "__main__":
main()
|