trace-artifact / figures /make_segment_alignment.py
idacy's picture
TRACE artifact: framework, corpus, instrumented case, provider case, evaluators, figures
2955ecc verified
Raw
History Blame Contribute Delete
6.47 kB
#!/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()