Skip to main content

A Python-based framework for graphically integrating multiple Python algorithms

Project description

🐷 PyG: PyInGraph

🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷
+---------+     +---------+      +---------+
|   🐍 A  | --> |   🐍 B  | --> |   🐍 C  |
+---------+     +---------+      +---------+
     |                                |
     v                                v
+---------+                      +---------+
|   🐍 D  | <------------------  |   🐍 E  |
+---------+                      +---------+
🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷

A Python framework for creating computational graphs where each node represents an algorithm block. Perfect for integrating multiple algorithms into a visual, executable workflow.

Quick Start

Installation

From PyPI (recommended):

pip install pyingraph

From source:

cd PyInGraph
pip install -e .

Try the Examples

Run the included demo to see PyInGraph in action:

cd examples
python demo_simple_add.py

This demo loads a simple graph that adds two numbers using custom algorithm blocks.

Key Features

  • Easy Workflow Integration: Algorithms, with inputs/outputs/internal-states, can be easily integrated via a graph structure. This graph-based approach, besides being code-efficient, enables a broad category of usages, such as system simulation, AI workflows, and network analysis (e.g., connectivity, condensation, etc.).
  • Local & Remote Modules: Load algorithms from files or HTTP repositories
  • Built-in Visualization: See your computational graph with NetworkX
  • Parameter Management: Configure algorithm parameters via JSON

How It Works

1. Create Algorithm Blocks

Each algorithm is a Python class that inherits from BlockBase:

Block 1 of 2: mod_source_constant.py

from pyingraph import BlockBase

class ConstantSource(BlockBase):
    """
    A constant source block that outputs a user-specified constant value.
    """
    def __init__(self):
        super().__init__()
        self.attrNamesArr = ["value"]  # Parameter name for the constant value
        self.value = 0.0  # Default value
    
    def read_inputs(self, inputs: list) -> None:
        pass
    
    def compute_outputs(self, time: float = None) -> list:
        return [self.value]
    
    def reset(self) -> None:
        pass

Block 2 of 2: mod_sink_print.py

from pyingraph import BlockBase

class SinkPrint(BlockBase):
    """
    A print sink block that prints all inputs it receives.
    This is useful for debugging and monitoring data flow in the graph.
    """
    def __init__(self):
        super().__init__()
        self.attrNamesArr = []  # No parameters needed
    
    def read_inputs(self, inputs: list) -> None:
        self.inputs_received = inputs
    
    def compute_outputs(self, time: float = None) -> list:
        print(f"SinkPrint received: {self.inputs_received}")
        return []  # Sink blocks typically don't produce outputs
    
    def reset(self) -> None:
        self.inputs_received = None

2. Define Your Graph

Create a JSON file graph_example.json describing your computational graph:

{
    "nodes": [
        {
            "id": "node1",
            "name": "Constant Source 1",
            "folder_url": "",
            "folder_path": "./",
            "class_file": "mod_source_constant.py",
            "class_name": "ConstantSource",
            "parameters": {
                "value": 1.0
            }
        },
        {
            "id": "node2",
            "name": "Sink print",
            "folder_url": "",
            "folder_path": "./",
            "class_file": "mod_sink_print.py",
            "class_name": "SinkPrint",
            "parameters": {}
        }
    ],
    "edges": [
        {
            "source_node_id": "node1",
            "target_node_id": "node2",
            "source_port_idx": 0,
            "target_port_idx": 0,
            "properties": {}
        }
    ]
}

3. Load and Run

from pyingraph import GraphLoader

loader = GraphLoader("graph_example.json", flag_remote=False)
loader.load()
nx_graph = loader.get_nx_graph()
loader.visualize_graph(block=True)  # See your graph
loader.simple_traverse_graph()  # Execute the graph

[!IMPORTANT] Make sure all module files are in the current or child directories of the above loader script. The project-folder-based path should be specified in the folder_path field of the graph JSON file.

Examples Included

The examples/ folder contains ready-to-run demos:

  • demo_simple_add.py: Simple demo of a basic graph that adds two numbers, using local modules and graph
  • demo_control_system.py: Control system simulation example, using remote modules and graph
  • graph_simple_demo.json: Graph definition for the simple demo demo_simple_add.py
  • local_modules/: Sample algorithm blocks:
    • mod_source_constant.py: Constant value source
    • mod_summer.py: Addition operation
    • mod_sink_print.py: Output print as display

Remote Module Support

Load algorithm blocks from HTTP repositories by setting flag_remote=True:

loader = GraphLoader("http://example.com/graph.json", flag_remote=True)

By default, flag_remote=False

Getting Started

  1. Install: pip install -e .
  2. Run demo: cd examples && python demo_simple_add.py
  3. Study examples: Check out the local_modules/ for sample blocks
  4. Create your own: Inherit from BlockBase and define your algorithm
  5. Build graphs: Write JSON descriptions connecting your blocks

Dependencies

  • Python >= 3.7
  • numpy, matplotlib, networkx
  • httpimport, requests (for remote modules)

Support

Questions? Contact: bobobone@qq.com

License

MIT License - see LICENSE file for details.

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.5.tar.gz (11.5 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.5-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyingraph-0.1.5.tar.gz
  • Upload date:
  • Size: 11.5 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.5.tar.gz
Algorithm Hash digest
SHA256 27eac80b08e3ea4966ee0bca1fc4ba66f6e0b737094298e728abd6a24cf84cb2
MD5 1af4441543fcd163ee47a5912cbb9edd
BLAKE2b-256 10a5c4d1264126d1df18263561df145a0c60d97d3918053ae0a2fcdba0d0e01d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyingraph-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 9.9 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8a94c676ea2feffa385391d188f135c900b6814b5b2e7830c1e23c9586ac9744
MD5 cbe9d7f4bd3f09126814c96300529f3b
BLAKE2b-256 dec988072fd8e5d2b1f327a5a8ecb97f61890d8efa221282721e443faa232b3c

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