Skip to content

Commit dd50a3c

Browse files
committed
feat: handle some more types
1 parent f1410f9 commit dd50a3c

File tree

5 files changed

+515
-111
lines changed

5 files changed

+515
-111
lines changed

doc/NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515
name of the edge attribute storing the weights?
1616

1717
- Shall we treat the `weight` edge attribute implicitly as edge weights?
18+
19+
- Shall we allow dicts mapping vertex names to floats to be treated as a
20+
`VERTEX_QTY` abstract type?

src/codegen/types.yaml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,20 @@ VERTEX_INDEX_PAIRS:
111111
VERTEX_SELECTOR:
112112
PY_TYPE: VertexSelector
113113
INCONV:
114-
IN: "%C% = vertex_selector_to_igraph_vs_t(%I%, %C1%)"
114+
IN: "%C% = vertex_selector_to_igraph_vs_t(%I%, %I1%)"
115115
DEFAULT:
116116
ALL: '"all"'
117117
CALL: "%C%.unwrap()"
118118

119+
VERTEX_QTY:
120+
PY_TYPE: Iterable[float]
121+
PY_RETURN_TYPE: npt.NDArray[np_type_of_igraph_real_t]
122+
INCONV:
123+
IN: "%C% = vertex_qty_to_igraph_vector_t_view(%I%, %I1%)"
124+
INOUT: "%C% = vertex_qty_to_igraph_vector_t(%I%, %I1%)"
125+
OUT: "%C% = _Vector.create(0)"
126+
OUTCONV: "%I% = igraph_vector_t_to_numpy_array(%C%)"
127+
119128
EDGE:
120129
PY_TYPE: EdgeLike
121130
PY_RETURN_TYPE: int
@@ -134,7 +143,7 @@ EDGE_INDICES:
134143
EDGE_SELECTOR:
135144
PY_TYPE: EdgeSelector
136145
INCONV:
137-
IN: "%C% = edge_selector_to_igraph_es_t(%I%, %C1%)"
146+
IN: "%C% = edge_selector_to_igraph_es_t(%I%, %I1%)"
138147
DEFAULT:
139148
ALL: '"all"'
140149
CALL: "%C%.unwrap()"
@@ -143,8 +152,8 @@ EDGEWEIGHTS:
143152
PY_TYPE: Iterable[float]
144153
PY_RETURN_TYPE: npt.NDArray[np_type_of_igraph_real_t]
145154
INCONV:
146-
IN: "%C% = edge_weights_to_igraph_vector_t_view(%I%)"
147-
INOUT: "%C% = edge_weights_to_igraph_vector_t(%I%)"
155+
IN: "%C% = edge_weights_to_igraph_vector_t_view(%I%, %I1%)"
156+
INOUT: "%C% = edge_weights_to_igraph_vector_t(%I%, %I1%)"
148157
OUT: "%C% = _Vector.create(0)"
149158
OUTCONV: "%I% = igraph_vector_t_to_numpy_array(%C%)"
150159

@@ -176,6 +185,11 @@ STAR_MODE:
176185
INCONV:
177186
IN: "%C% = c_int(%I%)"
178187

188+
TRANSITIVITY_MODE:
189+
PY_TYPE: TransitivityMode
190+
INCONV:
191+
IN: "%C% = c_int(%I%)"
192+
179193
TREE_MODE:
180194
PY_TYPE: TreeMode
181195
INCONV:

src/igraph_ctypes/_internal/conversion.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
"vertexlike_to_igraph_integer_t",
9999
"vertex_pairs_to_igraph_vector_int_t",
100100
"vertex_selector_to_igraph_vs_t",
101+
"vertex_qty_to_igraph_vector_t",
102+
"vertex_qty_to_igraph_vector_t_view",
101103
)
102104

103105

@@ -142,15 +144,15 @@ def edge_selector_to_igraph_es_t(
142144
return _EdgeSelector.create_with(igraph_es_1, index)
143145

144146

145-
def edge_weights_to_igraph_vector_t(weights: Iterable[float]) -> _Vector:
147+
def edge_weights_to_igraph_vector_t(weights: Iterable[float], graph: _Graph) -> _Vector:
146148
"""Converts a Python iterable of floating-point numbers to a vector of
147149
edge weights.
148150
"""
149151
return iterable_to_igraph_vector_t(weights)
150152

151153

152154
def edge_weights_to_igraph_vector_t_view(
153-
weights: Optional[Iterable[float]],
155+
weights: Optional[Iterable[float]], graph: _Graph
154156
) -> Optional[_Vector]:
155157
"""Converts a Python iterable of floating-point numbers to a vector of
156158
edge weights, possibly creating a shallow view if the input is an
@@ -159,7 +161,9 @@ def edge_weights_to_igraph_vector_t_view(
159161
When the input is `None`, the return value will also be `None`, which is
160162
interpreted by the C core of igraph as all edges having equal weight.
161163
"""
162-
return edge_weights_to_igraph_vector_t(weights) if weights is not None else None
164+
return (
165+
edge_weights_to_igraph_vector_t(weights, graph) if weights is not None else None
166+
)
163167

164168

165169
def iterable_edge_indices_to_igraph_vector_int_t(
@@ -439,6 +443,28 @@ def vertex_selector_to_igraph_vs_t(
439443
return _VertexSelector.create_with(igraph_vs_1, index)
440444

441445

446+
def vertex_qty_to_igraph_vector_t(weights: Iterable[float], graph: _Graph) -> _Vector:
447+
"""Converts a Python iterable of floating-point numbers to a vector of
448+
vertex-related quantities.
449+
"""
450+
return iterable_to_igraph_vector_t(weights)
451+
452+
453+
def vertex_qty_to_igraph_vector_t_view(
454+
weights: Optional[Iterable[float]], graph: _Graph
455+
) -> Optional[_Vector]:
456+
"""Converts a Python iterable of floating-point numbers to a vector of
457+
vertex-related quantities, possibly creating a shallow view if the input is
458+
an appropriate NumPy array.
459+
460+
When the input is `None`, the return value will also be `None`, which is
461+
interpreted by the C core of igraph as all edges having equal weight.
462+
"""
463+
return (
464+
vertex_qty_to_igraph_vector_t(weights, graph) if weights is not None else None
465+
)
466+
467+
442468
################################################################################
443469
# Conversion from igraph data types to Python #
444470
################################################################################

src/igraph_ctypes/_internal/enums.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"MatrixStorage",
88
"NeighborMode",
99
"StarMode",
10+
"TransitivityMode",
1011
"TreeMode",
1112
"VertexSequenceType",
1213
"WheelMode",
@@ -75,6 +76,13 @@ class StarMode(IntEnum):
7576
MUTUAL = 3
7677

7778

79+
class TransitivityMode(IntEnum):
80+
"""Python counterpart of an ``igraph_transitivity_mode_t`` enum."""
81+
82+
NAN = 0
83+
ZERO = 1
84+
85+
7886
class TreeMode(IntEnum):
7987
"""Python counterpart of an ``igraph_tree_mode_t`` enum."""
8088

0 commit comments

Comments
 (0)