|
26 | 26 | igraph_vector_bool_push_back, |
27 | 27 | igraph_vector_bool_size, |
28 | 28 | igraph_vector_bool_view, |
| 29 | + igraph_vector_e, |
| 30 | + igraph_vector_e_ptr, |
| 31 | + igraph_vector_init_array, |
29 | 32 | igraph_vector_int_e, |
30 | 33 | igraph_vector_int_e_ptr, |
31 | 34 | igraph_vector_int_init_array, |
| 35 | + igraph_vector_int_list_get_ptr, |
| 36 | + igraph_vector_int_list_push_back, |
| 37 | + igraph_vector_int_list_size, |
32 | 38 | igraph_vector_int_push_back, |
33 | 39 | igraph_vector_int_size, |
34 | 40 | igraph_vector_int_view, |
35 | | - igraph_vector_e, |
36 | | - igraph_vector_e_ptr, |
37 | | - igraph_vector_init_array, |
| 41 | + igraph_vector_list_get_ptr, |
| 42 | + igraph_vector_list_push_back, |
| 43 | + igraph_vector_list_size, |
38 | 44 | igraph_vector_push_back, |
39 | 45 | igraph_vector_size, |
40 | 46 | igraph_vector_view, |
|
67 | 73 | _Vector, |
68 | 74 | _VectorBool, |
69 | 75 | _VectorInt, |
| 76 | + _VectorIntList, |
| 77 | + _VectorList, |
70 | 78 | _VertexSelector, |
71 | 79 | ) |
72 | 80 |
|
|
89 | 97 | "igraph_vector_t_to_numpy_array", |
90 | 98 | "igraph_vector_bool_t_to_numpy_array", |
91 | 99 | "igraph_vector_int_t_to_numpy_array", |
| 100 | + "igraph_vector_int_list_t_to_list_of_numpy_array", |
| 101 | + "igraph_vector_list_t_to_list_of_numpy_array", |
92 | 102 | "iterable_edge_indices_to_igraph_vector_int_t", |
| 103 | + "iterable_of_edge_index_iterable_to_igraph_vector_int_list_t", |
| 104 | + "iterable_of_iterable_to_igraph_vector_int_list_t", |
| 105 | + "iterable_of_iterable_to_igraph_vector_list_t", |
| 106 | + "iterable_of_vertex_index_iterable_to_igraph_vector_int_list_t", |
93 | 107 | "iterable_to_igraph_vector_bool_t", |
94 | 108 | "iterable_to_igraph_vector_bool_t_view", |
95 | 109 | "iterable_to_igraph_vector_int_t", |
|
106 | 120 | "vertex_selector_to_igraph_vs_t", |
107 | 121 | "vertex_colors_to_igraph_vector_t", |
108 | 122 | "vertex_colors_to_igraph_vector_t_view", |
109 | | - "vertex_qty_to_igraph_vector_t", |
110 | | - "vertex_qty_to_igraph_vector_t_view", |
| 123 | + "vertex_qtys_to_igraph_vector_t", |
| 124 | + "vertex_qtys_to_igraph_vector_t_view", |
111 | 125 | ) |
112 | 126 |
|
113 | 127 |
|
@@ -289,6 +303,60 @@ def iterable_vertex_indices_to_igraph_vector_int_t( |
289 | 303 | return result |
290 | 304 |
|
291 | 305 |
|
| 306 | +def iterable_of_iterable_to_igraph_vector_int_list_t( |
| 307 | + items: Iterable[Iterable[int]], |
| 308 | +) -> _VectorIntList: |
| 309 | + result = _VectorIntList.create(0) |
| 310 | + |
| 311 | + # TODO: |
| 312 | + # |
| 313 | + # - if items is a sequence (i.e. we know its length), we could optimize the |
| 314 | + # conversion |
| 315 | + # - we could re-use an already-constructed _VectorInt if we had an alternative |
| 316 | + # iterable_to_igraph_vector_int_t() that receives a _VectorInt from the |
| 317 | + # outside |
| 318 | + |
| 319 | + for item in items: |
| 320 | + vec = iterable_to_igraph_vector_int_t(item) |
| 321 | + igraph_vector_int_list_push_back(result, vec) |
| 322 | + vec.release() |
| 323 | + |
| 324 | + return result |
| 325 | + |
| 326 | + |
| 327 | +def iterable_of_iterable_to_igraph_vector_list_t( |
| 328 | + items: Iterable[Iterable[float]], |
| 329 | +) -> _VectorList: |
| 330 | + result = _VectorList.create(0) |
| 331 | + |
| 332 | + # TODO: |
| 333 | + # |
| 334 | + # - if items is a sequence (i.e. we know its length), we could optimize the |
| 335 | + # conversion |
| 336 | + # - we could re-use an already-constructed _VectorInt if we had an alternative |
| 337 | + # iterable_to_igraph_vector_t() that receives a _VectorInt from the |
| 338 | + # outside |
| 339 | + |
| 340 | + for item in items: |
| 341 | + vec = iterable_to_igraph_vector_t(item) |
| 342 | + igraph_vector_list_push_back(result, vec) |
| 343 | + vec.release() |
| 344 | + |
| 345 | + return result |
| 346 | + |
| 347 | + |
| 348 | +def iterable_of_edge_index_iterable_to_igraph_vector_int_list_t( |
| 349 | + items: Iterable[Iterable[VertexLike]], |
| 350 | +) -> _VectorIntList: |
| 351 | + return iterable_of_iterable_to_igraph_vector_int_list_t(items) |
| 352 | + |
| 353 | + |
| 354 | +def iterable_of_vertex_index_iterable_to_igraph_vector_int_list_t( |
| 355 | + items: Iterable[Iterable[VertexLike]], |
| 356 | +) -> _VectorIntList: |
| 357 | + return iterable_of_iterable_to_igraph_vector_int_list_t(items) |
| 358 | + |
| 359 | + |
292 | 360 | def sequence_to_igraph_matrix_int_t(items: MatrixIntLike) -> _MatrixInt: |
293 | 361 | """Converts a sequence of sequences of Python integers to an igraph matrix |
294 | 362 | of integers. Each sequence in the top-level sequence must have the same |
@@ -553,14 +621,14 @@ def vertex_colors_to_igraph_vector_t_view( |
553 | 621 | return iterable_to_igraph_vector_int_t_view(colors) |
554 | 622 |
|
555 | 623 |
|
556 | | -def vertex_qty_to_igraph_vector_t(weights: Iterable[float], graph: _Graph) -> _Vector: |
| 624 | +def vertex_qtys_to_igraph_vector_t(weights: Iterable[float], graph: _Graph) -> _Vector: |
557 | 625 | """Converts a Python iterable of floating-point numbers to a vector of |
558 | 626 | vertex-related quantities. |
559 | 627 | """ |
560 | 628 | return iterable_to_igraph_vector_t(weights) |
561 | 629 |
|
562 | 630 |
|
563 | | -def vertex_qty_to_igraph_vector_t_view( |
| 631 | +def vertex_qtys_to_igraph_vector_t_view( |
564 | 632 | weights: Optional[Iterable[float]], graph: _Graph |
565 | 633 | ) -> Optional[_Vector]: |
566 | 634 | """Converts a Python iterable of floating-point numbers to a vector of |
@@ -643,3 +711,39 @@ def igraph_vector_int_t_to_numpy_array( |
643 | 711 | if n > 0: |
644 | 712 | memmove(result.ctypes.data, igraph_vector_int_e_ptr(vector, 0), result.nbytes) |
645 | 713 | return result |
| 714 | + |
| 715 | + |
| 716 | +def igraph_vector_list_t_to_list_of_numpy_array( |
| 717 | + vector_list: _VectorList, |
| 718 | +) -> List[npt.NDArray[np_type_of_igraph_real_t]]: |
| 719 | + n = igraph_vector_list_size(vector_list) |
| 720 | + vec = _Vector() |
| 721 | + result = [] |
| 722 | + |
| 723 | + for i in range(n): |
| 724 | + # We are re-using the same _Vector instance to wrap different |
| 725 | + # low-level igraph_vector_t instances because it's only temporary |
| 726 | + # until we convert it to a NumPy array |
| 727 | + ptr = igraph_vector_list_get_ptr(vector_list, i) |
| 728 | + vec._set_wrapped_instance(ptr.contents) |
| 729 | + result.append(igraph_vector_t_to_numpy_array(vec)) |
| 730 | + |
| 731 | + return result |
| 732 | + |
| 733 | + |
| 734 | +def igraph_vector_int_list_t_to_list_of_numpy_array( |
| 735 | + vector_list: _VectorIntList, |
| 736 | +) -> List[npt.NDArray[np_type_of_igraph_integer_t]]: |
| 737 | + n = igraph_vector_int_list_size(vector_list) |
| 738 | + vec = _VectorInt() |
| 739 | + result = [] |
| 740 | + |
| 741 | + for i in range(n): |
| 742 | + # We are re-using the same _VectorInt instance to wrap different |
| 743 | + # low-level igraph_vector_int_t instances because it's only temporary |
| 744 | + # until we convert it to a NumPy array |
| 745 | + ptr = igraph_vector_int_list_get_ptr(vector_list, i) |
| 746 | + vec._set_wrapped_instance(ptr.contents) |
| 747 | + result.append(igraph_vector_int_t_to_numpy_array(vec)) |
| 748 | + |
| 749 | + return result |
0 commit comments