Skip to main content

Modular modeling framework for scientific modeling and prototyping.

Project description

GitHub version PyPI version shields.io PyPI pyversions Unittests Docs

MModel is a lightweight and modular model-building framework for small-scale and nonlinear models. The package aims to solve scientific program prototyping and distribution difficulties, making it easier to create modular, fast, and user-friendly packages.

Quickstart

To create a nonlinear model that has the result of (x + y)log(x + y, base):

import math
import numpy as np

def func(sum_xy, log_xy):
    """Function that adds a value to the multiplied inputs."""

    return sum_xy * log_xy + 6

The graph is defined using grouped edges (the networkx syntax of edge the definition also works.)

from mmodel import ModelGraph, Model, MemHandler
# create graph edges
grouped_edges = [
    ("add", ["log", "function node"]),
    ("log", "function node"),
]

The functions are then added to node attributes. The order of definition is node_name, node_func, output, input (if different from original function), and modifiers.

# define note objects
node_objects = [
    ("add", np.add, "sum_xy", ["x", "y"]),
    ("log", math.log, "log_xy", ["sum_xy", "log_base"]),
    ("function node", func, "result"),
]

G = ModelGraph(name="example_graph")
G.add_grouped_edges_from(grouped_edges)
G.set_node_objects_from(node_objects)

To define the model, the name, graph, and handler need to be specified. Additional parameters include modifiers, descriptions, and returns lists. The input parameters of the model are determined based on the node information.

example_model = Model("example_model", G, handler=MemHandler, description="Test model.")

The model behaves like a Python function, with additional metadata. The graph can be plotted using the draw method.

>>> print(example_model)
example_model(log_base, x, y)
returns: z
graph: example_graph
handler: MemHandler

Test model.

>>> example_model(2, 5, 3) # (5 + 3)log(5 + 3, 2) + 6
30.0

>>> example_model.draw()

The resulting graph contains the model metadata and detailed node information.

One key feature of mmodel that differs from other workflow is modifiers, which modify callables post definition. Modifiers work on both the node level and model level.

Example: Use loop_input modifier on the graph to loop the nodes that require the “log_base” parameter.

from mmodel import loop_input

H = G.subgraph(inputs=["log_base"])
H.name = "example_subgraph"
loop_node = Model("submodel", H, handler=MemHandler)

looped_G = G.replace_subgraph(
    H,
    "loop_node",
    loop_node,
    output="looped_z",
    modifiers=[loop_input("log_base")],
)
looped_G.name = "looped_graph"

looped_model = Model("looped_model", looped_G, loop_node.handler)

We can inspect the loop node as well as the new model.

>>> print(looped_model)
looped_model(log_base, x, y)
returns: looped_z
graph: looped_graph
handler: MemHandler()

>>> print(looped_model.node_metadata("loop_node"))
submodel(log_base, sum_xy)
return: looped_z
functype: mmodel.Model
modifiers:
  - loop_input('log_base')

>>> looped_model([2, 4], 5, 3) # (5 + 3)log(5 + 3, 2) + 6
[30.0, 18.0]

Use the draw method to draw the graph. There are three styles “plain”, “short”, and “verbose”, which differ by the level of detail of the node information. A graph output is displayed in Jupyter Notebook or can be saved using the export option.

G.draw(style="short")
example_model.draw(style="plain", export="example.pdf") # default to draw_graph

Installation

Graphviz installation

To view the graph, Graphviz needs to be installed: Graphviz Installation For windows installation, please choose “add Graphviz to the system PATH for all users/current users” during the setup.

MModel installation

pip install mmodel

Development installation

MModel uses poetry as the build system. The package works with both pip and poetry installation. For macos systems, sometimes brew install results in unexpected installation path, it is recommended to install with conda:

conda install -c conda-forge pygraphviz

To install test and docs, despondencies run:

pip install .[test] .[docs]

To run the tests in different python environments and cases (py38, py39, py310, py311, coverage and docs):

tox

To create the documentation, run under the “/docs” directory:

make html

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mmodel-0.6.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

mmodel-0.6.0-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file mmodel-0.6.0.tar.gz.

File metadata

  • Download URL: mmodel-0.6.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.13 Windows/10

File hashes

Hashes for mmodel-0.6.0.tar.gz
Algorithm Hash digest
SHA256 96a7f8e4d6787d9dc8c55006dc4ff7c5847b91dbea34501963d9c28714e1048f
MD5 c3b6c1d1004ab5df26cf3be48ccc3f1a
BLAKE2b-256 64ca7acef45111f68df71de98c0fc9197c6206539fbbae4f90729eff6c59b8e0

See more details on using hashes here.

File details

Details for the file mmodel-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: mmodel-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.13 Windows/10

File hashes

Hashes for mmodel-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c6dd1519f10f3c29e55e61d2609b533cd2560e400da11bd9d3f88b729bc72b3
MD5 a1360d4f733bfc8ada909783f8665b65
BLAKE2b-256 a5b7b081c1fe67852b6dd30066fd3060038069d12abcf89ab278a0311ec204c1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page