Layer Flag References¶
layer_flags
¶
Aggregations and interpreters of layer flags on area and border layers.
LayerAggregation
dataclass
¶
Bases: ABC
Common base of AreaAggregation and BorderAggregation. Subclasses are frozen dataclasses.
An aggregation that fetches flags besides the base flag lists them in
input_flags. The layer flag is available only if these inputs are
available. It passes the effective_config that aggregate gets to
each of these fetches, as an interpreter does, so the config of the call
reaches the inputs. fetch passes it, together with the parameters that
the spec of the input declares.
Attributes:
| Name | Type | Description |
|---|---|---|
version |
int
|
Bump by hand when the logic changes. Part of the version identity of each layer flag. |
Source code in src/mesqual/layer_flags/aggregations.py
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 | |
identity
¶
identity() -> tuple[str, dict[str, Any]]
Return the link of the aggregation in the version identity: class name and fields.
Examples:
>>> WeightedMean(weight='Node.Results.demand_total').identity()
('WeightedMean', {'weight': 'Node.Results.demand_total', 'version': 2})
Source code in src/mesqual/layer_flags/aggregations.py
54 55 56 57 58 59 60 61 62 | |
input_flags
¶
input_flags(dataset: Dataset, layer: AreaLayer | BorderLayer, variant: LayerVariantValues) -> tuple[str, ...]
Return the flags that aggregate fetches besides the base flag, for example a weight.
The layer flag interpreter checks their availability without fetching them. Default: none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The platform dataset. |
required |
layer
|
AreaLayer | BorderLayer
|
The layer of the flag. |
required |
variant
|
LayerVariantValues
|
The variant values of the flag, as |
required |
Source code in src/mesqual/layer_flags/aggregations.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
check_spec
¶
check_spec(spec: LayerFlagSpec) -> None
Raise ValueError if the layer spec does not give what the aggregation needs. Default: no check.
LayerFlagSpec calls it at declaration, so a misdeclared spec
fails there and not in describe_flags or fetch.
Source code in src/mesqual/layer_flags/aggregations.py
82 83 84 85 86 87 | |
fetch
staticmethod
¶
fetch(dataset: Dataset, flag: str, effective_config: DatasetConfig | None, params: dict[str, Any]) -> DataFrame
Fetch a flag with effective_config and the parameters of params.
Parameters that the spec of flag does not declare are left out.
Source code in src/mesqual/layer_flags/aggregations.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
AreaAggregation
dataclass
¶
Bases: LayerAggregation
Aggregates the values of base objects to areas. Subclasses are frozen dataclasses.
A platform package or a study subclasses it for special cases.
Source code in src/mesqual/layer_flags/aggregations.py
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 | |
aggregate
abstractmethod
¶
aggregate(values: DataFrame | None, mapping: Series | DataFrame, dataset: Dataset, layer: AreaLayer, variant: LayerVariantValues, effective_config: DatasetConfig, **params: Any) -> DataFrame
Return the aggregated result with the area as object level.
For a model flag, one row per area. For a time series, one column per area; an aggregation on a border base may add column levels, for example a partner area.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
DataFrame | None
|
The base flag for the same variant values. On a node
base, one column per base object, or one row per base object
for a model flag; objects without area are left out. On a
border base, one column per border. |
required |
mapping
|
Series | DataFrame
|
For a node base: base object id to area id, from the
layer membership column, without objects that have no area.
For a border base: the border model, with |
required |
dataset
|
Dataset
|
The platform dataset, to fetch more flags, for example weights. |
required |
layer
|
AreaLayer
|
The layer to aggregate to. |
required |
variant
|
LayerVariantValues
|
The variant values of the flag, including the layer. For
a border base, also the |
required |
effective_config
|
DatasetConfig
|
The effective config of the layer flag fetch. Pass it to each fetch. |
required |
**params
|
Any
|
The resolved parameters of the flag. |
{}
|
Source code in src/mesqual/layer_flags/aggregations.py
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 | |
BorderAggregation
dataclass
¶
Bases: LayerAggregation
Aggregates edge values or area values to borders. Subclasses are frozen dataclasses.
Source code in src/mesqual/layer_flags/aggregations.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
aggregate
abstractmethod
¶
aggregate(values: DataFrame | None, border_model: DataFrame | None, dataset: Dataset, layer: BorderLayer, variant: LayerVariantValues, effective_config: DatasetConfig, **params: Any) -> DataFrame
Return one column per border, or one row per border for a model flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
DataFrame | None
|
The base flag for the same variant values: one column per
edge, or per area for an area base. For an edge model base,
the edge model. |
required |
border_model
|
DataFrame | None
|
The border model of the layer, with |
required |
dataset
|
Dataset
|
The platform dataset, to fetch more flags. |
required |
layer
|
BorderLayer
|
The layer to aggregate to. |
required |
variant
|
LayerVariantValues
|
The variant values of the flag, including the layer. For
an area base, also the |
required |
effective_config
|
DatasetConfig
|
The effective config of the layer flag fetch. Pass it to each fetch. |
required |
**params
|
Any
|
The resolved parameters of the flag. |
{}
|
Source code in src/mesqual/layer_flags/aggregations.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
Sum
dataclass
¶
Bases: AreaAggregation
Sum over the objects of each area, with the NaN and index rules of AreaSumCalculator.calculate.
Source code in src/mesqual/layer_flags/aggregations.py
204 205 206 207 208 209 210 211 212 213 | |
aggregate
¶
aggregate(values, mapping, dataset, layer, variant, effective_config, **params) -> DataFrame
Return one column per area with the sum of its objects.
Source code in src/mesqual/layer_flags/aggregations.py
210 211 212 213 | |
WeightedMean
dataclass
¶
Bases: AreaAggregation
Mean over the objects of each area, weighted by the flag weight. Uses AreaPriceCalculator.
Attributes:
| Name | Type | Description |
|---|---|---|
weight |
str
|
Weight flag. It may use the placeholders of the base name,
for example |
Examples:
>>> WeightedMean(weight='Node.Results.demand_total')
WeightedMean(version=2, weight='Node.Results.demand_total')
Source code in src/mesqual/layer_flags/aggregations.py
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 249 250 251 252 253 254 255 256 257 258 259 260 | |
check_spec
¶
check_spec(spec: LayerFlagSpec) -> None
Raise ValueError if weight has a placeholder that is not in the variant values of the spec.
Source code in src/mesqual/layer_flags/aggregations.py
233 234 235 236 237 238 239 240 241 | |
input_flags
¶
input_flags(dataset: Dataset, layer: AreaLayer | BorderLayer, variant: LayerVariantValues) -> tuple[str, ...]
Return the weight flag for the variant values.
Source code in src/mesqual/layer_flags/aggregations.py
243 244 245 246 247 248 249 250 | |
aggregate
¶
aggregate(values, mapping, dataset, layer, variant, effective_config, **params) -> DataFrame
Return one column per area with the weighted mean of its objects.
Source code in src/mesqual/layer_flags/aggregations.py
252 253 254 255 256 257 | |
AreaModel
dataclass
¶
Bases: AreaAggregation
The area model: one row per area, with node count and representative point. Uses AreaModelGenerator.
Source code in src/mesqual/layer_flags/aggregations.py
263 264 265 266 267 268 269 270 271 | |
aggregate
¶
aggregate(values, mapping, dataset, layer, variant, effective_config, **params) -> DataFrame
Return one row per area, indexed by layer.index.
Source code in src/mesqual/layer_flags/aggregations.py
267 268 269 270 271 | |
BorderModel
dataclass
¶
Bases: BorderAggregation
The border model: one row per border and direction, with its areas, edge counts and geometry.
A border runs from area_from to area_to. Both directions of an area
pair are borders. An edge counts with the border if its first end is in
area_from and its second end in area_to, and against the border the
other way round. An edge with both ends in one area, or with an end
without area (AreaMembership.has_area), is on no border. So a border
joins two areas that are both set. The columns n_edges_with_border and
n_edges_against_border count the edges per border.
The columns opposite_border, canonical_border and is_canonical come
from AreaBorderModelGenerator. canonical_border is the border of the
pair with the area names in alphabetical order. is_canonical is True
where the border is its canonical border.
The model always has the geometry columns (projection_point,
azimuth_angle, geo_line_string, areas_touch). A missing geometry is
NaN. The values come from AreaBorderGeometryCalculator. They need
polygons in the column geometry of the area layer model. The
calculator gets the CRS of these polygons: the CRS of a GeoDataFrame
or of a geometry column. It reads polygons without a CRS as lon/lat
coordinates in EPSG:4326. A border whose geometry fails gets NaN in the
geometry columns, and the fetch logs a warning. If
the dataset cannot give these polygons, the geometry columns are NaN
and the fetch logs the reason at INFO, once per dataset and area layer.
If the availability of the area layer model is unknown, the fetch fetches it.
Attributes:
| Name | Type | Description |
|---|---|---|
geometry |
bool
|
Compute the geometry. With |
Source code in src/mesqual/layer_flags/aggregations.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
input_flags
¶
input_flags(dataset: Dataset, layer: AreaLayer | BorderLayer, variant: LayerVariantValues) -> tuple[str, ...]
Return the base model of the area layer. The optional geometry adds no input.
Source code in src/mesqual/layer_flags/aggregations.py
315 316 317 318 319 320 321 322 | |
aggregate
¶
aggregate(values, border_model, dataset, layer, variant, effective_config, **params) -> DataFrame
Return one row per border, indexed by layer.index.
Source code in src/mesqual/layer_flags/aggregations.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
BorderEdgeSum
dataclass
¶
Bases: BorderAggregation
Sum of an edge value over the edges of each border, in both directions.
For edge values without a direction, for example a cost or an income per
line. An edge from A to B and an edge from B to A both add their value
to the border A - B and to the border B - A. So both borders of a pair
have the same value, and a sum over all borders counts each edge twice.
To count each edge once, select one direction with the column
is_canonical of the border model.
An edge with both ends in one area, or with an end without area, is on
no border. A time step where all edges of a border are NaN gives NaN.
The base needs no variant. It groups the edges by their areas
(layer_edge_areas).
Source code in src/mesqual/layer_flags/aggregations.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
input_flags
¶
input_flags(dataset: Dataset, layer: AreaLayer | BorderLayer, variant: LayerVariantValues) -> tuple[str, ...]
Return the inputs of layer_edge_areas.
Source code in src/mesqual/layer_flags/aggregations.py
406 407 408 409 410 411 412 413 | |
aggregate
¶
aggregate(values, border_model, dataset, layer, variant, effective_config, **params) -> DataFrame
Return one column per border: its edges in border direction plus its edges against it.
Source code in src/mesqual/layer_flags/aggregations.py
415 416 417 418 419 420 | |
sum_by_border
classmethod
¶
sum_by_border(values: DataFrame, edge_areas: DataFrame, border_model: DataFrame, against: DataFrame | None = None) -> DataFrame
Return one column per border: the edges in border direction plus the edges against it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
DataFrame
|
One column per edge. An edge in border direction adds
its value of |
required |
edge_areas
|
DataFrame
|
Two columns per edge, as |
required |
border_model
|
DataFrame
|
The border model, with columns of the same two names: the source and the target area of each border. |
required |
against
|
DataFrame | None
|
One column per edge. An edge against the border
direction adds its value of |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One column per border of |
Source code in src/mesqual/layer_flags/aggregations.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
BorderFlowSum
dataclass
¶
Bases: BorderEdgeSum
Sum of the line flows across each border, in border direction.
A line against the border direction counts with its directions swapped:
its down adds to the border up, its up to the border down, and
its net with a minus sign. total (up plus down) is the same in both
directions. The base needs a direction variant with the values up,
down and, if used, net and total. Any other value raises
ValueError. For up and down it fetches the swapped variant through
self.fetch. It groups the lines by their areas (layer_edge_areas).
For example, a line from B to A on the border A-B with up 30 and down 50 adds 50 to the border up and 30 to the border down.
Source code in src/mesqual/layer_flags/aggregations.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
check_spec
¶
check_spec(spec: LayerFlagSpec) -> None
Raise ValueError if the spec has no direction variant.
Source code in src/mesqual/layer_flags/aggregations.py
496 497 498 499 | |
input_flags
¶
input_flags(dataset: Dataset, layer: AreaLayer | BorderLayer, variant: LayerVariantValues) -> tuple[str, ...]
Return the inputs of layer_edge_areas, and the swapped base variant for up and down.
Source code in src/mesqual/layer_flags/aggregations.py
501 502 503 504 505 506 507 508 509 510 | |
BorderCapacitySum
dataclass
¶
Bases: BorderFlowSum
Same as BorderFlowSum, on line capacities.
Source code in src/mesqual/layer_flags/aggregations.py
558 559 560 | |
PriceSpread
dataclass
¶
Bases: BorderAggregation
Area value at the end of the border minus the value at its start. Base: an area spec.
Source code in src/mesqual/layer_flags/aggregations.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 | |
aggregate
¶
aggregate(values, border_model, dataset, layer, variant, effective_config, **params) -> DataFrame
Return one column per border: value of area_to minus value of area_from.
Source code in src/mesqual/layer_flags/aggregations.py
567 568 569 570 571 572 573 574 575 576 | |
LayerFlagInterpreter
¶
Bases: Dataset
Common base of AreaFlagInterpreter and BorderFlagInterpreter. Not used directly.
A subclass lists layer specs in flag_specs and is registered on a
platform dataset with register_interpreter. It fetches the base flags
and the layer model through parent_dataset.fetch, so overrides of the
base flags reach the layer flags. It passes effective_config to each
of these fetches and to aggregate, so the config of the call reaches
every input, also the inputs that the aggregation fetches. It passes to
each fetch only the parameters that the target spec declares, and the
aggregation parameters only to aggregate.
A spec that consumes base dimensions gets values=None in aggregate:
the interpreter does not fetch the base flag, and the aggregation fetches
what it needs. Only a model spec on a node base always gets the base
model as values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_dataset
|
Dataset | None
|
The platform dataset. |
None
|
**kwargs
|
Any
|
Takes the interpreter arguments of any platform; not used. The platform dataset rejects unknown names. |
{}
|
Source code in src/mesqual/layer_flags/interpreters.py
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 | |
accepted_flags
property
¶
accepted_flags: set[str]
One flag per matching layer and variant value of each spec in the flag index of the dataset.
flag_is_accepted
¶
flag_is_accepted(flag: Any) -> bool
True if a spec of this interpreter declares the flag on a layer of the dataset.
Source code in src/mesqual/layer_flags/interpreters.py
53 54 55 56 57 58 59 | |
AreaFlagInterpreter
¶
Bases: LayerFlagInterpreter
Answers the flags of AreaFlagSpecs.
On a node or node-connected base, it fetches the base flag and the base model, takes the mapping from the layer membership column, leaves out the objects without area, and calls the aggregation. The aggregation maps one object level: each column of the base is one object. A flag with several column levels from node values comes from an interpreter on the layer that builds it from its inputs, not from an aggregation of an object flag.
On a border base, it fetches the border model of the matching border layer as the mapping. The aggregation may build several column levels, for example a trade balance per area and partner.
Source code in src/mesqual/layer_flags/interpreters.py
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 178 179 180 181 182 | |
BorderFlagInterpreter
¶
Bases: LayerFlagInterpreter
Answers the flags of BorderFlagSpecs.
It fetches the base flag (with the area layer of the border layer for an area base) and the border model of the layer, and calls the aggregation. For the border model flag itself, it passes the edge model as values and no border model.
Source code in src/mesqual/layer_flags/interpreters.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
layer_edge_area_inputs
¶
layer_edge_area_inputs(dataset: Dataset, layer: BorderLayer) -> tuple[str, str]
The flags that layer_edge_areas fetches: the base model of the area layer and the edge model of layer.
Source code in src/mesqual/layer_flags/aggregations.py
530 531 532 | |
layer_edge_areas
¶
layer_edge_areas(dataset: Dataset, layer: BorderLayer, effective_config: DatasetConfig | None, params: dict[str, Any]) -> DataFrame
AreaMembership.edge_areas for the edge model of layer, with the areas of its area layer.
It fetches the flags of layer_edge_area_inputs with LayerAggregation.fetch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
effective_config
|
DatasetConfig | None
|
The config for the fetches: the effective config
of the enclosing fetch, or |
required |
Source code in src/mesqual/layer_flags/aggregations.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | |
edges_on_no_border
¶
edges_on_no_border(dataset: Dataset, layer: BorderLayer) -> int
Return the number of edges of the edge model of layer that cross no border.
An edge with both ends in one area, or with an end without area, is on no border. It fetches model flags only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
A dataset whose model results have no dataset level, for example a platform dataset or a merge collection. |
required |
layer
|
BorderLayer
|
The border layer. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
The edge model or the base model of the area layer of
|
Examples:
>>> from mesqual.testing import DummyScenario, make_dummy_dataset
>>> dataset = make_dummy_dataset(DummyScenario(name='base', node_without_country=True))
>>> edges_on_no_border(dataset, dataset.flag_index.get_layer('CountryBorder.Model'))
2
Source code in src/mesqual/layer_flags/coverage.py
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 | |
objects_without_area
¶
Return the number of objects of the base model of layer that belong to no area.
An object with an empty value in the layer membership column belongs to no area. It fetches the base model only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
A dataset whose model results have no dataset level, for example a platform dataset or a merge collection. |
required |
layer
|
AreaLayer
|
The area layer. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
The base model of |
Examples:
>>> from mesqual.testing import DummyScenario, make_dummy_dataset
>>> dataset = make_dummy_dataset(DummyScenario(name='base', node_without_country=True))
>>> objects_without_area(dataset, dataset.flag_index.get_layer('Country.Results.price'))
1
Source code in src/mesqual/layer_flags/coverage.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |