|
| 1 | +from ctypes import c_void_p |
| 2 | +from numpy import mean, median |
| 3 | +from typing import Any, Callable, Iterable |
| 4 | + |
| 5 | +from igraph_ctypes._internal.enums import AttributeCombinationType, AttributeType |
| 6 | +from igraph_ctypes._internal.types import IntArray |
| 7 | + |
| 8 | +from .value_list import AttributeValueList |
| 9 | + |
| 10 | + |
| 11 | +Handler = Callable[[AttributeValueList, list[IntArray], c_void_p], Iterable[Any] | None] |
| 12 | + |
| 13 | + |
| 14 | +def apply_attribute_combinations( |
| 15 | + values: AttributeValueList, |
| 16 | + mapping: list[IntArray], |
| 17 | + comb_type: int, |
| 18 | + comb_func: c_void_p, |
| 19 | +) -> Iterable[Any] | None: |
| 20 | + """Applies an igraph attribute combination specification entry to a |
| 21 | + vector of attributes. |
| 22 | +
|
| 23 | + Args: |
| 24 | + values: an attribute value list from the old graph where the entries have |
| 25 | + to be combined into a new atribute value list |
| 26 | + mapping: list of integer arrays where the i-th entry lists the indices |
| 27 | + from the values array that are to be merged into the i-th entry of |
| 28 | + the returned value list |
| 29 | + comb_type: type of attribute combination to apply; one of the constants |
| 30 | + from the AttributeCombinationType_ enum |
| 31 | + comb_func: pointer to a Python function to invoke for custom, user-defined |
| 32 | + attribute combinations (i.e. when comb_type is equal to |
| 33 | + `AttributeCombinationType.FUNCTION`) |
| 34 | +
|
| 35 | + Returns: |
| 36 | + an iterable of the combined values or `None` if the values should be ignored |
| 37 | + (i.e. when `comb_type` is `AttributeCombinationType.IGNORE`) |
| 38 | + """ |
| 39 | + if comb_type == AttributeCombinationType.IGNORE: |
| 40 | + return None |
| 41 | + |
| 42 | + try: |
| 43 | + handler = _handlers[comb_type] |
| 44 | + except IndexError: |
| 45 | + handler = _combine_ignore |
| 46 | + |
| 47 | + return handler(values, mapping, comb_func) |
| 48 | + |
| 49 | + |
| 50 | +def _combine_ignore( |
| 51 | + values: AttributeValueList, |
| 52 | + mapping: list[IntArray], |
| 53 | + comb_func: c_void_p, |
| 54 | +) -> None: |
| 55 | + return None |
| 56 | + |
| 57 | + |
| 58 | +def _combine_with_function( |
| 59 | + values: AttributeValueList, |
| 60 | + mapping: list[IntArray], |
| 61 | + comb_func: c_void_p, |
| 62 | +) -> None: |
| 63 | + raise NotImplementedError # TODO(ntamas) |
| 64 | + |
| 65 | + |
| 66 | +def _combine_sum( |
| 67 | + values: AttributeValueList, |
| 68 | + mapping: list[IntArray], |
| 69 | + comb_func: c_void_p, |
| 70 | +) -> Iterable[Any]: |
| 71 | + return (values[item].sum() for item in mapping) |
| 72 | + |
| 73 | + |
| 74 | +def _combine_prod( |
| 75 | + values: AttributeValueList, |
| 76 | + mapping: list[IntArray], |
| 77 | + comb_func: c_void_p, |
| 78 | +) -> Iterable[Any]: |
| 79 | + return (values[item].prod() for item in mapping) |
| 80 | + |
| 81 | + |
| 82 | +def _combine_min( |
| 83 | + values: AttributeValueList, |
| 84 | + mapping: list[IntArray], |
| 85 | + comb_func: c_void_p, |
| 86 | +) -> Iterable[Any]: |
| 87 | + return (values[item].min() for item in mapping) |
| 88 | + |
| 89 | + |
| 90 | +def _combine_max( |
| 91 | + values: AttributeValueList, |
| 92 | + mapping: list[IntArray], |
| 93 | + comb_func: c_void_p, |
| 94 | +) -> Iterable[Any]: |
| 95 | + return (values[item].min() for item in mapping) |
| 96 | + |
| 97 | + |
| 98 | +def _combine_random( |
| 99 | + values: AttributeValueList, |
| 100 | + mapping: list[IntArray], |
| 101 | + comb_func: c_void_p, |
| 102 | +) -> Iterable[Any]: |
| 103 | + raise NotImplementedError # TODO(ntamas) |
| 104 | + |
| 105 | + |
| 106 | +def _combine_first( |
| 107 | + values: AttributeValueList, |
| 108 | + mapping: list[IntArray], |
| 109 | + comb_func: c_void_p, |
| 110 | +) -> Iterable[Any]: |
| 111 | + indices = [item[0] for item in mapping] |
| 112 | + return values[indices] |
| 113 | + |
| 114 | + |
| 115 | +def _combine_last( |
| 116 | + values: AttributeValueList, |
| 117 | + mapping: list[IntArray], |
| 118 | + comb_func: c_void_p, |
| 119 | +) -> Iterable[Any]: |
| 120 | + indices = [item[-1] for item in mapping] |
| 121 | + return values[indices] |
| 122 | + |
| 123 | + |
| 124 | +def _combine_mean( |
| 125 | + values: AttributeValueList, |
| 126 | + mapping: list[IntArray], |
| 127 | + comb_func: c_void_p, |
| 128 | +) -> Iterable[Any]: |
| 129 | + return (mean(values[item]) for item in mapping) |
| 130 | + |
| 131 | + |
| 132 | +def _combine_median( |
| 133 | + values: AttributeValueList, |
| 134 | + mapping: list[IntArray], |
| 135 | + comb_func: c_void_p, |
| 136 | +) -> Iterable[Any]: |
| 137 | + return (median(values[item]) for item in mapping) |
| 138 | + |
| 139 | + |
| 140 | +def _combine_concat( |
| 141 | + values: AttributeValueList, |
| 142 | + mapping: list[IntArray], |
| 143 | + comb_func: c_void_p, |
| 144 | +) -> Iterable[Any]: |
| 145 | + if values.type != AttributeType.STRING: |
| 146 | + raise TypeError(f"cannot concatenate attributes of type {values.type}") |
| 147 | + return ("".join(values[item]) for item in mapping) |
| 148 | + |
| 149 | + |
| 150 | +_handlers: list[Handler] = [ |
| 151 | + _combine_ignore, |
| 152 | + _combine_first, |
| 153 | + _combine_with_function, |
| 154 | + _combine_sum, |
| 155 | + _combine_prod, |
| 156 | + _combine_min, |
| 157 | + _combine_max, |
| 158 | + _combine_random, |
| 159 | + _combine_first, |
| 160 | + _combine_last, |
| 161 | + _combine_mean, |
| 162 | + _combine_median, |
| 163 | + _combine_concat, |
| 164 | +] |
| 165 | +"""Table of attribute combination handler functions.""" |
0 commit comments