cegraph 0.1.0a0__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.
- cegraph-0.1.0a0/.github/ISSUE_TEMPLATE/bug_report.yml +40 -0
- cegraph-0.1.0a0/.github/ISSUE_TEMPLATE/feature_request.yml +20 -0
- cegraph-0.1.0a0/.github/dependabot.yml +17 -0
- cegraph-0.1.0a0/.github/workflows/publish.yml +24 -0
- cegraph-0.1.0a0/.github/workflows/test.yml +32 -0
- cegraph-0.1.0a0/.gitignore +14 -0
- cegraph-0.1.0a0/.pre-commit-config.yaml +14 -0
- cegraph-0.1.0a0/CHANGELOG.md +0 -0
- cegraph-0.1.0a0/CONTRIBUTING.md +0 -0
- cegraph-0.1.0a0/LICENSE +21 -0
- cegraph-0.1.0a0/PKG-INFO +203 -0
- cegraph-0.1.0a0/README.md +148 -0
- cegraph-0.1.0a0/SECURITY.md +0 -0
- cegraph-0.1.0a0/pyproject.toml +65 -0
- cegraph-0.1.0a0/src/cegraph/__init__.py +33 -0
- cegraph-0.1.0a0/src/cegraph/_version.py +1 -0
- cegraph-0.1.0a0/src/cegraph/causal/__init__.py +0 -0
- cegraph-0.1.0a0/src/cegraph/causal/counterfactual.py +149 -0
- cegraph-0.1.0a0/src/cegraph/causal/optimizer.py +90 -0
- cegraph-0.1.0a0/src/cegraph/core/__init__.py +0 -0
- cegraph-0.1.0a0/src/cegraph/core/context.py +48 -0
- cegraph-0.1.0a0/src/cegraph/core/graph.py +154 -0
- cegraph-0.1.0a0/src/cegraph/core/node.py +87 -0
- cegraph-0.1.0a0/src/cegraph/core/tracer.py +133 -0
- cegraph-0.1.0a0/src/cegraph/exceptions.py +30 -0
- cegraph-0.1.0a0/tests/test_counterfactual.py +152 -0
- cegraph-0.1.0a0/tests/test_graph.py +161 -0
- cegraph-0.1.0a0/tests/test_overhead.py +101 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
name: Bug Report
|
|
2
|
+
description: File a bug report
|
|
3
|
+
labels: ["bug"]
|
|
4
|
+
body:
|
|
5
|
+
- type: markdown
|
|
6
|
+
attributes:
|
|
7
|
+
value: "Thanks for reporting a bug!"
|
|
8
|
+
- type: input
|
|
9
|
+
id: version
|
|
10
|
+
attributes:
|
|
11
|
+
label: cegraph version
|
|
12
|
+
placeholder: "0.1.0"
|
|
13
|
+
validations:
|
|
14
|
+
required: true
|
|
15
|
+
- type: textarea
|
|
16
|
+
id: description
|
|
17
|
+
attributes:
|
|
18
|
+
label: Describe the bug
|
|
19
|
+
validations:
|
|
20
|
+
required: true
|
|
21
|
+
- type: textarea
|
|
22
|
+
id: reproduction
|
|
23
|
+
attributes:
|
|
24
|
+
label: Minimal reproduction
|
|
25
|
+
render: python
|
|
26
|
+
validations:
|
|
27
|
+
required: true
|
|
28
|
+
- type: textarea
|
|
29
|
+
id: expected
|
|
30
|
+
attributes:
|
|
31
|
+
label: Expected behavior
|
|
32
|
+
validations:
|
|
33
|
+
required: true
|
|
34
|
+
- type: textarea
|
|
35
|
+
id: environment
|
|
36
|
+
attributes:
|
|
37
|
+
label: Environment
|
|
38
|
+
placeholder: "OS, Python version, numpy version"
|
|
39
|
+
validations:
|
|
40
|
+
required: true
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: Feature Request
|
|
2
|
+
description: Suggest a new feature
|
|
3
|
+
labels: ["enhancement"]
|
|
4
|
+
body:
|
|
5
|
+
- type: textarea
|
|
6
|
+
id: problem
|
|
7
|
+
attributes:
|
|
8
|
+
label: Problem this feature solves
|
|
9
|
+
validations:
|
|
10
|
+
required: true
|
|
11
|
+
- type: textarea
|
|
12
|
+
id: solution
|
|
13
|
+
attributes:
|
|
14
|
+
label: Proposed solution
|
|
15
|
+
validations:
|
|
16
|
+
required: true
|
|
17
|
+
- type: textarea
|
|
18
|
+
id: alternatives
|
|
19
|
+
attributes:
|
|
20
|
+
label: Alternatives considered
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: "pip"
|
|
4
|
+
directory: "/"
|
|
5
|
+
schedule:
|
|
6
|
+
interval: "weekly"
|
|
7
|
+
labels:
|
|
8
|
+
- "dependencies"
|
|
9
|
+
- package-ecosystem: "github-actions"
|
|
10
|
+
directory: "/"
|
|
11
|
+
schedule:
|
|
12
|
+
interval: "weekly"
|
|
13
|
+
labels:
|
|
14
|
+
- "dependencies"
|
|
15
|
+
ignore:
|
|
16
|
+
- dependency-name: "actions/*"
|
|
17
|
+
update-types: ["version-update:semver-major"]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
environment: pypi
|
|
11
|
+
permissions:
|
|
12
|
+
id-token: write
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.11"
|
|
19
|
+
- name: Build
|
|
20
|
+
run: |
|
|
21
|
+
pip install build
|
|
22
|
+
python -m build
|
|
23
|
+
- name: Publish to PyPI
|
|
24
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main, develop]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12"]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
- uses: actions/setup-python@v5
|
|
19
|
+
with:
|
|
20
|
+
python-version: ${{ matrix.python-version }}
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: pip install -e ".[dev]"
|
|
23
|
+
- name: Lint with ruff
|
|
24
|
+
run: ruff check src/ tests/
|
|
25
|
+
- name: Type check with mypy
|
|
26
|
+
run: mypy src/cegraph
|
|
27
|
+
- name: Run tests
|
|
28
|
+
run: pytest tests/ -v --tb=short --cov=cegraph --cov-report=xml
|
|
29
|
+
- name: Upload coverage
|
|
30
|
+
uses: codecov/codecov-action@v4
|
|
31
|
+
with:
|
|
32
|
+
file: ./coverage.xml
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
3
|
+
rev: v0.4.4
|
|
4
|
+
hooks:
|
|
5
|
+
- id: ruff
|
|
6
|
+
args: [--fix]
|
|
7
|
+
- id: ruff-format
|
|
8
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
9
|
+
rev: v4.6.0
|
|
10
|
+
hooks:
|
|
11
|
+
- id: trailing-whitespace
|
|
12
|
+
- id: end-of-file-fixer
|
|
13
|
+
- id: check-yaml
|
|
14
|
+
- id: check-toml
|
|
File without changes
|
|
File without changes
|
cegraph-0.1.0a0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 keylordelrey
|
|
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.
|
cegraph-0.1.0a0/PKG-INFO
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cegraph
|
|
3
|
+
Version: 0.1.0a0
|
|
4
|
+
Summary: Causal-aware execution runtime for production Python systems.
|
|
5
|
+
Project-URL: Homepage, https://github.com/keyreyla/cegraph
|
|
6
|
+
Project-URL: Repository, https://github.com/keyreyla/cegraph
|
|
7
|
+
Project-URL: Issues, https://github.com/keyreyla/cegraph/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/keyreyla/cegraph/blob/main/CHANGELOG.md
|
|
9
|
+
License: MIT License
|
|
10
|
+
|
|
11
|
+
Copyright (c) 2026 keylordelrey
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
in the Software without restriction, including without limitation the rights
|
|
16
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
furnished to do so, subject to the following conditions:
|
|
19
|
+
|
|
20
|
+
The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
SOFTWARE.
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Keywords: causal-graph,causal-inference,counterfactual,debugging,execution-graph,mlops,observability,production-ml,runtime,tracing
|
|
32
|
+
Classifier: Development Status :: 3 - Alpha
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: Intended Audience :: Science/Research
|
|
35
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
36
|
+
Classifier: Programming Language :: Python :: 3
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
42
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
43
|
+
Classifier: Topic :: System :: Monitoring
|
|
44
|
+
Requires-Python: >=3.10
|
|
45
|
+
Requires-Dist: numpy>=1.24
|
|
46
|
+
Provides-Extra: dev
|
|
47
|
+
Requires-Dist: build; extra == 'dev'
|
|
48
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
49
|
+
Requires-Dist: pytest-benchmark; extra == 'dev'
|
|
50
|
+
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
|
|
51
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
52
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
53
|
+
Requires-Dist: twine; extra == 'dev'
|
|
54
|
+
Description-Content-Type: text/markdown
|
|
55
|
+
|
|
56
|
+
<div align="center">
|
|
57
|
+
|
|
58
|
+
# cegraph
|
|
59
|
+
|
|
60
|
+
**Causal-aware execution runtime for production Python systems.**
|
|
61
|
+
|
|
62
|
+
[](https://github.com/keyreyla/cegraph/actions/workflows/test.yml)
|
|
63
|
+
[](https://pypi.org/project/cegraph)
|
|
64
|
+
[](https://pypi.org/project/cegraph)
|
|
65
|
+
[](LICENSE)
|
|
66
|
+
|
|
67
|
+
[Installation](#installation) • [Quick Start](#quick-start) • [Why cegraph](#why-cegraph) • [Documentation](#documentation)
|
|
68
|
+
|
|
69
|
+
</div>
|
|
70
|
+
|
|
71
|
+
cegraph is a Python library that tracks causal dependencies in your execution pipelines, runs counterfactual simulations, and adapts to changing conditions — all with less than 4% runtime overhead.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from cegraph import causal_node, CausalGraph, Context, counterfactual
|
|
75
|
+
|
|
76
|
+
@causal_node(sensitivity=["price", "demand"])
|
|
77
|
+
def fetch_market_data(symbol: str) -> dict:
|
|
78
|
+
return {"price": 100.0, "demand": 0.75, "symbol": symbol}
|
|
79
|
+
|
|
80
|
+
@causal_node(sensitivity=["price"], constraint=lambda x: 50 <= x <= 500)
|
|
81
|
+
def apply_pricing_strategy(base_price: float) -> float:
|
|
82
|
+
return base_price * 1.15
|
|
83
|
+
|
|
84
|
+
graph = CausalGraph()
|
|
85
|
+
graph.connect(fetch_market_data, apply_pricing_strategy)
|
|
86
|
+
graph.validate()
|
|
87
|
+
|
|
88
|
+
with Context(graph=graph) as ctx:
|
|
89
|
+
market = fetch_market_data("AAPL")
|
|
90
|
+
final_price = apply_pricing_strategy(market["price"])
|
|
91
|
+
|
|
92
|
+
cf = counterfactual(
|
|
93
|
+
base_trace=ctx.tracer.records,
|
|
94
|
+
interventions={"fetch_market_data": {"price": 120.0, "demand": 0.95, "symbol": "AAPL"}},
|
|
95
|
+
confidence_threshold=0.8,
|
|
96
|
+
)
|
|
97
|
+
print(f"Impact: {cf.overall_impact:.3f}, Confidence: {cf.confidence:.3f}")
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Installation
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
pip install cegraph
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Requires Python 3.10+. The only dependency is `numpy`.
|
|
107
|
+
|
|
108
|
+
## Quick Start
|
|
109
|
+
|
|
110
|
+
### 1. Define causal nodes
|
|
111
|
+
|
|
112
|
+
Decorate your functions with `@causal_node` to mark them as causally-tracked execution units:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from cegraph import causal_node
|
|
116
|
+
|
|
117
|
+
@causal_node(sensitivity=["input_vars"])
|
|
118
|
+
def preprocess(data: dict) -> float:
|
|
119
|
+
return sum(data.values()) / len(data)
|
|
120
|
+
|
|
121
|
+
@causal_node(sensitivity=["threshold"], constraint=lambda x: 0 <= x <= 1)
|
|
122
|
+
def classify(score: float) -> str:
|
|
123
|
+
return "high" if score > 0.5 else "low"
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 2. Build the causal graph
|
|
127
|
+
|
|
128
|
+
Connect nodes to declare dependencies:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from cegraph import CausalGraph
|
|
132
|
+
|
|
133
|
+
graph = CausalGraph()
|
|
134
|
+
graph.connect(preprocess, classify)
|
|
135
|
+
graph.validate() # detects cycles, validates type compatibility
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 3. Execute with tracing
|
|
139
|
+
|
|
140
|
+
Run inside a `Context` to automatically capture causal traces:
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from cegraph import Context
|
|
144
|
+
|
|
145
|
+
with Context(graph=graph, buffer_size=1000) as ctx:
|
|
146
|
+
data = {"a": 0.8, "b": 0.6}
|
|
147
|
+
features = preprocess(data)
|
|
148
|
+
result = classify(features)
|
|
149
|
+
|
|
150
|
+
summary = ctx.tracer.summary()
|
|
151
|
+
# {'classify': {'count': 1, 'mean_latency_ms': 0.12, 'p95_latency_ms': 0.12}}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### 4. Run counterfactual simulations
|
|
155
|
+
|
|
156
|
+
Ask "what if" questions about your pipeline:
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from cegraph import counterfactual
|
|
160
|
+
|
|
161
|
+
cf = counterfactual(
|
|
162
|
+
base_trace=ctx.tracer.records,
|
|
163
|
+
interventions={"preprocess": 0.9}, # what if score was 0.9?
|
|
164
|
+
n_perturbations=100,
|
|
165
|
+
)
|
|
166
|
+
print(cf.overall_impact, cf.confidence, cf.confidence_flag)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### 5. Optimize with constraints
|
|
170
|
+
|
|
171
|
+
Detect constraint violations and auto-fallback:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from cegraph import optimize
|
|
175
|
+
|
|
176
|
+
opt = optimize(
|
|
177
|
+
context=ctx,
|
|
178
|
+
objective="minimize_latency",
|
|
179
|
+
constraints={"max_latency_ms": 50, "min_confidence": 0.7},
|
|
180
|
+
)
|
|
181
|
+
print(opt.status, opt.action_taken)
|
|
182
|
+
# "ok", "fallback_cache", or "violated" with recommendations
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Why cegraph
|
|
186
|
+
|
|
187
|
+
Production ML pipelines, pricing engines, and distributed systems fail when they rely on static assumptions. cegraph solves three specific problems:
|
|
188
|
+
|
|
189
|
+
- **Root-cause analysis**: When accuracy drops, cegraph's causal trace tells you which node caused it — not just that something went wrong.
|
|
190
|
+
- **What-if simulation**: Run counterfactuals inside your live pipeline without deploying a separate simulation environment.
|
|
191
|
+
- **Adaptive fallback**: When constraints are violated (latency spikes, confidence drops), cegraph deterministically falls back to cached results or bypass mode.
|
|
192
|
+
|
|
193
|
+
### Performance
|
|
194
|
+
|
|
195
|
+
cegraph is designed for hot execution paths. The tracer uses a lock-free ring buffer, zero-copy hashing, and adaptive sampling to keep overhead under 4% — well below the 15% design constraint.
|
|
196
|
+
|
|
197
|
+
## Documentation
|
|
198
|
+
|
|
199
|
+
| Resource | Description |
|
|
200
|
+
|----------|-------------|
|
|
201
|
+
| [API Reference](docs/reference.md) | Full API documentation |
|
|
202
|
+
| [Architecture](docs/explanation.md) | How cegraph works under the hood |
|
|
203
|
+
| [Changelog](CHANGELOG.md) | Release history |
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# cegraph
|
|
4
|
+
|
|
5
|
+
**Causal-aware execution runtime for production Python systems.**
|
|
6
|
+
|
|
7
|
+
[](https://github.com/keyreyla/cegraph/actions/workflows/test.yml)
|
|
8
|
+
[](https://pypi.org/project/cegraph)
|
|
9
|
+
[](https://pypi.org/project/cegraph)
|
|
10
|
+
[](LICENSE)
|
|
11
|
+
|
|
12
|
+
[Installation](#installation) • [Quick Start](#quick-start) • [Why cegraph](#why-cegraph) • [Documentation](#documentation)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
cegraph is a Python library that tracks causal dependencies in your execution pipelines, runs counterfactual simulations, and adapts to changing conditions — all with less than 4% runtime overhead.
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
from cegraph import causal_node, CausalGraph, Context, counterfactual
|
|
20
|
+
|
|
21
|
+
@causal_node(sensitivity=["price", "demand"])
|
|
22
|
+
def fetch_market_data(symbol: str) -> dict:
|
|
23
|
+
return {"price": 100.0, "demand": 0.75, "symbol": symbol}
|
|
24
|
+
|
|
25
|
+
@causal_node(sensitivity=["price"], constraint=lambda x: 50 <= x <= 500)
|
|
26
|
+
def apply_pricing_strategy(base_price: float) -> float:
|
|
27
|
+
return base_price * 1.15
|
|
28
|
+
|
|
29
|
+
graph = CausalGraph()
|
|
30
|
+
graph.connect(fetch_market_data, apply_pricing_strategy)
|
|
31
|
+
graph.validate()
|
|
32
|
+
|
|
33
|
+
with Context(graph=graph) as ctx:
|
|
34
|
+
market = fetch_market_data("AAPL")
|
|
35
|
+
final_price = apply_pricing_strategy(market["price"])
|
|
36
|
+
|
|
37
|
+
cf = counterfactual(
|
|
38
|
+
base_trace=ctx.tracer.records,
|
|
39
|
+
interventions={"fetch_market_data": {"price": 120.0, "demand": 0.95, "symbol": "AAPL"}},
|
|
40
|
+
confidence_threshold=0.8,
|
|
41
|
+
)
|
|
42
|
+
print(f"Impact: {cf.overall_impact:.3f}, Confidence: {cf.confidence:.3f}")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install cegraph
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Requires Python 3.10+. The only dependency is `numpy`.
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
### 1. Define causal nodes
|
|
56
|
+
|
|
57
|
+
Decorate your functions with `@causal_node` to mark them as causally-tracked execution units:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from cegraph import causal_node
|
|
61
|
+
|
|
62
|
+
@causal_node(sensitivity=["input_vars"])
|
|
63
|
+
def preprocess(data: dict) -> float:
|
|
64
|
+
return sum(data.values()) / len(data)
|
|
65
|
+
|
|
66
|
+
@causal_node(sensitivity=["threshold"], constraint=lambda x: 0 <= x <= 1)
|
|
67
|
+
def classify(score: float) -> str:
|
|
68
|
+
return "high" if score > 0.5 else "low"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 2. Build the causal graph
|
|
72
|
+
|
|
73
|
+
Connect nodes to declare dependencies:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from cegraph import CausalGraph
|
|
77
|
+
|
|
78
|
+
graph = CausalGraph()
|
|
79
|
+
graph.connect(preprocess, classify)
|
|
80
|
+
graph.validate() # detects cycles, validates type compatibility
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 3. Execute with tracing
|
|
84
|
+
|
|
85
|
+
Run inside a `Context` to automatically capture causal traces:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from cegraph import Context
|
|
89
|
+
|
|
90
|
+
with Context(graph=graph, buffer_size=1000) as ctx:
|
|
91
|
+
data = {"a": 0.8, "b": 0.6}
|
|
92
|
+
features = preprocess(data)
|
|
93
|
+
result = classify(features)
|
|
94
|
+
|
|
95
|
+
summary = ctx.tracer.summary()
|
|
96
|
+
# {'classify': {'count': 1, 'mean_latency_ms': 0.12, 'p95_latency_ms': 0.12}}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 4. Run counterfactual simulations
|
|
100
|
+
|
|
101
|
+
Ask "what if" questions about your pipeline:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from cegraph import counterfactual
|
|
105
|
+
|
|
106
|
+
cf = counterfactual(
|
|
107
|
+
base_trace=ctx.tracer.records,
|
|
108
|
+
interventions={"preprocess": 0.9}, # what if score was 0.9?
|
|
109
|
+
n_perturbations=100,
|
|
110
|
+
)
|
|
111
|
+
print(cf.overall_impact, cf.confidence, cf.confidence_flag)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 5. Optimize with constraints
|
|
115
|
+
|
|
116
|
+
Detect constraint violations and auto-fallback:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from cegraph import optimize
|
|
120
|
+
|
|
121
|
+
opt = optimize(
|
|
122
|
+
context=ctx,
|
|
123
|
+
objective="minimize_latency",
|
|
124
|
+
constraints={"max_latency_ms": 50, "min_confidence": 0.7},
|
|
125
|
+
)
|
|
126
|
+
print(opt.status, opt.action_taken)
|
|
127
|
+
# "ok", "fallback_cache", or "violated" with recommendations
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Why cegraph
|
|
131
|
+
|
|
132
|
+
Production ML pipelines, pricing engines, and distributed systems fail when they rely on static assumptions. cegraph solves three specific problems:
|
|
133
|
+
|
|
134
|
+
- **Root-cause analysis**: When accuracy drops, cegraph's causal trace tells you which node caused it — not just that something went wrong.
|
|
135
|
+
- **What-if simulation**: Run counterfactuals inside your live pipeline without deploying a separate simulation environment.
|
|
136
|
+
- **Adaptive fallback**: When constraints are violated (latency spikes, confidence drops), cegraph deterministically falls back to cached results or bypass mode.
|
|
137
|
+
|
|
138
|
+
### Performance
|
|
139
|
+
|
|
140
|
+
cegraph is designed for hot execution paths. The tracer uses a lock-free ring buffer, zero-copy hashing, and adaptive sampling to keep overhead under 4% — well below the 15% design constraint.
|
|
141
|
+
|
|
142
|
+
## Documentation
|
|
143
|
+
|
|
144
|
+
| Resource | Description |
|
|
145
|
+
|----------|-------------|
|
|
146
|
+
| [API Reference](docs/reference.md) | Full API documentation |
|
|
147
|
+
| [Architecture](docs/explanation.md) | How cegraph works under the hood |
|
|
148
|
+
| [Changelog](CHANGELOG.md) | Release history |
|
|
File without changes
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cegraph"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Causal-aware execution runtime for production Python systems."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {file = "LICENSE"}
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
keywords = [
|
|
13
|
+
"causal-inference", "runtime", "observability", "mlops",
|
|
14
|
+
"execution-graph", "counterfactual", "tracing", "debugging",
|
|
15
|
+
"causal-graph", "production-ml",
|
|
16
|
+
]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Intended Audience :: Science/Research",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
28
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
29
|
+
"Topic :: System :: Monitoring",
|
|
30
|
+
]
|
|
31
|
+
dependencies = ["numpy>=1.24"]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
dev = ["pytest>=7.0", "pytest-benchmark", "pytest-cov>=4.0", "ruff", "mypy", "build", "twine"]
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/keyreyla/cegraph"
|
|
38
|
+
Repository = "https://github.com/keyreyla/cegraph"
|
|
39
|
+
Issues = "https://github.com/keyreyla/cegraph/issues"
|
|
40
|
+
Changelog = "https://github.com/keyreyla/cegraph/blob/main/CHANGELOG.md"
|
|
41
|
+
|
|
42
|
+
[tool.hatch.version]
|
|
43
|
+
path = "src/cegraph/_version.py"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = ["src/cegraph"]
|
|
47
|
+
|
|
48
|
+
[tool.ruff]
|
|
49
|
+
target-version = "py310"
|
|
50
|
+
line-length = 120
|
|
51
|
+
|
|
52
|
+
[tool.ruff.lint]
|
|
53
|
+
select = ["E", "F", "I", "N", "W", "UP"]
|
|
54
|
+
|
|
55
|
+
[tool.ruff.lint.per-file-ignores]
|
|
56
|
+
"*.py" = ["N818"]
|
|
57
|
+
|
|
58
|
+
[tool.mypy]
|
|
59
|
+
python_version = "3.10"
|
|
60
|
+
strict = true
|
|
61
|
+
ignore_missing_imports = true
|
|
62
|
+
|
|
63
|
+
[tool.pytest.ini_options]
|
|
64
|
+
testpaths = ["tests"]
|
|
65
|
+
python_files = ["test_*.py"]
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from cegraph._version import __version__
|
|
2
|
+
from cegraph.causal.counterfactual import CounterfactualResult, ImpactScore, counterfactual
|
|
3
|
+
from cegraph.causal.optimizer import OptimizationResult, optimize
|
|
4
|
+
from cegraph.core.context import Context
|
|
5
|
+
from cegraph.core.graph import CausalGraph
|
|
6
|
+
from cegraph.core.node import causal_node
|
|
7
|
+
from cegraph.core.tracer import CausalTracer, TraceRecord
|
|
8
|
+
from cegraph.exceptions import (
|
|
9
|
+
CausalConstraintViolation,
|
|
10
|
+
CausalCycleError,
|
|
11
|
+
CausalTypeError,
|
|
12
|
+
CegraphError,
|
|
13
|
+
TracerOverflowWarning,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"__version__",
|
|
18
|
+
"causal_node",
|
|
19
|
+
"CausalGraph",
|
|
20
|
+
"Context",
|
|
21
|
+
"CausalTracer",
|
|
22
|
+
"TraceRecord",
|
|
23
|
+
"counterfactual",
|
|
24
|
+
"CounterfactualResult",
|
|
25
|
+
"ImpactScore",
|
|
26
|
+
"optimize",
|
|
27
|
+
"OptimizationResult",
|
|
28
|
+
"CegraphError",
|
|
29
|
+
"CausalCycleError",
|
|
30
|
+
"CausalTypeError",
|
|
31
|
+
"CausalConstraintViolation",
|
|
32
|
+
"TracerOverflowWarning",
|
|
33
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0a0"
|
|
File without changes
|