Skip to content

Commit 20bdbd5

Browse files
committed
feat: added support for more types
1 parent 4286809 commit 20bdbd5

File tree

6 files changed

+226
-32
lines changed

6 files changed

+226
-32
lines changed

src/codegen/run.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ def generate_enums( # noqa: C901
117117
"Adjacency": "AdjacencyMode",
118118
"AttributeElemtype": "AttributeElementType",
119119
"BlissSh": "BLISSSplittingHeuristics",
120+
"ColoringGreedy": "GreedyColoringHeuristics",
120121
"Degseq": "DegreeSequenceMode",
121122
"EdgeorderType": "EdgeOrder",
122123
"EitType": "EdgeIteratorType",
124+
"ErdosRenyi": "ErdosRenyiType",
123125
"ErrorType": "ErrorCode",
124126
"EsType": "EdgeSequenceType",
125127
"FasAlgorithm": "FeedbackArcSetAlgorithm",
@@ -137,8 +139,10 @@ def generate_enums( # noqa: C901
137139
"VitType": "VertexIteratorType",
138140
"VsType": "VertexSequenceType",
139141
}
140-
EXTRA_ENUM_MEMBERS: dict[str, Sequence[tuple[str, int]]] = {
141-
"Loops": [("IGNORE", 0)]
142+
EXTRA_ENUM_MEMBERS: dict[str, Sequence[tuple[str, Union[int, str]]]] = {
143+
"GreedyColoringHeuristics": [("NEIGHBORS", "COLORED_NEIGHBORS")],
144+
"LayoutGrid": [("NO_GRID", "NOGRID"), ("AUTO_GRID", "AUTOGRID")],
145+
"Loops": [("IGNORE", 0)],
142146
}
143147

144148
def process_enum(fp: TextIO, spec) -> Optional[str]: # noqa: C901
@@ -181,6 +185,7 @@ def process_enum(fp: TextIO, spec) -> Optional[str]: # noqa: C901
181185

182186
last_value = -1
183187
all_members: dict[str, str] = {}
188+
all_values: dict[str, int] = {}
184189
for entry in entries:
185190
key, sep, value = entry.replace(" ", "").partition("=")
186191
if key.startswith("UNUSED_"):
@@ -210,11 +215,18 @@ def process_enum(fp: TextIO, spec) -> Optional[str]: # noqa: C901
210215

211216
fp.write(f" {key} = {value_int}\n")
212217
all_members[key.lower()] = key
218+
all_values[key.lower()] = value_int
213219
last_value = value_int
214220

215-
for key, value_int in EXTRA_ENUM_MEMBERS.get(name, ()):
221+
for key, value_int_or_str in EXTRA_ENUM_MEMBERS.get(name, ()):
222+
if isinstance(value_int_or_str, str):
223+
value_int = all_members[value_int_or_str.lower()]
224+
aliased_to = value_int_or_str
225+
else:
226+
value_int = value_int_or_str
227+
aliased_to = key
216228
fp.write(f" {key} = {value_int}\n")
217-
all_members[key.lower()] = key
229+
all_members[key.lower()] = aliased_to
218230

219231
fp.write("\n")
220232
fp.write(f" _string_map: ClassVar[dict[str, {name}]]\n")

src/codegen/types.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ VERTEX_QTYS:
161161
OUT: "%C% = _Vector.create(0)"
162162
OUTCONV: "%I% = igraph_vector_t_to_numpy_array(%C%)"
163163

164+
VERTEX_WEIGHTS:
165+
PY_TYPE: Iterable[float]
166+
PY_RETURN_TYPE: RealArray
167+
INCONV:
168+
IN: "%C% = vertex_weights_to_igraph_vector_t_view(%I%, %I1%)"
169+
INOUT: "%C% = vertex_weights_to_igraph_vector_t(%I%, %I1%)"
170+
OUT: "%C% = _Vector.create(0)"
171+
OUTCONV: "%I% = igraph_vector_t_to_numpy_array(%C%)"
172+
164173
EDGE:
165174
PY_TYPE: EdgeLike
166175
PY_RETURN_TYPE: int
@@ -303,6 +312,11 @@ EIGENALGO:
303312
INCONV:
304313
IN: "%C% = c_int(%I%)"
305314

315+
ERDOS_RENYI_TYPE:
316+
PY_TYPE: ErdosRenyiType
317+
INCONV:
318+
IN: "%C% = c_int(%I%)"
319+
306320
FAS_ALGORITHM:
307321
PY_TYPE: FeedbackArcSetAlgorithm
308322
INCONV:
@@ -313,11 +327,21 @@ FWALGORITHM:
313327
INCONV:
314328
IN: "%C% = c_int(%I%)"
315329

330+
GREEDY_COLORING_HEURISTIC:
331+
PY_TYPE: GreedyColoringHeuristics
332+
INCONV:
333+
IN: "%C% = c_int(%I%)"
334+
316335
IMITATE_ALGORITHM:
317336
PY_TYPE: ImitateAlgorithm
318337
INCONV:
319338
IN: "%C% = c_int(%I%)"
320339

340+
LAYOUT_GRID:
341+
PY_TYPE: LayoutGrid
342+
INCONV:
343+
IN: "%C% = c_int(%I%)"
344+
321345
LOOPS:
322346
PY_TYPE: Loops
323347
INCONV:

src/igraph_ctypes/_internal/conversion.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@
157157
"vertex_colors_to_igraph_vector_t_view",
158158
"vertex_qtys_to_igraph_vector_t",
159159
"vertex_qtys_to_igraph_vector_t_view",
160+
"vertex_weights_to_igraph_vector_t",
161+
"vertex_weights_to_igraph_vector_t_view",
160162
)
161163

162164

@@ -776,6 +778,28 @@ def vertex_qtys_to_igraph_vector_t_view(
776778
return iterable_to_igraph_vector_t_view(weights) if weights else None
777779

778780

781+
def vertex_weights_to_igraph_vector_t(
782+
weights: Iterable[float], graph: Graph
783+
) -> _Vector:
784+
"""Converts a Python iterable of floating-point numbers to a vector of
785+
vertex weights.
786+
"""
787+
return iterable_to_igraph_vector_t(weights)
788+
789+
790+
def vertex_weights_to_igraph_vector_t_view(
791+
weights: Optional[Iterable[float]], graph: Graph
792+
) -> Optional[_Vector]:
793+
"""Converts a Python iterable of floating-point numbers to a vector of
794+
vertex weights, possibly creating a shallow view if the input is an
795+
appropriate NumPy array.
796+
797+
When the input is `None`, the return value will also be `None`, which is
798+
interpreted by the C core of igraph as all vertices having equal weight.
799+
"""
800+
return iterable_to_igraph_vector_t_view(weights) if weights is not None else None
801+
802+
779803
################################################################################
780804
# Conversion from igraph data types to Python #
781805
################################################################################

src/igraph_ctypes/_internal/enums.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ def from_(cls, value: Any):
140140
}
141141

142142

143-
class ColoringGreedy(IntEnum):
143+
class GreedyColoringHeuristics(IntEnum):
144144
"""Python counterpart of an ``igraph_coloring_greedy_t`` enum."""
145145

146146
COLORED_NEIGHBORS = 0
147147
DSATUR = 1
148+
NEIGHBORS = COLORED_NEIGHBORS
148149

149-
_string_map: ClassVar[dict[str, ColoringGreedy]]
150+
_string_map: ClassVar[dict[str, GreedyColoringHeuristics]]
150151

151152
@classmethod
152153
def from_(cls, value: Any):
@@ -155,20 +156,21 @@ def from_(cls, value: Any):
155156
Raises:
156157
ValueError: if the object cannot be converted
157158
"""
158-
if isinstance(value, ColoringGreedy):
159+
if isinstance(value, GreedyColoringHeuristics):
159160
return value
160161
elif isinstance(value, int):
161162
return cls(value)
162163
else:
163164
try:
164165
return cls._string_map[value]
165166
except KeyError:
166-
raise ValueError(f"{value!r} cannot be converted to ColoringGreedy") from None
167+
raise ValueError(f"{value!r} cannot be converted to GreedyColoringHeuristics") from None
167168

168169

169-
ColoringGreedy._string_map = {
170-
'colored_neighbors': ColoringGreedy.COLORED_NEIGHBORS,
171-
'dsatur': ColoringGreedy.DSATUR,
170+
GreedyColoringHeuristics._string_map = {
171+
'colored_neighbors': GreedyColoringHeuristics.COLORED_NEIGHBORS,
172+
'dsatur': GreedyColoringHeuristics.DSATUR,
173+
'neighbors': GreedyColoringHeuristics.COLORED_NEIGHBORS,
172174
}
173175

174176

@@ -820,13 +822,13 @@ def from_(cls, value: Any):
820822
}
821823

822824

823-
class ErdosRenyi(IntEnum):
825+
class ErdosRenyiType(IntEnum):
824826
"""Python counterpart of an ``igraph_erdos_renyi_t`` enum."""
825827

826828
GNP = 0
827829
GNM = 1
828830

829-
_string_map: ClassVar[dict[str, ErdosRenyi]]
831+
_string_map: ClassVar[dict[str, ErdosRenyiType]]
830832

831833
@classmethod
832834
def from_(cls, value: Any):
@@ -835,20 +837,20 @@ def from_(cls, value: Any):
835837
Raises:
836838
ValueError: if the object cannot be converted
837839
"""
838-
if isinstance(value, ErdosRenyi):
840+
if isinstance(value, ErdosRenyiType):
839841
return value
840842
elif isinstance(value, int):
841843
return cls(value)
842844
else:
843845
try:
844846
return cls._string_map[value]
845847
except KeyError:
846-
raise ValueError(f"{value!r} cannot be converted to ErdosRenyi") from None
848+
raise ValueError(f"{value!r} cannot be converted to ErdosRenyiType") from None
847849

848850

849-
ErdosRenyi._string_map = {
850-
'gnm': ErdosRenyi.GNM,
851-
'gnp': ErdosRenyi.GNP,
851+
ErdosRenyiType._string_map = {
852+
'gnm': ErdosRenyiType.GNM,
853+
'gnp': ErdosRenyiType.GNP,
852854
}
853855

854856

@@ -1540,6 +1542,8 @@ class LayoutGrid(IntEnum):
15401542
GRID = 0
15411543
NOGRID = 1
15421544
AUTOGRID = 2
1545+
NO_GRID = NOGRID
1546+
AUTO_GRID = AUTOGRID
15431547

15441548
_string_map: ClassVar[dict[str, LayoutGrid]]
15451549

@@ -1562,8 +1566,10 @@ def from_(cls, value: Any):
15621566

15631567

15641568
LayoutGrid._string_map = {
1569+
'auto_grid': LayoutGrid.AUTOGRID,
15651570
'autogrid': LayoutGrid.AUTOGRID,
15661571
'grid': LayoutGrid.GRID,
1572+
'no_grid': LayoutGrid.NOGRID,
15671573
'nogrid': LayoutGrid.NOGRID,
15681574
}
15691575

@@ -2072,7 +2078,6 @@ def from_(cls, value: Any):
20722078
'AttributeType',
20732079
'BLISSSplittingHeuristics',
20742080
'BarabasiAlgorithm',
2075-
'ColoringGreedy',
20762081
'CommunityComparison',
20772082
'Connectedness',
20782083
'DRLLayoutPreset',
@@ -2081,12 +2086,13 @@ def from_(cls, value: Any):
20812086
'EdgeOrder',
20822087
'EdgeSequenceType',
20832088
'EigenAlgorithm',
2084-
'ErdosRenyi',
2089+
'ErdosRenyiType',
20852090
'ErrorCode',
20862091
'FeedbackArcSetAlgorithm',
20872092
'FileFormat',
20882093
'FloydWarshallAlgorithm',
20892094
'GetAdjacency',
2095+
'GreedyColoringHeuristics',
20902096
'ImitateAlgorithm',
20912097
'LaplacianNormalization',
20922098
'LaplacianSpectralEmbeddingType',

0 commit comments

Comments
 (0)