Concatenate a list of Axes. Args: axes: A collection of Axis objects. Returns: The concatenation of the axes. If all axes have labels, the result has the concatenation of the labels. Else, the result has no labels, and its size is the sum of the sizes of the axes. Raises
(axes)
| 556 | @tc.returns(Axis) |
| 557 | @tc.accepts(tc.Collection(Axis)) |
| 558 | def concat_axes(axes): |
| 559 | """Concatenate a list of Axes. |
| 560 | |
| 561 | Args: |
| 562 | axes: A collection of Axis objects. |
| 563 | |
| 564 | Returns: |
| 565 | The concatenation of the axes. |
| 566 | If all axes have labels, the result has the concatenation of the labels. |
| 567 | Else, the result has no labels, and its size is the sum of the sizes |
| 568 | of the axes. |
| 569 | |
| 570 | Raises: |
| 571 | ValueError: If `others` is not a collection of Axes or if it is empty. |
| 572 | """ |
| 573 | if not axes: |
| 574 | raise ValueError('axes must not be empty') |
| 575 | for a in axes: |
| 576 | if not isinstance(a, Axis): |
| 577 | raise ValueError('Expected an Axis, but got %r of type %r' % (a, type(a))) |
| 578 | |
| 579 | names = set(a.name for a in axes) |
| 580 | if len(names) > 1: |
| 581 | raise ValueError('axes do not all have the same name: %r' % names) |
| 582 | name, = names |
| 583 | |
| 584 | all_have_labels = all(a.labels is not None for a in axes) |
| 585 | any_has_unknown_size = any(a.size is None for a in axes) |
| 586 | |
| 587 | if all_have_labels: |
| 588 | value = tuple(label for a in axes for label in a.labels) |
| 589 | elif any_has_unknown_size: |
| 590 | value = None |
| 591 | else: |
| 592 | value = sum(len(a) for a in axes) |
| 593 | return Axis(name, value) |
| 594 | |
| 595 | |
| 596 | @tc.returns(LabeledTensor) |