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.2.tar.gz (11.2 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.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyingraph-0.1.2.tar.gz
  • Upload date:
  • Size: 11.2 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.2.tar.gz
Algorithm Hash digest
SHA256 63b9bd5f235c32cf3790dcfefd115a00ee4d5c00b4e8cfbfcbad8f8df9dcda71
MD5 c1bc46efdd619ade120ed5906dcb12c4
BLAKE2b-256 2dbc109d35c78a325b5ceb9d4a909836932602e0350b5be5ea3aefa1d6f9ef5f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyingraph-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f1471e641aa795c68596c0f863e2a844895e2833b1faaa5ed35bd92ffb84c1b5
MD5 40bcb028cbd89b86e42aa546c7f40ddf
BLAKE2b-256 1c832d5b667a089030e6c34cacdaa8a16de20a755596c32d6e083bd585bc5c5f

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