-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_report.py
More file actions
303 lines (273 loc) · 10.6 KB
/
Copy pathcompute_report.py
File metadata and controls
303 lines (273 loc) · 10.6 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# ---
# title: GEE Compute Usage Report
# author: Brendan Casey
# created: 2026-07-10
# notes:
# Collects Earth Engine compute usage information and
# writes it to a plain-text report. Three signals are
# captured:
#
# 1. Per-algorithm EECU profiles (ee.profilePrinting)
# for interactive computations, i.e., anything that
# forces evaluation such as reduceRegion().getInfo().
# Rows with the highest EECU-s are the compute choke
# points.
# 2. Total batch EECU-seconds for export tasks, read
# from the task status after completion.
# 3. Warnings and errors for troubleshooting: exceptions
# raised inside a profiled section (with traceback),
# Python warnings emitted there (e.g., EE deprecation
# notices), failed export tasks, and manual notes added
# with log_warning(). All are echoed to the console as
# they happen and collected into an "Issues detected"
# summary at the top of the report.
#
# Only evaluated results and batch tasks consume
# EECUs, so profile sections must contain a
# getInfo()-style call to produce output. To find
# choke points cheaply, run the pipeline on a
# small test AOI with profiling on, then
# extrapolate.
# ---
import datetime
import io
import os
import sys
import time
import traceback
import warnings
from contextlib import contextmanager
import ee
class ComputeReport:
"""Collect EE compute usage and write a txt report.
Besides EECU profiles, the report captures anything that
helps troubleshoot a run: exceptions raised inside a
profiled section, Python warnings emitted there (e.g., EE
deprecation notices), failed export tasks, and any manual
notes added with log_warning(). All of these are echoed to
the console as they happen and collected into an "Issues
detected" summary at the top of the report file.
Args:
name (str): Report name, used in the output file
name (e.g., the script name).
out_dir (str): Directory for the report file.
Defaults to the repo-root 'gee_compute_reports'
directory, resolved from this module's location
so it is independent of the working directory
VS Code runs a script from.
enabled (bool): If False, all methods are no-ops
so calling code needs no conditionals.
"""
def __init__(self, name, out_dir=None, enabled=True):
self.name = name
self.enabled = enabled
if out_dir is None:
# Repo root is two levels up from this module
# (python/utils/compute_report.py), so reports
# always land in the project's top-level
# gee_compute_reports/ regardless of cwd.
repo_root = os.path.dirname(
os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
)
)
out_dir = os.path.join(
repo_root, "gee_compute_reports"
)
self.out_dir = out_dir
self._blocks = []
# Short, high-signal lines surfaced in the report
# header so failures/warnings are easy to spot.
self._issues = []
def _record_issue(self, level, message):
"""Note a warning/error and echo it to the console.
Args:
level (str): "WARNING" or "ERROR".
message (str): One-line summary of the issue.
"""
line = f"[{level}] {message}"
self._issues.append(line)
print(line, file=sys.stderr)
def log_warning(self, message):
"""Record a manual troubleshooting note.
Use for conditions the script detects itself (empty
collection, suspicious stats, skipped step) that
would not otherwise raise. No-op when disabled.
Args:
message (str): The note to record.
"""
if not self.enabled:
return
self._record_issue("WARNING", message)
self._blocks.append(f"--- Warning ---\n{message}\n")
@contextmanager
def section(self, name, raise_on_error=True):
"""Profile a block of code and record EECU usage.
Wraps the block in ee.profilePrinting so every
computation evaluated inside it (getInfo, etc.)
is profiled per algorithm.
Python warnings emitted in the block are captured and
recorded. If the block raises (e.g., an EEException
from a getInfo call), the error and traceback are
recorded against this section and echoed to the
console.
Args:
name (str): Section label used in the report.
raise_on_error (bool): If True (default), a failure
is re-raised after being recorded, so the
script still fails loudly. Set False for
optional diagnostic blocks (e.g., a min/max
preview) whose failure should not abort the
run; the error is recorded and execution
continues past the block. When False, code
after the block must tolerate a result the
block never assigned (initialize it first).
"""
if not self.enabled:
yield
return
buf = io.StringIO()
start = time.time()
status = "OK"
error_text = None
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
try:
with ee.profilePrinting(destination=buf):
yield
except Exception as exc:
status = "FAILED"
error_text = (
f"{type(exc).__name__}: {exc}\n"
f"{traceback.format_exc()}"
)
self._record_issue(
"ERROR",
f"Section '{name}' failed: "
f"{type(exc).__name__}: {exc}",
)
if raise_on_error:
raise
finally:
elapsed = time.time() - start
profile = buf.getvalue().strip()
if not profile:
profile = (
"No server-side computation was "
"evaluated in this section. Add a "
"getInfo()-style call to profile it."
)
block = (
f"--- Section: {name} ---\n"
f"Status: {status}\n"
f"Wall time: {elapsed:.1f} s\n"
)
for w in caught:
warn_msg = f"{w.category.__name__}: {w.message}"
self._record_issue(
"WARNING",
f"Section '{name}': {warn_msg}",
)
block += f"Warning: {warn_msg}\n"
if error_text is not None:
block += f"Error:\n{error_text}\n"
block += (
f"EECU profile (highest compute first):\n"
f"{profile}\n"
)
self._blocks.append(block)
def log_task(self, task, poll_interval=30):
"""Wait for an export task and record its EECU use.
Blocks until the task finishes. Batch EECU totals
are only available on completed tasks.
Args:
task (ee.batch.Task): A started export task.
poll_interval (int): Seconds between checks.
"""
if not self.enabled:
return
description = task.config.get(
"description", task.id
)
print(
f"Waiting for task '{description}' to record "
f"compute usage..."
)
while task.active():
time.sleep(poll_interval)
status = task.status()
eecu = status.get("batch_eecu_usage_seconds")
start_ms = status.get("start_timestamp_ms")
update_ms = status.get("update_timestamp_ms")
runtime = (
f"{(update_ms - start_ms) / 1000:.0f} s"
if start_ms and update_ms
else "unknown"
)
eecu_text = (
f"{eecu:.1f}" if eecu is not None else "unavailable"
)
block = (
f"--- Export task: {description} ---\n"
f"State: {status['state']}\n"
f"Runtime: {runtime}\n"
f"Batch EECU-seconds: {eecu_text}\n"
)
if status["state"] == "FAILED":
error_message = status.get("error_message")
block += f"Error: {error_message}\n"
self._record_issue(
"ERROR",
f"Export '{description}' failed: "
f"{error_message}",
)
self._blocks.append(block)
def write(self):
"""Write collected blocks to a timestamped txt file.
Returns:
str: Path to the report file, or None if the
report is disabled or empty.
"""
if not self.enabled or not self._blocks:
return None
os.makedirs(self.out_dir, exist_ok=True)
timestamp = datetime.datetime.now().strftime(
"%Y%m%d_%H%M%S"
)
path = os.path.join(
self.out_dir,
f"{self.name}_compute_{timestamp}.txt",
)
header = (
f"{'=' * 60}\n"
f"GEE Compute Usage Report: {self.name}\n"
f"Generated: "
f"{datetime.datetime.now():%Y-%m-%d %H:%M:%S}\n"
f"{'=' * 60}\n\n"
"How to read this report:\n"
"- EECU-s = Earth Engine Compute Unit seconds.\n"
"- In section profiles, rows are sorted by\n"
" compute; the top rows are the choke points.\n"
"- 'Count' is how many times an algorithm ran;\n"
" high counts suggest repeated work that\n"
" could be cached or restructured.\n"
"- Batch EECU-seconds is the total compute\n"
" consumed by an export task.\n"
"- Issues detected (below) lists warnings and\n"
" errors captured during the run; each points\n"
" to the section or export where it occurred.\n\n"
)
if self._issues:
header += (
f"{'-' * 60}\n"
f"Issues detected ({len(self._issues)}):\n"
)
header += "\n".join(self._issues) + "\n"
header += f"{'-' * 60}\n\n"
else:
header += "No warnings or errors were captured.\n\n"
with open(path, "w") as f:
f.write(header)
f.write("\n".join(self._blocks))
print(f"Compute report written to: {path}")
return path