Membership Pairs Appender¶
BaseMembershipPairsAppender
¶
Bases: ABC
Base class that adds one combination column per pair of from/to columns.
A pair is two columns with the same base name and the from and to
identifiers, for example node_from and node_to. There are three
combinations:
- Directional (
append_combo_columns): keeps the direction, A to B. - Sorted (
append_sorted_combo_columns): the values in sorted order, so A to B and B to A get the same value. - Opposite (
append_opposite_combo_columns): the reverse direction, B to A.
A subclass sets how two values combine, for example a string
(StringMembershipPairsAppender) or a tuple (TupleMembershipPairsAppender).
Either a suffix or a prefix must be set for each combination type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_identifier
|
str
|
Suffix of the source columns. Defaults to '_from'. |
'_from'
|
to_identifier
|
str
|
Suffix of the target columns. Defaults to '_to'. |
'_to'
|
combo_col_suffix
|
str
|
Suffix for directional combination column names. Defaults to '_combo'. |
'_combo'
|
combo_col_prefix
|
str
|
Prefix for directional combination column names. Defaults to None. |
None
|
sorted_combo_col_suffix
|
str
|
Suffix for sorted combination column names. Defaults to '_combo_sorted'. |
'_combo_sorted'
|
sorted_combo_col_prefix
|
str
|
Prefix for sorted combination column names. Defaults to None. |
None
|
opposite_combo_col_suffix
|
str
|
Suffix for opposite combination column names. Defaults to '_combo_opposite'. |
'_combo_opposite'
|
opposite_combo_col_prefix
|
str
|
Prefix for opposite combination column names. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither suffix nor prefix is provided for any combination type. |
Source code in src/mesqual/energy_data_handling/model_handling/membership_pairs_appender.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
append_combo_columns
¶
append_combo_columns(df_with_from_to_columns: DataFrame) -> DataFrame
Adds one column per from/to pair with the combination in the from/to direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_with_from_to_columns
|
DataFrame
|
DataFrame containing paired relationship columns (e.g., 'node_from'/'node_to', 'region_from'/'region_to') |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Enhanced DataFrame with directional combination columns added. Original data preserved, new columns follow configured naming pattern. |
Examples:
>>> appender = StringMembershipPairsAppender(separator=' - ')
>>> appender.append_combo_columns(dataset.fetch('Line.Model'))[['node_from', 'node_to', 'node_combo']].iloc[:2]
node_from node_to node_combo
Line
L1 N1 N2 N1 - N2
L2 N2 N3 N2 - N3
Source code in src/mesqual/energy_data_handling/model_handling/membership_pairs_appender.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
append_sorted_combo_columns
¶
append_sorted_combo_columns(df_with_from_to_columns: DataFrame) -> DataFrame
Adds one column per from/to pair with the two values in sorted order.
A to B and B to A get the same value, for example to find the lines of one corridor in both directions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_with_from_to_columns
|
DataFrame
|
DataFrame containing paired relationship columns with potential bidirectional connections |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Enhanced DataFrame with sorted combination columns added. Bidirectional relationships receive identical identifiers. |
Examples:
>>> borders = pd.DataFrame({'country_from': ['C1', 'C2'], 'country_to': ['C2', 'C1']})
>>> StringMembershipPairsAppender(separator=' - ').append_sorted_combo_columns(borders)['country_combo_sorted'].tolist()
['C1 - C2', 'C1 - C2']
Source code in src/mesqual/energy_data_handling/model_handling/membership_pairs_appender.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
append_opposite_combo_columns
¶
append_opposite_combo_columns(df_with_from_to_columns: DataFrame) -> DataFrame
Adds one column per from/to pair with the combination in the to/from direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_with_from_to_columns
|
DataFrame
|
DataFrame containing directional relationships where reverse analysis is needed |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Enhanced DataFrame with opposite-direction combination columns added. Each relationship receives its reverse-direction identifier. |
Examples:
>>> appender = StringMembershipPairsAppender(separator=' - ')
>>> appender.append_opposite_combo_columns(dataset.fetch('Line.Model'))['node_combo_opposite'].iloc[:2].tolist()
['N2 - N1', 'N3 - N2']
Source code in src/mesqual/energy_data_handling/model_handling/membership_pairs_appender.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
StringMembershipPairsAppender
¶
Bases: BaseMembershipPairsAppender
Combines a pair of values into one string, joined by separator.
Source code in src/mesqual/energy_data_handling/model_handling/membership_pairs_appender.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
__init__
¶
__init__(from_identifier: str = '_from', to_identifier: str = '_to', combo_col_suffix: str = '_combo', combo_col_prefix: str = None, sorted_combo_col_suffix: str = '_combo_sorted', sorted_combo_col_prefix: str = None, opposite_combo_col_suffix: str = '_combo_opposite', opposite_combo_col_prefix: str = None, separator: str = ' - ')
Initialize the string-based membership pairs appender.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_identifier
|
str
|
Suffix of the source columns. Defaults to '_from'. |
'_from'
|
to_identifier
|
str
|
Suffix of the target columns. Defaults to '_to'. |
'_to'
|
combo_col_suffix
|
str
|
Suffix for directional combination column names. Defaults to '_combo'. |
'_combo'
|
combo_col_prefix
|
str
|
Prefix for directional combination column names. Defaults to None. |
None
|
sorted_combo_col_suffix
|
str
|
Suffix for sorted combination column names. Defaults to '_combo_sorted'. |
'_combo_sorted'
|
sorted_combo_col_prefix
|
str
|
Prefix for sorted combination column names. Defaults to None. |
None
|
opposite_combo_col_suffix
|
str
|
Suffix for opposite combination column names. Defaults to '_combo_opposite'. |
'_combo_opposite'
|
opposite_combo_col_prefix
|
str
|
Prefix for opposite combination column names. Defaults to None. |
None
|
separator
|
str
|
String separator for joining paired values. Defaults to ' - '. Use ' → ' for directional flows, '-' for neutral connections, ' <-> ' for bidirectional relationships. |
' - '
|
Source code in src/mesqual/energy_data_handling/model_handling/membership_pairs_appender.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
TupleMembershipPairsAppender
¶
Bases: BaseMembershipPairsAppender
Combines a pair of values into a tuple, for use as dictionary keys or index values.
Examples:
>>> appender = TupleMembershipPairsAppender()
>>> appender.append_combo_columns(dataset.fetch('Line.Model'))['node_combo'].tolist()[:2]
[('N1', 'N2'), ('N2', 'N3')]
Source code in src/mesqual/energy_data_handling/model_handling/membership_pairs_appender.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |