A framework and specification language for simulating data based on user-defined graphical models
Project description
DagSim
DagSim is a Python-based framework and specification language for simulating data based on a Directed Acyclic Graph ( DAG) structure, without any constraints on variable types or functional relations. A succinct YAML format for defining the structure of the simulation model promotes transparency, while separate user-provided functions for generating each variable based on its parents ensure the modularization of the simulation code.
Installation
DagSim can be easily installed using pip.
Installing DagSim using pip
To install the DagSim package using pip
, run:
pip install dagsim
Quickstart
To check that DagSim is installed properly, run the following command in the console/terminal:
dagsim-quickstart
Installing graphviz
If you use pip
, you need to install graphviz on the system level in order to use the drawing functionality in DagSim.
Please follow the instrcutions here on how to install graphviz depending on the
operating system.
Simple example
Python code
Suppose we are interested in simulating two variables, X and Y, where X follows a standard Gaussian distribution, and Y is the square of X.
For each node we need a function to simulate the node's values:
- For X, we can use the
numpy.random.normal
function - For Y, we can use either
numpy.power
or define our own function. We will use the second to illustrate how one can use user-define functions.
# needed imports
import dagsim.base as ds
import numpy as np
Here, we define our own square
function:
def square(arg):
return arg * arg
Then, we define the nodes in our graph/model by giving each node a name, the function to use in order to evaluate its value, and the arguments of the function, if any:
X = ds.Node(name="X", function=np.random.normal)
Y = ds.Node(name="Y", function=square, kwargs={"arg": X})
After that, we define the graph itself by giving it a name (optional) and a list of all the nodes to be included:
graph = ds.Graph(name="demo_graph", list_nodes=[X, Y])
If you wish, you can draw the graph by calling the draw
method, as follows:
graph.draw()
Finally, we simulate data from this graph by calling the simulate
method, and giving it the number of samples you
want to simulate, and a name for the csv_file (optional) where the data should be saved.
data = graph.simulate(num_samples=10, csv_name="demo_data")
Here, data
would be a dictionary with keys being the names of the nodes in the graph, and the corresponding values
being the simulated values for each node returned as a Python list
.
For more detailed instructions, check
this page, and
for other simple examples, please refer to the tutorials
folder.
YAML Specification
dagsim also allows the specification of a simulation using a YAML file. You can run dagsim on a YAML file by running:
dagsim path/to/yaml/file [-v|--verbose] [-d|--draw] [-o output/path|--output_path=output/path]
For a tutorial on using a YAMl file for simulation, check this page.
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
File details
Details for the file dagsim-1.0.9.tar.gz
.
File metadata
- Download URL: dagsim-1.0.9.tar.gz
- Upload date:
- Size: 30.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8b7887ee1d176858a52ce7b74af58a382debfcb2e960c4cb319371b70c1802d |
|
MD5 | 1e55426a3b26e938891f8aab2b06c4c9 |
|
BLAKE2b-256 | 7e72d656f862fb51fb5e427488bbac44fa1c00505b6d0b321083d695e355a9a8 |
File details
Details for the file dagsim-1.0.9-py3-none-any.whl
.
File metadata
- Download URL: dagsim-1.0.9-py3-none-any.whl
- Upload date:
- Size: 32.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a63b3ab641e697c8ce21fe44b2416ac2a7439812cbc7ef1a414dcebcd464b7fa |
|
MD5 | 38672e4e8b38d6206706677b1e5200e4 |
|
BLAKE2b-256 | a8ba246964c426e8a5a5ce8f9cc5ecfe93d9d4782a1f31fbbab13df104ef035e |