Skip to main content

Modular modeling framework for nonlinear scientific models

Project description

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. The package is well tested with 100 % coverage.

Quickstart

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

from mmodel import ModelGraph, Model, MemHandler
import math

def func_a(x, y):
    return x + y

def func_b(sum_xy, base):
    return math.log(sum_xy, base)

def func_c(sum_xy, log_xy):
    return sum_xy * log_xy

# create graph links

grouped_edges = [
    ("func a", ["func b", "func c"]),
    ("func b", "func c"),
]

node_objects = [
    ("func a", func_a, ["sum_xy"]),
    ("func b", func_b, ["log_xy"]),
    ("func c", func_c, ["result"]),
]

graph = ModelGraph(name="Example")
graph.add_grouped_edges_from(grouped_edges)
graph.set_node_objects_from(node_objects)

example_func = Model(graph, handler=MemHandler)

>>> print(example_func)
Example model
  signature: base, x, y
  returns: result
  handler: MemHandler
  modifiers: none

>>> example_func(2, 5, 3) # (5 + 3)log(5 + 3, 2)
24.0

The resulting example_func is callable.

One key feature of mmodel is modifiers, which modify callables post definition. To loop the “base” parameter.

from mmodel import subgraph_by_parameters, modify_subgraph, loop_modifier

subgraph = subgraph_by_parameters(graph, ["base"])
loop_node = Model(subgraph, MemHandler, [loop_modifier("base")])
looped_graph = modify_subgraph(graph, subgraph, "loop node", loop_node)
looped_model = Model(looped_graph, handler=MemHandler)

>>> print(looped_model)
Example model
  signature: base, x, y
  returns: result
  handler: MemHandler
  modifiers: none

>>> looped_model([2, 4], 5, 3) # (5 + 3)log(5 + 3, 2)
[24.0, 12.0]

Modifiers can also be added to the whole model or a single node.

To draw the graph or the underlying graph of the model:

graph.draw()
example_func.draw()

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.

To install test and docs, despondencies run:

pip install .[test] .[docs]

(For zsh shell, run pip install ".[test]")

To run the tests, run:

pytest

To run the tests in different python environments (py38, py39, 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.3.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

mmodel-0.3.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mmodel-0.3.0.tar.gz
  • Upload date:
  • Size: 13.8 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.3.0.tar.gz
Algorithm Hash digest
SHA256 3acdeed051983b286ff215039ff28633b4ae035b851c9d11d98644b5f70975b7
MD5 6bdb8221278c7f46cc40dcc21cc367c7
BLAKE2b-256 5cb526102034a9df21fb8d53f36ee52995be85c1798270272eb9f39208b8fc07

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mmodel-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dcc50a7bab41ba952b3a01ab61fadbc281ce8a5d5a51d4654a80804f8644ff5c
MD5 89dc070ff23c42d7e43f2b7a4a9af43a
BLAKE2b-256 92e9205cbba2e2d142da5c75116a06b3060510ab19410500479c89593b3cb793

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