Skip to main content

Fast subgraph isomorphism (C++ backend) for Python

Project description

fastiso

This is a Python module built on CPython that provides bindings for the C++ solver FASTiso.
The library supports:

  • Graph isomorphism
  • Subgraph isomorphism
  • Monomorphism (Non-induced subgraph isomorphism)

Author

Camille Coti. Vladimir Reinharz and Wilfried Agbeto.

Usage

CSV interface

fastiso accept csv format.

import fastiso

pattern = ['3', '0,1,B53', '1,2,CWW']
target = ['4', 'a,b,B53', 'b,c,CWW', 'c,d,CHH']
"""
csv_iso(
    pattern:iterable,
    target:iterable,
    algo:"iso|sub-iso|monomorphism",
    format:"csv|ucsv",
    get_all_solution:bool, 
    count_solution:bool
)
"""
print(fastiso.csv_iso(pattern, target, "sub-iso", "csv", 0, 1)) #get 1
print(fastiso.csv_iso(pattern, target, "monomorphism", "csv", 1, 0)) #get [{'0': 'a', '1': 'b', '2': 'c'}]

NetworkX interface

fastiso integrates natively with networkx.Graph (networkx.MultiGraph) or nx.DiGraph (networkx.MultiDiGraph) objects.

import networkx as nx
import fastiso

pattern = nx.DiGraph()
pattern.add_edge(0, 1, label="B53")
pattern.add_edge(1, 2, label="CWW")

target = nx.DiGraph()
target.add_edge("a", "b", label="B53")
target.add_edge("b", "c", label="CWW")
target.add_edge("c", "d", label="CHH")
"""
nx_iso(        
    pattern:NetworkxGraph, 
    target:NetworkxGraph, 
    algo="iso|sub-iso|monomorphism", 
    all_solution:bool, 
    count_solution:bool,
    node_label_key=None, 
    edge_label_key=None,
    node_label_eq_fn=None, 
    edge_label_eq_fn=None 
)
"""
print(fastiso.nx_iso(pattern, target, "sub-iso", all_solution=0, count_solution=1, edge_label_key="label")) #get 1
print(fastiso.nx_iso(pattern, target, "monomorphism", all_solution=1, count_solution=0, edge_label_key="label")) #get [{'0': 'a', '1': 'b', '2': 'c'}]

Node & edge attributes

In many applications, it is often useful to assign labels to nodes or edges. You can use the parameters node_label_key and edge_label_key to specify which node or edge attributes should be used as labels.

FASTiso operates on the principle of node and edge "coloring" based on these labels. Two nodes or edges can only be mapped if they share the same color (label). By default, this is determined by strict equality (label1 == label2).

Through the node_label_eq_fn and edge_label_eq_fn parameters, you can define custom logic for label equality, allowing for more flexible comparisons. However, please note that your custom comparison method must not violate the property of disjoint color classes: each node or edge must belong to exactly one color class. FASTiso does not support cases where a node could belong to multiple color classes simultaneously.

def eq_fn(label1, label2) -> bool:
    ...

True  same color
False  different colors
import networkx as nx
import fastiso

pattern = nx.DiGraph()
pattern.add_node(1, type='Fe')
pattern.add_node(2, type='O')
pattern.add_edge(1, 2)
pattern.add_edge(2, 1)

target = nx.DiGraph()
target.add_node("a", type='Cu')
target.add_node("b", type='N')
target.add_edge("a", "b")
target.add_edge("b", "a")

print(fastiso.nx_iso(pattern, target, "iso", all_solution=0, count_solution=1, node_label_key="type")) #get 0
##grouping labels by families
def are_same_family(label1, label2):
    families = {
        'Fe': 'Metal', 'Cu': 'Metal',
        'O': 'Non-Metal', 'N': 'Non-Metal'
    }
    return families.get(label1) == families.get(label2)
print(fastiso.nx_iso(pattern, target, "sub-iso", all_solution=0, count_solution=1, node_label_key="type", node_label_eq_fn=are_same_family)) #get 1

File Formats

CSV

The CSV format is recommended for labeled graphs, each line represents an edge or a node, except the first line gives the number n of nodes.

unlabeled graph,

  • edge : "node1_id,node2_id"
  • node : "node_id"
3
0,1
1,0
1,2
2,1
0,2
2,0

node label

to add a node label, put a semicolon (;) after node id.

  • edge : "node1_id;node1_label,node2_id;node2_label"
  • node : "node_id;node_label"
3
0;a,1;b
1;b,0;a
1;b,2;a
2;a,1;b
0;a,2;a
2;a,0;a

edge label

To add an edge label, add a third column : "node1_id;node1_label,node2_id;node2_label",edge_label.

6
0,1,s
1,0,s
1,2,t
2,1,t
0,2,d1
2,0,d2
3,2,t
2,3,t
4,1,s
1,4,s
5

By default, the CSV format is for directed graphs. for undirected graphs use UCSV.

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

fastiso-0.1.1.tar.gz (30.1 MB view details)

Uploaded Source

Built Distribution

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

fastiso-0.1.1-cp313-cp313-macosx_15_0_arm64.whl (241.3 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

File details

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

File metadata

  • Download URL: fastiso-0.1.1.tar.gz
  • Upload date:
  • Size: 30.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for fastiso-0.1.1.tar.gz
Algorithm Hash digest
SHA256 128d9ebb5a911049d06fc017cf8fe5b98be487f8216d3437fe89e72cc4e98141
MD5 71b569a5c8053484c106ccca11813d39
BLAKE2b-256 8e2da0ff7da30c45c4024db7d788d9ef09435262e9ea30c701316ddd4249ec8a

See more details on using hashes here.

File details

Details for the file fastiso-0.1.1-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for fastiso-0.1.1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 e0fc0cb6bc6b8b04c878c48ae0c4a398449257cc2325f1d08f0dbf3b4fb3c161
MD5 26625190ea50232a3c0b77af7885dbf4
BLAKE2b-256 33abb9fcb9abf70e12f745b254010f666bbb16f4ff783c1cc6431e4bdd07c5c6

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