-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_images.py
More file actions
174 lines (153 loc) · 6.12 KB
/
generate_images.py
File metadata and controls
174 lines (153 loc) · 6.12 KB
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
#!/usr/bin/env python3
import os
from pathlib import Path
os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib")
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
OUT_DIR = Path(__file__).resolve().parent.parent / "docs" / "images"
DIAGRAMS = [
{
"slug": "parser_sequence",
"title": "fin/parser.py",
"actors": [
"User",
"parser.main()",
"parse_file()",
"Input File",
"Regex",
"plot.show()",
"pandas",
"matplotlib",
],
"messages": [
("User", "parser.main()", "run parser with filename"),
("parser.main()", "parse_file()", "parse_file(file_path)"),
("parse_file()", "Input File", "open and iterate lines"),
("Input File", "parse_file()", "line"),
("parse_file()", "Regex", "match TOTAL amount"),
("parse_file()", "Regex", "match date"),
("parse_file()", "parse_file()", "append amount and date"),
("parse_file()", "parser.main()", "dates, dollars"),
("parser.main()", "User", "print extracted totals"),
("parser.main()", "plot.show()", "show(dates, dollars)"),
("plot.show()", "pandas", "build DataFrame"),
("plot.show()", "matplotlib", "render line chart"),
("matplotlib", "User", "display plot window"),
],
},
{
"slug": "four_percent_rule_sequence",
"title": "fin/four_percent_rule.py",
"actors": ["User", "main()", "argparse", "Projection Loop"],
"messages": [
("User", "main()", "run script with optional flags"),
("main()", "argparse", "parse_args()"),
("argparse", "main()", "balance, age, growth, withdrawal,\ninflation, years"),
("main()", "main()", "compute first-year withdrawal"),
("main()", "User", "print projection header"),
("main()", "Projection Loop", "compute growth and end balance"),
("Projection Loop", "main()", "yearly values"),
("main()", "User", "print row"),
("main()", "main()", "inflate next withdrawal"),
("main()", "User", "print ending balance"),
],
},
{
"slug": "pdf_sequence",
"title": "fin/pdf.py",
"actors": [
"User",
"pdf.main()",
"extract_pdf_text()",
"PdfReader",
"split_sections()",
"extract_transactions()",
"extract_descriptions()",
"normalize_payee()",
"CSV writers",
],
"messages": [
("User", "pdf.main()", "run pdf.py"),
("pdf.main()", "extract_pdf_text()", "extract_pdf_text(pdf_path)"),
("extract_pdf_text()", "PdfReader", "load PDF pages"),
("PdfReader", "extract_pdf_text()", "extracted text"),
("extract_pdf_text()", "pdf.main()", "full_text"),
("pdf.main()", "split_sections()", "split_sections(full_text)"),
("split_sections()", "pdf.main()", "transactions_text,\ndescriptions_text"),
("pdf.main()", "extract_transactions()", "extract withdrawal rows"),
("extract_transactions()", "pdf.main()", "transactions"),
("pdf.main()", "extract_descriptions()", "extract payee descriptions"),
("extract_descriptions()", "pdf.main()", "descriptions"),
("pdf.main()", "normalize_payee()", "normalize each description"),
("normalize_payee()", "pdf.main()", "canonical payee"),
("pdf.main()", "CSV writers", "write 3 CSV outputs"),
("pdf.main()", "User", "print totals and output paths"),
],
},
]
def draw_sequence_diagram(
title: str,
actors: list[str],
messages: list[tuple[str, str, str]],
output_path: Path,
) -> None:
fig_width = max(12, len(actors) * 1.8)
fig_height = max(8, len(messages) * 0.75 + 2.5)
fig, ax = plt.subplots(figsize=(fig_width, fig_height), dpi=180)
ax.set_xlim(0, len(actors) + 1)
ax.set_ylim(len(messages) + 2, 0)
ax.axis("off")
x_positions = {actor: idx + 1 for idx, actor in enumerate(actors)}
for actor, xpos in x_positions.items():
ax.text(
xpos,
0.6,
actor,
ha="center",
va="center",
fontsize=10,
fontweight="bold",
bbox={"boxstyle": "round,pad=0.3", "facecolor": "#f3f4f6", "edgecolor": "#374151"},
)
ax.plot([xpos, xpos], [1.0, len(messages) + 1], linestyle="--", linewidth=1, color="#9ca3af")
for row, (src, dst, label) in enumerate(messages, start=1):
y = row + 0.4
x1 = x_positions[src]
x2 = x_positions[dst]
if src == dst:
loop_width = 0.35
ax.plot([x1, x1 + loop_width], [y, y], color="#2563eb", linewidth=1.6)
ax.plot([x1 + loop_width, x1 + loop_width], [y, y + 0.3], color="#2563eb", linewidth=1.6)
ax.annotate(
"",
xy=(x1, y + 0.3),
xytext=(x1 + loop_width, y + 0.3),
arrowprops={"arrowstyle": "->", "lw": 1.6, "color": "#2563eb"},
)
ax.text(x1 + loop_width + 0.05, y + 0.15, label, fontsize=8.5, va="center", color="#111827")
continue
ax.annotate(
"",
xy=(x2, y),
xytext=(x1, y),
arrowprops={"arrowstyle": "->", "lw": 1.6, "color": "#2563eb"},
)
ax.text((x1 + x2) / 2, y - 0.12, label, ha="center", va="bottom", fontsize=8.5, color="#111827")
ax.set_title(title, fontsize=14, fontweight="bold", pad=18)
fig.tight_layout()
output_path.parent.mkdir(parents=True, exist_ok=True)
fig.savefig(output_path, bbox_inches="tight")
plt.close(fig)
def main() -> None:
for diagram in DIAGRAMS:
output_path = OUT_DIR / f"{diagram['slug']}.png"
draw_sequence_diagram(
diagram["title"],
diagram["actors"],
diagram["messages"],
output_path,
)
print(f"Wrote: {output_path}")
if __name__ == "__main__":
main()