Metadata-Version: 2.4
Name: cfdl-sdk
Version: 0.14.0
Classifier: License :: Other/Proprietary License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Rust
Requires-Dist: pandas>=2.0
Requires-Dist: pytest>=8 ; extra == 'dev'
Requires-Dist: maturin>=1.7,<2.0 ; extra == 'dev'
Requires-Dist: jupyter ; extra == 'notebooks'
Requires-Dist: matplotlib>=3.8 ; extra == 'notebooks'
Requires-Dist: nbconvert ; extra == 'notebooks'
Requires-Dist: papermill ; extra == 'notebooks'
Requires-Dist: matplotlib>=3.8 ; extra == 'viz'
Provides-Extra: dev
Provides-Extra: notebooks
Provides-Extra: viz
Summary: Python bindings for the CFDL SDK
Home-Page: https://cfdl.dev
Author: Matthew McCrea
License: PolyForm-Noncommercial-1.0.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://cfdl.dev
Project-URL: Repository, https://github.com/cfdl-dev/cfdl

# CFDL Python SDK

`cfdl-sdk` compiles and runs [CFDL](https://cfdl.dev) cash-flow models from
Python, with pandas accessors over the results. The compiler and engine are
embedded in-process (a Rust extension module) — no separate binary or server.

## Install (editable, local)

From the repository root:

```bash
pip install -e "python/[dev,viz]"
```

`pandas>=2.0` is a hard dependency. The optional `viz` extra adds matplotlib.

## Quickstart

```python
import cfdl_sdk

results = cfdl_sdk.run(
    "examples/cre_developer",
    packs_dir="packs",
    config="examples/cre_developer/run.base.json",
)

results.cashflows()      # wide DataFrame: one column per stream, PeriodIndex
results.metrics()        # flat Series of core + domain metrics
results.metrics_frame()  # metric / value / currency / source(core|domain:<pack>)
results.scenarios()      # one row per scenario (when the run declares them)
results.monte_carlo()    # per-metric summary stats (mean/stdev/min/max/p50)
results.search()         # a search's candidates; path and winner in .attrs
results.annual()         # annual rollup, when present
```

`config` accepts a dict, a path to a run-config JSON file, or a raw JSON
string. `pack` (for domain metrics) is auto-detected from a `pack` file in the
model directory, or pass it explicitly. Split compile/run:

```python
model = cfdl_sdk.compile("examples/cre_developer", packs_dir="packs")
results = model.run(config={"deterministic": {"annual_discount_rate": 0.08}})
```

## Charts and plotting (`[viz]` extra)

A run publishes its charts, the pack's, the run configuration's and the
defaults, each resolved by the engine. `results.charts()` lists them, the
default first, and `results.plot.chart` draws one as the playground does,
in the site's colors, returning the matplotlib `Axes`:

```python
results.charts()                                  # id, label, kind, origin, default, diagnostics
results.plot.chart()                              # the run's default chart
results.plot.chart("returns")                     # a chart by its id
results.plot.chart("noi", grain="quarterly")      # monthly, quarterly, semiannual or annual
results.plot.chart("trend", scenario="downside")  # a scenario's own run
results.plot.distribution("model.npv")            # any metric across the trials
results.plot.cashflows()                          # step plot of per-period flows
results.plot.cumulative()                         # cumulative net cash flow
```

Periods fold into the grain by each series' own rule (cash sums, a balance
takes its last month, a ratio divides the sums), percentiles are the
engine's, and values read by their unit. `dark=True` draws in the dark theme;
`cfdl_sdk.viz.use_style()` applies the CFDL style to every figure.

## Errors

`CompileError` carries structured `.diagnostics` (each a `Diagnostic` with
`code`, `message`, `span`, ...); `RunError` carries an engine message. Both
subclass `CfdlError`.

## Low-level passthrough

`compile_model(model_dir, packs_dir=None) -> str` and
`run_ir(ir_json, packs_dir=None, config_json=None, rate=0.0, as_of=None,
pack=None, trials=None) -> str` return raw JSON strings, mirroring the `cfdl`
CLI. `trials` names the partition a run computes (`"none"` for the base partition,
`"<from>..<to>"` for a range of Monte Carlo trials);
`merge_results(partitions, entities=False) -> str` merges partitions into the document
the whole run produces, or sub-pool runs by summing their cash, and
`partition_eligibility(ir_json) -> str` reports whether a model can be partitioned by
entity: its `parts`, the `feedback` that keeps it whole, and the
`truncations`, such as a clean-up call, that partition by applying the write to
each sub-pool. `cfdl_sdk.merge(partitions)` is the same over `Results` objects, and
`Model.run(trials=...)` the same keyword.

## Tests

```bash
make py-test     # or: python3 -m pytest -q python/tests
```

