Skip to content

Commit 54a4b1d

Browse files
committed
working: refactorize gedlibpy module.
1 parent 85032ce commit 54a4b1d

20 files changed

+2958
-939
lines changed

gklearn/gedlib/bind_errors.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
bind_errors
3+
4+
5+
6+
@Author: jajupmochi
7+
@Date: May 31 2025
8+
"""
9+
10+
11+
#####################
12+
##ERRORS MANAGEMENT##
13+
#####################
14+
15+
class Error(Exception):
16+
"""
17+
Class for error's management. This one is general.
18+
"""
19+
pass
20+
21+
22+
class EditCostError(Error):
23+
"""
24+
Class for Edit Cost Error. Raise an error if an edit cost function doesn't exist in the library (not in list_of_edit_cost_options).
25+
26+
:attribute message: The message to print when an error is detected.
27+
:type message: string
28+
"""
29+
30+
31+
def __init__(self, message):
32+
"""
33+
Inits the error with its message.
34+
35+
:param message: The message to print when the error is detected
36+
:type message: string
37+
"""
38+
self.message = message
39+
40+
41+
class MethodError(Error):
42+
"""
43+
Class for Method Error. Raise an error if a computation method doesn't exist in the library (not in list_of_method_options).
44+
45+
:attribute message: The message to print when an error is detected.
46+
:type message: string
47+
"""
48+
49+
50+
def __init__(self, message):
51+
"""
52+
Inits the error with its message.
53+
54+
:param message: The message to print when the error is detected
55+
:type message: string
56+
"""
57+
self.message = message
58+
59+
60+
class InitError(Error):
61+
"""
62+
Class for Init Error. Raise an error if an init option doesn't exist in the library (not in list_of_init_options).
63+
64+
:attribute message: The message to print when an error is detected.
65+
:type message: string
66+
"""
67+
68+
69+
def __init__(self, message):
70+
"""
71+
Inits the error with its message.
72+
73+
:param message: The message to print when the error is detected
74+
:type message: string
75+
"""
76+
self.message = message

gklearn/gedlib/common_bind.pxd

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# distutils: language = c++
2+
3+
"""
4+
Python GedLib module for the common definition bindings
5+
======================
6+
7+
This module allows using a C++ library for edit distance between graphs (GedLib) with Python.
8+
9+
10+
Authors
11+
-------------------
12+
Linlin Jia
13+
David Blumenthal
14+
Natacha Lambert
15+
16+
Copyright (C) 2019-2025 by all the authors
17+
18+
Classes & Functions
19+
-------------------
20+
21+
"""
22+
23+
from libcpp.vector cimport vector
24+
from libcpp.string cimport string
25+
26+
27+
#################################
28+
##DECLARATION OF C++ INTERFACES##
29+
#################################
30+
31+
32+
cdef extern from "src/gedlib_bind.hpp" namespace "pyged":
33+
cdef vector[string] getEditCostStringOptions() except +
34+
cdef vector[string] getMethodStringOptions() except +
35+
cdef vector[string] getInitStringOptions() except +
36+
cdef size_t getDummyNode() except +

gklearn/gedlib/common_bind.pyx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# distutils: language = c++
2+
3+
"""
4+
Python GedLib module for the common definition bindings
5+
======================
6+
7+
This module allows using a C++ library for edit distance between graphs (GedLib) with Python.
8+
9+
10+
Authors
11+
-------------------
12+
Linlin Jia
13+
David Blumenthal
14+
Natacha Lambert
15+
16+
Copyright (C) 2019-2025 by all the authors
17+
18+
Classes & Functions
19+
-------------------
20+
21+
"""
22+
23+
#############################
24+
##CYTHON WRAPPER INTERFACES##
25+
#############################
26+
27+
28+
def get_edit_cost_options():
29+
"""
30+
Searchs the differents edit cost functions and returns the result.
31+
32+
:return: The list of edit cost functions
33+
:rtype: list[string]
34+
35+
.. warning:: This function is useless for an external use. Please use directly list_of_edit_cost_options.
36+
.. note:: Prefer the list_of_edit_cost_options attribute of this module.
37+
"""
38+
39+
return [option.decode('utf-8') for option in getEditCostStringOptions()]
40+
41+
42+
def get_method_options():
43+
"""
44+
Searchs the differents method for edit distance computation between graphs and returns the result.
45+
46+
:return: The list of method to compute the edit distance between graphs
47+
:rtype: list[string]
48+
49+
.. warning:: This function is useless for an external use. Please use directly list_of_method_options.
50+
.. note:: Prefer the list_of_method_options attribute of this module.
51+
"""
52+
return [option.decode('utf-8') for option in getMethodStringOptions()]
53+
54+
55+
def get_init_options():
56+
"""
57+
Searchs the differents initialization parameters for the environment computation for graphs and returns the result.
58+
59+
:return: The list of options to initialize the computation environment
60+
:rtype: list[string]
61+
62+
.. warning:: This function is useless for an external use. Please use directly list_of_init_options.
63+
.. note:: Prefer the list_of_init_options attribute of this module.
64+
"""
65+
return [option.decode('utf-8') for option in getInitStringOptions()]
66+
67+
68+
def get_dummy_node():
69+
"""
70+
Returns the ID of a dummy node.
71+
72+
:return: The ID of the dummy node (18446744073709551614 for my computer, the hugest number possible)
73+
:rtype: size_t
74+
75+
.. note:: A dummy node is used when a node isn't associated to an other node.
76+
"""
77+
return getDummyNode()
78+
79+
80+
#####################################################################
81+
##LISTS OF EDIT COST FUNCTIONS, METHOD COMPUTATION AND INIT OPTIONS##
82+
#####################################################################
83+
84+
list_of_edit_cost_options = get_edit_cost_options()
85+
list_of_method_options = get_method_options()
86+
list_of_init_options = get_init_options()

gklearn/gedlib/gedlibpy.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
gedlibpy
3+
4+
Helper for GEDLIBPY Python bindings.
5+
6+
@Author: jajupmochi
7+
@Date: May 31 2025
8+
"""
9+
from gedlibpy_gxl import GEDEnvGXL
10+
from gedlibpy_attr import GEDEnvAttr
11+
12+
13+
def GEDEnv(env_type='attr'):
14+
"""
15+
A factory function that returns the appropriate GED environment.
16+
17+
This function provides a unified interface for different GED environments, such as GXL
18+
and attribute-based graphs.
19+
The specific environment will be selected based on the provided parameter `env_type`.
20+
21+
Parameters
22+
----------
23+
env_type : str
24+
The type of the GED environment to initialize. Options are 'gxl', 'str', or 'attr'.
25+
'gxl' and 'str' initialize a GXL-based environment, where node and edge labels are strings.
26+
The default is 'attr', which initializes an attribute-based environment,
27+
where node and edge labels can be dictionaries with string keys and values of
28+
various types (namely, string, int, float, list/np.array of strings,
29+
and list/np.array of integers, and list/np.array of floats).
30+
31+
Returns
32+
-------
33+
ged_env : GEDEnvGXL or GEDEnvAttr
34+
An instance of the appropriate GED environment class based on the specified type.
35+
36+
Raises
37+
------
38+
ValueError
39+
If the provided `env_type` is not recognized (not 'gxl', 'str', or 'attr').
40+
"""
41+
env_type = env_type.lower()
42+
if env_type in ['gxl', 'str']:
43+
return GEDEnvGXL()
44+
elif env_type == 'attr':
45+
return GEDEnvAttr()
46+
else:
47+
raise ValueError(
48+
f'Unknown environment type: {env_type}. Please use "gxl", "str", or "attr".'
49+
f'"gxl" and "str" are equivalent and use string labels for nodes and edges, '
50+
f'while "attr" uses attribute-based labels with dictionaries with string '
51+
f'keys and values of various types, namely, string, int, float, '
52+
f'list/np.array of strings, list/np.array of integers, and list/np.array of floats.'
53+
)

gklearn/gedlib/gedlibpy.pyx

100755100644
File mode changed.

0 commit comments

Comments
 (0)