quantology-modelmaker 0.1.0__py3-none-any.whl

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.
Files changed (49) hide show
  1. modelmaker/__init__.py +2 -0
  2. modelmaker/api.py +1073 -0
  3. modelmaker/blocks/__init__.py +0 -0
  4. modelmaker/blocks/base.py +78 -0
  5. modelmaker/blocks/library.py +327 -0
  6. modelmaker/blocks/modelling.py +312 -0
  7. modelmaker/blocks/stat_tests.py +133 -0
  8. modelmaker/cache.py +44 -0
  9. modelmaker/compiler.py +478 -0
  10. modelmaker/graph.py +171 -0
  11. modelmaker/llm/__init__.py +28 -0
  12. modelmaker/llm/anthropic_provider.py +79 -0
  13. modelmaker/llm/base.py +85 -0
  14. modelmaker/llm/claude_cli_provider.py +90 -0
  15. modelmaker/llm/gemini_provider.py +138 -0
  16. modelmaker/llm/lmstudio_provider.py +140 -0
  17. modelmaker/llm/openai_provider.py +138 -0
  18. modelmaker/llm/prompts.py +189 -0
  19. modelmaker/llm/settings.py +47 -0
  20. modelmaker/llm/stub_provider.py +29 -0
  21. modelmaker/metadata_transforms.py +109 -0
  22. modelmaker/packet.py +111 -0
  23. modelmaker/project.py +129 -0
  24. modelmaker/run_worker.py +143 -0
  25. modelmaker/runner.py +1313 -0
  26. modelmaker/session.py +400 -0
  27. modelmaker/static/assets/index-B9cHjTMW.css +1 -0
  28. modelmaker/static/assets/index-EvjTc-1k.js +34 -0
  29. modelmaker/static/brand-quantology/favicon.svg +4 -0
  30. modelmaker/static/brand-quantology/icon-mark-light.svg +13 -0
  31. modelmaker/static/icons.svg +24 -0
  32. modelmaker/static/index.html +20 -0
  33. modelmaker/tui/__init__.py +0 -0
  34. modelmaker/tui/app.py +785 -0
  35. modelmaker/tui/canvas.py +388 -0
  36. modelmaker/tui/charts.py +84 -0
  37. modelmaker/tui/client.py +270 -0
  38. modelmaker/tui/inspector.py +285 -0
  39. modelmaker/tui/paramspecs.py +128 -0
  40. modelmaker/tui/preview.py +71 -0
  41. modelmaker/tui/screens.py +182 -0
  42. modelmaker/util.py +44 -0
  43. quantology_modelmaker-0.1.0.dist-info/METADATA +247 -0
  44. quantology_modelmaker-0.1.0.dist-info/RECORD +49 -0
  45. quantology_modelmaker-0.1.0.dist-info/WHEEL +5 -0
  46. quantology_modelmaker-0.1.0.dist-info/entry_points.txt +3 -0
  47. quantology_modelmaker-0.1.0.dist-info/licenses/LICENSE +204 -0
  48. quantology_modelmaker-0.1.0.dist-info/licenses/NOTICE +12 -0
  49. quantology_modelmaker-0.1.0.dist-info/top_level.txt +1 -0
File without changes
@@ -0,0 +1,78 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any, Callable, Literal
5
+
6
+ PortType = Literal["dataframe", "model", "scalar_metric", "image", "master_scale", "any"]
7
+ # Pipeline role only -- governs runtime behavior (input blocks need no
8
+ # upstream and support refresh/probe; output blocks may take an injected
9
+ # output_dir). Orthogonal to where a block's code comes from: see
10
+ # BlockInstance.is_custom in graph.py for that axis. Any of the three can be
11
+ # either a registry (fixed-code) block or a custom (AI-authored) one.
12
+ BlockType = Literal["input", "standard", "output"]
13
+
14
+ # (input_metas_by_port, output_dataframes_by_port, params) -> output_metas_by_port
15
+ MetadataTransformFn = Callable[
16
+ [dict[str, dict[str, Any]], dict[str, Any], dict[str, Any]],
17
+ dict[str, dict[str, Any]],
18
+ ]
19
+
20
+ # A block's core function is generic and plain-dataframe: it never touches
21
+ # DataFramePacket/ColumnMeta. The engine unwraps packets before calling it
22
+ # and rewraps the result afterward, applying the metadata_transform. This
23
+ # is what keeps the compiled --with-metadata=off script "clean" by
24
+ # construction (see plan section 7).
25
+ BlockFn = Callable[..., Any]
26
+
27
+
28
+ @dataclass
29
+ class PortSpec:
30
+ name: str
31
+ type: PortType = "dataframe"
32
+ required: bool = True
33
+
34
+
35
+ @dataclass
36
+ class BlockSpec:
37
+ category: str
38
+ block_type: BlockType
39
+ display_name: str
40
+ inputs: list[PortSpec]
41
+ outputs: list[PortSpec]
42
+ fn: BlockFn
43
+ metadata_transform: MetadataTransformFn
44
+ probe: Callable[[dict[str, Any]], Any] | None = None
45
+ # Palette display grouping only -- purely cosmetic, unrelated to
46
+ # block_type's runtime role. Defaults to block_type so existing
47
+ # input/standard/output blocks group as before; a block library can set
48
+ # this to cluster related blocks (e.g. "modelling", "tests") regardless
49
+ # of what pipeline role each one plays.
50
+ group: str | None = None
51
+ # Optional lazy-mode twin of `fn`: same params, but reads/returns
52
+ # pl.LazyFrame instead of pl.DataFrame. None (the default, and every
53
+ # custom/AI-authored block) means this block never participates in a
54
+ # streaming run's fusion -- see Runner._fusable_spec/_build_fusion_groups
55
+ # in runner.py. For most registry blocks that are already pure
56
+ # expression-based code (filter/select/groupby_agg/join), `fn` itself
57
+ # works unchanged over a LazyFrame and can be reused verbatim here; only
58
+ # a block whose eager `fn` deliberately collects (read_csv) needs an
59
+ # actual separate implementation.
60
+ lazy_fn: BlockFn | None = None
61
+ # Optional streaming-sink twin of `fn`: takes a still-uncollected
62
+ # pl.LazyFrame plus the block's other params and writes it straight to
63
+ # its destination (e.g. LazyFrame.sink_csv), returning None -- never
64
+ # called through the normal eager path, only when this block is folded
65
+ # onto the end of a streaming run's fusion group as a terminal write
66
+ # (see Runner._build_fusion_groups' sink-attachment pass). None (the
67
+ # default) means this block is never eligible for that: it always runs
68
+ # as an ordinary checkpoint reading a materialized DataFrame from cache,
69
+ # exactly as before.
70
+ lazy_sink_fn: BlockFn | None = None
71
+
72
+
73
+ BLOCK_REGISTRY: dict[str, BlockSpec] = {}
74
+
75
+
76
+ def register_block(spec: BlockSpec) -> BlockSpec:
77
+ BLOCK_REGISTRY[spec.category] = spec
78
+ return spec
@@ -0,0 +1,327 @@
1
+ """Standard block library. Each `fn` is a plain function over pl.DataFrame /
2
+ literal params only — no DataFramePacket, no ColumnMeta. This is what a
3
+ --with-metadata=off compile emits verbatim (see plan section 7); the engine
4
+ wraps packets around calls to these at run time, and applies the paired
5
+ metadata_transform separately.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import os
11
+
12
+ import polars as pl
13
+
14
+ from ..metadata_transforms import broadcast_to_all_outputs, infer_dtypes, narrow_from_single_input, passthrough
15
+ from ..packet import ColumnMeta
16
+ from .base import BlockSpec, PortSpec, register_block
17
+
18
+
19
+ def read_csv(path: str, sample_rows: int | None = None) -> pl.DataFrame:
20
+ # `sample_rows`, when the engine's sample mode is on, is injected by
21
+ # Runner.run_block the same way output_dir/block_id are (see
22
+ # util.accepts_param) -- an input block opts in just by naming the
23
+ # parameter. Scanning with `n_rows` instead of reading the whole file
24
+ # and truncating afterward (the fallback for input blocks that don't
25
+ # accept this param -- see runner.py) is the one place lazy evaluation
26
+ # earns its keep for sample mode: the rest of the pipeline already runs
27
+ # on the truncated, small-by-construction sample, so nothing downstream
28
+ # needs lazy frames to iterate cheaply. Full runs (sample_rows=None)
29
+ # read exactly as before.
30
+ if sample_rows is not None:
31
+ return pl.scan_csv(path, n_rows=sample_rows).collect()
32
+ return pl.read_csv(path)
33
+
34
+
35
+ def _probe_read_csv(params: dict) -> float | None:
36
+ try:
37
+ return os.path.getmtime(params["path"])
38
+ except OSError:
39
+ return None
40
+
41
+
42
+ def _read_csv_lazy(path: str, sample_rows: int | None = None) -> pl.LazyFrame:
43
+ # The lazy twin of read_csv (see BlockSpec.lazy_fn) -- used only by a
44
+ # streaming run's fusion path (see Runner._dispatch_fused_group), which
45
+ # collects once at the end of a whole fused chain rather than here.
46
+ # Ordinary calls to `read_csv` above are untouched and still always
47
+ # return a materialized pl.DataFrame.
48
+ if sample_rows is not None:
49
+ return pl.scan_csv(path, n_rows=sample_rows)
50
+ return pl.scan_csv(path)
51
+
52
+
53
+ register_block(
54
+ BlockSpec(
55
+ category="read_csv",
56
+ block_type="input",
57
+ display_name="Read CSV",
58
+ inputs=[],
59
+ outputs=[PortSpec("out")],
60
+ fn=read_csv,
61
+ lazy_fn=_read_csv_lazy,
62
+ metadata_transform=infer_dtypes,
63
+ probe=_probe_read_csv,
64
+ )
65
+ )
66
+
67
+
68
+ def filter_rows(df: pl.DataFrame, expr: str) -> pl.DataFrame:
69
+ return df.filter(pl.sql_expr(expr))
70
+
71
+
72
+ register_block(
73
+ BlockSpec(
74
+ category="filter",
75
+ block_type="standard",
76
+ display_name="Filter",
77
+ inputs=[PortSpec("df")],
78
+ outputs=[PortSpec("out")],
79
+ fn=filter_rows,
80
+ # filter_rows is pure expression-based code (`.filter(pl.sql_expr(...))`),
81
+ # identical over pl.DataFrame/pl.LazyFrame -- reused verbatim as the
82
+ # lazy_fn a streaming run's fusion path calls (see BlockSpec.lazy_fn).
83
+ lazy_fn=filter_rows,
84
+ metadata_transform=passthrough,
85
+ )
86
+ )
87
+
88
+
89
+ def select_cols(df: pl.DataFrame, cols: list[str]) -> pl.DataFrame:
90
+ return df.select(cols)
91
+
92
+
93
+ register_block(
94
+ BlockSpec(
95
+ category="select",
96
+ block_type="standard",
97
+ display_name="Select columns",
98
+ inputs=[PortSpec("df")],
99
+ outputs=[PortSpec("out")],
100
+ fn=select_cols,
101
+ lazy_fn=select_cols, # same reasoning as filter's lazy_fn above
102
+ metadata_transform=narrow_from_single_input,
103
+ )
104
+ )
105
+
106
+
107
+ def groupby_agg(df: pl.DataFrame, by: list[str], aggs: dict[str, str]) -> pl.DataFrame:
108
+ agg_exprs = [getattr(pl.col(c), fn)().alias(c) for c, fn in aggs.items()]
109
+ return df.group_by(by).agg(agg_exprs)
110
+
111
+
112
+ def _groupby_meta(input_metas, outputs, params):
113
+ (in_meta,) = input_metas.values()
114
+ (df,) = outputs.values()
115
+ by = set(params.get("by", []))
116
+ result = {}
117
+ for name in df.columns:
118
+ if name in by and name in in_meta:
119
+ result[name] = in_meta[name]
120
+ else:
121
+ result[name] = ColumnMeta(dtype=str(df.schema[name]))
122
+ return {"out": result}
123
+
124
+
125
+ register_block(
126
+ BlockSpec(
127
+ category="groupby_agg",
128
+ block_type="standard",
129
+ display_name="Group by / aggregate",
130
+ inputs=[PortSpec("df")],
131
+ outputs=[PortSpec("out")],
132
+ fn=groupby_agg,
133
+ lazy_fn=groupby_agg, # same reasoning as filter's lazy_fn above
134
+ metadata_transform=_groupby_meta,
135
+ )
136
+ )
137
+
138
+
139
+ def join(left: pl.DataFrame, right: pl.DataFrame, on: list[str], how: str = "inner") -> pl.DataFrame:
140
+ return left.join(right, on=on, how=how)
141
+
142
+
143
+ def _join_meta(input_metas, outputs, params):
144
+ left_meta = input_metas["left"]
145
+ right_meta = input_metas["right"]
146
+ (df,) = outputs.values()
147
+ merged = {**right_meta, **left_meta}
148
+ return {"out": {name: merged[name] for name in df.columns if name in merged}}
149
+
150
+
151
+ register_block(
152
+ BlockSpec(
153
+ category="join",
154
+ block_type="standard",
155
+ display_name="Join",
156
+ inputs=[PortSpec("left"), PortSpec("right")],
157
+ outputs=[PortSpec("out")],
158
+ fn=join,
159
+ lazy_fn=join, # same reasoning as filter's lazy_fn above
160
+ metadata_transform=_join_meta,
161
+ )
162
+ )
163
+
164
+
165
+ def train_test_split(df: pl.DataFrame, test_size: float = 0.2, seed: int = 0) -> tuple[pl.DataFrame, pl.DataFrame]:
166
+ shuffled = df.sample(fraction=1.0, shuffle=True, seed=seed)
167
+ n_test = int(len(shuffled) * test_size)
168
+ test = shuffled.head(n_test)
169
+ train = shuffled.tail(len(shuffled) - n_test)
170
+ return train, test
171
+
172
+
173
+ register_block(
174
+ BlockSpec(
175
+ category="train_test_split",
176
+ block_type="standard",
177
+ display_name="Train/test split",
178
+ inputs=[PortSpec("df")],
179
+ outputs=[PortSpec("train"), PortSpec("test")],
180
+ fn=train_test_split,
181
+ metadata_transform=broadcast_to_all_outputs,
182
+ )
183
+ )
184
+
185
+
186
+ def write_csv(df: pl.DataFrame, filename: str, output_dir: str = ".") -> None:
187
+ df.write_csv(os.path.join(output_dir, filename))
188
+
189
+
190
+ def _write_csv_sink(df: pl.LazyFrame, filename: str, output_dir: str = ".") -> None:
191
+ # The streaming-sink twin of write_csv (see BlockSpec.lazy_sink_fn):
192
+ # called only when this block is folded onto the end of a streaming
193
+ # run's fusion group as its terminal write (see
194
+ # Runner._build_fusion_groups), so the group's whole upstream plan --
195
+ # source scan included -- is written straight to disk via the
196
+ # streaming engine without ever materializing a cached DataFrame for
197
+ # the block that feeds it.
198
+ df.sink_csv(os.path.join(output_dir, filename))
199
+
200
+
201
+ register_block(
202
+ BlockSpec(
203
+ category="write_csv",
204
+ block_type="output",
205
+ display_name="Write CSV",
206
+ inputs=[PortSpec("df")],
207
+ outputs=[],
208
+ fn=write_csv,
209
+ lazy_sink_fn=_write_csv_sink,
210
+ metadata_transform=lambda *_a, **_k: {},
211
+ )
212
+ )
213
+
214
+
215
+ def display_table(df: pl.DataFrame) -> pl.DataFrame:
216
+ """A no-op pass-through: exists to mark a point in the pipeline as a
217
+ reportable table. The engine/UI can preview it like any dataframe
218
+ output; the compiled script carries it through unchanged."""
219
+ return df
220
+
221
+
222
+ register_block(
223
+ BlockSpec(
224
+ category="display_table",
225
+ block_type="output",
226
+ display_name="Display table",
227
+ inputs=[PortSpec("df")],
228
+ outputs=[PortSpec("out")],
229
+ fn=display_table,
230
+ metadata_transform=passthrough,
231
+ )
232
+ )
233
+
234
+
235
+ def display_value(value):
236
+ """A no-op pass-through, like display_table, but for any port type --
237
+ a dataframe, a model/scalar_metric artifact (a plain dict), or an image.
238
+ Its ports are typed "any" so it wires up to whatever you point it at;
239
+ the UI figures out how to render whatever actually comes through."""
240
+ return value
241
+
242
+
243
+ def _display_value_meta(input_metas, outputs, params):
244
+ # Only reached when the value passed through actually was a DataFrame
245
+ # (see runner.py: the transform only runs when an output is one) --
246
+ # reuse the real upstream column metadata, same as display_table.
247
+ (in_meta,) = input_metas.values()
248
+ (df,) = outputs.values()
249
+ return {"value": {name: in_meta[name] if name in in_meta else ColumnMeta(dtype=str(df.schema[name])) for name in df.columns}}
250
+
251
+
252
+ register_block(
253
+ BlockSpec(
254
+ category="display_value",
255
+ block_type="output",
256
+ display_name="View value",
257
+ inputs=[PortSpec("value", type="any")],
258
+ outputs=[PortSpec("value", type="any")],
259
+ fn=display_value,
260
+ metadata_transform=_display_value_meta,
261
+ )
262
+ )
263
+
264
+
265
+ def generate_image(
266
+ df: pl.DataFrame,
267
+ kind: str = "hist",
268
+ x: str = "",
269
+ y: str = "",
270
+ bins: int = 30,
271
+ title: str = "",
272
+ output_dir: str = ".",
273
+ block_id: str = "",
274
+ ) -> bytes:
275
+ # Self-contained imports (rather than relying on this module's own
276
+ # top-level imports) so this function stays a valid, independent unit
277
+ # both when run live by the engine and when inlined verbatim into a
278
+ # compiled script.
279
+ import io
280
+ import os
281
+
282
+ import matplotlib
283
+
284
+ matplotlib.use("Agg")
285
+ import matplotlib.pyplot as plt
286
+
287
+ fig, ax = plt.subplots(figsize=(6, 4))
288
+ if kind == "hist":
289
+ ax.hist(df[x].to_list(), bins=bins)
290
+ elif kind == "bar":
291
+ ax.bar([str(v) for v in df[x].to_list()], df[y].to_list())
292
+ elif kind == "scatter":
293
+ ax.scatter(df[x].to_list(), df[y].to_list(), s=10, alpha=0.6)
294
+ elif kind == "line":
295
+ ax.plot(df[x].to_list(), df[y].to_list())
296
+ else:
297
+ raise ValueError(f"unknown chart kind: {kind!r}")
298
+ ax.set_xlabel(x)
299
+ if y:
300
+ ax.set_ylabel(y)
301
+ if title:
302
+ ax.set_title(title)
303
+ fig.tight_layout()
304
+
305
+ buf = io.BytesIO()
306
+ fig.savefig(buf, format="png", dpi=110)
307
+ plt.close(fig)
308
+
309
+ os.makedirs(output_dir, exist_ok=True)
310
+ filename = f"generate_image_{block_id}.png" if block_id else "generate_image.png"
311
+ with open(os.path.join(output_dir, filename), "wb") as f:
312
+ f.write(buf.getvalue())
313
+
314
+ return buf.getvalue()
315
+
316
+
317
+ register_block(
318
+ BlockSpec(
319
+ category="generate_image",
320
+ block_type="output",
321
+ display_name="Generate image",
322
+ inputs=[PortSpec("df")],
323
+ outputs=[PortSpec("image", type="image")],
324
+ fn=generate_image,
325
+ metadata_transform=lambda *_a, **_k: {},
326
+ )
327
+ )