Construct an Axis. Args: name: Name of the axis. value: Either None, an int or tf.compat.v1.Dimension giving the size of the axis, or a sequence that is not a string additionally providing coordinate (tick) labels. Raises: ValueError: If the user provides
(self, name, value)
| 72 | |
| 73 | @tc.accepts(object, string_types, AxisValue) |
| 74 | def __init__(self, name, value): |
| 75 | """Construct an Axis. |
| 76 | |
| 77 | Args: |
| 78 | name: Name of the axis. |
| 79 | value: Either None, an int or tf.compat.v1.Dimension giving the size of |
| 80 | the axis, or a sequence that is not a string additionally providing |
| 81 | coordinate (tick) labels. |
| 82 | |
| 83 | Raises: |
| 84 | ValueError: If the user provides labels with duplicate values. |
| 85 | """ |
| 86 | if isinstance(value, tensor_shape.Dimension): |
| 87 | dimension = value |
| 88 | labels = None |
| 89 | elif isinstance(value, int) or value is None: |
| 90 | dimension = tensor_shape.Dimension(value) |
| 91 | labels = None |
| 92 | else: |
| 93 | dimension = tensor_shape.Dimension(len(value)) |
| 94 | labels = tuple(value) |
| 95 | |
| 96 | if dimension.value == 0: |
| 97 | # Treat a zero-length axis as if it has labels. |
| 98 | labels = () |
| 99 | |
| 100 | if labels is not None: |
| 101 | index = dict(zip(labels, range(len(labels)))) |
| 102 | if len(index) != len(labels): |
| 103 | raise ValueError( |
| 104 | 'Tick labels must be unique, but got {}'.format(labels)) |
| 105 | else: |
| 106 | index = None |
| 107 | |
| 108 | self._name = name # type: string_types |
| 109 | self._dimension = dimension # type: tensor_shape.Dimension |
| 110 | self._labels = labels # type: Optional[tuple] |
| 111 | self._index = index # type: Optional[Dict[Any, int]] |
| 112 | |
| 113 | @property |
| 114 | @tc.returns(string_types) |