margin-data-storyteller 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- margin_data_storyteller-0.1.0/.gitignore +82 -0
- margin_data_storyteller-0.1.0/CHANGELOG.md +47 -0
- margin_data_storyteller-0.1.0/LICENSE +21 -0
- margin_data_storyteller-0.1.0/PKG-INFO +183 -0
- margin_data_storyteller-0.1.0/README.md +153 -0
- margin_data_storyteller-0.1.0/pyproject.toml +50 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/__init__.py +8 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/agent.py +150 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/cleaning.py +510 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/columns.py +627 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/facts/__init__.py +11 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/facts/base.py +333 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/facts/breakdown.py +230 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/facts/engine.py +342 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/facts/relations.py +438 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/facts/verify.py +71 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/ingest.py +246 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/layout.py +174 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/models.py +308 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/names.py +20 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/profile.py +363 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/py.typed +0 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/report.py +251 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/setup.py +252 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/story/__init__.py +6 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/story/charts.py +223 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/story/plan.py +224 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/story/template.py +102 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/story/tokens.py +98 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/story/validate.py +205 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/story/write.py +201 -0
- margin_data_storyteller-0.1.0/src/data_storyteller/values.py +335 -0
- margin_data_storyteller-0.1.0/tests/conftest.py +434 -0
- margin_data_storyteller-0.1.0/tests/test_agent.py +157 -0
- margin_data_storyteller-0.1.0/tests/test_cleaning.py +266 -0
- margin_data_storyteller-0.1.0/tests/test_facts.py +165 -0
- margin_data_storyteller-0.1.0/tests/test_ingest.py +109 -0
- margin_data_storyteller-0.1.0/tests/test_profile.py +132 -0
- margin_data_storyteller-0.1.0/tests/test_story.py +320 -0
- margin_data_storyteller-0.1.0/tests/test_values.py +98 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
/lib/
|
|
14
|
+
/lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
share/python-wheels/
|
|
20
|
+
*.egg-info/
|
|
21
|
+
.installed.cfg
|
|
22
|
+
*.egg
|
|
23
|
+
|
|
24
|
+
# Virtual environments
|
|
25
|
+
.venv/
|
|
26
|
+
venv/
|
|
27
|
+
ENV/
|
|
28
|
+
env/
|
|
29
|
+
|
|
30
|
+
# uv
|
|
31
|
+
.uv/
|
|
32
|
+
|
|
33
|
+
# Environment variables & secrets
|
|
34
|
+
.env
|
|
35
|
+
.env.local
|
|
36
|
+
.env.*.local
|
|
37
|
+
*.key
|
|
38
|
+
*.pem
|
|
39
|
+
|
|
40
|
+
# Testing & Coverage
|
|
41
|
+
.pytest_cache/
|
|
42
|
+
.coverage
|
|
43
|
+
.coverage.*
|
|
44
|
+
htmlcov/
|
|
45
|
+
.tox/
|
|
46
|
+
.nox/
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
|
|
51
|
+
# Type checking & linting
|
|
52
|
+
.mypy_cache/
|
|
53
|
+
.ruff_cache/
|
|
54
|
+
.dmypy.json
|
|
55
|
+
dmypy.json
|
|
56
|
+
|
|
57
|
+
# IDEs & Editors
|
|
58
|
+
.vscode/
|
|
59
|
+
.idea/
|
|
60
|
+
*.swp
|
|
61
|
+
*.swo
|
|
62
|
+
*~
|
|
63
|
+
|
|
64
|
+
# Operating System
|
|
65
|
+
.DS_Store
|
|
66
|
+
Thumbs.db
|
|
67
|
+
ehthumbs.db
|
|
68
|
+
desktop.ini
|
|
69
|
+
|
|
70
|
+
# Output / Reports
|
|
71
|
+
report.json
|
|
72
|
+
report.md
|
|
73
|
+
temp/
|
|
74
|
+
tmp/
|
|
75
|
+
|
|
76
|
+
# Private planning notes (kept locally, not published)
|
|
77
|
+
PLAN.md
|
|
78
|
+
# notes for Claude Code sessions; a CLAUDE.md in the repo is one of the detector's markers
|
|
79
|
+
CLAUDE.md
|
|
80
|
+
|
|
81
|
+
# the web UI bundled into margin-api at release time (scripts/build_release.py)
|
|
82
|
+
packages/api/src/margin_api/ui/
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this agent are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
|
|
5
|
+
[Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2026-10-29
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
- Reads CSV, TSV, Excel, Parquet, JSON and SQLite; detects encodings and delimiters, and
|
|
11
|
+
reads text files as text so nothing is guessed on the way in.
|
|
12
|
+
- Finds the table: skips title rows, combines column names split over two rows, names the
|
|
13
|
+
columns of a file without names, and makes every name unique.
|
|
14
|
+
- Cleans numbers (currency signs, percentages, separators, decimal commas, negatives in
|
|
15
|
+
brackets, Arabic-Indic digits), dates in any order, markers that mean no value, extra
|
|
16
|
+
spaces, different spellings of one label, empty rows and columns, total rows and
|
|
17
|
+
repeated rows.
|
|
18
|
+
- Asks, in one batch, about anything the file can't settle, with the evidence and a
|
|
19
|
+
suggested answer. Answers can be given up front, or every suggestion taken with
|
|
20
|
+
`assume_defaults`.
|
|
21
|
+
- A final check against the file: every value is in the result or left empty by a step
|
|
22
|
+
that says so, and every removed row is explained.
|
|
23
|
+
- Describes each column: its role, how its values add up and why, its unit, and whether it
|
|
24
|
+
looks like personal data (whose values are then left out of the report).
|
|
25
|
+
- `stop_after="story"` sets up the story: the main number, what one row is, the main date,
|
|
26
|
+
how unclear numbers add up, and which columns to focus on in a wide table.
|
|
27
|
+
- Writes the cleaned table as CSV and the report as JSON to `output_dir`.
|
|
28
|
+
- Finds the facts for the story: totals, changes over time (with incomplete first or last
|
|
29
|
+
periods left out), trends, breakdowns and concentration, groups that grew or shrank
|
|
30
|
+
(in total and per row), gaps between groups, correlations, anomalies (also at a finer
|
|
31
|
+
time grain, per day of each period), outlier rows and Simpson's paradox. p-values are
|
|
32
|
+
corrected for the number of tests.
|
|
33
|
+
- Every fact is recomputed in DuckDB from separately written SQL and must match within
|
|
34
|
+
0.0001, or the run fails.
|
|
35
|
+
- Link ids that repeat (a customer id in an orders table), columns of years, and 0/1 flags
|
|
36
|
+
are recognised.
|
|
37
|
+
- Writes the story: a second checkpoint to choose the insights and give context; pages laid
|
|
38
|
+
out by rules; a model writes with fact tokens instead of numbers; a validator refuses
|
|
39
|
+
typed numbers, unknown names, wrong directions, unsupported causes, unhedged
|
|
40
|
+
low-confidence claims and paradox-blind quotes, and sends the problems back twice at
|
|
41
|
+
most; a template writes the story when the model is missing or keeps failing.
|
|
42
|
+
- Charts for each story page, drawn from the facts: a line over time, bars, before-and-after
|
|
43
|
+
dots and a meter. Anomaly facts carry the series they were found in, also checked in
|
|
44
|
+
DuckDB.
|
|
45
|
+
- Its own result view in the web UI: the story page by page with its charts, every number
|
|
46
|
+
a button that opens its fact (method, rows, confidence, caveats, the SQL that checked
|
|
47
|
+
it), a table view for every chart, and how the data was cleaned.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 margin contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: margin-data-storyteller
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Cleans a table of data, shows every change it made, and tells the story in it with charts.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Waddah0-0/margin
|
|
6
|
+
Project-URL: Source, https://github.com/Waddah0-0/margin/tree/main/agents/data_storyteller
|
|
7
|
+
Project-URL: Issues, https://github.com/Waddah0-0/margin/issues
|
|
8
|
+
Author: Waddah Ali
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: agents,data-cleaning,data-storytelling,duckdb,llm,pandas
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Requires-Dist: duckdb>=1.1
|
|
23
|
+
Requires-Dist: margin-core<0.2,>=0.1.2
|
|
24
|
+
Requires-Dist: numpy>=1.26
|
|
25
|
+
Requires-Dist: openpyxl>=3.1
|
|
26
|
+
Requires-Dist: pandas>=2.2
|
|
27
|
+
Requires-Dist: pyarrow>=15
|
|
28
|
+
Requires-Dist: scipy>=1.12
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# Data Storyteller
|
|
32
|
+
|
|
33
|
+
Cleans a table of data, shows every change it made, and tells the story in it with charts.
|
|
34
|
+
|
|
35
|
+
> **Status: experimental.** Cleaning, facts, the written story and its charts work and are
|
|
36
|
+
> tested, also with a local model. The Power BI export is being built.
|
|
37
|
+
|
|
38
|
+
## What it does
|
|
39
|
+
|
|
40
|
+
Give it a spreadsheet or data file and it gives you back a clean table, with a report of
|
|
41
|
+
every change and the reason for it. Nothing changes silently: a value it can't read is
|
|
42
|
+
left empty and listed by row, a removed row is listed with the rule that removed it, and a
|
|
43
|
+
final check proves that every value in the file is either in the result or accounted for.
|
|
44
|
+
|
|
45
|
+
When the file can be read more than one way, it asks instead of guessing. Is `1.234` one
|
|
46
|
+
thousand two hundred and thirty-four, or one point two three four? Is `01/02/2024` the
|
|
47
|
+
first of February or the second of January? Is the row that says "Total" data? You answer
|
|
48
|
+
once, all questions together, and every answer is kept with the result.
|
|
49
|
+
|
|
50
|
+
It is for anyone who gets data from exports, other teams or other countries, and wants to
|
|
51
|
+
trust the numbers before working with them.
|
|
52
|
+
|
|
53
|
+
## How it works
|
|
54
|
+
|
|
55
|
+
1. **Read.** CSV, TSV, Excel, Parquet, JSON or SQLite. Text files are read as text, so
|
|
56
|
+
nothing is guessed on the way in (`007` stays `007`, `NA` stays `NA` until a step
|
|
57
|
+
decides what it means). The encoding and the delimiter are detected.
|
|
58
|
+
2. **Find the table.** Title rows above the data are skipped, column names split over two
|
|
59
|
+
rows are combined, and a file without column names gets `column_1`, `column_2`, ...
|
|
60
|
+
3. **Plan.** Each column is read as numbers, dates or text from its values, not its name.
|
|
61
|
+
The plan covers numbers written with currency signs, percent signs, thousands
|
|
62
|
+
separators, decimal commas, `(100)` for negatives, or Arabic-Indic digits; dates in any
|
|
63
|
+
order; markers like `N/A` or `#DIV/0!`; extra spaces; the same label spelled
|
|
64
|
+
differently; empty rows and columns; total rows; and repeated rows.
|
|
65
|
+
4. **Ask.** Only what the file can't answer, with the values that made it ask and a
|
|
66
|
+
suggested answer.
|
|
67
|
+
5. **Clean, then check** the result against the file.
|
|
68
|
+
6. **Describe** each column: what it holds, how its values add up (summed, averaged, or the
|
|
69
|
+
latest value), and whether it looks like personal data.
|
|
70
|
+
7. **Find the facts** (story mode): the totals; how the main number moves over time; where
|
|
71
|
+
it comes from and how concentrated it is; which groups grew or shrank; groups whose rates
|
|
72
|
+
differ; numbers that move together; periods that stand out; and two traps: a partial last
|
|
73
|
+
period (left out of every comparison) and Simpson's paradox. Each fact says how it was
|
|
74
|
+
computed, on how many rows, and how sure it is.
|
|
75
|
+
8. **Check every fact** by computing it again in DuckDB from SQL written separately. Any
|
|
76
|
+
disagreement fails the run.
|
|
77
|
+
9. **Choose the story** (the second checkpoint): the insights worth telling, best first,
|
|
78
|
+
and any context only you know, such as a price change or a campaign. Only that context
|
|
79
|
+
lets the story give a reason for something; otherwise it describes associations.
|
|
80
|
+
10. **Write the story.** Rules lay out the pages: how the main number moved, what drives
|
|
81
|
+
it, what else stands out, and hypotheses to test next. A model writes the words, but
|
|
82
|
+
never types a number: it refers to facts by id, and the numbers are filled in from the
|
|
83
|
+
facts. Before anything is shown, a validator refuses a draft that types a number, names
|
|
84
|
+
something not in the data, says "rose" for a fall, gives an unsupported cause, states a
|
|
85
|
+
low-confidence fact without hedging it, or quotes a change that Simpson's paradox makes
|
|
86
|
+
misleading. The model gets the problems back, twice at most; after that, or without a
|
|
87
|
+
model, the story is written from a template built from the facts' own sentences.
|
|
88
|
+
11. **Draw the charts** from the facts' own checked numbers, at most three a page: the main
|
|
89
|
+
number over time with standout periods marked, a breakdown with the group the sentence
|
|
90
|
+
is about in colour, a meter for concentration, before-and-after dots for groups that
|
|
91
|
+
changed, and the series around an anomaly. In the web UI every number in the story
|
|
92
|
+
opens the fact behind it, and every chart can be shown as a table.
|
|
93
|
+
|
|
94
|
+
## Input
|
|
95
|
+
|
|
96
|
+
| Field | Default | Meaning |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| `dataset` | required | Path to the file. The primary input. |
|
|
99
|
+
| `table` | asked | The sheet or table, when the file has more than one. |
|
|
100
|
+
| `stop_after` | `clean` | `clean`, or `story` to also set up the story (the main number, what one row is, how numbers add up), find its facts and write it. |
|
|
101
|
+
| `output_dir` | none | A folder for the cleaned table (`<name>.clean.csv`) and the report (`<name>.cleaning.json`). Nothing is written without it. |
|
|
102
|
+
| `answers` | `{}` | Answers given up front, by question id, for example `{"date_order:order_date": "dmy"}`. These are not asked. |
|
|
103
|
+
| `assume_defaults` | `false` | Ask nothing and take every suggested answer. Each one is marked as not confirmed. |
|
|
104
|
+
|
|
105
|
+
Question ids are stable, so a run can be repeated with the answers of an earlier one. They
|
|
106
|
+
are listed in the report under `answers`.
|
|
107
|
+
|
|
108
|
+
## Output
|
|
109
|
+
|
|
110
|
+
`data` holds:
|
|
111
|
+
|
|
112
|
+
- `source`: the file, its format, encoding, delimiter, tables, and where the column names
|
|
113
|
+
were found.
|
|
114
|
+
- `cleaning`: rows and columns in and out, every step (what it did, to which column, who
|
|
115
|
+
decided, examples before and after, values it could not read), and what the final check
|
|
116
|
+
verified.
|
|
117
|
+
- `columns`: for each column its type, role, how it adds up and why, its unit (a currency
|
|
118
|
+
or %), counts, range, most common values, and notes.
|
|
119
|
+
- `setup`: with `stop_after="story"`, the main number, what one row is, the main date, how
|
|
120
|
+
each number adds up, and the columns to focus on.
|
|
121
|
+
- `facts`: with `stop_after="story"`, the time axis, the periods left out as partial, and
|
|
122
|
+
every fact: its number, a plain sentence, how it was computed, rows behind it, confidence,
|
|
123
|
+
caveats, how much it matters, the SQL that recomputed it, and whether it matched.
|
|
124
|
+
- `story`: with `stop_after="story"`, the title, shape and pages (title, headline, body,
|
|
125
|
+
actions to test, charts, and the facts behind each sentence, with each number marked by
|
|
126
|
+
the fact it came from), who wrote it (`model` or `template`), how many drafts the model
|
|
127
|
+
wrote, and what the validator refused. A chart is a kind (`line`, `bars`, `dumbbell` or
|
|
128
|
+
`meter`), a title, its fact, and its points.
|
|
129
|
+
- `files`: the files written to `output_dir`.
|
|
130
|
+
|
|
131
|
+
Findings: in story mode, the story first, one finding per page; then one per change, each
|
|
132
|
+
with its evidence; one per column note (personal data, a column that is mostly empty); and
|
|
133
|
+
the final check.
|
|
134
|
+
|
|
135
|
+
## Limits
|
|
136
|
+
|
|
137
|
+
- One table per run. Tables are not joined.
|
|
138
|
+
- Files up to 500 MB, read into memory.
|
|
139
|
+
- Personal data is detected from column names and value patterns, so it can miss some. It
|
|
140
|
+
stays in the cleaned file; only this report leaves it out.
|
|
141
|
+
- How a number adds up is guessed from its name (sales add up, prices are averaged). A
|
|
142
|
+
name that says nothing is asked about only in story mode.
|
|
143
|
+
- Facts describe associations. A correlation or a gap between groups is never stated as a
|
|
144
|
+
cause. Groups under 30 rows are not compared, and p-values are corrected for the number of
|
|
145
|
+
tests.
|
|
146
|
+
- The model only ever sees facts (aggregates) and column names, never a row of data or a
|
|
147
|
+
personal-data column. With a hosted model (Anthropic or OpenAI) those facts are sent to
|
|
148
|
+
that company; the default is a model on your own machine.
|
|
149
|
+
- Old `.xls` workbooks are not supported; save them as `.xlsx` or CSV. Excel formulas are
|
|
150
|
+
never run: the values Excel last saved are used.
|
|
151
|
+
|
|
152
|
+
## Usage
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
pip install margin-cli margin-data-storyteller
|
|
156
|
+
margin run data-storyteller sales.xlsx --set output_dir=clean
|
|
157
|
+
margin run data-storyteller sales.csv --set 'answers={"decimal:amount": "comma"}'
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
In a terminal the questions are asked there; in the web UI they appear on the run page.
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
import asyncio
|
|
164
|
+
|
|
165
|
+
from margin_core import run_agent
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
async def main() -> None:
|
|
169
|
+
result = await run_agent("data-storyteller", {"dataset": "sales.csv", "assume_defaults": True})
|
|
170
|
+
print(result.summary)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
asyncio.run(main())
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Development
|
|
177
|
+
|
|
178
|
+
This agent lives in the [margin](https://github.com/Waddah0-0/margin) monorepo, in
|
|
179
|
+
[`agents/data_storyteller`](https://github.com/Waddah0-0/margin/tree/main/agents/data_storyteller). If you are reading this in its mirror
|
|
180
|
+
repository, that copy is read-only: open issues and pull requests in the main repository.
|
|
181
|
+
|
|
182
|
+
Run its tests with `uv run pytest agents/data_storyteller`. The messy files the tests use
|
|
183
|
+
are built by `tests/conftest.py`, byte for byte, so encodings and line endings stay exact.
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# Data Storyteller
|
|
2
|
+
|
|
3
|
+
Cleans a table of data, shows every change it made, and tells the story in it with charts.
|
|
4
|
+
|
|
5
|
+
> **Status: experimental.** Cleaning, facts, the written story and its charts work and are
|
|
6
|
+
> tested, also with a local model. The Power BI export is being built.
|
|
7
|
+
|
|
8
|
+
## What it does
|
|
9
|
+
|
|
10
|
+
Give it a spreadsheet or data file and it gives you back a clean table, with a report of
|
|
11
|
+
every change and the reason for it. Nothing changes silently: a value it can't read is
|
|
12
|
+
left empty and listed by row, a removed row is listed with the rule that removed it, and a
|
|
13
|
+
final check proves that every value in the file is either in the result or accounted for.
|
|
14
|
+
|
|
15
|
+
When the file can be read more than one way, it asks instead of guessing. Is `1.234` one
|
|
16
|
+
thousand two hundred and thirty-four, or one point two three four? Is `01/02/2024` the
|
|
17
|
+
first of February or the second of January? Is the row that says "Total" data? You answer
|
|
18
|
+
once, all questions together, and every answer is kept with the result.
|
|
19
|
+
|
|
20
|
+
It is for anyone who gets data from exports, other teams or other countries, and wants to
|
|
21
|
+
trust the numbers before working with them.
|
|
22
|
+
|
|
23
|
+
## How it works
|
|
24
|
+
|
|
25
|
+
1. **Read.** CSV, TSV, Excel, Parquet, JSON or SQLite. Text files are read as text, so
|
|
26
|
+
nothing is guessed on the way in (`007` stays `007`, `NA` stays `NA` until a step
|
|
27
|
+
decides what it means). The encoding and the delimiter are detected.
|
|
28
|
+
2. **Find the table.** Title rows above the data are skipped, column names split over two
|
|
29
|
+
rows are combined, and a file without column names gets `column_1`, `column_2`, ...
|
|
30
|
+
3. **Plan.** Each column is read as numbers, dates or text from its values, not its name.
|
|
31
|
+
The plan covers numbers written with currency signs, percent signs, thousands
|
|
32
|
+
separators, decimal commas, `(100)` for negatives, or Arabic-Indic digits; dates in any
|
|
33
|
+
order; markers like `N/A` or `#DIV/0!`; extra spaces; the same label spelled
|
|
34
|
+
differently; empty rows and columns; total rows; and repeated rows.
|
|
35
|
+
4. **Ask.** Only what the file can't answer, with the values that made it ask and a
|
|
36
|
+
suggested answer.
|
|
37
|
+
5. **Clean, then check** the result against the file.
|
|
38
|
+
6. **Describe** each column: what it holds, how its values add up (summed, averaged, or the
|
|
39
|
+
latest value), and whether it looks like personal data.
|
|
40
|
+
7. **Find the facts** (story mode): the totals; how the main number moves over time; where
|
|
41
|
+
it comes from and how concentrated it is; which groups grew or shrank; groups whose rates
|
|
42
|
+
differ; numbers that move together; periods that stand out; and two traps: a partial last
|
|
43
|
+
period (left out of every comparison) and Simpson's paradox. Each fact says how it was
|
|
44
|
+
computed, on how many rows, and how sure it is.
|
|
45
|
+
8. **Check every fact** by computing it again in DuckDB from SQL written separately. Any
|
|
46
|
+
disagreement fails the run.
|
|
47
|
+
9. **Choose the story** (the second checkpoint): the insights worth telling, best first,
|
|
48
|
+
and any context only you know, such as a price change or a campaign. Only that context
|
|
49
|
+
lets the story give a reason for something; otherwise it describes associations.
|
|
50
|
+
10. **Write the story.** Rules lay out the pages: how the main number moved, what drives
|
|
51
|
+
it, what else stands out, and hypotheses to test next. A model writes the words, but
|
|
52
|
+
never types a number: it refers to facts by id, and the numbers are filled in from the
|
|
53
|
+
facts. Before anything is shown, a validator refuses a draft that types a number, names
|
|
54
|
+
something not in the data, says "rose" for a fall, gives an unsupported cause, states a
|
|
55
|
+
low-confidence fact without hedging it, or quotes a change that Simpson's paradox makes
|
|
56
|
+
misleading. The model gets the problems back, twice at most; after that, or without a
|
|
57
|
+
model, the story is written from a template built from the facts' own sentences.
|
|
58
|
+
11. **Draw the charts** from the facts' own checked numbers, at most three a page: the main
|
|
59
|
+
number over time with standout periods marked, a breakdown with the group the sentence
|
|
60
|
+
is about in colour, a meter for concentration, before-and-after dots for groups that
|
|
61
|
+
changed, and the series around an anomaly. In the web UI every number in the story
|
|
62
|
+
opens the fact behind it, and every chart can be shown as a table.
|
|
63
|
+
|
|
64
|
+
## Input
|
|
65
|
+
|
|
66
|
+
| Field | Default | Meaning |
|
|
67
|
+
|---|---|---|
|
|
68
|
+
| `dataset` | required | Path to the file. The primary input. |
|
|
69
|
+
| `table` | asked | The sheet or table, when the file has more than one. |
|
|
70
|
+
| `stop_after` | `clean` | `clean`, or `story` to also set up the story (the main number, what one row is, how numbers add up), find its facts and write it. |
|
|
71
|
+
| `output_dir` | none | A folder for the cleaned table (`<name>.clean.csv`) and the report (`<name>.cleaning.json`). Nothing is written without it. |
|
|
72
|
+
| `answers` | `{}` | Answers given up front, by question id, for example `{"date_order:order_date": "dmy"}`. These are not asked. |
|
|
73
|
+
| `assume_defaults` | `false` | Ask nothing and take every suggested answer. Each one is marked as not confirmed. |
|
|
74
|
+
|
|
75
|
+
Question ids are stable, so a run can be repeated with the answers of an earlier one. They
|
|
76
|
+
are listed in the report under `answers`.
|
|
77
|
+
|
|
78
|
+
## Output
|
|
79
|
+
|
|
80
|
+
`data` holds:
|
|
81
|
+
|
|
82
|
+
- `source`: the file, its format, encoding, delimiter, tables, and where the column names
|
|
83
|
+
were found.
|
|
84
|
+
- `cleaning`: rows and columns in and out, every step (what it did, to which column, who
|
|
85
|
+
decided, examples before and after, values it could not read), and what the final check
|
|
86
|
+
verified.
|
|
87
|
+
- `columns`: for each column its type, role, how it adds up and why, its unit (a currency
|
|
88
|
+
or %), counts, range, most common values, and notes.
|
|
89
|
+
- `setup`: with `stop_after="story"`, the main number, what one row is, the main date, how
|
|
90
|
+
each number adds up, and the columns to focus on.
|
|
91
|
+
- `facts`: with `stop_after="story"`, the time axis, the periods left out as partial, and
|
|
92
|
+
every fact: its number, a plain sentence, how it was computed, rows behind it, confidence,
|
|
93
|
+
caveats, how much it matters, the SQL that recomputed it, and whether it matched.
|
|
94
|
+
- `story`: with `stop_after="story"`, the title, shape and pages (title, headline, body,
|
|
95
|
+
actions to test, charts, and the facts behind each sentence, with each number marked by
|
|
96
|
+
the fact it came from), who wrote it (`model` or `template`), how many drafts the model
|
|
97
|
+
wrote, and what the validator refused. A chart is a kind (`line`, `bars`, `dumbbell` or
|
|
98
|
+
`meter`), a title, its fact, and its points.
|
|
99
|
+
- `files`: the files written to `output_dir`.
|
|
100
|
+
|
|
101
|
+
Findings: in story mode, the story first, one finding per page; then one per change, each
|
|
102
|
+
with its evidence; one per column note (personal data, a column that is mostly empty); and
|
|
103
|
+
the final check.
|
|
104
|
+
|
|
105
|
+
## Limits
|
|
106
|
+
|
|
107
|
+
- One table per run. Tables are not joined.
|
|
108
|
+
- Files up to 500 MB, read into memory.
|
|
109
|
+
- Personal data is detected from column names and value patterns, so it can miss some. It
|
|
110
|
+
stays in the cleaned file; only this report leaves it out.
|
|
111
|
+
- How a number adds up is guessed from its name (sales add up, prices are averaged). A
|
|
112
|
+
name that says nothing is asked about only in story mode.
|
|
113
|
+
- Facts describe associations. A correlation or a gap between groups is never stated as a
|
|
114
|
+
cause. Groups under 30 rows are not compared, and p-values are corrected for the number of
|
|
115
|
+
tests.
|
|
116
|
+
- The model only ever sees facts (aggregates) and column names, never a row of data or a
|
|
117
|
+
personal-data column. With a hosted model (Anthropic or OpenAI) those facts are sent to
|
|
118
|
+
that company; the default is a model on your own machine.
|
|
119
|
+
- Old `.xls` workbooks are not supported; save them as `.xlsx` or CSV. Excel formulas are
|
|
120
|
+
never run: the values Excel last saved are used.
|
|
121
|
+
|
|
122
|
+
## Usage
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
pip install margin-cli margin-data-storyteller
|
|
126
|
+
margin run data-storyteller sales.xlsx --set output_dir=clean
|
|
127
|
+
margin run data-storyteller sales.csv --set 'answers={"decimal:amount": "comma"}'
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
In a terminal the questions are asked there; in the web UI they appear on the run page.
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
import asyncio
|
|
134
|
+
|
|
135
|
+
from margin_core import run_agent
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
async def main() -> None:
|
|
139
|
+
result = await run_agent("data-storyteller", {"dataset": "sales.csv", "assume_defaults": True})
|
|
140
|
+
print(result.summary)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
asyncio.run(main())
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Development
|
|
147
|
+
|
|
148
|
+
This agent lives in the [margin](https://github.com/Waddah0-0/margin) monorepo, in
|
|
149
|
+
[`agents/data_storyteller`](https://github.com/Waddah0-0/margin/tree/main/agents/data_storyteller). If you are reading this in its mirror
|
|
150
|
+
repository, that copy is read-only: open issues and pull requests in the main repository.
|
|
151
|
+
|
|
152
|
+
Run its tests with `uv run pytest agents/data_storyteller`. The messy files the tests use
|
|
153
|
+
are built by `tests/conftest.py`, byte for byte, so encodings and line endings stay exact.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "margin-data-storyteller"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Cleans a table of data, shows every change it made, and tells the story in it with charts."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
authors = [{ name = "Waddah Ali" }]
|
|
10
|
+
keywords = ["data-cleaning", "data-storytelling", "pandas", "duckdb", "llm", "agents"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 3 - Alpha",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Intended Audience :: Science/Research",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Typing :: Typed",
|
|
20
|
+
"Topic :: Scientific/Engineering :: Information Analysis",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"margin-core>=0.1.2,<0.2", # 0.1.2: agents can ask questions (ctx.ask)
|
|
24
|
+
"duckdb>=1.1",
|
|
25
|
+
"numpy>=1.26",
|
|
26
|
+
"openpyxl>=3.1",
|
|
27
|
+
"pandas>=2.2",
|
|
28
|
+
"pyarrow>=15",
|
|
29
|
+
"scipy>=1.12",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://github.com/Waddah0-0/margin"
|
|
34
|
+
Source = "https://github.com/Waddah0-0/margin/tree/main/agents/data_storyteller"
|
|
35
|
+
Issues = "https://github.com/Waddah0-0/margin/issues"
|
|
36
|
+
|
|
37
|
+
[project.entry-points."margin.agents"]
|
|
38
|
+
data-storyteller = "data_storyteller.agent:DataStorytellerAgent"
|
|
39
|
+
|
|
40
|
+
[tool.margin]
|
|
41
|
+
id = "data-storyteller"
|
|
42
|
+
stage = "experimental"
|
|
43
|
+
mirror = "Waddah0-0/margin-data-storyteller"
|
|
44
|
+
|
|
45
|
+
[build-system]
|
|
46
|
+
requires = ["hatchling"]
|
|
47
|
+
build-backend = "hatchling.build"
|
|
48
|
+
|
|
49
|
+
[tool.hatch.build.targets.wheel]
|
|
50
|
+
packages = ["src/data_storyteller"]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""Data Storyteller."""
|
|
2
|
+
|
|
3
|
+
from data_storyteller.agent import DataStorytellerAgent
|
|
4
|
+
from data_storyteller.models import DataStorytellerInput, DataStorytellerOutput
|
|
5
|
+
|
|
6
|
+
__version__ = "0.1.0"
|
|
7
|
+
|
|
8
|
+
__all__ = ["DataStorytellerAgent", "DataStorytellerInput", "DataStorytellerOutput", "__version__"]
|