add one-time script to trim score_details_v3 crps_data to totals#274
Closed
Thykof wants to merge 1 commit into
Closed
add one-time script to trim score_details_v3 crps_data to totals#274Thykof wants to merge 1 commit into
Thykof wants to merge 1 commit into
Conversation
PR #273 changed the live writer to keep only the per-interval "Total" rows, an aggregate "Gaps"/"Total" row, and the "Overall"/"Total" row in detailed_crps_data, dropping the verbose per-increment entries. This adds verify/trim_score_details_v3.py to rewrite the historical miner_scores.score_details_v3 rows into that same shape so the column is uniform and far smaller. The aggregate gap total is reconstructed by summing the per-interval "Total" of every interval whose name ends with "_gaps" — the same value the new writer accumulates. Keyset-paginated by id, idempotent (re-running recomputes the identical list and skips the write), and dry-run by default (--apply to persist). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a one-time, keyset-paginated migration script to shrink historical miner_scores.score_details_v3["crps_data"] to the same “totals-only (+ optional Gaps aggregate + Overall)” shape, plus unit tests for the trimming helper.
Changes:
- Added
verify/trim_score_details_v3.pyCLI to scan/updateMinerScore.score_details_v3in batches (dry-run by default,--applyto persist). - Implemented
trim_crps_data()to drop per-increment entries, keep per-intervalTotalrows, reconstruct theGapsaggregate, and keep the finalOveralltotal. - Added unit tests for trimming behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
verify/trim_score_details_v3.py |
Implements the trimming logic and a batch migration runner for historical DB rows. |
tests/test_trim_score_details_v3.py |
Adds unit tests validating trimming order, gap reconstruction, and idempotency. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| from synth.db.models import MinerScore, get_engine | ||
|
|
||
| load_dotenv() |
Comment on lines
+36
to
+45
| def trim_crps_data(crps_data: list[dict]) -> list[dict]: | ||
| """Reduce a verbose ``crps_data`` list to the post-#273 shape. | ||
|
|
||
| Keeps the per-interval ``Total`` rows, appends a reconstructed | ||
| ``Gaps``/``Total`` aggregate, then the ``Overall``/``Total`` row — matching | ||
| the order the live writer produces. Error payloads (e.g. | ||
| ``[{"error": ...}]``) and anything without ``Total`` rows are left as-is. | ||
| """ | ||
| if not isinstance(crps_data, list): | ||
| return crps_data |
Comment on lines
+55
to
+66
| gap_total = sum( | ||
| d["CRPS"] | ||
| for d in totals | ||
| if str(d.get("Interval", "")).endswith("_gaps") | ||
| ) | ||
|
|
||
| overall = [d for d in totals if d.get("Interval") == "Overall"] | ||
| trimmed = [d for d in totals if d.get("Interval") != "Overall"] | ||
| if gap_total > 0: | ||
| trimmed.append( | ||
| {"Interval": "Gaps", "Increment": "Total", "CRPS": gap_total} | ||
| ) |
Comment on lines
+101
to
+107
| if apply: | ||
| for score_id, new_details in updates: | ||
| connection.execute( | ||
| update(MinerScore) | ||
| .where(MinerScore.id == score_id) | ||
| .values(score_details_v3=new_details) | ||
| ) |
| twice = trim_crps_data(once) | ||
|
|
||
| self.assertEqual(once, twice) | ||
|
|
|
|
||
| Historical rows stored a verbose ``crps_data`` list with one entry per scoring | ||
| increment (``Increment`` = 1, 2, 3, ...) alongside the per-interval ``Total`` | ||
| rows. PR #273 changed the live writer (``crps_calculation.py``) to keep only: |
Comment on lines
+71
to
+73
| def main(apply: bool, batch_size: int) -> None: | ||
| engine = get_engine() | ||
| after_id = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR #273 changed the live writer to keep only the per-interval "Total" rows, an aggregate "Gaps"/"Total" row, and the "Overall"/"Total" row in detailed_crps_data, dropping the verbose per-increment entries.
This adds verify/trim_score_details_v3.py to rewrite the historical miner_scores.score_details_v3 rows into that same shape so the column is uniform and far smaller. The aggregate gap total is reconstructed by summing the per-interval "Total" of every interval whose name ends with "_gaps" — the same value the new writer accumulates.
Keyset-paginated by id, idempotent (re-running recomputes the identical list and skips the write), and dry-run by default (--apply to persist).