Skip to main content

A Python-based framework for graphically integrating multiple Python algorithms

Project description

🐷 PyG: PyInGraph

A Python-based framework for graphically integrating multiple Python algorithms through a node-based computational graph system.

Overview

PyInGraph provides a flexible framework for creating and executing computational graphs where each node represents a Python algorithm block. The framework supports both local and remote module loading, making it ideal for distributed algorithm development and integration.

Features

  • Node-based Architecture: Create computational graphs using algorithm blocks as nodes
  • Remote Module Support: Load algorithm blocks from remote repositories via HTTP
  • Parameter Management: Flexible parameter loading and validation system
  • State Management: Support for stateful algorithm blocks with reset capabilities
  • NetworkX Integration: Built-in support for graph visualization and analysis
  • Time-based Simulation: Support for time-dependent algorithm execution

Installation

From Source

cd PyInGraph
pip install -e .

Dependencies

  • Python >= 3.7
  • numpy
  • matplotlib
  • networkx
  • httpimport
  • requests

Quick Start

Creating a Custom Algorithm Block

from pyingraph import BlockBase

class MyAlgorithmBlock(BlockBase):
    def __init__(self):
        super().__init__()
        self.state = 0
        self.attrNamesArr = ["gain", "offset"]  # Define required parameters
    
    def read_inputs(self, inputs: list) -> None:
        if len(inputs) != 1:
            raise ValueError("Expected exactly one input")
        self.input_value = inputs[0]
    
    def compute_outputs(self, time=None) -> list:
        self.state += 1
        result = self.input_value * self.gain + self.offset + self.state
        return [result]
    
    def reset(self) -> None:
        self.state = 0

Loading and Executing a Graph

from pyingraph import GraphLoader

# Load graph from JSON description
loader = GraphLoader("my_graph.json", flag_remote=False)
loader.load()

# Get nodes and edges
nodes = loader.get_nodes()
edges = loader.get_edges()

# Get NetworkX graph for visualization
nx_graph = loader.get_nx_graph()

Graph Description Format

Create a JSON file describing your computational graph:

{
  "nodes": [
    {
      "id": "node1",
      "name": "Input Block",
      "class_file": "my_algorithm_block.py",
      "class_name": "MyAlgorithmBlock",
      "parameters": {
        "gain": 2.0,
        "offset": 1.0
      }
    }
  ],
  "edges": [
    {
      "source_node_id": "node1",
      "target_node_id": "node2",
      "source_port_idx": 0,
      "target_port_idx": 0
    }
  ]
}

Core Components

BlockBase

The abstract base class for all algorithm blocks. Every custom block must inherit from BlockBase and implement:

  • __init__(): Initialize the block and define required parameters in attrNamesArr
  • read_inputs(inputs: list): Validate and store input values
  • compute_outputs(time: float): Perform the algorithm computation
  • reset(): Reset internal states (optional)

GraphLoader

Handles loading and instantiation of computational graphs from JSON descriptions. Supports:

  • Local module loading
  • Remote module loading via HTTP
  • Parameter validation and injection
  • NetworkX graph generation

Remote Module Support

PyInGraph supports loading algorithm blocks from remote repositories:

# Load graph with remote modules
loader = GraphLoader("http://example.com/graph.json", flag_remote=True)
loader.load()

In your JSON description, specify the remote folder URL:

{
  "nodes": [
    {
      "id": "remote_node",
      "folder_url": "http://example.com/algorithms/",
      "class_file": "remote_algorithm.py",
      "class_name": "RemoteAlgorithm",
      "parameters": {}
    }
  ]
}

Development

Running Tests

pip install -e ".[dev]"
pytest

Code Formatting

black src/
flake8 src/

API Reference

BlockBase Methods

  • read_parameters(parDict: dict): Load parameters from dictionary
  • read_inputs(inputs: list): Abstract method to read input values
  • compute_outputs(time: float): Abstract method to compute outputs
  • reset(): Abstract method to reset internal states

GraphLoader Methods

  • load(): Load and parse the graph description
  • get_nodes(): Get list of instantiated node objects
  • get_edges(): Get list of edge descriptions
  • get_nx_graph(): Get NetworkX DiGraph representation

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For questions and support, please contact: bobobone@qq.com

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

pyingraph-0.1.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyingraph-0.1.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file pyingraph-0.1.1.tar.gz.

File metadata

  • Download URL: pyingraph-0.1.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyingraph-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9a2c09874a00d30b67f7176aa1541229dbb95781a60699cbada198d83b61c106
MD5 a2e31ed581a801f61e0114cee545c9f5
BLAKE2b-256 aa57da2d00ea66f14f0074251cb465e1a242bed087eaa4e8ef83f585d6d9ee51

See more details on using hashes here.

File details

Details for the file pyingraph-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyingraph-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyingraph-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0edac22f27f0bbc62c457dbf2b7e75342cadbe4c30635e490afa162cde7e2c75
MD5 92a1dcfd2f0e2784a0bdbb8196a6cbba
BLAKE2b-256 d56f71818b771b6ef24f2fa2ba87831ec72c439266c0a0c12d905e315ca78b59

See more details on using hashes here.

Supported by

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