Civic-Digital-Twins Modeling Framework
Project description
Civic-Digital-Twins Modeling Framework
This repository contains a Python package implementing a Civic-Digital-Twins modeling framework. The framework is designed to support defining digital twins models and evaluating them in simulated environments with varying contextual conditions. We develop this package at @fbk-most, a research unit at Fondazione Bruno Kessler.
Note: this package is currently in an early development stage.
Conceptual Overview
The framework is organised in three layers.
Engine layer
The engine (civic_digital_twins.dt_model.engine) is an embedded DSL
compiler. The programmer builds a computation graph (DAG) by composing
typed nodes — constants, placeholders, and operations — using ordinary Python
expressions. The graph is then linearised by topological sorting and
evaluated by a NumPy-based interpreter that maps each node to the
corresponding numpy operation.
from civic_digital_twins.dt_model.engine.frontend import graph, linearize
from civic_digital_twins.dt_model.engine.numpybackend import executor
import numpy as np
a = graph.placeholder("a")
b = graph.placeholder("b")
c = a * 2 + b
state = executor.State(values={a: np.asarray(3.0), b: np.asarray(1.0)})
executor.evaluate_nodes(state, *linearize.forest(c))
print(state.get_node_value(c)) # 7.0
See docs/design/dd-cdt-engine.md for a full description of the engine.
Model / simulation layer
The model layer (civic_digital_twins.dt_model) provides higher-level
abstractions built on top of the engine:
Index/TimeseriesIndex— named wrappers around graph nodes. An index can be a constant, a distribution (sampled at evaluation time), or a formula.Model— a named collection ofIndexobjects. A model is abstract when some indexes are unbound (distributions or placeholders); it becomes concrete once all indexes are resolved.Evaluation— evaluates a model over a sequence of weighted scenarios, each of which maps every abstract index to a concrete value.Ensemble/WeightedScenario— a protocol and type alias that define the scenario contract consumed byEvaluation.
from civic_digital_twins.dt_model import Evaluation, Model
from civic_digital_twins.dt_model.model.index import Index, UniformDistIndex
# Two distribution-backed indexes
x = UniformDistIndex("x", loc=0.0, scale=1.0)
y = UniformDistIndex("y", loc=0.0, scale=1.0)
result = Index("result", x + y)
model = Model("example", [x, y, result])
Usage patterns
Two concrete usage patterns are illustrated in the examples/ directory.
Direct pattern (examples/mobility_bologna/) — the model consists
entirely of Index and TimeseriesIndex objects;
DistributionEnsemble samples each distribution-backed index to produce
weighted scenarios; Evaluation.evaluate() runs the engine and returns an
EvaluationResult.
Vertical extension pattern (examples/overtourism_molveno/) — the
domain-specific layer introduces ContextVariable, PresenceVariable,
Constraint, and OvertourismModel on top of the core types.
OvertourismEnsemble samples context variables and produces weighted
scenarios. Evaluation.evaluate(axes={pv: array, …}) evaluates the model
on a multi-dimensional grid, returning arrays of shape (N₀, …, Nₖ, S)
where S is the number of scenarios and each Nᵢ corresponds to one
presence axis.
Installation
The package name is civic-digital-twins on PyPi. Install
using pip:
pip install civic-digital-twins
or, using uv:
uv add civic-digital-twins
The main package name is civic_digital_twins:
import civic_digital_twins
or
from civic_digital_twins import dt_model
Minimum Python Version
Python 3.11.
API Stability Guarantees
The package is currently in an early development stage. We do not anticipate breaking APIs without a good reason to do so, yet, breaking changes may occur from time to time. We generally expect subpackages within the top-level package to change more frequently.
Development Setup
We use uv for managing the development environment.
To get started, run:
git clone https://github.com/fbk-most/civic-digital-twins
cd civic-digital-twins
uv venv
source .venv/bin/activate
uv sync --dev
We use pytest for testing. To run tests use this command (from inside the virtual environment):
pytest
Each pull request is automatically tested using GitHub Actions. The workflow
is defined in .github/workflows/test.yml.
Updating Dependencies
uv self update
uv sync --upgrade
Releasing
-
Make sure the version number in
pyproject.tomlis correct. -
Update
CHANGELOG.md: set the release date on the current version section and add a comparison link at the bottom. -
Create and push a git tag:
git tag v<version> && git push origin v<version>. -
Build the package:
uv build. -
Check the artifacts:
uv run --with twine twine check dist/*. -
Upload to PyPI:
uv run --with twine twine upload dist/*.
Documentation
| Document | Description |
|---|---|
| Getting Started | Step-by-step guide covering the direct and vertical extension usage patterns. |
| dd-cdt-engine.md | DSL compiler engine — graph nodes, topological sorting, NumPy executor. |
| dd-cdt-model.md | Model / simulation layer — Model, Evaluation, WeightedScenario, and the vertical extension pattern. |
License
SPDX-License-Identifier: Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file civic_digital_twins-0.6.0.tar.gz.
File metadata
- Download URL: civic_digital_twins-0.6.0.tar.gz
- Upload date:
- Size: 152.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
319e7b11ffba3b141d66bc870877471ff804bac7dbb3f901f136618111c4594a
|
|
| MD5 |
623d41e131fc8c3c089958cf9d96b504
|
|
| BLAKE2b-256 |
64a8dda59ec1a540c16ecb567707f774f587a9eaffeecc50c92f6b2aecb5fe45
|
File details
Details for the file civic_digital_twins-0.6.0-py3-none-any.whl.
File metadata
- Download URL: civic_digital_twins-0.6.0-py3-none-any.whl
- Upload date:
- Size: 41.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
862a6dd33538e30649af701ceaa1892a0af04b85362c38ab1863cbae3a0d049c
|
|
| MD5 |
562c9a7e9dcabfb8287a3ab78aefa8d7
|
|
| BLAKE2b-256 |
9338cab34f12bb59ac41673a8c650ec098621c383d977e00317567733686d4bc
|